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_soundcloud.go

114 lines
2.8 KiB
Go
Raw Normal View History

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
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
//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-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, 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
return sc.NewSong(user, apiResponse, nil)
2015-08-03 22:40:19 +02:00
}
}
// Creates a track and adds to the queue
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 15:26:39 +02:00
id, err := trackData.Int("id")
2015-08-03 22:40:19 +02:00
if err != nil {
return "", err
}
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 {
return "", err
}
url, err := trackData.String("permalink_url")
if err != nil {
return "", err
}
2015-08-03 22:40:19 +02:00
2015-08-10 17:00:47 +02:00
song := &YouTubeDLSong{
2015-08-13 15:26:39 +02:00
id: string(id),
2015-08-03 22:40:19 +02:00
title: title,
url: url,
2015-08-03 22:40:19 +02:00
thumbnail: thumbnail,
2015-08-03 23:39:45 +02:00
submitter: user,
duration: string(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
}