-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathspotify.go
108 lines (90 loc) · 2.68 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
package main
import (
"context"
"fmt"
"io/ioutil"
"strings"
"github.com/anisse/librespot-golang/src/Spotify"
"github.com/anisse/librespot-golang/src/librespot"
"github.com/anisse/librespot-golang/src/librespot/core"
"github.com/anisse/librespot-golang/src/librespot/utils"
)
func getPreferredAudioFile(track *Spotify.Track) (selectedFile *Spotify.AudioFile) {
for _, file := range track.GetFile() {
// Take highest quality available ogg vorbis file
if (file.GetFormat() == Spotify.AudioFile_OGG_VORBIS_96 && selectedFile == nil) ||
(file.GetFormat() == Spotify.AudioFile_OGG_VORBIS_160 && (selectedFile == nil || selectedFile.GetFormat() == Spotify.AudioFile_OGG_VORBIS_96)) ||
(file.GetFormat() == Spotify.AudioFile_OGG_VORBIS_320) {
selectedFile = file
}
}
return
}
func playTrack(ctx context.Context, session *core.Session, id string) error {
track, err := session.Mercury().GetTrack(utils.Base62ToHex(id))
if err != nil {
return err
}
selectedFile := getPreferredAudioFile(track)
if selectedFile == nil {
for _, altTrack := range track.GetAlternative() {
selectedFile = getPreferredAudioFile(altTrack)
if selectedFile != nil {
break
}
}
}
if selectedFile == nil {
return fmt.Errorf("Could not find track for id: %s", id)
}
audioFile, err := session.Player().LoadTrack(selectedFile, track.GetGid())
if err != nil {
return err
}
defer audioFile.Cancel()
err = playOgg(ctx, audioFile)
return err
}
func playlistTracks(session *core.Session, id string) ([]string, error) {
list, err := session.Mercury().GetPlaylist(id)
if err != nil {
return nil, err
}
contents := list.GetContents()
if contents == nil {
return nil, fmt.Errorf("No contents in playlist")
}
items := contents.GetItems()
if items == nil {
return nil, fmt.Errorf("No items in playlist content")
}
if len(items) == 0 {
return []string{}, nil
}
tracks := make([]string, 0, len(items))
for _, v := range items {
t := strings.TrimPrefix(*v.Uri, "spotify:track:")
tracks = append(tracks, t)
}
return tracks, nil
}
func openSession() (*core.Session, error) {
const (
usernameFile = DATADIR + "username"
blobFile = DATADIR + "blob.bin"
deviceName = "beatbox"
)
blobBytes, err := ioutil.ReadFile(blobFile)
if err != nil {
return nil, fmt.Errorf("unable to read auth blob from %s: %s\n", blobFile, err.Error())
}
userBytes, err := ioutil.ReadFile(usernameFile)
if err != nil {
return nil, fmt.Errorf("unable to username from %s: %s\n", usernameFile, err.Error())
}
session, err := librespot.LoginSaved(string(userBytes), blobBytes, deviceName)
if err != nil {
return nil, fmt.Errorf("opening spotify session: %s", err.Error())
}
return session, nil
}