-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsubsonic-fetch.js
280 lines (275 loc) · 7.1 KB
/
subsonic-fetch.js
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
/* js-subsonic:subsonic-fetch - fetch and promises based library for accessing Subsonic servers */
/* Copyright @2016 Joseph W Shelby */
/* Released under the MIT License */
var fetch = require('node-fetch');
var Promise = require("bluebird");
fetch.Promise = Promise;
var URI = require('urijs');
var SubSonicFetch = {
serverAPIVersion: "1.13.0",
_u: '',
_s: '',
_ts: '',
_tt: '',
_b: '0',
coverArtCache: {},
_buildURI: function(method, params, traditional) {
var uri = this._s + "/rest/" + method + ".view",
that = this;
uri = URI(uri);
uri.addQuery(params || {});
uri.addQuery({
v: that.serverAPIVersion,
f: 'json',
c: 'SubFire',
u: that._u
});
if (that._tt && that._ts) {
uri.addQuery({
s: that._ts,
t: that._tt
});
} else if (that._p && versionCompare(that.serverAPIVersion, "1.13.0") >= 0) {
var s = Math.random().toString(36).replace(/[^a-z]+/g, '');
var t = md5(that._p + s);
uri.addQuery({
s: s,
t: t
});
} else {
// currently unsupported
uri.addQuery({
p: that._encP
});
}
// console.log(params);
// uri = uri + $.param(params, traditional);
return uri.toString();
},
// I hate that i'm having to add this last
_execute: function(method, params, traditional) {
var that = this;
return new Promise(function(rs, rj) {
var uri = that._buildURI(method, params, traditional);
fetch(uri.toString()).then(function(res) {
console.log(res);
if (!res.ok) {
rj(res);
return;
}
return res.json();
}).then(function(res) {
console.log(res);
var reply = res['subsonic-response'];
if (reply.status === 'failed') {
rj(reply);
} else {
rs(reply);
}
}).catch(function(err) {
console.log(err);
rj(err);
});
});
},
open: function(server, username, bitrate, seed, token, ping) {
// reset capabilities
this.serverAPIVersion = "1.13.0";
this._u = username;
this._s = server;
this._b = bitrate || '0';
this._tt = token;
this._ts = seed;
return ping ? this.ping() : this;
},
ping: function() {
var that = this;
return this._execute("ping");
// this._execute("ping", {}, function(err, res) {
// if (err) console.log(err);
// window.err = window.err || err;
// if (err && typeof err !== 'string') {
// if (err.error.code === 10 || err.error.code === 20 || err.error.code === 30) {
// console.warn("downgrading API version to", err.version);
// that.serverAPIVersion = err.version;
// return that.ping(cb);
// } else {
// if (cb) return cb(err);
// }
// } else if (err) {
// if (cb) return cb(err);
// }
// that.serverAPIVersion = res.version;
// if (cb) cb(null, res);
// // that.addChatMessage(navigator.userAgent);
// });
},
getPlaylists: function() {
return this._execute("getPlaylists");
},
getPlaylist: function(id) {
return this._execute("getPlaylist", {
id: id
});
},
getMusicFolders: function() {
return this._execute("getMusicFolders");
},
getIndexes: function(id) {
var params = id === null || id === undefined ? {} : {
musicFolderId: id
};
return this._execute("getIndexes", params);
},
getMusicDirectory: function(id) {
return this._execute("getMusicDirectory", {
id: id
});
},
getGenres: function(id) {
return this._execute("getGenres", {
id: id
});
},
getArtists: function(cb) {
return this._execute("getArtists", {});
},
getArtist: function(id) {
return this._execute("getArtist", {
id: id
});
},
getTopSongs: function(name, count) {
return this._execute("getTopSongs", {
artist: name,
count: count || 50
});
},
getAlbum: function(id) {
return this._execute("getAlbum", {
id: id
});
},
getSong: function(id) {
return this._execute("getSong", {
id: id
});
},
getAlbumList: function(type, params) {
params = params || {};
params.type = type;
return this._execute("getAlbumList", params);
},
getAlbumList2: function(type, params) {
params = params || {};
params.type = type;
return this._execute("getAlbumList2", params);
},
randomizeFolder: function(id, size) {
var params = id === null || id === undefined ? {} : {
musicFolderId: id
};
params.size = size || 50;
return this._execute("getRandomSongs", params);
},
getCoverArtURL: function(id, size) {
var params = {
id: id
};
if (size) params.size = size;
var url = this._buildURI("getCoverArt", params);
var key = trimImageUrlEssentials(url);
var firstUrl = this.coverArtCache[key];
if (!firstUrl) {
this.coverArtCache[key] = url;
} else {
// console.log("match", "using", firstUrl, "instead of", url);
url = firstUrl;
}
return url;
},
getHLSURL: function(id, params) {
params = $.extend(params || {}, {
id: id,
maxBitRate: this._b
});
return this._buildURI("hls", params);
},
getStreamingURL: function(id, params) {
params = $.extend(params || {}, {
id: id,
maxBitRate: this._b
});
return this._buildURI("stream", params);
},
getDownloadURL: function(id) {
return this._buildURI("download", {
id: id
});
},
getArtistInfo: function(id, useID3) {
var method = useID3 ? "getArtistInfo2" : "getArtistInfo";
var params = {
id: id,
count: 20
};
return this._execute(method, params);
},
getSimilarSongs: function(id, useID3, count) {
var method = useID3 ? "getSimilarSongs2" : "getSimilarSongs";
var params = {
id: id,
count: count
};
return this._execute(method, params);
},
search: function(search, useID3, params) {
var method = useID3 ? "search3" : "search2";
params.query = search;
return this._execute(method, params);
},
savePlayQueue: function(ids, current) {
var method = "savePlayQueue";
var params = {
id: ids,
current: current
};
return this._execute(method, params, true);
},
getPlayQueue: function(cb) {
return this._execute("getPlayQueue", null);
},
getChatMessages: function(since) {
if (typeof since === 'function') {
cb = since;
since = null;
}
return this._execute("getChatMessages", since ? {
since: since
} : null);
},
addChatMessage: function(message) {
return this._execute("addChatMessage", {
message: message
});
},
createBookmark: function(id, position, comment) {
var params = {
id: id,
position: position
};
if (comment) params.comment = comment;
return this._execute("createBookmark", params);
},
deleteBookmark: function(id) {
var params = {
id: id
};
return this._execute("deleteBookmark", params);
},
getBookmarks: function(cb) {
return this._execute("getBookmarks", null);
}
};
console.log(SubSonicFetch);
module.exports = SubSonicFetch;