Fixing build issues

This commit is contained in:
MichaelOultram 2015-07-28 12:35:52 +01:00
parent 020c12e67d
commit 1c5f0f33bf
3 changed files with 33 additions and 102 deletions

View file

@ -176,7 +176,11 @@ func add(user *gumble.User, username, url string) {
dj.SendPrivateMessage(user, INVALID_URL_MSG) dj.SendPrivateMessage(user, INVALID_URL_MSG)
} else { } else {
oldLength := dj.queue.Len() oldLength := dj.queue.Len()
urlService.NewRequest(user, url)
if err := urlService.NewRequest(user, url); err == nil {
dj.client.Self.Channel.Send(SONG_ADDED_HTML, false)
// Starts playing the new song if nothing else is playing
if oldLength == 0 && dj.queue.Len() != 0 && !dj.audioStream.IsPlaying() { if oldLength == 0 && dj.queue.Len() != 0 && !dj.audioStream.IsPlaying() {
if err := dj.queue.CurrentSong().Download(); err == nil { if err := dj.queue.CurrentSong().Download(); err == nil {
dj.queue.CurrentSong().Play() dj.queue.CurrentSong().Play()
@ -186,6 +190,9 @@ func add(user *gumble.User, username, url string) {
dj.queue.OnSongFinished() dj.queue.OnSongFinished()
} }
} }
} else {
dj.SendPrivateMessage(user, err)
}
} }
} }
} }

View file

