-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathspotigo.go
511 lines (455 loc) · 13.6 KB
/
spotigo.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
package spotigo
import (
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"math/big"
"net/http"
"net/url"
"regexp"
)
const alphabet = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
type Client struct {
Host string
Pass string
}
type Track struct {
Artist string
Artists []Artist
Title string
Duration int
StreamURL string
ArtURL string
TrackID string
URI string
}
type Artist struct {
Name string
TopTracks []*Track
Albums []*Album
Singles []*Album
ArtURL string
ArtistID string
URI string
}
type Album struct {
Title string
Artist string
Artists []Artist
Discs []*Disc
ArtURL string
AlbumID string
URI string
}
type Disc struct {
Number int
Tracks []*Track
}
type SpotigoTrackInfo struct {
Gid string `json:"gid"`
Name string `json:"name"` //Track name
TrackNumber int `json:"number"` //Track number
DiscNumber int `json:"disc_number"` //Disc number
Duration int `json:"duration"` //Track duration in milliseconds
Album SpotigoAlbumInfo `json:"album"` //Album
Artist []SpotigoArtistInfo `json:"artist"` //Artist
}
type SpotigoEmbed struct {
ThumbnailURL string `json:"thumbnail_url"` //Thumbnail
}
type SpotigoAlbumInfo struct {
Gid string `json:"gid"`
Name string `json:"name"` //Album name
Artist []SpotigoArtistInfo `json:"artist"` //Artist list (main, ft.)
Discs []SpotigoDisc `json:"disc"` //Virtual CD list
Date SpotigoDate `json:"date"` //Album release date
}
type SpotigoDisc struct {
Number int `json:"number"` //Virtual CD number
Tracks []SpotigoGid `json:"track"` //Virtual CD track list
}
type SpotigoArtistInfo struct {
Gid string `json:"gid"`
Name string `json:"name"` //Artist name
TopTracks []SpotigoTopTracks `json:"top_track"` //Top tracks
Albums []SpotigoAlbums `json:"album_group"` //Albums
Singles []SpotigoAlbums `json:"single_group"` //Single tracks inside albums
}
type SpotigoTopTracks struct {
Tracks []SpotigoGid `json:"track"`
}
type SpotigoAlbums struct {
Albums []SpotigoGid `json:"album"`
}
type SpotigoDate struct {
Year int
Month int
Day int
}
type SpotigoGid struct {
Gid string //Spotify GID
ID string
}
func (gid *SpotigoGid) GetID() (string, error) {
str, err := base64.StdEncoding.DecodeString(gid.Gid)
if err != nil {
return "", err
}
id := ConvertTo62(str)
return id, nil
}
type SpotigoPlaylist struct {
Gid string `json:"gid"`
Length int `json:"length"`
Attributes SpotigoPlaylistAttributes `json:"attributes"`
Contents SpotigoPlaylistContents `json:"contents"`
//Additional data added by Spotigo
UserID string `json:"-"`
PlaylistID string `json:"-"`
PlaylistURI string `json:"-"`
ImageURL string `json:"-"`
}
type SpotigoPlaylistAttributes struct {
Name string `json:"name"`
Description string `json:"description"`
}
type SpotigoPlaylistContents struct {
Position int `json:"pos"`
Truncated bool `json:"truncated"`
Items []SpotigoPlaylistItem `json:"items"`
}
type SpotigoPlaylistItem struct {
TrackURI string `json:"uri"`
Attributes SpotigoPlaylistItemAttributes `json:"attributes"`
}
type SpotigoPlaylistItemAttributes struct {
AddedBy string `json:"added_by"`
Timestamp int64 `json:"timestamp"`
}
type SpotigoSearch struct {
Results SpotigoSearchResults `json:"results"` //Search results
}
type SpotigoSearchResults struct {
Tracks SpotigoSearchHits `json:"tracks"` //Tracks
Albums SpotigoSearchHits `json:"albums"` //Albums
Artists SpotigoSearchHits `json:"artists"` //Artists
Playlists SpotigoSearchHits `json:"playlists"` //Playlists
}
type SpotigoSearchHits struct {
Hits []SpotigoSearchHit `json:"hits"` //Hits
}
type SpotigoSearchHitAlbum struct {
//Artists //Unknown data type
ImageURL string `json:"image"`
Name string `json:"name"`
URI string `json:"uri"`
}
type SpotigoSearchHitArtist struct {
ImageURL string `json:"image"`
Name string `json:"name"`
URI string `json:"uri"`
}
type SpotigoSearchHit struct {
Album SpotigoSearchHitAlbum `json:"album"`
Artists []SpotigoSearchHitArtist `json:"artists"`
ImageURL string `json:"image"`
Name string `json:"name"`
URI string `json:"uri"`
ID string `json:"-"` //Track ID, album ID, etc
Duration int `json:"duration"`
FollowersCount int `json:"followersCount"`
Author string `json:"author"`
}
func (hit *SpotigoSearchHit) GetType() string {
regex := regexp.MustCompile("^spotify:(track|artist|album|user):([a-zA-Z0-9]+).*$")
hitURI := regex.FindStringSubmatch(hit.URI)
if len(hitURI) <= 0 {
return ""
}
return hitURI[1]
}
func (hit *SpotigoSearchHit) GetID() []string {
switch hit.GetType() {
case "track", "artist", "album":
regex := regexp.MustCompile("^spotify:(track|artist|album):([a-zA-Z0-9]+)$")
hitURI := regex.FindStringSubmatch(hit.URI)
if len(hitURI) <= 0 {
return make([]string, 0)
}
return []string{hitURI[2]}
case "user":
regex := regexp.MustCompile("^spotify:user:(\\w\\S+):playlist:(\\w\\S+)$")
hitURI := regex.FindStringSubmatch(hit.URI)
if len(hitURI) <= 0 {
return make([]string, 0)
}
user, _ := url.QueryUnescape(hitURI[1])
return []string{user, hitURI[2]}
}
return make([]string, 0)
}
func (c *Client) Search(query string) (*SpotigoSearch, error) {
query = url.QueryEscape(query)
searchJSON, err := http.Get(fmt.Sprintf("http://%s/search/?query=%s&pass=%s", c.Host, query, c.Pass))
if err != nil {
return nil, err
}
searchResults := &SpotigoSearch{}
err = unmarshal(searchJSON, searchResults)
if err != nil {
return nil, err
}
return searchResults, nil
}
func (c *Client) GetTrackInfo(url string) (*Track, error) {
regex := regexp.MustCompile("^(https:\\/\\/open.spotify.com\\/track\\/|spotify:track:)([a-zA-Z0-9]+)(.*)$")
trackID := regex.FindStringSubmatch(url)
if len(trackID) <= 0 {
return nil, errors.New("error finding track ID")
}
data := &Track{}
data.TrackID = trackID[len(trackID)-2]
trackJSON, err := http.Get(fmt.Sprintf("http://%s/track/%s?pass=%s", c.Host, data.TrackID, c.Pass))
if err != nil {
return data, err
}
trackOEmbedJSON, err := http.Get(fmt.Sprintf("https://embed.spotify.com/oembed?url=spotify:track:%s", data.TrackID))
if err != nil {
return data, err
}
trackInfo := &SpotigoTrackInfo{}
err = unmarshal(trackJSON, trackInfo)
if err != nil {
return data, err
}
if trackInfo.Name == "" {
return data, errors.New("error getting track info")
}
trackOEmbed := &SpotigoEmbed{}
err = unmarshal(trackOEmbedJSON, trackOEmbed)
if err != nil {
return data, err
}
if trackOEmbed.ThumbnailURL == "" {
return data, errors.New("error getting track thumbnail")
}
data.Artist = trackInfo.Artist[0].Name
if len(trackInfo.Artist) > 1 {
data.Artist += " ft. " + trackInfo.Artist[1].Name
if len(trackInfo.Artist) > 2 {
for _, artist := range trackInfo.Artist[2:] {
data.Artist += ", " + artist.Name
}
}
}
for _, trackArtist := range trackInfo.Artist {
artistGid := &SpotigoGid{Gid: trackArtist.Gid}
artistID, err := artistGid.GetID()
if err != nil {
continue
}
artist, err := c.GetArtistInfo("spotify:artist:" + artistID)
if err == nil {
data.Artists = append(data.Artists, *artist)
}
}
data.StreamURL = fmt.Sprintf("http://%s/download/%s?pass=%s", c.Host, data.TrackID, c.Pass)
data.Title = trackInfo.Name
data.Duration = trackInfo.Duration
data.ArtURL = trackOEmbed.ThumbnailURL
return data, nil
}
func (c *Client) GetArtistInfo(url string) (*Artist, error) {
regex := regexp.MustCompile("^(https:\\/\\/open.spotify.com\\/artist\\/|spotify:artist:)([a-zA-Z0-9]+)(.*)$")
artistID := regex.FindStringSubmatch(url)
if len(artistID) <= 0 {
return nil, errors.New("error finding artist ID")
}
data := &Artist{}
data.ArtistID = artistID[len(artistID)-2]
artistJSON, err := http.Get(fmt.Sprintf("http://%s/artist/%s?pass=%s", c.Host, data.ArtistID, c.Pass))
if err != nil {
return data, err
}
artistOEmbedJSON, err := http.Get(fmt.Sprintf("https://embed.spotify.com/oembed?url=spotify:artist:%s", data.ArtistID))
if err != nil {
return data, err
}
artistInfo := &SpotigoArtistInfo{}
err = unmarshal(artistJSON, artistInfo)
if err != nil {
return data, err
}
if artistInfo.Name == "" {
return data, errors.New("error getting artist info")
}
artistOEmbed := &SpotigoEmbed{}
err = unmarshal(artistOEmbedJSON, artistOEmbed)
if err != nil {
return data, err
}
if artistOEmbed.ThumbnailURL == "" {
return data, errors.New("error getting artist thumbnail")
}
data.Name = artistInfo.Name
if len(artistInfo.TopTracks) > 0 {
for _, topTrack := range artistInfo.TopTracks[0].Tracks {
trackID, err := topTrack.GetID()
if err != nil {
continue
}
/*trackInfo, err := c.GetTrackInfo("spotify:track:" + trackID)
if err == nil {
data.TopTracks = append(data.TopTracks, trackInfo)
}*/
data.TopTracks = append(data.TopTracks, &Track{TrackID: trackID, URI: "spotify:track:" + trackID})
}
}
/*for _, albumGroup := range artistInfo.Albums {
for _, album := range albumGroup.Albums {
albumID, err := album.GetID()
if err != nil {
continue
}
albumInfo, err := c.GetAlbumInfo("spotify:album:" + albumID)
if err == nil {
data.Albums = append(data.Albums, albumInfo)
}
}
}
for _, singleGroup := range artistInfo.Singles {
for _, album := range singleGroup.Albums {
albumID, err := album.GetID()
if err != nil {
continue
}
albumInfo, err := c.GetAlbumInfo("spotify:album:" + albumID)
if err == nil {
data.Albums = append(data.Albums, albumInfo)
}
}
}*/
data.ArtURL = artistOEmbed.ThumbnailURL
return data, nil
}
func (c *Client) GetAlbumInfo(url string) (*Album, error) {
regex := regexp.MustCompile("^(https:\\/\\/open.spotify.com\\/album\\/|spotify:album:)([a-zA-Z0-9]+)(.*)$")
albumID := regex.FindStringSubmatch(url)
if len(albumID) <= 0 {
return nil, errors.New("error finding album ID")
}
data := &Album{}
data.AlbumID = albumID[len(albumID)-2]
albumJSON, err := http.Get(fmt.Sprintf("http://%s/album/%s?pass=%s", c.Host, data.AlbumID, c.Pass))
if err != nil {
return data, err
}
albumOEmbedJSON, err := http.Get(fmt.Sprintf("https://embed.spotify.com/oembed?url=spotify:album:%s", data.AlbumID))
if err != nil {
return data, err
}
albumInfo := &SpotigoAlbumInfo{}
err = unmarshal(albumJSON, albumInfo)
if err != nil {
return data, err
}
if albumInfo.Name == "" {
return data, errors.New("error getting album info")
}
albumOEmbed := &SpotigoEmbed{}
err = unmarshal(albumOEmbedJSON, albumOEmbed)
if err != nil {
return data, err
}
if albumOEmbed.ThumbnailURL == "" {
return data, errors.New("error getting album thumbnail")
}
data.Title = albumInfo.Name
if len(albumInfo.Artist) > 1 {
data.Artist += " ft. " + albumInfo.Artist[1].Name
if len(albumInfo.Artist) > 2 {
for _, artist := range albumInfo.Artist[2:] {
data.Artist += ", " + artist.Name
}
}
}
for _, albumArtist := range albumInfo.Artist {
artistGid := &SpotigoGid{Gid: albumArtist.Gid}
artistID, err := artistGid.GetID()
if err != nil {
continue
}
artist, err := c.GetArtistInfo("spotify:artist:" + artistID)
if err == nil {
data.Artists = append(data.Artists, *artist)
}
}
for discN, albumDisc := range albumInfo.Discs {
disc := &Disc{Number: discN}
for _, track := range albumDisc.Tracks {
trackID, err := track.GetID()
if err != nil {
continue
}
/*trackInfo, err := c.GetTrackInfo("spotify:track:" + trackID)
if err == nil {
disc.Tracks = append(disc.Tracks, trackInfo)
}*/
disc.Tracks = append(disc.Tracks, &Track{TrackID: trackID, URI: "spotify:track:" + trackID})
}
data.Discs = append(data.Discs, disc)
}
data.ArtURL = albumOEmbed.ThumbnailURL
return data, nil
}
func (c *Client) GetPlaylist(url string) (*SpotigoPlaylist, error) {
regex := regexp.MustCompile("^(https:\\/\\/open.spotify.com\\/user\\/|spotify:user:)(\\w\\S+)(\\/playlist\\/|:playlist:)(\\w\\S+)(.*)$")
matches := regex.FindStringSubmatch(url)
if len(matches) <= 0 {
return nil, errors.New("error finding playlist/user ID")
}
userID := matches[len(matches)-4]
playlistID := matches[len(matches)-2]
data := &SpotigoPlaylist{}
data.UserID = userID
data.PlaylistID = playlistID
playlistJSON, err := http.Get(fmt.Sprintf("http://%s/playlist/spotify:user:%s:playlist:%s?pass=%s", c.Host, userID, playlistID, c.Pass))
if err != nil {
return data, err
}
err = unmarshal(playlistJSON, data)
if err != nil {
return data, err
}
return data, nil
}
// Gets the json from the API and assigns the data to the target.
// The target being a QueryResult struct
func unmarshal(body *http.Response, target interface{}) error {
defer body.Body.Close()
return json.NewDecoder(body.Body).Decode(target)
}
func ConvertTo62(raw []byte) string {
bi := big.Int{}
bi.SetBytes(raw)
rem := big.NewInt(0)
base := big.NewInt(62)
zero := big.NewInt(0)
result := ""
for bi.Cmp(zero) > 0 {
_, rem = bi.DivMod(&bi, base, rem)
result += string(alphabet[int(rem.Uint64())])
}
for len(result) < 22 {
result += "0"
}
return reverse(result)
}
func reverse(s string) string {
r := []rune(s)
for i, j := 0, len(r)-1; i < len(r)/2; i, j = i+1, j-1 {
r[i], r[j] = r[j], r[i]
}
return string(r)
}