2015-08-02 19:55:51 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
|
|
|
|
"github.com/jmoiron/jsonq"
|
|
|
|
"github.com/layeh/gumble/gumble"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Regular expressions for soundcloud urls
|
|
|
|
var soundcloudSongPattern = `https?:\/\/(www)?\.soundcloud\.com\/([\w-]+)\/([\w-]+)`
|
|
|
|
var soundcloudPlaylistPattern = `https?:\/\/(www)?\.soundcloud\.com\/([\w-]+)\/sets\/([\w-]+)`
|
|
|
|
|
|
|
|
// ------
|
|
|
|
// TYPES
|
|
|
|
// ------
|
|
|
|
|
|
|
|
// YouTube implements the Service interface
|
|
|
|
type SoundCloud struct{}
|
|
|
|
|
|
|
|
// YouTubePlaylist holds the metadata for a YouTube playlist.
|
|
|
|
type SoundCloudPlaylist struct {
|
|
|
|
id string
|
|
|
|
title string
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------
|
|
|
|
// SOUNDCLOUD SERVICE
|
|
|
|
// ------------------
|
|
|
|
|
|
|
|
// Name of the service
|
|
|
|
func (sc SoundCloud) ServiceName() string {
|
|
|
|
return "SoundCloud"
|
|
|
|
}
|
|
|
|
|
|
|
|
// Checks to see if service will accept URL
|
|
|
|
func (sc SoundCloud) URLRegex(url string) bool {
|
|
|
|
return RegexpFromURL(url, []string{soundcloudSongPattern, soundcloudPlaylistPattern}) != nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Creates the requested song/playlist and adds to the queue
|
|
|
|
func (sc SoundCloud) NewRequest(user *gumble.User, url string) (string, error) {
|
|
|
|
var apiResponse *jsonq.JsonQuery
|
|
|
|
var err error
|
2015-08-03 23:15:51 +02:00
|
|
|
url = fmt.Sprintf("http://api.soundcloud.com/resolve?url=%s&client_id=%s", url, os.Getenv("SOUNDCLOUD_API_KEY"))
|
2015-08-02 19:55:51 +02:00
|
|
|
if apiResponse, err = PerformGetRequest(url); err != nil {
|
2015-08-03 23:15:51 +02:00
|
|
|
return "", errors.New(INVALID_API_KEY)
|
2015-08-02 19:55:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
tracks, err := apiResponse.ArrayOfObjects("tracks")
|
|
|
|
if err == nil {
|
2015-08-03 22:40:19 +02:00
|
|
|
// PLAYLIST
|
|
|
|
if dj.HasPermission(user.Name, dj.conf.Permissions.AdminAddPlaylists) {
|
|
|
|
// Check duration of playlist
|
|
|
|
// duration, _ := apiResponse.Int("duration")
|
|
|
|
|
|
|
|
// Create playlist
|
|
|
|
title, _ := apiResponse.String("title")
|
|
|
|
permalink, _ := apiResponse.String("permalink_url")
|
|
|
|
playlist := &SoundCloudPlaylist{
|
|
|
|
id: permalink,
|
|
|
|
title: title,
|
2015-08-02 19:55:51 +02:00
|
|
|
}
|
|
|
|
|
2015-08-03 22:40:19 +02:00
|
|
|
// Add all tracks
|
|
|
|
for _, t := range tracks {
|
|
|
|
sc.NewSong(user.Name, jsonq.NewQuery(t), playlist)
|
|
|
|
}
|
|
|
|
return playlist.Title(), err
|
|
|
|
} else {
|
|
|
|
return "", errors.New("NO_PLAYLIST_PERMISSION")
|
2015-08-02 19:55:51 +02:00
|
|
|
}
|
|
|
|
} else {
|
2015-08-03 22:40:19 +02:00
|
|
|
return sc.NewSong(user.Name, apiResponse, nil)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Creates a track and adds to the queue
|
2015-08-03 23:15:51 +02:00
|
|
|
func (sc SoundCloud) NewSong(user string, trackData *jsonq.JsonQuery, playlist Playlist) (string, error) {
|
2015-08-03 22:40:19 +02:00
|
|
|
title, err := trackData.String("title")
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
id, err := trackData.String("id")
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
duration, err := trackData.Int("duration")
|
|
|
|
if err != nil {
|
2015-08-02 19:55:51 +02:00
|
|
|
return "", err
|
|
|
|
}
|
2015-08-03 22:40:19 +02:00
|
|
|
thumbnail, err := trackData.String("artwork_uri")
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2015-08-03 23:18:49 +02:00
|
|
|
song := &YouTubeDL{
|
2015-08-03 22:40:19 +02:00
|
|
|
id: id,
|
|
|
|
title: title,
|
|
|
|
thumbnail: thumbnail,
|
|
|
|
submitter: user.Name,
|
|
|
|
duration: duration,
|
|
|
|
playlist: playlist,
|
|
|
|
skippers: make([]string, 0),
|
|
|
|
dontSkip: false,
|
|
|
|
}
|
|
|
|
dj.queue.AddSong(song)
|
|
|
|
return title, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ----------------
|
|
|
|
// YOUTUBE PLAYLIST
|
|
|
|
// ----------------
|
|
|
|
|
|
|
|
// AddSkip adds a skip to the playlist's skippers slice.
|
|
|
|
func (p *SoundCloudPlaylist) AddSkip(username string) error {
|
|
|
|
for _, user := range dj.playlistSkips[p.ID()] {
|
|
|
|
if username == user {
|
|
|
|
return errors.New("This user has already skipped the current song.")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
dj.playlistSkips[p.ID()] = append(dj.playlistSkips[p.ID()], username)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// RemoveSkip removes a skip from the playlist's skippers slice. If username is not in the slice
|
|
|
|
// an error is returned.
|
2015-08-03 23:15:51 +02:00
|
|
|
func (p *SoundCloudPlaylist) RemoveSkip(username string) error {
|
2015-08-03 22:40:19 +02:00
|
|
|
for i, user := range dj.playlistSkips[p.ID()] {
|
|
|
|
if username == user {
|
|
|
|
dj.playlistSkips[p.ID()] = append(dj.playlistSkips[p.ID()][:i], dj.playlistSkips[p.ID()][i+1:]...)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return errors.New("This user has not skipped the song.")
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteSkippers removes the skippers entry in dj.playlistSkips.
|
2015-08-03 23:15:51 +02:00
|
|
|
func (p *SoundCloudPlaylist) DeleteSkippers() {
|
2015-08-03 22:40:19 +02:00
|
|
|
delete(dj.playlistSkips, p.ID())
|
|
|
|
}
|
|
|
|
|
|
|
|
// SkipReached calculates the current skip ratio based on the number of users within MumbleDJ's
|
|
|
|
// channel and the number of usernames in the skippers slice. If the value is greater than or equal
|
|
|
|
// to the skip ratio defined in the config, the function returns true, and returns false otherwise.
|
2015-08-03 23:15:51 +02:00
|
|
|
func (p *SoundCloudPlaylist) SkipReached(channelUsers int) bool {
|
2015-08-03 22:40:19 +02:00
|
|
|
if float32(len(dj.playlistSkips[p.ID()]))/float32(channelUsers) >= dj.conf.General.PlaylistSkipRatio {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// ID returns the id of the YouTubePlaylist.
|
2015-08-03 23:15:51 +02:00
|
|
|
func (p *SoundCloudPlaylist) ID() string {
|
2015-08-03 22:40:19 +02:00
|
|
|
return p.id
|
|
|
|
}
|
|
|
|
|
|
|
|
// Title returns the title of the YouTubePlaylist.
|
2015-08-03 23:15:51 +02:00
|
|
|
func (p *SoundCloudPlaylist) Title() string {
|
2015-08-03 22:40:19 +02:00
|
|
|
return p.title
|
2015-08-02 19:55:51 +02:00
|
|
|
}
|