From 5fed70ca662130676bbca4d8496a1d83a639508c Mon Sep 17 00:00:00 2001 From: MichaelOultram Date: Sat, 15 Aug 2015 22:22:59 +0100 Subject: [PATCH] Cleanup comments --- commands.go | 2 +- main.go | 33 +++++++++--------- service.go | 17 +++++++-- service_soundcloud.go | 80 +++++++++++++++---------------------------- service_youtube.go | 18 +++------- 5 files changed, 65 insertions(+), 85 deletions(-) diff --git a/commands.go b/commands.go index ee449e2..5d3d0de 100644 --- a/commands.go +++ b/commands.go @@ -164,7 +164,7 @@ func add(user *gumble.User, url string) error { dj.SendPrivateMessage(user, NO_ARGUMENT_MSG) return errors.New("NO_ARGUMENT") } else { - err := findServiceAndAdd(user, url) + err := FindServiceAndAdd(user, url) if err != nil { dj.SendPrivateMessage(user, err.Error()) } diff --git a/main.go b/main.go index 9b0f445..1bd6df7 100644 --- a/main.go +++ b/main.go @@ -135,21 +135,34 @@ func (dj *mumbledj) SendPrivateMessage(user *gumble.User, message string) { // CheckAPIKeys enables the services with API keys in the environment varaibles func CheckAPIKeys() { + anyDisabled := false + // Checks YouTube API key if os.Getenv("YOUTUBE_API_KEY") == "" { - fmt.Printf("The youtube service has been disabled as you do not have a YouTube API key defined in your environment variables.\n" + - "Please see the following link for info on how to fix this: https://github.com/matthieugrieger/mumbledj#youtube-api-keys\n") + anyDisabled = true + fmt.Printf("The youtube service has been disabled as you do not have a YouTube API key defined in your environment variables.\n") } else { services = append(services, YouTube{}) } // Checks Soundcloud API key if os.Getenv("SOUNDCLOUD_API_KEY") == "" { - fmt.Printf("The soundcloud service has been disabled as you do not have a Soundcloud API key defined in your environment variables.\n" + - "Please see the following link for info on how to fix this: https://github.com/matthieugrieger/mumbledj#soundcloud-api-keys\n") + anyDisabled = true + fmt.Printf("The soundcloud service has been disabled as you do not have a Soundcloud API key defined in your environment variables.\n") } else { services = append(services, SoundCloud{}) } + + // Checks to see if any service was disabled + if anyDisabled { + fmt.Printf("Please see the following link for info on how to enable services: https://github.com/matthieugrieger/mumbledj\n") + } + + // Exits application if no services are enabled + if services == nil { + fmt.Printf("No services are enabled, and thus closing\n") + os.Exit(1) + } } // Verbose prints out messages only if verbose flag is true @@ -165,18 +178,6 @@ func isNil(a interface{}) bool { return a == nil || reflect.ValueOf(a).IsNil() } -// RegexpFromURL loops through an array of patterns to see if it matches the url -func RegexpFromURL(url string, patterns []string) *regexp.Regexp { - for _, pattern := range patterns { - if re, err := regexp.Compile(pattern); err == nil { - if re.MatchString(url) { - return re - } - } - } - return nil -} - // dj variable declaration. This is done outside of main() to allow global use. var dj = mumbledj{ keepAlive: make(chan bool), diff --git a/service.go b/service.go index caaf680..624314f 100644 --- a/service.go +++ b/service.go @@ -16,7 +16,6 @@ import ( // Service interface. Each service will implement these functions type Service interface { - ServiceName() string URLRegex(string) bool NewRequest(*gumble.User, string) (string, error) } @@ -54,7 +53,7 @@ type Playlist interface { var services []Service -func findServiceAndAdd(user *gumble.User, url string) error { +func FindServiceAndAdd(user *gumble.User, url string) error { var urlService Service // Checks all services to see if any can take the URL @@ -65,7 +64,7 @@ func findServiceAndAdd(user *gumble.User, url string) error { } if urlService == nil { - Verbose("Invalid_URL") + Verbose("INVALID_URL") return errors.New("INVALID_URL") } else { oldLength := dj.queue.Len() @@ -91,3 +90,15 @@ func findServiceAndAdd(user *gumble.User, url string) error { return err } } + +// RegexpFromURL loops through an array of patterns to see if it matches the url +func RegexpFromURL(url string, patterns []string) *regexp.Regexp { + for _, pattern := range patterns { + if re, err := regexp.Compile(pattern); err == nil { + if re.MatchString(url) { + return re + } + } + } + return nil +} diff --git a/service_soundcloud.go b/service_soundcloud.go index 250ca26..a1c2b2a 100644 --- a/service_soundcloud.go +++ b/service_soundcloud.go @@ -11,7 +11,7 @@ import ( ) // Regular expressions for soundcloud urls -var soundcloudSongPattern = `https?:\/\/(www\.)?soundcloud\.com\/([\w-]+)\/([\w-]+)` +var soundcloudSongPattern = `https?:\/\/(www\.)?soundcloud\.com\/([\w-]+)\/([\w-]+)(#t=\n\n?(:\n\n)*)?` var soundcloudPlaylistPattern = `https?:\/\/(www\.)?soundcloud\.com\/([\w-]+)\/sets\/([\w-]+)` // SoundCloud implements the Service interface @@ -21,17 +21,12 @@ type SoundCloud struct{} // SOUNDCLOUD SERVICE // ------------------ -// Name of the service -func (sc SoundCloud) ServiceName() string { - return "SoundCloud" -} - -// Checks to see if service will accept URL +// URLRegex checks to see if service will accept URL func (sc SoundCloud) URLRegex(url string) bool { return RegexpFromURL(url, []string{soundcloudSongPattern, soundcloudPlaylistPattern}) != nil } -// Creates the requested song/playlist and adds to the queue +// NewRequest creates the requested song/playlist and adds to the queue func (sc SoundCloud) NewRequest(user *gumble.User, url string) (string, error) { var apiResponse *jsonq.JsonQuery var err error @@ -74,53 +69,34 @@ func (sc SoundCloud) NewRequest(user *gumble.User, url string) (string, error) { } } -// Creates a track and adds to the queue -func (sc SoundCloud) NewSong(user *gumble.User, trackData *jsonq.JsonQuery, playlist Playlist) (string, error) { - title, err := trackData.String("title") - if err != nil { - return "", err - } - - id, err := trackData.Int("id") - if err != nil { - return "", err - } - - duration, err := trackData.Int("duration") - if err != nil { - return "", err - } +// NewSong creates a track and adds to the queue +func (sc SoundCloud) NewSong(user *gumble.User, trackData *jsonq.JsonQuery, playlist Playlist) (Song, error) { + title, _ := trackData.String("title") + id, _ := trackData.Int("id") + duration, _ := trackData.Int("duration") + url, _ := trackData.String("permalink_url") thumbnail, err := trackData.String("artwork_url") if err != nil { // Song has no artwork, using profile avatar instead - userObj, err := trackData.Object("user") - if err != nil { - return "", err - } - - thumbnail, err = jsonq.NewQuery(userObj).String("avatar_url") - if err != nil { - return "", err + userObj, _ := trackData.Object("user") + thumbnail, _ = jsonq.NewQuery(userObj).String("avatar_url") + } + + if dj.conf.General.MaxSongDuration == 0 || (duration/1000) <= dj.conf.General.MaxSongDuration { + song := &YouTubeSong{ + id: strconv.Itoa(id), + title: title, + url: url, + thumbnail: thumbnail, + submitter: user, + duration: strconv.Itoa(duration), + format: "mp3", + playlist: playlist, + skippers: make([]string, 0), + dontSkip: false, } + dj.queue.AddSong(song) + return song, nil } - - url, err := trackData.String("permalink_url") - if err != nil { - return "", err - } - - song := &YouTubeSong{ - id: strconv.Itoa(id), - title: title, - url: url, - thumbnail: thumbnail, - submitter: user, - duration: strconv.Itoa(duration), - format: "mp3", - playlist: playlist, - skippers: make([]string, 0), - dontSkip: false, - } - dj.queue.AddSong(song) - return title, nil + return nil, errors.New(VIDEO_TOO_LONG_MSG) } diff --git a/service_youtube.go b/service_youtube.go index 2592061..6457e20 100644 --- a/service_youtube.go +++ b/service_youtube.go @@ -25,11 +25,9 @@ import ( // Regular expressions for youtube urls var youtubePlaylistPattern = `https?:\/\/www\.youtube\.com\/playlist\?list=([\w-]+)` var youtubeVideoPatterns = []string{ - `https?:\/\/www\.youtube\.com\/watch\?v=([\w-]+)(\&t=\d*m?\d*s?)?`, - `https?:\/\/youtube\.com\/watch\?v=([\w-]+)(\&t=\d*m?\d*s?)?`, + `https?:\/\/(www\.)?youtube\.com\/watch\?v=([\w-]+)(\&t=\d*m?\d*s?)?`, + `https?:\/\/(www\.)?youtube\.com\/v\/([\w-]+)(\?t=\d*m?\d*s?)?`, `https?:\/\/youtu.be\/([\w-]+)(\?t=\d*m?\d*s?)?`, - `https?:\/\/youtube.com\/v\/([\w-]+)(\?t=\d*m?\d*s?)?`, - `https?:\/\/www.youtube.com\/v\/([\w-]+)(\?t=\d*m?\d*s?)?`, } // ------ @@ -43,17 +41,12 @@ type YouTube struct{} // YOUTUBE SERVICE // --------------- -// Name of the service -func (yt YouTube) ServiceName() string { - return "Youtube" -} - -// Checks to see if service will accept URL +// URLRegex checks to see if service will accept URL func (yt YouTube) URLRegex(url string) bool { return RegexpFromURL(url, append(youtubeVideoPatterns, []string{youtubePlaylistPattern}...)) != nil } -// Creates the requested song/playlist and adds to the queue +// NewRequest creates the requested song/playlist and adds to the queue func (yt YouTube) NewRequest(user *gumble.User, url string) (string, error) { var shortURL, startOffset = "", "" if re, err := regexp.Compile(youtubePlaylistPattern); err == nil { @@ -85,8 +78,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. +// NewSong gathers the metadata for a song extracted from a YouTube video, and returns the song. func (yt YouTube) NewSong(user *gumble.User, id, offset string, playlist Playlist) (Song, error) { var apiResponse *jsonq.JsonQuery var err error