Started work on interfacing services
This commit is contained in:
parent
8928ea3b3d
commit
7b737b03ab
18
commands.go
18
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.
|
// the URL to the queue if the format matches.
|
||||||
func add(user *gumble.User, username, url string) {
|
func add(user *gumble.User, username, url string) {
|
||||||
if url == "" {
|
if url == "" {
|
||||||
dj.SendPrivateMessage(user, NO_ARGUMENT_MSG)
|
dj.SendPrivateMessage(user, NO_ARGUMENT_MSG)
|
||||||
} else {
|
} 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{
|
youtubePatterns := []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?:\/\/youtube\.com\/watch\?v=([\w-]+)(\&t=\d*m?\d*s?)?`,
|
||||||
|
|
11
service.go
11
service.go
|
@ -7,6 +7,13 @@
|
||||||
|
|
||||||
package main
|
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
|
// Song interface. Each service will implement these
|
||||||
// functions in their Song types.
|
// functions in their Song types.
|
||||||
type Song interface {
|
type Song interface {
|
||||||
|
@ -37,3 +44,7 @@ type Playlist interface {
|
||||||
ID() string
|
ID() string
|
||||||
Title() string
|
Title() string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var services = []Service{
|
||||||
|
YoutubeService{},
|
||||||
|
}
|
||||||
|
|
|
@ -24,6 +24,47 @@ import (
|
||||||
"github.com/layeh/gumble/gumble_ffmpeg"
|
"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
|
// 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 _, err := os.Stat(fmt.Sprintf("%s/.mumbledj/songs/%s", dj.homeDir, s.Filename())); os.IsNotExist(err) {
|
||||||
|
|
||||||
if dj.verbose {
|
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())
|
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)
|
dj.queue.AddSong(playlistSong)
|
||||||
if dj.verbose {
|
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())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Reference in a new issue