-
Notifications
You must be signed in to change notification settings - Fork 0
/
spotify.go
700 lines (653 loc) · 19.2 KB
/
spotify.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
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
package main
import (
"encoding/base64"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"os"
"regexp"
"sync"
"time"
"github.com/eolso/librespot-golang/Spotify"
"github.com/eolso/librespot-golang/librespot"
"github.com/eolso/librespot-golang/librespot/core"
"github.com/eolso/librespot-golang/librespot/mercury"
"github.com/eolso/librespot-golang/librespot/utils"
)
/*
- GetPlaylist(playlistID string) (*ObjectPlaylist, error)
- GetUserPlaylist(userID, playlistID string) (*ObjectPlaylist, error)
- GetSuggest(query string) ([]string, error)
*/
var spoturire = regexp.MustCompile(`<a href="spotify:(.*?):(.*?)">(.*?)<\/a>`)
// SpotifyClient holds a Spotify client
type SpotifyClient struct {
sync.Mutex
Session *core.Session
Service *Service
}
func (s *SpotifyClient) Provider() string {
return "spotify"
}
func (s *SpotifyClient) SetService(service *Service) {
s.Service = service
}
func (s *SpotifyClient) Authenticate(cfg *HandlerConfig) (handler Handler, err error) {
if cfg.Username == "" || cfg.Password == "" || cfg.BlobPath == "" {
return nil, fmt.Errorf("spotify: must provide username, password, and path to file for storing auth blob")
}
username := cfg.Username
password := cfg.Password
deviceName := cfg.DeviceName
blobPath := cfg.BlobPath
if _, err := os.Stat(blobPath); !os.IsNotExist(err) {
blobBytes, err := ioutil.ReadFile(blobPath)
if err != nil {
return nil, err
}
session, err := librespot.LoginSaved(username, blobBytes, deviceName)
if err != nil {
return nil, err
}
s.Session = session
return s, nil
}
session, err := librespot.Login(username, password, deviceName)
if err != nil {
return nil, err
}
s.Session = session
err = ioutil.WriteFile(blobPath, session.ReusableAuthBlob(), 0600)
if err != nil {
return nil, err
}
return s, nil
}
// NewSpotify authenticates to Spotify and returns a Spotify session
func NewSpotify(username, password, deviceName, blobPath string) (*SpotifyClient, error) {
if username == "" || password == "" || blobPath == "" {
return nil, fmt.Errorf("must provide username, password, and filepath to auth blob")
}
//Don't return unless blob authentication is successful, we'll reauth if it fails and only return an error then
if _, err := os.Stat(blobPath); !os.IsNotExist(err) { //File exists
blobBytes, err := ioutil.ReadFile(blobPath)
if err == nil {
session, err := librespot.LoginSaved(username, blobBytes, deviceName)
if err == nil {
return &SpotifyClient{Session: session}, nil
}
}
}
session, err := librespot.Login(username, password, deviceName)
if err != nil {
return nil, err
}
err = ioutil.WriteFile(blobPath, session.ReusableAuthBlob(), 0600)
if err != nil {
return nil, err
}
return &SpotifyClient{Session: session}, nil
}
func (s *SpotifyClient) mercuryGet(url string) []byte {
m := s.Session.Mercury()
done := make(chan []byte)
go m.Request(mercury.Request{
Method: "GET",
Uri: url,
Payload: [][]byte{},
}, func(res mercury.Response) {
done <- res.CombinePayload()
})
result := <-done
return result
}
func (s *SpotifyClient) mercuryGetJson(url string, result interface{}) (err error) {
data := s.mercuryGet(url)
//Trace.Printf("Spotify Mercury JSON: %s\n", data)
err = json.Unmarshal(data, result)
return
}
// Creator gets an artist object from Spotify
func (s *SpotifyClient) Creator(creatorID string) (creator *ObjectCreator, err error) {
s.Lock()
defer s.Unlock()
spotCreator, err := s.Session.Mercury().GetArtist(utils.Base62ToHex(creatorID))
if err != nil {
return nil, err
}
biography := ""
if len(spotCreator.Biography) > 0 {
spotBios := spotCreator.Biography
for i := 0; i < len(spotBios); i++ {
if spotBios[i] != nil && spotBios[i].Text != nil {
biography += *spotBios[i].Text
if i == len(spotBios)-1 {
biography += "\n\n"
}
}
}
if biography != "" {
biography = s.ReplaceURI(biography)
}
}
topTracks := make([]*Object, 0)
for i := 0; i < len(spotCreator.TopTrack); i++ {
for j := 0; j < len(spotCreator.TopTrack[i].Track); j++ {
topTrack := spotCreator.TopTrack[i].Track[j]
objStream := &ObjectStream{URI: "spotify:track:" + gid2Id(topTrack.Gid)}
obj := &Object{
URI: "spotify:track:" + gid2Id(topTrack.Gid),
Type: "stream",
Provider: "spotify",
Object: &json.RawMessage{},
}
obj.Object.UnmarshalJSON(objStream.JSON())
topTracks = append(topTracks, obj)
}
}
albums := make([]*Object, 0)
for i := 0; i < len(spotCreator.AlbumGroup); i++ {
for j := 0; j < len(spotCreator.AlbumGroup[i].Album); j++ {
spotAlbum := spotCreator.AlbumGroup[i].Album[j]
objAlbum := &ObjectAlbum{URI: "spotify:album:" + gid2Id(spotAlbum.Gid)}
obj := &Object{
URI: "spotify:album:" + gid2Id(spotAlbum.Gid),
Type: "album",
Provider: "spotify",
Object: &json.RawMessage{},
}
obj.Object.UnmarshalJSON(objAlbum.JSON())
albums = append(albums, obj)
}
}
appearances := make([]*Object, 0)
for i := 0; i < len(spotCreator.AppearsOnGroup); i++ {
for j := 0; j < len(spotCreator.AppearsOnGroup[i].Album); j++ {
spotAlbum := spotCreator.AppearsOnGroup[i].Album[j]
objAlbum := &ObjectAlbum{URI: "spotify:album:" + gid2Id(spotAlbum.Gid)}
obj := &Object{
URI: "spotify:album:" + gid2Id(spotAlbum.Gid),
Type: "album",
Provider: "spotify",
Object: &json.RawMessage{},
}
obj.Object.UnmarshalJSON(objAlbum.JSON())
appearances = append(appearances, obj)
}
}
singles := make([]*Object, 0)
for i := 0; i < len(spotCreator.SingleGroup); i++ {
for j := 0; j < len(spotCreator.SingleGroup[i].Album); j++ {
spotAlbum := spotCreator.SingleGroup[i].Album[j]
objAlbum := &ObjectAlbum{URI: "spotify:album:" + gid2Id(spotAlbum.Gid)}
obj := &Object{
URI: "spotify:album:" + gid2Id(spotAlbum.Gid),
Type: "album",
Provider: "spotify",
Object: &json.RawMessage{},
}
obj.Object.UnmarshalJSON(objAlbum.JSON())
singles = append(singles, obj)
}
}
related := make([]*Object, len(spotCreator.Related))
for i := 0; i < len(related); i++ {
relatedCreator := spotCreator.Related[i]
objCreator := &ObjectCreator{
URI: "spotify:artist:" + gid2Id(relatedCreator.Gid),
Name: *relatedCreator.Name,
}
related[i] = &Object{
URI: "spotify:artist:" + gid2Id(relatedCreator.Gid),
Type: "creator",
Provider: "spotify",
Object: &json.RawMessage{},
}
related[i].Object.UnmarshalJSON(objCreator.JSON())
}
creator = &ObjectCreator{
URI: "spotify:artist:" + creatorID,
Name: *spotCreator.Name,
Description: biography,
Genres: spotCreator.Genre,
TopStreams: topTracks,
Albums: albums,
Appearances: appearances,
Singles: singles,
Related: related,
}
return
}
// Album gets an album object from Spotify
func (s *SpotifyClient) Album(albumID string) (album *ObjectAlbum, err error) {
s.Lock()
defer s.Unlock()
spotAlbum, err := s.Session.Mercury().GetAlbum(utils.Base62ToHex(albumID))
if err != nil {
return nil, err
}
creators := make([]*Object, len(spotAlbum.Artist))
for i := 0; i < len(creators); i++ {
objCreator := &ObjectCreator{
URI: "spotify:artist:" + gid2Id(spotAlbum.Artist[i].Gid),
Name: *spotAlbum.Artist[i].Name,
}
creators[i] = &Object{
URI: "spotify:artist:" + gid2Id(spotAlbum.Artist[i].Gid),
Type: "creator",
Provider: "spotify",
Object: &json.RawMessage{},
}
creators[i].Object.UnmarshalJSON(objCreator.JSON())
}
discs := make([]*ObjectDisc, len(spotAlbum.Disc))
for i := 0; i < len(discs); i++ {
discStreams := make([]*Object, len(spotAlbum.Disc[i].Track))
for j := 0; j < len(spotAlbum.Disc[i].Track); j++ {
spotTrack := spotAlbum.Disc[i].Track[j]
objStream := &ObjectStream{
URI: "spotify:track:" + gid2Id(spotTrack.Gid),
ID: gid2Id(spotTrack.Gid),
}
discStreams[j] = &Object{
URI: "spotify:track:" + gid2Id(spotTrack.Gid),
Type: "stream",
Provider: "spotify",
Object: &json.RawMessage{},
}
discStreams[j].Object.UnmarshalJSON(objStream.JSON())
}
discs[i] = &ObjectDisc{
Streams: discStreams,
}
if spotAlbum.Disc[i].Number != nil {
discs[i].Disc = int(*spotAlbum.Disc[i].Number)
}
if spotAlbum.Disc[i].Name != nil {
discs[i].Name = *spotAlbum.Disc[i].Name
}
}
copyrights := make([]string, len(spotAlbum.Copyright))
for i := 0; i < len(copyrights); i++ {
copyrights[i] = *spotAlbum.Copyright[i].Text
}
album = &ObjectAlbum{
URI: "spotify:album:" + albumID,
Name: *spotAlbum.Name,
Creators: creators,
Discs: discs,
Copyrights: copyrights,
}
if spotAlbum.Label != nil {
album.Label = *spotAlbum.Label
}
if spotAlbum.Date != nil {
date := *spotAlbum.Date
dateTime := ""
if date.Year != nil && date.Month != nil {
dateTime = fmt.Sprintf("%d-%d", *date.Year, *date.Month)
if date.Day != nil {
dateTime += fmt.Sprintf("-%d", *date.Day)
}
}
if date.Hour != nil && date.Minute != nil {
if dateTime != "" {
dateTime += " "
}
dateTime += fmt.Sprintf("%d:%d", *date.Hour, *date.Minute)
}
album.DateTime = dateTime
}
return
}
// Stream gets a stream object from Spotify
func (s *SpotifyClient) Stream(trackID string) (stream *ObjectStream, err error) {
s.Lock()
defer s.Unlock()
spotTrack, err := s.Session.Mercury().GetTrack(utils.Base62ToHex(trackID))
if err != nil {
return nil, err
}
creators := make([]*Object, len(spotTrack.Artist))
for i := 0; i < len(creators); i++ {
creator := spotTrack.Artist[i]
objCreator := &ObjectCreator{
Name: *creator.Name,
URI: "spotify:artist:" + gid2Id(creator.Gid),
}
creators[i] = &Object{
URI: "spotify:artist:" + gid2Id(creator.Gid),
Type: "creator",
Provider: "spotify",
Object: &json.RawMessage{},
}
creators[i].Object.UnmarshalJSON(objCreator.JSON())
}
formats := make([]*ObjectFormat, 0)
formatList := s.FormatList()
for i := 0; i < len(formatList); i++ {
for j := 0; j < len(spotTrack.File); j++ {
switch i {
case 0:
if *spotTrack.File[j].Format == Spotify.AudioFile_OGG_VORBIS_320 {
formats = append(formats, formatList[i])
formats[len(formats)-1].File = spotTrack.File[j]
break
}
case 1:
if *spotTrack.File[j].Format == Spotify.AudioFile_OGG_VORBIS_320 {
formats = append(formats, formatList[i])
formats[len(formats)-1].File = spotTrack.File[j]
break
}
case 2:
if *spotTrack.File[j].Format == Spotify.AudioFile_OGG_VORBIS_320 {
formats = append(formats, formatList[i])
formats[len(formats)-1].File = spotTrack.File[j]
break
}
case 3:
if *spotTrack.File[j].Format == Spotify.AudioFile_OGG_VORBIS_320 {
formats = append(formats, formatList[i])
formats[len(formats)-1].File = spotTrack.File[j]
break
}
case 4:
if *spotTrack.File[j].Format == Spotify.AudioFile_OGG_VORBIS_320 {
formats = append(formats, formatList[i])
formats[len(formats)-1].File = spotTrack.File[j]
break
}
case 5:
if *spotTrack.File[j].Format == Spotify.AudioFile_OGG_VORBIS_320 {
formats = append(formats, formatList[i])
formats[len(formats)-1].File = spotTrack.File[j]
break
}
case 6:
if *spotTrack.File[j].Format == Spotify.AudioFile_OGG_VORBIS_320 {
formats = append(formats, formatList[i])
formats[len(formats)-1].File = spotTrack.File[j]
break
}
}
}
}
stream = &ObjectStream{
Provider: s.Provider(),
URI: "spotify:track:" + trackID,
ID: trackID,
Name: *spotTrack.Name,
Creators: creators,
Duration: int64(*spotTrack.Duration) / 1000,
Formats: formats,
}
if spotTrack.Album != nil {
album := &Object{
URI: "spotify:album:" + gid2Id(spotTrack.Album.Gid),
Type: "album",
Provider: "spotify",
Object: &json.RawMessage{},
}
stream.Album = album
albumObj := &ObjectAlbum{
Name: *spotTrack.Album.Name,
URI: "spotify:album:" + gid2Id(spotTrack.Album.Gid),
}
stream.Album.Object.UnmarshalJSON(albumObj.JSON())
}
return
}
// Format gets a format object from a Spotify stream object
func (s *SpotifyClient) StreamFormat(w http.ResponseWriter, r *http.Request, stream *ObjectStream, format int) (err error) {
objFormat := stream.GetFormat(format)
if objFormat == nil {
return fmt.Errorf("spotify: unknown format %d for stream %s", format, stream.ID)
}
file, ok := objFormat.File.(*Spotify.AudioFile)
if !ok || file == nil {
stream, err = s.Stream(stream.ID)
if err != nil {
return fmt.Errorf("spotify: failed to get stream %s: %v", stream.ID, err)
}
objFormat = stream.GetFormat(format)
if objFormat == nil {
return fmt.Errorf("spotify: unknown format %d for stream %s after resyncing", format, stream.ID)
}
file = objFormat.File.(*Spotify.AudioFile)
if file == nil {
return fmt.Errorf("spotify: unknown file for format %d from stream %s after resyncing", format, stream.ID)
}
}
streamer, err := s.Session.Player().LoadTrackWithIdAndFormat(file.FileId, file.GetFormat(), id2Gid(stream.ID))
if err != nil {
return fmt.Errorf("spotify: failed to load track for stream %s: %v", stream.ID, err)
}
w.Header().Set("Content-Type", "audio/ogg")
http.ServeContent(w, r, stream.ID, time.Time{}, streamer)
return nil
}
// FormatList returns all the possible formats as templates ordered from best to worst
func (s *SpotifyClient) FormatList() (formats []*ObjectFormat) {
formats = []*ObjectFormat{
&ObjectFormat{
ID: 0,
Name: "Very High OGG",
Format: "ogg",
Codec: "vorbis",
BitRate: 320000,
BitDepth: 16,
SampleRate: 44100,
},
&ObjectFormat{
ID: 1,
Name: "Very High MP3",
Format: "mp3",
Codec: "mp3",
BitRate: 320000,
BitDepth: 16,
SampleRate: 44100,
},
&ObjectFormat{
ID: 2,
Name: "High MP3",
Format: "mp3",
Codec: "mp3",
BitRate: 256000,
BitDepth: 16,
SampleRate: 44100,
},
&ObjectFormat{
ID: 3,
Name: "Normal OGG",
Format: "ogg",
Codec: "vorbis",
BitRate: 160000,
BitDepth: 16,
SampleRate: 44100,
},
&ObjectFormat{
ID: 4,
Name: "Normal MP3",
Format: "mp3",
Codec: "mp3",
BitRate: 160000,
BitDepth: 16,
SampleRate: 44100,
},
&ObjectFormat{
ID: 5,
Name: "Low OGG",
Format: "ogg",
Codec: "vorbis",
BitRate: 96000,
BitDepth: 16,
SampleRate: 44100,
},
&ObjectFormat{
ID: 6,
Name: "Low MP3",
Format: "mp3",
Codec: "mp3",
BitRate: 96000,
BitDepth: 16,
SampleRate: 44100,
},
}
return
}
// Search returns the results matching a given query
func (s *SpotifyClient) Search(query string) (results *ObjectSearchResults, err error) {
s.Lock()
defer s.Unlock()
searchResponse, err := s.Session.Mercury().Search(query, 10, s.Session.Country(), s.Session.Username())
if err != nil {
return nil, err
}
results = &ObjectSearchResults{}
results.Query = query
if searchResponse.Results.Artists.Total > 0 {
artists := searchResponse.Results.Artists.Hits
for i := 0; i < len(artists); i++ {
creator := &ObjectCreator{
Name: artists[i].Name,
URI: artists[i].Uri,
}
obj := &Object{URI: creator.URI, Type: "creator", Provider: "spotify", Object: &json.RawMessage{}}
obj.Object.UnmarshalJSON(creator.JSON())
results.Creators = append(results.Creators, obj)
}
}
if searchResponse.Results.Albums.Total > 0 {
albums := searchResponse.Results.Albums.Hits
for i := 0; i < len(albums); i++ {
album := &ObjectAlbum{
Name: albums[i].Name,
URI: albums[i].Uri,
}
obj := &Object{URI: album.URI, Type: "album", Provider: "spotify", Object: &json.RawMessage{}}
obj.Object.UnmarshalJSON(album.JSON())
results.Albums = append(results.Albums, obj)
}
}
if searchResponse.Results.Tracks.Total > 0 {
tracks := searchResponse.Results.Tracks.Hits
for i := 0; i < len(tracks); i++ {
stream := &ObjectStream{Name: tracks[i].Name}
stream.URI = tracks[i].Uri
for _, artist := range tracks[i].Artists {
objCreator := &ObjectCreator{Name: artist.Name, URI: artist.Uri}
obj := &Object{URI: artist.Uri, Type: "creator", Provider: "spotify", Object: &json.RawMessage{}}
obj.Object.UnmarshalJSON(objCreator.JSON())
stream.Creators = append(stream.Creators, obj)
}
stream.Album = &Object{URI: tracks[i].Album.Uri, Type: "album", Provider: "spotify", Object: &json.RawMessage{}}
objAlbum := &ObjectAlbum{Name: tracks[i].Album.Name, URI: tracks[i].Album.Uri}
stream.Album.Object.UnmarshalJSON(objAlbum.JSON())
stream.Artworks = []*ObjectArtwork{
&ObjectArtwork{
URL: tracks[i].Image,
},
}
stream.Duration = int64(tracks[i].Duration) / 1000
objStream := &Object{URI: stream.URI, Type: "stream", Provider: "spotify", Object: &json.RawMessage{}}
objStream.Object.UnmarshalJSON(stream.JSON())
results.Streams = append(results.Streams, objStream)
}
}
/*if searchResponse.Results.Playlists.Total > 0 {
playlists := searchResponse.Results.Playlists.Hits
for i := 0; i < len(playlists); i++ {
playlist := &ObjectPlaylist{
Name: playlists[i].Name,
URI: playlists[i].Uri,
}
results.Playlists = append(results.Playlists, &Object{Type: "playlist", Provider: "spotify", Object: playlist})
}
}*/
return
}
// gid2Id converts a given GID to an ID
func gid2Id(gid []byte) (id string) {
dstId := make([]byte, base64.StdEncoding.DecodedLen(len(gid)))
_, err := base64.StdEncoding.Decode(dstId, gid)
if err != nil {
//Error.Printf("Gid2Id: %v str(%v) err(%s)\n", gid, string(gid), err.Error())
id = utils.ConvertTo62(gid)
return
}
id = utils.ConvertTo62(dstId)
return
}
// id2Gid converts a given ID to a GID
func id2Gid(id string) (gid []byte) {
dstId := make([]byte, base64.StdEncoding.EncodedLen(len(id)))
base64.StdEncoding.Encode(dstId, []byte(id))
if len(dstId) > 0 {
id = string(dstId)
}
gid = utils.Convert62(id)
return
}
type SpotifyLyrics struct {
Colors *Colors
HasVocalRemoval bool
Lyrics *SpotifyLyricsInner
}
type Colors struct {
Background json.Number
HighlightText json.Number
Text json.Number
}
type SpotifyLyricsInner struct {
FullscreenAction string
IsDenseTypeface bool
IsRtlLanguage bool
Language string
Lines []*Line
Provider string
ProviderDisplayName string
ProviderLyricsID string
SyncLyricsURI string
SyncType json.Number //0=Unsynced,1=LineSynced
}
type Line struct {
StartTimeMs string
EndTimeMs string
Words string
Syllables []json.RawMessage
}
func (s *SpotifyClient) Transcribe(stream *ObjectStream) (err error) {
s.Lock()
defer s.Unlock()
uri := fmt.Sprintf("hm://color-lyrics/v2/track/%s", stream.URI)
lyrics := &SpotifyLyrics{}
err = s.mercuryGetJson(uri, lyrics)
//Trace.Printf("Spotify Lyrics object: %v\n", lyrics)
return
}
// ReplaceURI replaces all instances of a URI with a libremedia-acceptable URI
func (s *SpotifyClient) ReplaceURI(text string) string {
return spoturire.ReplaceAllStringFunc(text, spotifyReplaceURI)
}
func spotifyReplaceURI(link string) string {
fmt.Println("Testing string: " + link)
match := spoturire.FindAllStringSubmatch(link, 1)
if len(match) > 0 {
typed := match[0][1]
id := match[0][2]
name := match[0][3]
switch typed {
case "album":
typed = "album"
case "artist":
typed = "creator"
case "search":
//TODO: Handle search URIs
return "[" + name + "](/search?q=" + name + ")"
}
return "[" + name + "](/" + typed + "?uri=spotify:" + typed + ":" + id + ")"
}
return link
}