forked from crtr0/node-voicebox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
75 lines (67 loc) · 1.92 KB
/
index.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
var request = require('request');
module.exports = function (roomId) {
return new VoiceBox(roomId);
};
function VoiceBox (roomId) {
this.roomId = roomId;
}
VoiceBox.prototype.search = function (search, cb) {
var opts = {
uri : 'http://voiceboxpdx.com/api/v1/songs/search.json?query='
+ search.replace(/\s+/g, '+')
,
json : true
};
request(opts, function (err, res, body) {
if (err) cb(err)
else cb(body.songs)
});
};
VoiceBox.prototype.getQueue = function (cb) {
var opts = {
uri : 'http://voiceboxpdx.com/api/v1/queue.json?room_code='
+ this.roomId,
json : true,
};
return request.get(opts, function (err, res, body) {
if (err) cb(err)
else cb(null, body.queue)
});
}
VoiceBox.prototype.clearQueue = function (cb) {
var opts = {
method : 'DELETE',
uri : 'http://voiceboxpdx.com/api/v1/queue.json?room_code='
+ this.roomId,
};
return request(opts, cb);
}
VoiceBox.prototype.addSong = function (id, cb) {
return request.post('http://voiceboxpdx.com/api/v1/queue.json?'
+ 'room_code=' + this.roomId + '&song_id=' + id, cb);
}
VoiceBox.prototype.bump = function (id) {
var self = this;
self.getQueue(function (err, queue) {
if (err) console.error(err);
self.clearQueue(function (err) {
self.addSong(id, function (err) {
queue.forEach(function (q) {
self.addSong(q.song_id);
});
});
});
});
};
VoiceBox.prototype.deleteItem = function deleteItem (ix, cb) {
var self = this;
self.getQueue(function (err, queue) {
self.clearQueue(function (err) {
if (err) return cb(err);
queue.forEach(function (q) {
if (q.index === ix) return;
self.addSong(q.song_id);
});
});
});
}