/
api.go
133 lines (112 loc) · 3.6 KB
/
api.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
package mercury
import (
"github.com/RoflCopter24/librespot-golang/Spotify"
"encoding/json"
"fmt"
"github.com/golang/protobuf/proto"
"github.com/RoflCopter24/librespot-golang/librespot/metadata"
"net/url"
)
func (m *Client) mercuryGet(url string) []byte {
done := make(chan []byte)
go m.Request(Request{
Method: "GET",
Uri: url,
Payload: [][]byte{},
}, func(res Response) {
done <- res.CombinePayload()
})
result := <-done
return result
}
func (m *Client) mercuryGetJson(url string, result interface{}) (err error) {
data := m.mercuryGet(url)
// fmt.Printf("%s", data)
err = json.Unmarshal(data, result)
return
}
func (m *Client) mercuryGetProto(url string, result proto.Message) (err error) {
data := m.mercuryGet(url)
// ioutil.WriteFile("/tmp/proto.blob", data, 0644)
err = proto.Unmarshal(data, result)
return
}
func (m *Client) GetRootPlaylist(username string) (*Spotify.SelectedListContent, error) {
uri := fmt.Sprintf("hm://playlist/user/%s/rootlist", username)
result := &Spotify.SelectedListContent{}
err := m.mercuryGetProto(uri, result)
return result, err
}
func (m *Client) GetPlaylist(id string) (*Spotify.SelectedListContent, error) {
uri := fmt.Sprintf("hm://playlist/%s", id)
result := &Spotify.SelectedListContent{}
err := m.mercuryGetProto(uri, result)
return result, err
}
func (m *Client) GetToken(clientId string, scopes string) (*metadata.Token, error) {
uri := fmt.Sprintf("hm://keymaster/token/authenticated?client_id=%s&scope=%s", url.QueryEscape(clientId),
url.QueryEscape(scopes))
token := &metadata.Token{}
err := m.mercuryGetJson(uri, token)
return token, err
}
func (m *Client) Search(search string, limit int, country string, username string) (*metadata.SearchResponse, error) {
v := url.Values{}
v.Set("entityVersion", "2")
v.Set("limit", fmt.Sprintf("%d", limit))
v.Set("imageSize", "large")
v.Set("catalogue", "")
v.Set("country", country)
v.Set("platform", "zelda")
v.Set("username", username)
uri := fmt.Sprintf("hm://searchview/km/v4/search/%s?%s", url.QueryEscape(search), v.Encode())
result := &metadata.SearchResponse{}
err := m.mercuryGetJson(uri, result)
return result, err
}
func (m *Client) Suggest(search string) (*metadata.SuggestResult, error) {
uri := "hm://searchview/km/v3/suggest/" + url.QueryEscape(search) + "?limit=3&intent=2516516747764520149&sequence=0&catalogue=&country=&locale=&platform=zelda&username="
data := m.mercuryGet(uri)
return parseSuggest(data)
}
func (m *Client) GetTrack(id string) (*Spotify.Track, error) {
uri := "hm://metadata/4/track/" + id
result := &Spotify.Track{}
err := m.mercuryGetProto(uri, result)
return result, err
}
func (m *Client) GetArtist(id string) (*Spotify.Artist, error) {
uri := "hm://metadata/4/artist/" + id
result := &Spotify.Artist{}
err := m.mercuryGetProto(uri, result)
return result, err
}
func (m *Client) GetAlbum(id string) (*Spotify.Album, error) {
uri := "hm://metadata/4/album/" + id
result := &Spotify.Album{}
err := m.mercuryGetProto(uri, result)
return result, err
}
func parseSuggest(body []byte) (*metadata.SuggestResult, error) {
result := &metadata.SuggestResult{}
err := json.Unmarshal(body, result)
if err != nil {
fmt.Println("err", err)
}
for _, s := range result.Sections {
switch s.Typ {
case "top-results":
err = json.Unmarshal(s.RawItems, &result.TopHits)
case "album-results":
err = json.Unmarshal(s.RawItems, &result.Albums)
case "artist-results":
err = json.Unmarshal(s.RawItems, &result.Artists)
case "track-results":
err = json.Unmarshal(s.RawItems, &result.Tracks)
}
if err != nil {
return nil, err
}
}
return result, nil
}