From 99b6abdd2cff8a3781e2264246048aaf51380d82 Mon Sep 17 00:00:00 2001 From: MichaelOultram Date: Thu, 13 Aug 2015 13:44:38 +0100 Subject: [PATCH] Checking output of youtube-dl when downloading --- service_soundcloud.go | 15 ++++++++++----- service_youtube.go | 11 +++++------ youtube_dl.go | 8 +++++--- 3 files changed, 20 insertions(+), 14 deletions(-) diff --git a/service_soundcloud.go b/service_soundcloud.go index 9784468..6c3a5ef 100644 --- a/service_soundcloud.go +++ b/service_soundcloud.go @@ -44,7 +44,7 @@ func (sc SoundCloud) NewRequest(user *gumble.User, url string) (string, error) { // PLAYLIST if dj.HasPermission(user.Name, dj.conf.Permissions.AdminAddPlaylists) { // Check duration of playlist - // duration, _ := apiResponse.Int("duration") + duration, _ := apiResponse.Int("duration") // Create playlist title, _ := apiResponse.String("title") @@ -56,7 +56,7 @@ func (sc SoundCloud) NewRequest(user *gumble.User, url string) (string, error) { // Add all tracks for _, t := range tracks { - sc.NewSong(user.Name, jsonq.NewQuery(t), playlist) + sc.NewSong(user, jsonq.NewQuery(t), playlist) } if err == nil { return playlist.Title(), nil @@ -69,12 +69,12 @@ func (sc SoundCloud) NewRequest(user *gumble.User, url string) (string, error) { } } else { // SONG - return sc.NewSong(user.Name, apiResponse, nil) + return sc.NewSong(user, apiResponse, nil) } } // Creates a track and adds to the queue -func (sc SoundCloud) NewSong(user string, trackData *jsonq.JsonQuery, playlist Playlist) (string, error) { +func (sc SoundCloud) NewSong(user *gumble.User, trackData *jsonq.JsonQuery, playlist Playlist) (string, error) { title, err := trackData.String("title") if err != nil { return "", err @@ -83,7 +83,7 @@ func (sc SoundCloud) NewSong(user string, trackData *jsonq.JsonQuery, playlist P if err != nil { return "", err } - duration, err := trackData.String("duration") + duration, err := trackData.Int("duration") if err != nil { return "", err } @@ -91,10 +91,15 @@ func (sc SoundCloud) NewSong(user string, trackData *jsonq.JsonQuery, playlist P if err != nil { return "", err } + url, err := trackData.String("permalink_url") + if err != nil { + return "", err + } song := &YouTubeDLSong{ id: id, title: title, + url: url, thumbnail: thumbnail, submitter: user, duration: duration, diff --git a/service_youtube.go b/service_youtube.go index 542baa0..4f86ca7 100644 --- a/service_youtube.go +++ b/service_youtube.go @@ -60,7 +60,7 @@ func (yt YouTube) NewRequest(user *gumble.User, url string) (string, error) { if re.MatchString(url) { if dj.HasPermission(user.Name, dj.conf.Permissions.AdminAddPlaylists) { shortURL = re.FindStringSubmatch(url)[1] - playlist, err := yt.NewPlaylist(user.Name, shortURL) + playlist, err := yt.NewPlaylist(user, shortURL) return playlist.Title(), err } else { return "", errors.New("NO_PLAYLIST_PERMISSION") @@ -72,7 +72,7 @@ func (yt YouTube) NewRequest(user *gumble.User, url string) (string, error) { if len(matches[0]) == 3 { startOffset = matches[0][2] } - song, err := yt.NewSong(user.Name, shortURL, startOffset, nil) + song, err := yt.NewSong(user, shortURL, startOffset, nil) if err == nil { return song.Title(), nil } else { @@ -87,7 +87,7 @@ func (yt YouTube) NewRequest(user *gumble.User, url string) (string, error) { // NewSong gathers the metadata for a song extracted from a YouTube video, and returns // the song. -func (yt YouTube) NewSong(user, id, offset string, playlist Playlist) (Song, error) { +func (yt YouTube) NewSong(user *gumble.User, id, offset string, playlist Playlist) (Song, error) { var apiResponse *jsonq.JsonQuery var err error url := fmt.Sprintf("https://www.googleapis.com/youtube/v3/videos?part=snippet,contentDetails&id=%s&key=%s", @@ -181,12 +181,11 @@ func (yt YouTube) NewSong(user, id, offset string, playlist Playlist) (Song, err } // NewPlaylist gathers the metadata for a YouTube playlist and returns it. -func (yt YouTube) NewPlaylist(user, id string) (Playlist, error) { +func (yt YouTube) NewPlaylist(user *gumble.User, id string) (Playlist, error) { var apiResponse *jsonq.JsonQuery var err error // Retrieve title of playlist - url := fmt.Sprintf("https://www.googleapis.com/youtube/v3/playlists?part=snippet&id=%s&key=%s", - id, os.Getenv("YOUTUBE_API_KEY")) + url := fmt.Sprintf("https://www.googleapis.com/youtube/v3/playlists?part=snippet&id=%s&key=%s", id, os.Getenv("YOUTUBE_API_KEY")) if apiResponse, err = PerformGetRequest(url); err != nil { return nil, err } diff --git a/youtube_dl.go b/youtube_dl.go index fedfc28..618581d 100644 --- a/youtube_dl.go +++ b/youtube_dl.go @@ -15,8 +15,8 @@ type YouTubeDLSong struct { id string title string thumbnail string - submitter string - duration string + submitter *gumble.User + duration int url string offset int playlist Playlist @@ -39,7 +39,7 @@ func (dl *YouTubeDLSong) Download() error { // Checks to see if song is already downloaded if _, err := os.Stat(fmt.Sprintf("%s/.mumbledj/songs/%s", dj.homeDir, dl.Filename())); os.IsNotExist(err) { - cmd := exec.Command("youtube-dl", "--output", fmt.Sprintf("%s/.mumbledj/songs/%s", dj.homeDir, dl.Filename()), "--format m4a", "--prefer-ffmpeg", "--", dl.ID()) + cmd := exec.Command("youtube-dl", "--output", fmt.Sprintf("%s/.mumbledj/songs/%s", dj.homeDir, dl.Filename()), "--format m4a", "--prefer-ffmpeg", "--", dl.url) err = cmd.Run() if err == nil { if dj.conf.Cache.Enabled { @@ -51,6 +51,8 @@ func (dl *YouTubeDLSong) Download() error { for s := range cmd.Args { Verbose("youtube-dl args: " + cmd.Args[s]) } + b, _ := ioutil.ReadAll(cmd.Stdout) + Verbose(string(b)) return errors.New("Song download failed.") } }