diff --git a/main.go b/main.go index 2899bab..93b8973 100644 --- a/main.go +++ b/main.go @@ -21,14 +21,14 @@ type mumbledj struct { keepAlive chan bool defaultChannel string conf DjConfig - queue SongQueue + queue *SongQueue } func (dj *mumbledj) OnConnect(e *gumble.ConnectEvent) { if dj.client.Channels().Find(dj.defaultChannel) != nil { dj.client.Self().Move(dj.client.Channels().Find(dj.defaultChannel)) } else { - fmt.Println("No channel specified, moving to root...") + fmt.Println("Channel doesn't exist, moving to root channel...") } err := loadConfiguration() @@ -37,6 +37,7 @@ func (dj *mumbledj) OnConnect(e *gumble.ConnectEvent) { } else { panic(err) } + dj.queue = NewSongQueue() } func (dj *mumbledj) OnDisconnect(e *gumble.DisconnectEvent) { @@ -64,15 +65,18 @@ func (dj *mumbledj) HasPermission(username string, command bool) bool { var dj = mumbledj{ keepAlive: make(chan bool), + queue: NewSongQueue(), } func main() { - var address, port, username, password, channel string + var address, port, username, password, channel, debug string + flag.StringVar(&address, "server", "localhost", "address for Mumble server") flag.StringVar(&port, "port", "64738", "port for Mumble server") flag.StringVar(&username, "username", "MumbleDJ", "username of MumbleDJ on server") flag.StringVar(&password, "password", "", "password for Mumble server (if needed)") flag.StringVar(&channel, "channel", "", "default channel for MumbleDJ") + flag.StringVar(&debug, "debug", "false", "toggle debug messages") flag.Parse() dj.client = gumble.NewClient(&dj.config) diff --git a/song.go b/song.go index f1285c6..22322ee 100644 --- a/song.go +++ b/song.go @@ -9,8 +9,15 @@ package main import ( //"github.com/layeh/gumble/gumble_ffmpeg" + "encoding/json" "fmt" + "github.com/jmoiron/jsonq" + "io/ioutil" + "net/http" + "os" "os/exec" + "os/user" + "strings" ) type Song struct { @@ -23,15 +30,39 @@ type Song struct { } func NewSong(user, id string) *Song { + jsonUrl := fmt.Sprintf("http://gdata.youtube.com/feeds/api/videos/%s?v=2&alt=jsonc", id) + response, err := http.Get(jsonUrl) + jsonString := "" + if err == nil { + defer response.Body.Close() + body, err := ioutil.ReadAll(response.Body) + if err == nil { + jsonString = string(body) + } + } + + jsonData := map[string]interface{}{} + decoder := json.NewDecoder(strings.NewReader(jsonString)) + decoder.Decode(&jsonData) + jq := jsonq.NewQuery(jsonData) + + videoTitle, _ := jq.String("data", "title") + videoThumbnail, _ := jq.String("data", "thumbnail", "sqDefault") + duration, _ := jq.Int("data", "duration") + videoDuration := fmt.Sprintf("%d:%02d", duration/60, duration%60) + song := &Song{ - submitter: user, - youtubeId: id, + submitter: user, + title: videoTitle, + youtubeId: id, + duration: videoDuration, + thumbnailUrl: videoThumbnail, } return song } func (s *Song) Download() bool { - err := exec.Command(fmt.Sprintf("youtube-dl --output ~/.mumbledj/songs/%(id)s.%(ext)s --quiet --format bestaudio --audio-format vorbis --prefer-ffmpeg https://youtube.com/watch?v=%s", s.youtubeId)) + err := exec.Command(fmt.Sprintf("youtube-dl --output \"~/.mumbledj/songs/%(id)s.%(ext)s\" --quiet --format m4a %s", s.youtubeId)) if err == nil { return true } else { @@ -44,7 +75,22 @@ func (s *Song) Play() bool { } func (s *Song) Delete() bool { - return false + usr, err := user.Current() + if err == nil { + filePath := fmt.Sprintf("%s/.mumbledj/songs/%s.m4a", usr.HomeDir, s.youtubeId) + if _, err := os.Stat(filePath); err == nil { + err := os.Remove(filePath) + if err == nil { + return true + } else { + return false + } + } else { + return true + } + } else { + return false + } } func (s *Song) AddSkip(username string) bool { diff --git a/songqueue.go b/songqueue.go index c3a9797..bb3886a 100644 --- a/songqueue.go +++ b/songqueue.go @@ -8,21 +8,30 @@ package main type SongQueue struct { - queue Queue + queue *Queue } func NewSongQueue() *SongQueue { - return &SongQueue{} + return &SongQueue{ + queue: NewQueue(), + } } func (q *SongQueue) AddSong(s *Song) bool { - return false + beforeLen := q.queue.Len() + q.queue.Push(s) + if q.queue.Len() == beforeLen+1 { + return true + } else { + return false + } + return true } -func (q *SongQueue) NextSong() bool { - return false +func (q *SongQueue) NextSong() *Song { + return q.queue.Poll().(*Song) } -//func (q *SongQueue) CurrentSong() Song { -// return false -//} +func (q *SongQueue) CurrentSong() *Song { + return q.queue.Peek().(*Song) +}