diff --git a/commands.go b/commands.go index a313ae6..7bc5c22 100644 --- a/commands.go +++ b/commands.go @@ -158,12 +158,28 @@ func parseCommand(user *gumble.User, username, command string) { } } -// add performs !add functionality. Checks input URL for YouTube format, and adds +// add performs !add functionality. Checks input URL for service, and adds // the URL to the queue if the format matches. func add(user *gumble.User, username, url string) { if url == "" { dj.SendPrivateMessage(user, NO_ARGUMENT_MSG) } else { + var urlService *Service + + // Checks all services to see if any can take the URL + for _, service := range services { + if service.URLRegex(url) { + urlService = service + } + } + + if urlService == nil { + dj.SendPrivateMessage(user, INVALID_URL_MSG) + } + // else { + // urlService.NewRequest(user, url) + // } + youtubePatterns := []string{ `https?:\/\/www\.youtube\.com\/watch\?v=([\w-]+)(\&t=\d*m?\d*s?)?`, `https?:\/\/youtube\.com\/watch\?v=([\w-]+)(\&t=\d*m?\d*s?)?`, diff --git a/service.go b/service.go index 205c277..1015395 100644 --- a/service.go +++ b/service.go @@ -7,6 +7,13 @@ package main +// Service interface. Each service should implement these functions +type Service interface { + ServiceName() string + URLRegex(string) bool // Can service deal with URL + NewRequest(*gumble.User, string) error // Create song/playlist and add to the queue +} + // Song interface. Each service will implement these // functions in their Song types. type Song interface { @@ -37,3 +44,7 @@ type Playlist interface { ID() string Title() string } + +var services = []Service{ + YoutubeService{}, +} diff --git a/service_youtube.go b/service_youtube.go index d5ece7f..23a9b68 100644 --- a/service_youtube.go +++ b/service_youtube.go @@ -24,6 +24,47 @@ import ( "github.com/layeh/gumble/gumble_ffmpeg" ) +// --------------- +// YOUTUBE SERVICE +// --------------- + +type YouTubeService struct { +} + +// Name of the service +func (yt *YoutubeService) ServiceName() string { + return "Youtube" +} + +// Checks to see if service will accept URL +func (yt *YoutubeService) URLRegex(url) bool { + youtubePatterns := []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?:\/\/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?)?`, + `https?:\/\/www\.youtube\.com\/playlist\?list=([\w-]+)`, + } + matchFound := false + + for _, pattern := range youtubePatterns { + if re, err := regexp.Compile(pattern); err == nil { + if re.MatchString(url) { + matchFound = true + break + } + } + } + + return matchFound +} + +// Creates the requested song/playlist and adds to the queue +func (yt *YoutubeService) NewRequest(user, url) { + +} + // ------------ // YOUTUBE SONG // ------------ @@ -149,7 +190,7 @@ func (s *YouTubeSong) Download() error { if _, err := os.Stat(fmt.Sprintf("%s/.mumbledj/songs/%s", dj.homeDir, s.Filename())); os.IsNotExist(err) { if dj.verbose { - fmt.Printf("Downloading %s\n", s.Title) + fmt.Printf("Downloading %s\n", s.Title()) } cmd := exec.Command("youtube-dl", "--output", fmt.Sprintf(`~/.mumbledj/songs/%s`, s.Filename()), "--format", "m4a", "--", s.ID()) @@ -433,7 +474,7 @@ func NewYouTubePlaylist(user, id string) (*YouTubePlaylist, error) { } dj.queue.AddSong(playlistSong) if dj.verbose { - fmt.Printf("%s added song %s\n", playlistSong.Submitter, playlistSong.Title()) + fmt.Printf("%s added song %s\n", playlistSong.Submitter(), playlistSong.Title()) } } }