@ -140,6 +140,12 @@ func PerformStartupChecks() {
} }
} }
func Verbose(msg string) {
if dj.verbose {
fmt.Printf(msg)
}
}
// dj variable declaration. This is done outside of main() to allow global use. // dj variable declaration. This is done outside of main() to allow global use.
var dj = mumbledj{ var dj = mumbledj{
keepAlive: make(chan bool), keepAlive: make(chan bool),

View file

@ -25,6 +25,7 @@ import (
"github.com/layeh/gumble/gumble_ffmpeg" "github.com/layeh/gumble/gumble_ffmpeg"
) )
// Regular expressions for youtube urls
var youtubePlaylistPattern = `https?:\/\/www\.youtube\.com\/playlist\?list=([\w-]+)` var youtubePlaylistPattern = `https?:\/\/www\.youtube\.com\/playlist\?list=([\w-]+)`
var youtubeVideoPatterns = []string{ var youtubeVideoPatterns = []string{
`https?:\/\/www\.youtube\.com\/watch\?v=([\w-]+)(\&t=\d*m?\d*s?)?`, `https?:\/\/www\.youtube\.com\/watch\?v=([\w-]+)(\&t=\d*m?\d*s?)?`,
@ -115,7 +116,7 @@ func NewYouTubeSong(user, id, offset string, playlist *YouTubePlaylist) (*YouTub
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",
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, errors.New(INVALID_API_KEY)
} }
var offsetDays, offsetHours, offsetMinutes, offsetSeconds int64 var offsetDays, offsetHours, offsetMinutes, offsetSeconds int64
@ -192,18 +193,15 @@ func NewYouTubeSong(user, id, offset string, playlist *YouTubePlaylist) (*YouTub
duration: durationString, duration: durationString,
thumbnail: thumbnail, thumbnail: thumbnail,
skippers: make([]string, 0), skippers: make([]string, 0),
playlist: nil, playlist: playlist,
dontSkip: false, dontSkip: false,
} }
dj.queue.AddSong(song) dj.queue.AddSong(song)
Verbose(song.Submitter + " added track " + song.Title() + "\n")
if dj.verbose {
fmt.Printf("%s added track %s\n", song.Submitter, song.Title())
}
return song, nil return song, nil
} }
return nil, errors.New("Song exceeds the maximum allowed duration.") return nil, errors.New(VIDEO_TOO_LONG_MSG)
} }
// Download downloads the song via youtube-dl if it does not already exist on disk. // Download downloads the song via youtube-dl if it does not already exist on disk.
@ -212,28 +210,16 @@ func (s *YouTubeSong) 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, s.Filename())); os.IsNotExist(err) { if _, err := os.Stat(fmt.Sprintf("%s/.mumbledj/songs/%s", dj.homeDir, s.Filename())); os.IsNotExist(err) {
Verbose("Downloading " + s.Title() + "\n")
if dj.verbose {
fmt.Printf("Downloading %s\n", s.Title())
}
cmd := exec.Command("youtube-dl", "--output", fmt.Sprintf(`~/.mumbledj/songs/%s`, s.Filename()), "--format", "m4a", "--", s.ID()) cmd := exec.Command("youtube-dl", "--output", fmt.Sprintf(`~/.mumbledj/songs/%s`, s.Filename()), "--format", "m4a", "--", s.ID())
if err := cmd.Run(); err == nil { if err := cmd.Run(); err == nil {
if dj.conf.Cache.Enabled { if dj.conf.Cache.Enabled {
dj.cache.CheckMaximumDirectorySize() dj.cache.CheckMaximumDirectorySize()
} }
Verbose(s.Title() + " downloaded\n")
if dj.verbose {
fmt.Printf("%s downloaded\n", s.Title())
}
return nil return nil
} }
Verbose(s.Title() + " failed to download\n")
if dj.verbose {
fmt.Printf("%s failed to download\n", s.Title())
}
return errors.New("Song download failed.") return errors.New("Song download failed.")
} }
return nil return nil
@ -286,10 +272,7 @@ func (s *YouTubeSong) Play() {
dj.client.Self.Channel.Send(fmt.Sprintf(message, s.Thumbnail(), s.ID(), dj.client.Self.Channel.Send(fmt.Sprintf(message, s.Thumbnail(), s.ID(),
s.Title(), s.Duration(), s.Submitter(), s.Playlist().Title()), false) s.Title(), s.Duration(), s.Submitter(), s.Playlist().Title()), false)
} }
Verbose("Now playing " + s.Title() + "\n")
if dj.verbose {
fmt.Printf("Now playing %s\n", s.Title())
}
go func() { go func() {
dj.audioStream.Wait() dj.audioStream.Wait()
@ -304,11 +287,10 @@ func (s *YouTubeSong) Delete() error {
filePath := fmt.Sprintf("%s/.mumbledj/songs/%s.m4a", dj.homeDir, s.ID()) filePath := fmt.Sprintf("%s/.mumbledj/songs/%s.m4a", dj.homeDir, s.ID())
if _, err := os.Stat(filePath); err == nil { if _, err := os.Stat(filePath); err == nil {
if err := os.Remove(filePath); err == nil { if err := os.Remove(filePath); err == nil {
if dj.verbose { Verbose("Deleted " + s.Title() + "\n")
fmt.Printf("Deleting %s\n", s.Title())
}
return nil return nil
} }
Verbose("Failed to delete " + s.Title() + "\n")
return errors.New("Error occurred while deleting audio file.") return errors.New("Error occurred while deleting audio file.")
} }
return nil return nil
@ -435,72 +417,8 @@ func NewYouTubePlaylist(user, id string) (*YouTubePlaylist, error) {
for i := 0; i < numVideos; i++ { for i := 0; i < numVideos; i++ {
index := strconv.Itoa(i) index := strconv.Itoa(i)
videoTitle, err := apiResponse.String("items", index, "snippet", "title")
videoID, _ := apiResponse.String("items", index, "snippet", "resourceId", "videoId") videoID, _ := apiResponse.String("items", index, "snippet", "resourceId", "videoId")
videoThumbnail, _ := apiResponse.String("items", index, "snippet", "thumbnails", "high", "url") NewYouTubeSong(user, videoID, "", playlist)
// A completely separate API call just to get the duration of a video in a
// playlist? WHY GOOGLE, WHY?!
var durationResponse *jsonq.JsonQuery
url = fmt.Sprintf("https://www.googleapis.com/youtube/v3/videos?part=contentDetails&id=%s&key=%s",
videoID, os.Getenv("YOUTUBE_API_KEY"))
if durationResponse, err = PerformGetRequest(url); err != nil {
return nil, err
}
videoDuration, _ := durationResponse.String("items", "0", "contentDetails", "duration")
var days, hours, minutes, seconds int64
timestampExp := regexp.MustCompile(`P(?P<days>\d+D)?T(?P<hours>\d+H)?(?P<minutes>\d+M)?(?P<seconds>\d+S)?`)
timestampMatch := timestampExp.FindStringSubmatch(videoDuration)
timestampResult := make(map[string]string)
for i, name := range timestampExp.SubexpNames() {
if i < len(timestampMatch) {
timestampResult[name] = timestampMatch[i]
}
}
if timestampResult["days"] != "" {
days, _ = strconv.ParseInt(strings.TrimSuffix(timestampResult["days"], "D"), 10, 32)
}
if timestampResult["hours"] != "" {
hours, _ = strconv.ParseInt(strings.TrimSuffix(timestampResult["hours"], "H"), 10, 32)
}
if timestampResult["minutes"] != "" {
minutes, _ = strconv.ParseInt(strings.TrimSuffix(timestampResult["minutes"], "M"), 10, 32)
}
if timestampResult["seconds"] != "" {
seconds, _ = strconv.ParseInt(strings.TrimSuffix(timestampResult["seconds"], "S"), 10, 32)
}
totalSeconds := int((days * 86400) + (hours * 3600) + (minutes * 60) + seconds)
var durationString string
if hours != 0 {
if days != 0 {
durationString = fmt.Sprintf("%d:%02d:%02d:%02d", days, hours, minutes, seconds)
} else {
durationString = fmt.Sprintf("%d:%02d:%02d", hours, minutes, seconds)
}
} else {
durationString = fmt.Sprintf("%d:%02d", minutes, seconds)
}
if dj.conf.General.MaxSongDuration == 0 || totalSeconds <= dj.conf.General.MaxSongDuration {
playlistSong := &YouTubeSong{
submitter: user,
title: videoTitle,
id: videoID,
filename: videoID + ".m4a",
duration: durationString,
thumbnail: videoThumbnail,
skippers: make([]string, 0),
playlist: playlist,
dontSkip: false,
}
dj.queue.AddSong(playlistSong)
if dj.verbose {
fmt.Printf("%s added song %s\n", playlistSong.Submitter(), playlistSong.Title())
}
}
} }
return playlist, nil return playlist, nil
} }