-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathspotify.go
126 lines (102 loc) · 3.01 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
package main
// credit to https://github.com/lacymorrow/album-art
// thanks for the token too :)
import (
"encoding/base64"
"encoding/json"
"fmt"
"io"
"os"
"net/http"
"net/url"
// "sort"
"strings"
"github.com/godbus/dbus/v5"
)
const (
art_endpoint = "https://api.spotify.com/v1"
auth_endpoint = "https://accounts.spotify.com/api/token"
art_api_id = "3f974573800a4ff5b325de9795b8e603"
art_api_secret = "ff188d2860ff44baa57acc79c121a3b9"
art_api_auth = art_api_id + ":" + art_api_secret
)
type spotifyFetcher struct{}
type spotifyAccess struct{
AccessToken string `json:"access_token"`
}
type spotifySeach struct{
Tracks spotifyTrack `json:"tracks"`
Albums struct{
Items []spotifyAlbum `json:"items"`
} `json:"albums"`
}
type spotifyTrack struct{
Items []spotifyTrackObject `json:"items"`
}
type spotifyTrackObject struct{
Album spotifyAlbum `json:"album"`
}
type spotifyAlbum struct{
Images []spotifyArt
Name string
}
type spotifyArt struct {
Width int
Height int
URL string
}
func handleSpotErr(err error) string {
fmt.Fprintln(os.Stderr, err)
return "music"
}
func (spotifyFetcher) getAlbumArt(artist, album, title string, mdata map[string]dbus.Variant) string {
spotSearchQuery := url.PathEscape(url.QueryEscape(fmt.Sprintf("track:%s artist:%s album:%s", title, artist, album)))
if album == "" {
spotSearchQuery = url.PathEscape(url.QueryEscape(fmt.Sprintf("track:%s artist:%s", title, artist)))
}
artUrl, _ := url.Parse(fmt.Sprintf("%s/search?q=%s&type=track,album&limit=1", art_endpoint, spotSearchQuery))
authUrl, _ := url.Parse(auth_endpoint)
req, err := http.NewRequest("POST", authUrl.String(), strings.NewReader("grant_type=client_credentials"))
if err != nil {
return handleSpotErr(err)
}
req.Header.Add("Authorization", "Basic " + base64.StdEncoding.EncodeToString([]byte(art_api_auth)))
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
resp, err := http.DefaultClient.Do(req)
if err != nil {
return handleSpotErr(err)
}
body, err := io.ReadAll(resp.Body)
spot := &spotifyAccess{}
if err := json.Unmarshal(body, &spot); err != nil {
return handleSpotErr(err)
}
req, err = http.NewRequest("GET", artUrl.String(), strings.NewReader(""))
req.Header.Add("Authorization", "Bearer " + spot.AccessToken)
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
resp, err = http.DefaultClient.Do(req)
if err != nil {
return handleSpotErr(err)
}
body, err = io.ReadAll(resp.Body)
logger.Debug(string(body))
spotifyData := &spotifySeach{}
if err := json.Unmarshal(body, &spotifyData); err != nil {
return handleSpotErr(err)
}
// may or may not be needed (will see)
/*
sort.Slice(images, func(i, j int) bool {
return images[i].Width > images[j].Width
})
*/
if len(spotifyData.Tracks.Items) != 0 {
return spotifyData.Tracks.Items[0].Album.Images[0].URL
}
if len(spotifyData.Albums.Items) == 0 {
// nothing found
fmt.Fprintln(os.Stderr, "Nothing found on spotify for", album)
return "music"
}
return spotifyData.Albums.Items[0].Images[0].URL
}