/
soundcloud.go
145 lines (130 loc) · 3.1 KB
/
soundcloud.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package soundcloud
import (
"encoding/gob"
"fmt"
"io"
"log"
"net/http"
"time"
"github.com/mjibson/mog/codec"
"github.com/mjibson/mog/codec/mpa"
"github.com/mjibson/mog/protocol"
"github.com/mjibson/mog/protocol/soundcloud/soundcloud"
"golang.org/x/oauth2"
"google.golang.org/api/googleapi"
)
var config *oauth2.Config
var oauthClientID string
func init() {
gob.Register(new(Soundcloud))
}
func Init(clientID, clientSecret, redirect string) {
config := &oauth2.Config{
ClientID: clientID,
ClientSecret: clientSecret,
RedirectURL: redirect + "soundcloud",
Scopes: []string{"non-expiring"},
Endpoint: oauth2.Endpoint{
AuthURL: "https://soundcloud.com/connect",
TokenURL: "https://api.soundcloud.com/oauth2/token",
},
}
oauthClientID = clientID
protocol.RegisterOAuth("soundcloud", config, New)
}
func (s *Soundcloud) getService() (*soundcloud.Service, *http.Client, error) {
c := config.Client(oauth2.NoContext, s.Token)
svc, err := soundcloud.New(c, s.Token)
return svc, c, err
}
type Soundcloud struct {
Token *oauth2.Token
Name string
Favorites map[codec.ID]*soundcloud.Favorite
}
func New(params []string, token *oauth2.Token) (protocol.Instance, error) {
if token == nil {
return nil, fmt.Errorf("expected oauth token")
}
s := &Soundcloud{
Token: token,
}
service, _, err := s.getService()
if err != nil {
return nil, err
}
me, err := service.Me().Do()
if err != nil {
return nil, err
}
s.Name = me.Username
return s, nil
}
func (s *Soundcloud) Key() string {
return s.Name
}
func (s *Soundcloud) Info(id codec.ID) (*codec.SongInfo, error) {
f := s.Favorites[id]
if f == nil {
return nil, fmt.Errorf("could not find %v", id)
}
return toInfo(f), nil
}
func toInfo(f *soundcloud.Favorite) *codec.SongInfo {
return &codec.SongInfo{
Time: time.Duration(f.Duration) * time.Millisecond,
Artist: f.User.Username,
Title: f.Title,
ImageURL: f.ArtworkURL,
}
}
func (s *Soundcloud) SongList() protocol.SongList {
m := make(protocol.SongList)
for k, f := range s.Favorites {
m[k] = toInfo(f)
}
return m
}
func (s *Soundcloud) List() (protocol.SongList, error) {
if len(s.Favorites) == 0 {
return s.Refresh()
}
return s.SongList(), nil
}
func (s *Soundcloud) GetSong(id codec.ID) (codec.Song, error) {
log.Println("SOUNDCLOUD", id)
_, client, err := s.getService()
if err != nil {
return nil, err
}
f := s.Favorites[id]
if f == nil {
return nil, fmt.Errorf("bad id: %v", id)
}
return mpa.NewSong(func() (io.ReadCloser, int64, error) {
res, err := client.Get(f.StreamURL + "?client_id=" + oauthClientID)
if err != nil {
return nil, 0, err
}
if err := googleapi.CheckResponse(res); err != nil {
return nil, 0, err
}
return res.Body, 0, nil
})
}
func (s *Soundcloud) Refresh() (protocol.SongList, error) {
service, _, err := s.getService()
if err != nil {
return nil, err
}
favorites, err := service.Favorites().Do()
if err != nil {
return nil, err
}
favs := make(map[codec.ID]*soundcloud.Favorite)
for _, f := range favorites {
favs[codec.Int64(f.ID)] = f
}
s.Favorites = favs
return s.SongList(), err
}