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

103 lines
2.9 KiB
Go
Raw Normal View History

2015-08-02 19:55:51 +02:00
package main
import (
"errors"
"fmt"
"os"
"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-15 23:22:59 +02:00
var soundcloudSongPattern = `https?:\/\/(www\.)?soundcloud\.com\/([\w-]+)\/([\w-]+)(#t=\n\n?(:\n\n)*)?`
2015-08-13 15:36:30 +02:00
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
// ------------------
2015-08-15 23:22:59 +02:00
// URLRegex checks to see if service will accept URL
2015-08-02 19:55:51 +02:00
func (sc SoundCloud) URLRegex(url string) bool {
return RegexpFromURL(url, []string{soundcloudSongPattern, soundcloudPlaylistPattern}) != nil
}
2015-08-15 23:22:59 +02:00
// NewRequest creates the requested song/playlist and adds to the queue
2015-08-02 19:55:51 +02:00
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")
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 {
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
}
}
2015-08-15 23:22:59 +02:00
// NewSong creates a track and adds to the queue
func (sc SoundCloud) NewSong(user *gumble.User, trackData *jsonq.JsonQuery, playlist Playlist) (Song, error) {
title, _ := trackData.String("title")
id, _ := trackData.Int("id")
duration, _ := trackData.Int("duration")
url, _ := trackData.String("permalink_url")
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 {
// Song has no artwork, using profile avatar instead
2015-08-15 23:22:59 +02:00
userObj, _ := trackData.Object("user")
thumbnail, _ = jsonq.NewQuery(userObj).String("avatar_url")
2015-08-03 22:40:19 +02:00
}
2015-08-15 23:22:59 +02:00
if dj.conf.General.MaxSongDuration == 0 || (duration/1000) <= dj.conf.General.MaxSongDuration {
song := &YouTubeSong{
id: strconv.Itoa(id),
title: title,
url: url,
thumbnail: thumbnail,
submitter: user,
duration: strconv.Itoa(duration),
format: "mp3",
playlist: playlist,
skippers: make([]string, 0),
dontSkip: false,
}
dj.queue.AddSong(song)
return song, nil
2015-08-03 22:40:19 +02:00
}
2015-08-15 23:22:59 +02:00
return nil, errors.New(VIDEO_TOO_LONG_MSG)
2015-08-02 19:55:51 +02:00
}