forked from wildeyedskies/stmp
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapi.go
More file actions
311 lines (242 loc) · 6.71 KB
/
Copy pathapi.go
File metadata and controls
311 lines (242 loc) · 6.71 KB
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
package main
import (
"crypto/md5"
"encoding/json"
"fmt"
"io/ioutil"
"math/rand"
"net/http"
"net/url"
"crypto/tls"
)
// used for generating salt
var letters = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
type SubsonicConnection struct {
Username string
Password string
Host string
AcceptInvalidSslCert bool
}
func randSeq(n int) string {
b := make([]rune, n)
for i := range b {
b[i] = letters[rand.Intn(len(letters))]
}
return string(b)
}
func authToken(password string) (string, string) {
salt := randSeq(8)
token := fmt.Sprintf("%x", md5.Sum([]byte(password+salt)))
return token, salt
}
func defaultQuery(connection *SubsonicConnection) url.Values {
http.DefaultTransport.(*http.Transport).TLSClientConfig = &tls.Config{InsecureSkipVerify: connection.AcceptInvalidSslCert}
token, salt := authToken(connection.Password)
query := url.Values{}
query.Set("u", connection.Username)
query.Set("t", token)
query.Set("s", salt)
query.Set("v", "1.15.1")
query.Set("c", "stmp")
query.Set("f", "json")
return query
}
// response structs
type SubsonicError struct {
Code int `json:"code"`
Message string `json:"message"`
}
type SubsonicSong struct {
Id string `json:"id"`
Artist string `json:"artist"`
Title string `json:"title"`
Duration int `json:"duration"`
Track int `json:"track"`
DiskNumber int `json:"diskNumber"`
Path string `json:"path"`
}
type SubsonicSongAlbum struct {
Songs []SubsonicSong `json:"song"`
ArtistId string `json:"artistId"`
}
type SubsonicSongResponse struct {
Status string `json:"status"`
Version string `json:"version"`
Album SubsonicSongAlbum `json:"album"`
Error SubsonicError `json:"error"`
}
type Song struct {
Response SubsonicSongResponse `json:"subsonic-response"`
}
type SubsonicAlbum struct {
Id string `json:"id"`
Title string `json:"name"`
Duration int `json:"duration"`
}
type SubsonicAlbumArtist struct {
Albums []SubsonicAlbum `json:"album"`
}
type SubsonicAlbumResponse struct {
Status string `json:"status"`
Version string `json:"version"`
Artist SubsonicAlbumArtist `json:"artist"`
Error SubsonicError `json:"error"`
}
type Album struct {
Response SubsonicAlbumResponse `json:"subsonic-response"`
}
type SubsonicRandomSong struct {
Songs []SubsonicSong `json:"song"`
}
type SubsonicRandomSongsResponse struct {
Status string `json:"status"`
Version string `json:"version"`
RandomSongs SubsonicRandomSong `json:"randomSongs"`
Error SubsonicError `json:"error"`
}
type RandomSongs struct {
Response SubsonicRandomSongsResponse `json:"subsonic-response"`
}
type SubsonicArtist struct {
Id string
Name string
AlbumCount int
}
type SubsonicIndex struct {
Name string `json:"name"`
Artists []SubsonicArtist `json:"artist"`
}
type SubsonicIndexes struct {
Index []SubsonicIndex
}
type SubsonicArtistsResponse struct {
Status string `json:"status"`
Version string `json:"version"`
Indexes SubsonicIndexes `json:"artists"`
Error SubsonicError `json:"error"`
}
type Artists struct {
Response SubsonicArtistsResponse `json:"subsonic-response"`
}
type SubsonicPingResponse struct {
Status string `json:"status"`
Version string `json:"version"`
}
type Ping struct {
Response SubsonicPingResponse `json:"subsonic-response"`
}
// requests
func (connection *SubsonicConnection) GetServerInfo() (*SubsonicPingResponse, error) {
query := defaultQuery(connection)
requestUrl := connection.Host + "/rest/ping?" + query.Encode()
res, err := http.Get(requestUrl)
if err != nil {
return nil, err
}
if res.Body != nil {
defer res.Body.Close()
}
responseBody, readErr := ioutil.ReadAll(res.Body)
if readErr != nil {
return nil, err
}
var decodedBody Ping
err = json.Unmarshal(responseBody, &decodedBody)
if err != nil {
return nil, err
}
return &decodedBody.Response, nil
}
func (connection *SubsonicConnection) GetRandomSongs(count int) (*SubsonicRandomSongsResponse, error) {
query := defaultQuery(connection)
requestUrl := fmt.Sprintf("%s/rest/getRandomSongs?%s&size=%d", connection.Host, query.Encode(), count)
res, err := http.Get(requestUrl)
if err != nil {
return nil, err
}
if res.Body != nil {
defer res.Body.Close()
}
responseBody, readErr := ioutil.ReadAll(res.Body)
if readErr != nil {
return nil, err
}
var decodedBody RandomSongs
err = json.Unmarshal(responseBody, &decodedBody)
if err != nil {
return nil, err
}
return &decodedBody.Response, nil
}
func (connection *SubsonicConnection) GetArtists() (*SubsonicArtistsResponse, error) {
query := defaultQuery(connection)
requestUrl := connection.Host + "/rest/getArtists?" + query.Encode()
res, err := http.Get(requestUrl)
if err != nil {
return nil, err
}
if res.Body != nil {
defer res.Body.Close()
}
responseBody, readErr := ioutil.ReadAll(res.Body)
if readErr != nil {
return nil, err
}
var decodedBody Artists
err = json.Unmarshal(responseBody, &decodedBody)
if err != nil {
return nil, err
}
return &decodedBody.Response, nil
}
func (connection *SubsonicConnection) GetArtist(id string) (*SubsonicAlbumResponse, error) {
query := defaultQuery(connection)
query.Set("id", id)
requestUrl := connection.Host + "/rest/getArtist?" + query.Encode()
res, err := http.Get(requestUrl)
if err != nil {
return nil, err
}
if res.Body != nil {
defer res.Body.Close()
}
responseBody, readErr := ioutil.ReadAll(res.Body)
if readErr != nil {
return nil, err
}
var decodedBody Album
err = json.Unmarshal(responseBody, &decodedBody)
if err != nil {
return nil, err
}
return &decodedBody.Response, nil
}
func (connection *SubsonicConnection) GetAlbum(id string) (*SubsonicSongResponse, error) {
query := defaultQuery(connection)
query.Set("id", id)
requestUrl := connection.Host + "/rest/getAlbum?" + query.Encode()
res, err := http.Get(requestUrl)
if err != nil {
return nil, err
}
if res.Body != nil {
defer res.Body.Close()
}
responseBody, readErr := ioutil.ReadAll(res.Body)
if readErr != nil {
return nil, err
}
var decodedBody Song
err = json.Unmarshal(responseBody, &decodedBody)
if err != nil {
return nil, err
}
return &decodedBody.Response, nil
}
// note that this function does not make a request, it just formats the play url
// to pass to mpv
func (connection *SubsonicConnection) GetPlayUrl(song *SubsonicSong) string {
query := defaultQuery(connection)
query.Set("id", song.Id)
return connection.Host + "/rest/stream?" + query.Encode()
}