Checking output of youtube-dl when downloading

This commit is contained in:
MichaelOultram 2015-08-13 13:44:38 +01:00
parent b3b1d9c3f4
commit 99b6abdd2c
3 changed files with 20 additions and 14 deletions

View file

@ -44,7 +44,7 @@ func (sc SoundCloud) NewRequest(user *gumble.User, url string) (string, error) {
// PLAYLIST // PLAYLIST
if dj.HasPermission(user.Name, dj.conf.Permissions.AdminAddPlaylists) { if dj.HasPermission(user.Name, dj.conf.Permissions.AdminAddPlaylists) {
// Check duration of playlist // Check duration of playlist
// duration, _ := apiResponse.Int("duration") duration, _ := apiResponse.Int("duration")
// Create playlist // Create playlist
title, _ := apiResponse.String("title") title, _ := apiResponse.String("title")
@ -56,7 +56,7 @@ func (sc SoundCloud) NewRequest(user *gumble.User, url string) (string, error) {
// Add all tracks // Add all tracks
for _, t := range tracks { for _, t := range tracks {
sc.NewSong(user.Name, jsonq.NewQuery(t), playlist) sc.NewSong(user, jsonq.NewQuery(t), playlist)
} }
if err == nil { if err == nil {
return playlist.Title(), nil return playlist.Title(), nil
@ -69,12 +69,12 @@ func (sc SoundCloud) NewRequest(user *gumble.User, url string) (string, error) {
} }
} else { } else {
// SONG // SONG
return sc.NewSong(user.Name, apiResponse, nil) return sc.NewSong(user, apiResponse, nil)
} }
} }
// Creates a track and adds to the queue // 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") title, err := trackData.String("title")
if err != nil { if err != nil {
return "", err return "", err
@ -83,7 +83,7 @@ func (sc SoundCloud) NewSong(user string, trackData *jsonq.JsonQuery, playlist P
if err != nil { if err != nil {
return "", err return "", err
} }
duration, err := trackData.String("duration") duration, err := trackData.Int("duration")
if err != nil { if err != nil {
return "", err return "", err
} }
@ -91,10 +91,15 @@ func (sc SoundCloud) NewSong(user string, trackData *jsonq.JsonQuery, playlist P
if err != nil { if err != nil {
return "", err return "", err
} }
url, err := trackData.String("permalink_url")
if err != nil {
return "", err
}
song := &YouTubeDLSong{ song := &YouTubeDLSong{
id: id, id: id,
title: title, title: title,
url: url,
thumbnail: thumbnail, thumbnail: thumbnail,
submitter: user, submitter: user,
duration: duration, duration: duration,

View file

@ -60,7 +60,7 @@ func (yt YouTube) NewRequest(user *gumble.User, url string) (string, error) {
if re.MatchString(url) { if re.MatchString(url) {
if dj.HasPermission(user.Name, dj.conf.Permissions.AdminAddPlaylists) { if dj.HasPermission(user.Name, dj.conf.Permissions.AdminAddPlaylists) {
shortURL = re.FindStringSubmatch(url)[1] shortURL = re.FindStringSubmatch(url)[1]
playlist, err := yt.NewPlaylist(user.Name, shortURL) playlist, err := yt.NewPlaylist(user, shortURL)
return playlist.Title(), err return playlist.Title(), err
} else { } else {
return "", errors.New("NO_PLAYLIST_PERMISSION") 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 { if len(matches[0]) == 3 {
startOffset = matches[0][2] startOffset = matches[0][2]
} }
song, err := yt.NewSong(user.Name, shortURL, startOffset, nil) song, err := yt.NewSong(user, shortURL, startOffset, nil)
if err == nil { if err == nil {
return song.Title(), nil return song.Title(), nil
} else { } 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 // NewSong gathers the metadata for a song extracted from a YouTube video, and returns
// the song. // 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 apiResponse *jsonq.JsonQuery
var err error var err error
url := fmt.Sprintf("https://www.googleapis.com/youtube/v3/videos?part=snippet,contentDetails&id=%s&key=%s", 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. // 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 apiResponse *jsonq.JsonQuery
var err error var err error
// Retrieve title of playlist // Retrieve title of playlist
url := fmt.Sprintf("https://www.googleapis.com/youtube/v3/playlists?part=snippet&id=%s&key=%s", url := fmt.Sprintf("https://www.googleapis.com/youtube/v3/playlists?part=snippet&id=%s&key=%s", id, os.Getenv("YOUTUBE_API_KEY"))
id, os.Getenv("YOUTUBE_API_KEY"))
if apiResponse, err = PerformGetRequest(url); err != nil { if apiResponse, err = PerformGetRequest(url); err != nil {
return nil, err return nil, err
} }

View file

@ -15,8 +15,8 @@ type YouTubeDLSong struct {
id string id string
title string title string
thumbnail string thumbnail string
submitter string submitter *gumble.User
duration string duration int
url string url string
offset int offset int
playlist Playlist playlist Playlist
@ -39,7 +39,7 @@ func (dl *YouTubeDLSong) Download() error {
// Checks to see if song is already downloaded // 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) { 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() err = cmd.Run()
if err == nil { if err == nil {
if dj.conf.Cache.Enabled { if dj.conf.Cache.Enabled {
@ -51,6 +51,8 @@ func (dl *YouTubeDLSong) Download() error {
for s := range cmd.Args { for s := range cmd.Args {
Verbose("youtube-dl args: " + cmd.Args[s]) Verbose("youtube-dl args: " + cmd.Args[s])
} }
b, _ := ioutil.ReadAll(cmd.Stdout)
Verbose(string(b))
return errors.New("Song download failed.") return errors.New("Song download failed.")
} }
} }