forked from obscuredvision/squeezenode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
squeezerequest.js
117 lines (103 loc) · 4.35 KB
/
squeezerequest.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
var Promise = require('bluebird'),
jayson = require('jayson'),
_ = require('lodash'),
PlayerMode = require('./constants').PlayerMode;
function SqueezeRequest(address, port, username, password) {
var jsonrpc, client,
self = this;
self.address = (address !== undefined) ? address : 'localhost';
self.port = (port !== undefined) ? port : 9000;
self.username = username;
self.password = password;
self.defaultPlayer = '00:00:00:00:00:00';
jsonrpc = self.address + ':' + self.port + '/jsonrpc.js';
client = jayson.client.http(jsonrpc);
client.options.version = 1;
// Function to format the header for basic authentication.
self.formatBasicHeader = function (username, password) {
var tok = username + ':' + password;
var hash = new Buffer(tok).toString('base64');
return 'Basic ' + hash;
};
// Add a header for basic authentication if a username and password are given
if (username && password) {
if (!client.options.headers) {
client.options.headers = {};
}
client.options.headers['Authorization'] = self.formatBasicHeader(username, password);
}
/**
* get the parameters for a player mode
* @param mode {String} player mode of type PlayerMode
* @return {Array}
*/
self.playerModeToParams = function (mode) {
var params = [],
pmode = _.findKey(PlayerMode, function (value) {
return _.toLower(value) === _.toLower(mode);
});
if (_.includes([PlayerMode.PAUSE, PlayerMode.RESUME], pmode)) {
params.push('pause');
} else if (_.includes([PlayerMode.PLAY, PlayerMode.CONTINUE, PlayerMode.KEEP_GOING], pmode)) {
params.push('play');
} else if (_.includes([PlayerMode.MUTE, PlayerMode.UNMUTE], pmode)) {
params = _.union(params, ['mixer', 'muting']);
} else if (_.includes([PlayerMode.REPEAT, PlayerMode.REPEAT_ON, PlayerMode.REPEAT_OFF], pmode)) {
params = _.union(params, ['playlist', 'repeat']);
} else if (_.includes([PlayerMode.SHUFFLE, PlayerMode.SHUFFLE_ON, PlayerMode.SHUFFLE_OFF,
PlayerMode.TURN_ON_SHUFFLE, PlayerMode.TURN_OFF_SHUFFLE, PlayerMode.STOP_SHUFFLING,
PlayerMode.SHUFFLE_THE_MUSIC], pmode)) {
params = _.union(params, ['playlist', 'shuffle']);
} else if (_.includes([PlayerMode.STOP], pmode)) {
params.push('stop');
}
return params;
};
/**
* Get song info by track id or path to file
* @param trackIdOrUrl {String|Number} the song to get information for
* if trackId then it should be a number to a specific track
* if Url it should be a path with the file:// protocol
* @return {*} song information
*/
self.songInfo = function (trackIdOrUrl) {
return Promise.try(function () {
var params = ['songinfo', 0, 100, 'tags:aAsSelgGpPcdtyuJ'];
if (_.isNil(trackIdOrUrl)) {
throw new TypeError('trackIdOrUrl');
} else if (_.indexOf(trackIdOrUrl, 'file://') !== -1) {
params.push('url:' + trackIdOrUrl);
} else {
params.push('track_id:' + trackIdOrUrl);
}
return self.request(self.defaultPlayer, params).then(
function (reply) {
var response = {};
if (reply && reply.result) {
_.forEach(reply.result.songinfo_loop, function (value) {
_.assign(response, value);
});
response.exists = !(_.keys(response).length <= 4 && (!_.has(response, 'title') || _.isNil(response.title)))
}
return response;
});
});
};
self.throwError = function(payload) {
var message;
if (_.isPlainObject(payload)) {
message = JSON.stringify(payload);
} else {
message = payload;
}
throw new Error(message);
};
self.request = function (player, params) {
var finalParams = [],
call = Promise.promisify(client.request, {context: client});
finalParams.push(player);
finalParams.push(params);
return call('slim.request', finalParams);
};
}
module.exports = SqueezeRequest;