2015-04-09 03:44:22 +02:00
|
|
|
/*
|
|
|
|
* MumbleDJ
|
|
|
|
* By Matthieu Grieger
|
2015-04-14 08:40:39 +02:00
|
|
|
* service.go
|
2015-04-09 03:44:22 +02:00
|
|
|
* Copyright (c) 2014, 2015 Matthieu Grieger (MIT License)
|
|
|
|
*/
|
|
|
|
|
2015-07-30 18:28:01 +02:00
|
|
|
package main
|
2015-04-09 03:44:22 +02:00
|
|
|
|
2015-07-27 23:27:23 +02:00
|
|
|
import (
|
2015-07-28 22:40:44 +02:00
|
|
|
"errors"
|
2015-07-30 14:54:13 +02:00
|
|
|
"fmt"
|
2015-08-16 02:18:46 +02:00
|
|
|
"regexp"
|
2015-09-26 15:52:39 +02:00
|
|
|
"time"
|
2015-07-28 22:40:44 +02:00
|
|
|
|
2015-07-27 23:27:23 +02:00
|
|
|
"github.com/layeh/gumble/gumble"
|
|
|
|
)
|
|
|
|
|
2015-08-15 20:56:00 +02:00
|
|
|
// Service interface. Each service will implement these functions
|
2015-07-27 23:13:40 +02:00
|
|
|
type Service interface {
|
2015-09-26 15:52:39 +02:00
|
|
|
ServiceName() string
|
|
|
|
TrackName() string
|
2015-08-15 20:56:00 +02:00
|
|
|
URLRegex(string) bool
|
2015-09-26 15:52:39 +02:00
|
|
|
NewRequest(*gumble.User, string) ([]Song, error)
|
2015-07-27 23:13:40 +02:00
|
|
|
}
|
|
|
|
|
2015-04-09 22:20:40 +02:00
|
|
|
// Song interface. Each service will implement these
|
2015-04-09 03:44:22 +02:00
|
|
|
// functions in their Song types.
|
2015-04-09 03:47:39 +02:00
|
|
|
type Song interface {
|
2015-04-14 08:40:39 +02:00
|
|
|
Download() error
|
2015-04-09 03:47:39 +02:00
|
|
|
Play()
|
2015-04-14 08:40:39 +02:00
|
|
|
Delete() error
|
2015-04-16 08:32:32 +02:00
|
|
|
AddSkip(string) error
|
|
|
|
RemoveSkip(string) error
|
|
|
|
SkipReached(int) bool
|
2015-04-14 08:40:39 +02:00
|
|
|
Submitter() string
|
|
|
|
Title() string
|
|
|
|
ID() string
|
|
|
|
Filename() string
|
2015-08-13 14:57:45 +02:00
|
|
|
Duration() string
|
2015-04-14 08:40:39 +02:00
|
|
|
Thumbnail() string
|
2015-04-16 08:32:32 +02:00
|
|
|
Playlist() Playlist
|
2015-04-14 08:40:39 +02:00
|
|
|
DontSkip() bool
|
2015-04-16 08:32:32 +02:00
|
|
|
SetDontSkip(bool)
|
2015-04-09 03:47:39 +02:00
|
|
|
}
|
2015-04-09 03:44:22 +02:00
|
|
|
|
2015-04-09 22:20:40 +02:00
|
|
|
// Playlist interface. Each service will implement these
|
2015-04-09 03:44:22 +02:00
|
|
|
// functions in their Playlist types.
|
2015-04-09 03:47:39 +02:00
|
|
|
type Playlist interface {
|
2015-04-16 08:32:32 +02:00
|
|
|
AddSkip(string) error
|
|
|
|
RemoveSkip(string) error
|
2015-04-09 03:47:39 +02:00
|
|
|
DeleteSkippers()
|
2015-04-16 08:32:32 +02:00
|
|
|
SkipReached(int) bool
|
2015-04-14 08:40:39 +02:00
|
|
|
ID() string
|
|
|
|
Title() string
|
2015-04-09 03:47:39 +02:00
|
|
|
}
|
2015-07-28 00:00:32 +02:00
|
|
|
|
2015-08-15 20:17:26 +02:00
|
|
|
var services []Service
|
|
|
|
|
2015-09-26 15:52:39 +02:00
|
|
|
// FindServiceAndAdd tries the given url with each service
|
2015-09-25 18:33:13 +02:00
|
|
|
// and adds the song/playlist with the correct service
|
2015-08-15 23:22:59 +02:00
|
|
|
func FindServiceAndAdd(user *gumble.User, url string) error {
|
2015-07-28 22:38:35 +02:00
|
|
|
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 {
|
2015-08-16 02:10:35 +02:00
|
|
|
return errors.New(INVALID_URL_MSG)
|
2015-07-28 22:38:35 +02:00
|
|
|
} else {
|
2015-07-28 22:40:44 +02:00
|
|
|
var title string
|
2015-09-26 15:52:39 +02:00
|
|
|
var songsAdded = 0
|
2015-09-26 16:01:09 +02:00
|
|
|
var songArray []Song
|
2015-09-26 16:09:07 +02:00
|
|
|
var err error
|
2015-07-30 14:48:53 +02:00
|
|
|
|
2015-09-26 15:52:39 +02:00
|
|
|
// Get service to create songs
|
2015-09-26 16:09:07 +02:00
|
|
|
if songArray, err = urlService.NewRequest(user, url); err != nil {
|
2015-09-26 15:52:39 +02:00
|
|
|
return err
|
|
|
|
}
|
2015-07-28 22:38:35 +02:00
|
|
|
|
2015-09-26 15:52:39 +02:00
|
|
|
// Check Playlist Permission
|
|
|
|
if len(songArray) > 1 && !dj.HasPermission(user.Name, dj.conf.Permissions.AdminAddPlaylists) {
|
|
|
|
return errors.New(NO_PLAYLIST_PERMISSION_MSG)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Loop through all songs and add to the queue
|
|
|
|
oldLength := dj.queue.Len()
|
2015-09-26 16:14:09 +02:00
|
|
|
for songToAdd := range songArray {
|
2015-09-26 15:57:29 +02:00
|
|
|
// Check song is not too long
|
2015-09-26 16:14:09 +02:00
|
|
|
time, _ := time.ParseDuration(songToAdd.Duration)
|
2015-09-26 15:52:39 +02:00
|
|
|
if dj.conf.General.MaxSongDuration == 0 || int(time.Seconds()) <= dj.conf.General.MaxSongDuration {
|
2015-09-26 16:14:09 +02:00
|
|
|
if !isNil(songToAdd.Playlist()) {
|
|
|
|
title = songToAdd.Playlist().Title()
|
2015-07-28 22:38:35 +02:00
|
|
|
} else {
|
2015-09-26 16:14:09 +02:00
|
|
|
title = songToAdd.Title()
|
2015-07-28 22:38:35 +02:00
|
|
|
}
|
2015-09-26 15:52:39 +02:00
|
|
|
|
2015-09-26 15:57:29 +02:00
|
|
|
// Add song to queue
|
2015-09-26 15:52:39 +02:00
|
|
|
dj.queue.AddSong(song)
|
|
|
|
songsAdded++
|
2015-07-28 22:38:35 +02:00
|
|
|
}
|
2015-09-26 15:52:39 +02:00
|
|
|
}
|
|
|
|
|
2015-09-26 15:57:29 +02:00
|
|
|
// Alert channel of added song/playlist
|
2015-09-26 15:52:39 +02:00
|
|
|
if songsAdded == 0 {
|
|
|
|
return errors.New(TRACK_TOO_LONG_MSG)
|
|
|
|
} else if songsAdded == 1 {
|
|
|
|
dj.client.Self.Channel.Send(fmt.Sprintf(SONG_ADDED_HTML, user.Name, title), false)
|
2015-07-30 14:48:53 +02:00
|
|
|
} else {
|
2015-09-26 15:52:39 +02:00
|
|
|
dj.client.Self.Channel.Send(fmt.Sprintf(PLAYLIST_ADDED_HTML, user.Name, title), false)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Starts playing the new song if nothing else is playing
|
|
|
|
if oldLength == 0 && dj.queue.Len() != 0 && !dj.audioStream.IsPlaying() {
|
|
|
|
if err := dj.queue.CurrentSong().Download(); err == nil {
|
|
|
|
dj.queue.CurrentSong().Play()
|
|
|
|
} else {
|
|
|
|
dj.queue.CurrentSong().Delete()
|
|
|
|
dj.queue.OnSongFinished()
|
|
|
|
return errors.New(AUDIO_FAIL_MSG)
|
|
|
|
}
|
2015-07-28 22:38:35 +02:00
|
|
|
}
|
2015-09-26 16:01:09 +02:00
|
|
|
return nil
|
2015-07-28 22:38:35 +02:00
|
|
|
}
|
|
|
|
}
|
2015-08-15 23:22:59 +02:00
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|