This repository has been archived on 2019-06-23. You can view files and clone it, but cannot push or open issues or pull requests.
mumbledj/service.go

94 lines
2.1 KiB
Go
Raw Normal View History

2015-04-09 03:44:22 +02:00
/*
* MumbleDJ
* By Matthieu Grieger
* 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-07-28 22:40:44 +02:00
2015-07-27 23:27:23 +02:00
"github.com/layeh/gumble/gumble"
)
2015-07-27 23:13:40 +02:00
// Service interface. Each service should implement these functions
type Service interface {
ServiceName() string
2015-07-28 22:38:35 +02:00
URLRegex(string) bool // Can service deal with URL
NewRequest(*gumble.User, string) (string, error) // Create song/playlist and add to the queue
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 {
Download() error
2015-04-09 03:47:39 +02:00
Play()
Delete() error
AddSkip(string) error
RemoveSkip(string) error
SkipReached(int) bool
Submitter() string
Title() string
ID() string
Filename() string
Duration() string
Thumbnail() string
Playlist() Playlist
DontSkip() bool
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 {
AddSkip(string) error
RemoveSkip(string) error
2015-04-09 03:47:39 +02:00
DeleteSkippers()
SkipReached(int) bool
ID() string
Title() string
2015-04-09 03:47:39 +02:00
}
2015-07-28 00:00:32 +02:00
var services []Service
2015-07-30 14:48:53 +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-10 20:29:58 +02:00
Verbose("Invalid_URL")
2015-07-30 14:54:13 +02:00
return errors.New("INVALID_URL")
2015-07-28 22:38:35 +02:00
} else {
oldLength := dj.queue.Len()
2015-07-28 22:40:44 +02:00
var title string
var err error
2015-07-30 14:48:53 +02:00
2015-07-28 22:48:20 +02:00
if title, err = urlService.NewRequest(user, url); err == nil {
2015-07-30 14:48:53 +02:00
dj.client.Self.Channel.Send(fmt.Sprintf(SONG_ADDED_HTML, user.Name, title), false)
2015-07-28 22:38:35 +02:00
// 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()
2015-07-30 14:54:13 +02:00
return errors.New("FAILED_TO_DOWNLOAD")
2015-07-28 22:38:35 +02:00
}
}
2015-07-30 14:48:53 +02:00
} else {
dj.SendPrivateMessage(user, err.Error())
2015-07-28 22:38:35 +02:00
}
2015-07-30 14:48:53 +02:00
return err
2015-07-28 22:38:35 +02:00
}
}