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

131 lines
3.5 KiB
Go
Raw Normal View History

2015-09-25 18:25:52 +02:00
/*
* MumbleDJ
* By Matthieu Grieger
* service_soundcloud.go
* Copyright (c) 2014, 2015 Matthieu Grieger (MIT License)
*/
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
// ------------------
// ServiceName is the human readable version of the service name
func (sc SoundCloud) ServiceName() string {
return "Soundcloud"
}
// TrackName is the human readable version of the service name
2015-09-26 16:01:09 +02:00
func (sc SoundCloud) TrackName() string {
return "Song"
}
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
func (sc SoundCloud) NewRequest(user *gumble.User, url string) ([]Song, error) {
2015-08-02 19:55:51 +02:00
var apiResponse *jsonq.JsonQuery
var songArray []Song
2015-08-02 19:55:51 +02:00
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-09-26 16:01:09 +02:00
return nil, 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
// Create playlist
title, _ := apiResponse.String("title")
permalink, _ := apiResponse.String("permalink_url")
playlist := &YouTubePlaylist{
id: permalink,
title: title,
}
2015-08-02 19:55:51 +02:00
// Add all tracks
for _, t := range tracks {
2015-09-26 16:01:09 +02:00
if song, err := sc.NewSong(user, jsonq.NewQuery(t), 0, playlist); err == nil {
songArray = append(songArray, song)
2015-08-03 22:40:19 +02:00
}
2015-08-02 19:55:51 +02:00
}
return songArray, nil
2015-08-02 19:55:51 +02:00
} else {
2015-08-10 17:00:47 +02:00
// SONG
// Calculate offset
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
}
}
// Add the track
2015-09-26 16:01:09 +02:00
if song, err := sc.NewSong(user, apiResponse, offset, nil); err != nil {
return nil, err
}
return append(songArray, song), err
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) (Song, 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
}
timeDuration, _ := time.ParseDuration(strconv.Itoa(durationMS/1000) + "s")
duration := timeDuration.String() //Lazy way to display time
song := &YouTubeSong{
id: strconv.Itoa(id),
title: title,
url: url,
thumbnail: thumbnail,
submitter: user,
duration: duration,
offset: offset,
format: "mp3",
playlist: playlist,
skippers: make([]string, 0),
dontSkip: false,
service: sc,
2015-08-03 22:40:19 +02:00
}
return song, nil
2015-08-02 19:55:51 +02:00
}