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

113 lines
3.3 KiB
Go
Raw Normal View History

2015-08-02 19:55:51 +02:00
package main
import (
"errors"
"fmt"
"os"
"strconv"
"strings"
"time"
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-16 15:33:19 +02:00
timesplit := strings.Split(url, "#t=")
url = fmt.Sprintf("http://api.soundcloud.com/resolve?url=%s&client_id=%s", timesplit[0], 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) {
// 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), 0, playlist)
2015-08-03 22:40:19 +02:00
}
2015-08-16 15:33:19 +02:00
return playlist.Title(), nil
2015-08-02 19:55:51 +02:00
}
2015-08-16 15:33:19 +02:00
return "", errors.New(NO_PLAYLIST_PERMISSION_MSG)
2015-08-02 19:55:51 +02:00
} else {
2015-08-10 17:00:47 +02:00
// SONG
offset := 0
if len(timesplit) == 2 {
2015-08-16 15:33:19 +02:00
timesplit = strings.Split(timesplit[1], ":")
multiplier := 1
for i := len(timesplit) - 1; i >= 0; i-- {
2015-08-16 20:12:39 +02:00
time, _ := strconv.Atoi(timesplit[i])
offset += time * multiplier
2015-08-16 20:10:49 +02:00
multiplier *= 60
2015-08-16 15:33:19 +02:00
}
2015-08-16 15:37:49 +02:00
fmt.Printf("Offset: " + strconv.Itoa(offset)) // DEBUG
}
return sc.NewSong(user, apiResponse, offset, 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, offset int, playlist Playlist) (string, error) {
2015-08-15 23:22:59 +02:00
title, _ := trackData.String("title")
id, _ := trackData.Int("id")
durationMS, _ := trackData.Int("duration")
2015-08-15 23:22:59 +02:00
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
}
// Check song is not longer than the MaxSongDuration
if dj.conf.General.MaxSongDuration == 0 || (durationMS/1000) <= dj.conf.General.MaxSongDuration {
timeDuration, _ := time.ParseDuration(strconv.Itoa(durationMS) + "ms")
2015-08-16 15:33:19 +02:00
duration := timeDuration.String()
2015-08-15 23:22:59 +02:00
song := &YouTubeSong{
id: strconv.Itoa(id),
title: title,
url: url,
thumbnail: thumbnail,
submitter: user,
duration: duration,
offset: offset,
2015-08-15 23:22:59 +02:00
format: "mp3",
playlist: playlist,
skippers: make([]string, 0),
dontSkip: false,
}
dj.queue.AddSong(song)
2015-08-16 02:19:51 +02:00
return song.Title(), nil
2015-08-03 22:40:19 +02:00
}
2015-08-16 02:18:46 +02:00
return "", errors.New(VIDEO_TOO_LONG_MSG)
2015-08-02 19:55:51 +02:00
}