forked from nitzzzu/alexa-local-skills
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsubsonic.js
71 lines (59 loc) · 1.75 KB
/
subsonic.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
// http://www.subsonic.org/pages/api.jsp
const rp = require('request-promise-native');
const md5 = require('md5');
let subsonic = {
server: '',
username: '',
salt: '',
authtoken: '',
getOptions: function(uri, params) {
let qs = Object.assign(params || {}, {
v: '1.15.0',
f: 'json',
c: 'alexa',
u: this.username,
s: this.salt,
t: this.authtoken
});
let options = {
uri,
qs,
json: true
};
return options;
},
execute: function(method, params) {
let uri = `${this.server}/rest/${method}.view`;
try {
return rp(this.getOptions(uri, params));
} catch (e) {}
},
open: function(server, username, password) {
this.server = server;
this.username = username;
this.salt = Math.random()
.toString(36)
.replace(/[^a-z]+/g, '');
this.authtoken = md5(password + this.salt);
},
ping: async function() {
return await this.execute('ping');
},
search: async function(search, useID3, params) {
let method = useID3 ? 'search3' : 'search2';
if (!params) {
params = {};
}
params.query = search;
return await this.execute(method, params);
},
streamUrl: async function(search) {
let response = await subsonic.search(search, true);
if (response['subsonic-response'] && response['subsonic-response'].searchResult3) {
let uri = `${this.server}/rest/stream?id=${response['subsonic-response'].searchResult3.song[0].id}`;
return this.getOptions(uri);
}
return '';
}
};
module.exports = subsonic;