Started work on interfacing services

This commit is contained in:
MichaelOultram 2015-07-27 22:13:40 +01:00
parent 8928ea3b3d
commit 7b737b03ab
3 changed files with 71 additions and 3 deletions

View file

@ -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?)?`,

View file

@ -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{},
}

View file

@ -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())
}
}
}