2015-08-02 19:55:51 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
2015-08-13 16:07:34 +02:00
|
|
|
"strconv"
|
2015-08-02 19:55:51 +02:00
|
|
|
|
|
|
|
"github.com/jmoiron/jsonq"
|
|
|
|
"github.com/layeh/gumble/gumble"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Regular expressions for soundcloud urls
|
2015-08-13 15:36:30 +02:00
|
|
|
var soundcloudSongPattern = `https?:\/\/(www\.)?soundcloud\.com\/([\w-]+)\/([\w-]+)`
|
|
|
|
var soundcloudPlaylistPattern = `https?:\/\/(www\.)?soundcloud\.com\/([\w-]+)\/sets\/([\w-]+)`
|
2015-08-02 19:55:51 +02:00
|
|
|
|
2015-08-10 17:00:47 +02:00
|
|
|
// SoundCloud implements the Service interface
|
2015-08-02 19:55:51 +02:00
|
|
|
type SoundCloud struct{}
|
|
|
|
|
|
|
|
// ------------------
|
|
|
|
// 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
|
2015-08-13 15:00:12 +02:00
|
|
|
//duration, _ := apiResponse.Int("duration")
|
2015-08-03 22:40:19 +02:00
|
|
|
|
|
|
|
// Create playlist
|
|
|
|
title, _ := apiResponse.String("title")
|
|
|
|
permalink, _ := apiResponse.String("permalink_url")
|
2015-08-15 20:56:00 +02:00
|
|
|
playlist := &YouTubePlaylist{
|
2015-08-03 22:40:19 +02:00
|
|
|
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 {
|
2015-08-13 14:44:38 +02:00
|
|
|
sc.NewSong(user, jsonq.NewQuery(t), playlist)
|
2015-08-03 22:40:19 +02:00
|
|
|
}
|
2015-08-10 20:29:58 +02:00
|
|
|
if err == nil {
|
|
|
|
return playlist.Title(), nil
|
|
|
|
} else {
|
|
|
|
Verbose("soundcloud.NewRequest: " + err.Error())
|
2015-08-10 20:31:12 +02:00
|
|
|
return "", err
|
2015-08-10 20:29:58 +02:00
|
|
|
}
|
2015-08-03 22:40:19 +02:00
|
|
|
} else {
|
|
|
|
return "", errors.New("NO_PLAYLIST_PERMISSION")
|
2015-08-02 19:55:51 +02:00
|
|
|
}
|
|
|
|
} else {
|
2015-08-10 17:00:47 +02:00
|
|
|
// SONG
|
2015-08-13 14:44:38 +02:00
|
|
|
return sc.NewSong(user, apiResponse, nil)
|
2015-08-03 22:40:19 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Creates a track and adds to the queue
|
2015-08-13 14:44:38 +02:00
|
|
|
func (sc SoundCloud) NewSong(user *gumble.User, 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
|
|
|
|
}
|
2015-08-13 16:32:20 +02:00
|
|
|
|
2015-08-13 15:26:39 +02:00
|
|
|
id, err := trackData.Int("id")
|
2015-08-03 22:40:19 +02:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2015-08-13 16:32:20 +02:00
|
|
|
|
2015-08-13 14:44:38 +02:00
|
|
|
duration, err := trackData.Int("duration")
|
2015-08-03 22:40:19 +02:00
|
|
|
if err != nil {
|
2015-08-02 19:55:51 +02:00
|
|
|
return "", err
|
|
|
|
}
|
2015-08-13 15:30:11 +02:00
|
|
|
thumbnail, err := trackData.String("artwork_url")
|
2015-08-03 22:40:19 +02:00
|
|
|
if err != nil {
|
2015-08-13 16:32:20 +02:00
|
|
|
// Song has no artwork, using profile avatar instead
|
|
|
|
userObj, err := trackData.Object("user")
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
thumbnail, err = jsonq.NewQuery(userObj).String("avatar_url")
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2015-08-03 22:40:19 +02:00
|
|
|
}
|
2015-08-13 16:32:20 +02:00
|
|
|
|
2015-08-13 14:44:38 +02:00
|
|
|
url, err := trackData.String("permalink_url")
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2015-08-03 22:40:19 +02:00
|
|
|
|
2015-08-15 20:56:00 +02:00
|
|
|
song := &YouTubeSong{
|
2015-08-13 16:07:34 +02:00
|
|
|
id: strconv.Itoa(id),
|
2015-08-03 22:40:19 +02:00
|
|
|
title: title,
|
2015-08-13 14:44:38 +02:00
|
|
|
url: url,
|
2015-08-03 22:40:19 +02:00
|
|
|
thumbnail: thumbnail,
|
2015-08-03 23:39:45 +02:00
|
|
|
submitter: user,
|
2015-08-13 16:07:34 +02:00
|
|
|
duration: strconv.Itoa(duration),
|
2015-08-13 15:33:37 +02:00
|
|
|
format: "mp3",
|
2015-08-03 22:40:19 +02:00
|
|
|
playlist: playlist,
|
|
|
|
skippers: make([]string, 0),
|
|
|
|
dontSkip: false,
|
|
|
|
}
|
|
|
|
dj.queue.AddSong(song)
|
2015-08-10 17:00:47 +02:00
|
|
|
return title, nil
|
2015-08-02 19:55:51 +02:00
|
|
|
}
|