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-]+)`
|
|
|
|
|
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
|
|
|
|
// duration, _ := apiResponse.Int("duration")
|
|
|
|
|
|
|
|
// Create playlist
|
|
|
|
title, _ := apiResponse.String("title")
|
|
|
|
permalink, _ := apiResponse.String("permalink_url")
|
2015-08-10 17:00:47 +02:00
|
|
|
playlist := &YouTubeDLPlaylist{
|
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 {
|
|
|
|
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-10 17:00:47 +02:00
|
|
|
// SONG
|
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
|
|
|
|
}
|
2015-08-03 23:44:03 +02:00
|
|
|
duration, err := trackData.String("duration")
|
2015-08-03 22:40:19 +02:00
|
|
|
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-10 17:00:47 +02:00
|
|
|
song := &YouTubeDLSong{
|
2015-08-03 22:40:19 +02:00
|
|
|
id: id,
|
|
|
|
title: title,
|
|
|
|
thumbnail: thumbnail,
|
2015-08-03 23:39:45 +02:00
|
|
|
submitter: user,
|
2015-08-03 23:44:03 +02:00
|
|
|
duration: duration,
|
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
|
|
|
}
|