Cleanup comments

This commit is contained in:
MichaelOultram 2015-08-15 22:22:59 +01:00
parent c538af8a14
commit 5fed70ca66
5 changed files with 65 additions and 85 deletions

View file

@ -164,7 +164,7 @@ func add(user *gumble.User, url string) error {
dj.SendPrivateMessage(user, NO_ARGUMENT_MSG) dj.SendPrivateMessage(user, NO_ARGUMENT_MSG)
return errors.New("NO_ARGUMENT") return errors.New("NO_ARGUMENT")
} else { } else {
err := findServiceAndAdd(user, url) err := FindServiceAndAdd(user, url)
if err != nil { if err != nil {
dj.SendPrivateMessage(user, err.Error()) dj.SendPrivateMessage(user, err.Error())
} }

33
main.go
View file

@ -135,21 +135,34 @@ func (dj *mumbledj) SendPrivateMessage(user *gumble.User, message string) {
// CheckAPIKeys enables the services with API keys in the environment varaibles // CheckAPIKeys enables the services with API keys in the environment varaibles
func CheckAPIKeys() { func CheckAPIKeys() {
anyDisabled := false
// Checks YouTube API key // Checks YouTube API key
if os.Getenv("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" + anyDisabled = true
"Please see the following link for info on how to fix this: https://github.com/matthieugrieger/mumbledj#youtube-api-keys\n") fmt.Printf("The youtube service has been disabled as you do not have a YouTube API key defined in your environment variables.\n")
} else { } else {
services = append(services, YouTube{}) services = append(services, YouTube{})
} }
// Checks Soundcloud API key // Checks Soundcloud API key
if os.Getenv("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" + anyDisabled = true
"Please see the following link for info on how to fix this: https://github.com/matthieugrieger/mumbledj#soundcloud-api-keys\n") fmt.Printf("The soundcloud service has been disabled as you do not have a Soundcloud API key defined in your environment variables.\n")
} else { } else {
services = append(services, SoundCloud{}) 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 // 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() 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. // 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

@ -16,7 +16,6 @@ import (
// Service interface. Each service will implement these functions // Service interface. Each service will implement these functions
type Service interface { type Service interface {
ServiceName() string
URLRegex(string) bool URLRegex(string) bool
NewRequest(*gumble.User, string) (string, error) NewRequest(*gumble.User, string) (string, error)
} }
@ -54,7 +53,7 @@ type Playlist interface {
var services []Service var services []Service
func findServiceAndAdd(user *gumble.User, url string) error { func FindServiceAndAdd(user *gumble.User, url string) error {
var urlService Service var urlService Service
// Checks all services to see if any can take the URL // 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 { if urlService == nil {
Verbose("Invalid_URL") Verbose("INVALID_URL")
return errors.New("INVALID_URL") return errors.New("INVALID_URL")
} else { } else {
oldLength := dj.queue.Len() oldLength := dj.queue.Len()
@ -91,3 +90,15 @@ func findServiceAndAdd(user *gumble.User, url string) error {
return err 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
}

View file

@ -11,7 +11,7 @@ import (
) )
// Regular expressions for soundcloud urls // 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-]+)` var soundcloudPlaylistPattern = `https?:\/\/(www\.)?soundcloud\.com\/([\w-]+)\/sets\/([\w-]+)`
// SoundCloud implements the Service interface // SoundCloud implements the Service interface
@ -21,17 +21,12 @@ type SoundCloud struct{}
// SOUNDCLOUD SERVICE // SOUNDCLOUD SERVICE
// ------------------ // ------------------
// Name of the service // URLRegex checks to see if service will accept URL
func (sc SoundCloud) ServiceName() string {
return "SoundCloud"
}
// Checks to see if service will accept URL
func (sc SoundCloud) URLRegex(url string) bool { func (sc SoundCloud) URLRegex(url string) bool {
return RegexpFromURL(url, []string{soundcloudSongPattern, soundcloudPlaylistPattern}) != nil 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) { func (sc SoundCloud) NewRequest(user *gumble.User, url string) (string, error) {
var apiResponse *jsonq.JsonQuery var apiResponse *jsonq.JsonQuery
var err error 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 // NewSong creates a track and adds to the queue
func (sc SoundCloud) NewSong(user *gumble.User, trackData *jsonq.JsonQuery, playlist Playlist) (string, error) { func (sc SoundCloud) NewSong(user *gumble.User, trackData *jsonq.JsonQuery, playlist Playlist) (Song, error) {
title, err := trackData.String("title") title, _ := trackData.String("title")
if err != nil { id, _ := trackData.Int("id")
return "", err duration, _ := trackData.Int("duration")
} url, _ := trackData.String("permalink_url")
id, err := trackData.Int("id")
if err != nil {
return "", err
}
duration, err := trackData.Int("duration")
if err != nil {
return "", err
}
thumbnail, err := trackData.String("artwork_url") thumbnail, err := trackData.String("artwork_url")
if err != nil { if err != nil {
// Song has no artwork, using profile avatar instead // Song has no artwork, using profile avatar instead
userObj, err := trackData.Object("user") userObj, _ := trackData.Object("user")
if err != nil { thumbnail, _ = jsonq.NewQuery(userObj).String("avatar_url")
return "", err }
}
if dj.conf.General.MaxSongDuration == 0 || (duration/1000) <= dj.conf.General.MaxSongDuration {
thumbnail, err = jsonq.NewQuery(userObj).String("avatar_url") song := &YouTubeSong{
if err != nil { id: strconv.Itoa(id),
return "", err 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
} }
return nil, errors.New(VIDEO_TOO_LONG_MSG)
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
} }

View file

@ -25,11 +25,9 @@ import (
// Regular expressions for youtube urls // 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?)?`,
`https?:\/\/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?:\/\/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 // YOUTUBE SERVICE
// --------------- // ---------------
// Name of the service // URLRegex checks to see if service will accept URL
func (yt YouTube) ServiceName() string {
return "Youtube"
}
// Checks to see if service will accept URL
func (yt YouTube) URLRegex(url string) bool { func (yt YouTube) URLRegex(url string) bool {
return RegexpFromURL(url, append(youtubeVideoPatterns, []string{youtubePlaylistPattern}...)) != nil 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) { func (yt YouTube) NewRequest(user *gumble.User, url string) (string, error) {
var shortURL, startOffset = "", "" var shortURL, startOffset = "", ""
if re, err := regexp.Compile(youtubePlaylistPattern); err == nil { 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 // NewSong gathers the metadata for a song extracted from a YouTube video, and returns the song.
// the song.
func (yt YouTube) NewSong(user *gumble.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