-
Notifications
You must be signed in to change notification settings - Fork 0
/
spotifyonsqueez.js
111 lines (94 loc) · 3.46 KB
/
spotifyonsqueez.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
exports.action = function(data, callback, config, SARAH){
// CONFIG
name= data.name;
var answers = config.answers;
configM = config.modules.spotifyonsqueez;
if (!configM.ip){
console.log("Missing squeezebox config");
callback({'tts' : 'Il manque la configuration de l i p'});
return;
}
if (!configM.portSpotify){
console.log("Missing squeezebox config");
callback({'tts' : 'Il manque la configuration du port'});
return;
}
if ( data.action == 'playlist') {
launchPlaylsit( name, callback, configM, answers);
}
else if ( data.action == 'update') {
update(callback, configM);
}
else {
callback({'tts' : "Je ne sais pas faire"});
}
}
exports.cron = function(callback, config, task){
console.log("Update des playlists");
update(callback, config.modules.spotifyonsqueez);
callback({'tts': ""});
}
var sendURL = function(url, callback, cb){
var request = require('request');
request({ 'uri' : url}, function (err, response, body ) {
if (err || response.statusCode != 200){
callback({'tts' : "La connexion à la squizebox"});
return;
}
cb(body);
});
}
var launchPlaylsit = function(name, callback, config, answers){
var name = name.toLowerCase();
var trouve = false;
console.log('Lancement de la playsliste spotify :'+name);
sendURL('http://'+config.ip+':'+config.portSpotify+'/playlists.json?user='+config.utilisateur, callback, function(body){
var json = JSON.parse(body);
for (var i=0 ; i < json.playlists.length ; i++)
{
var listName = json.playlists[i].name.toLowerCase();
if ( listName == name) {
trouve = true;
playUri(json.playlists[i].uri, callback,config, answers);
}
}
if (trouve == false){
callback({'tts' : "Je ne trouve pas la playlist"});
}
return;
}) ;
}
var playUri = function (uri, callback, config){
var url = 'http://'+config.ip+':'+config.portSqueezeServer+'/status.xml?player='+config.squeezeboxPricipale+'&p0=playlist&p1=play&p2='+uri;
sendURL(url, callback, function(){});
var answers = config.answers.split('|');
var answer = answers[ Math.floor(Math.random() * answers.length)];
console.log(answer);
callback({'tts': answer})
}
// ------------------------------------------
// UPDATING XML
// ------------------------------------------
var update = function(callback, config){
var fs = require('fs');
var file = 'plugins/spotifyonsqueez/spotifyonsqueez.xml';
var xml = fs.readFileSync(file,'utf8');
sendURL('http://'+config.ip+':'+config.portSpotify+'/playlists.json?user='+config.utilisateur, callback, function(body){
var replace = '§ -->\n';
replace += '<rule id="rulePlaylistName">\n';
replace += ' <one-of>\n';
var json;
json = JSON.parse(body);
for (var i=0 ; i < json.playlists.length ; i++) {
replace += ' <item>'+json.playlists[i].name.toLowerCase()+'<tag>out.action="playlist";out.name="'+json.playlists[i].name.toLowerCase()+'";</tag></item>\n';
}
replace += ' </one-of>\n';
replace += '</rule>\n';
replace += '<!-- §';
console.log('maj ok');
var regexp = new RegExp('§[^§]+§','gm');
xml= xml.replace(regexp,replace);
fs.writeFileSync(file, xml, 'utf8');
})
callback({'tts' : "Voilà j'ai mis les playlist à jour"});
}