-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplex.js
149 lines (131 loc) · 4.36 KB
/
plex.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
exports.action = function(data, callback, config, SARAH){
var sectionSerieUrl='';
config = config.modules.plex;
if (!config.server){
console.log("Missing Plex server config");
callback({'tts' : 'Il manque la configuration du serveur plex'});
return;
}
if (!config.client){
console.log("Missing client config");
callback({'tts' : 'Il manque la configuration du client'});
return;
}
if (config.Sectionserie) {
sectionSerieUrl='library/sections/'+config.Sectionserie+'/recentlyAdded';
}
switch (data.action)
{
case 'client':
get_plexClient(callback, config);
break;
case 'lastAdd':
get_lastAdd(sectionSerieUrl, callback, config);
break;
case 'navigation':
navigation(data.commande, callback, config);
break;
case 'playback':
playback(data.commande, callback, config);
break;
default:
output(callback, "Je ne sais pas faire cela ");
}
}
var playback = function(action, callback, config) {
commande = 'system/players/'+config.client+'/playback/'+action;
getPlex(commande, config, callback) ;
};
var navigation = function(action, callback, config) {
commande = 'system/players/'+config.client+'/navigation/'+action;
getPlex(commande, config, callback) ;
};
var get_lastAdd = function(section, callback, config){
console.log('');
console.log('#### PLEX ####');
console.log('');
console.log('Récupération des derniers ajouts');
getPlex( section, config, function(res){
var xml2js = require('xml2js');
var parser = new xml2js.Parser({trim: true});
parser.parseString(res, function (err, xml){
var retour ='';
if(!xml.MediaContainer.Video){
retour ='Vous n\'avez rien de nouveau';
}else{
retour += 'Je regarde sur Plex ce qui a été ajouté et que vous n\'avez pas vus.';
for (var i=0 ; i < 10 ; i++)
{ var hier = new Date();
var ajout = xml.MediaContainer.Video[i].$.addedAt+'000';
hier.setDate(hier.getDate() - config.lastAddTime);
if (ajout > hier.getTime() && !xml.MediaContainer.Video[i].$.viewCount ){
if (xml.MediaContainer.Video[i].$.type == "movie") {
retour+=' le film '+xml.MediaContainer.Video[i].$.title+' ';
}
if (xml.MediaContainer.Video[i].$.type == "episode") {
retour+=' un épisode de '+xml.MediaContainer.Video[i].$.grandparentTitle+',';
}
}
}
console.log(retour);
output(callback, retour);
};
});
});
console.log('');
}
var get_plexClient = function(callback, config){
console.log('');
console.log('#### PLEX ####');
console.log('');
console.log('Récupération des clients plex');
getPlex('clients', config, function(res){
var xml2js = require('xml2js');
var parser = new xml2js.Parser({trim: true});
parser.parseString(res, function (err, xml){
var name ='';
if (!xml.MediaContainer.Server) {
name += 'J\'ai trouvé aucun client Plex';
console.log('Pas de client connecté');
}else {
var longeur = xml.MediaContainer.Server.length;
name ="J'ai trouvé";
for (var i=0 ; i < longeur ; i++)
{ console.log(i);
switch (i)
{
case 0:
name += " ";
break;
case 1:
name += " et ";
break;
default:
name += ", ";
break;
};
name += xml.MediaContainer.Server[i].$.name ;
}
};
output(callback, name);
});
});
console.log('');
}
var getPlex = function(commande, config, callback){
var SERVER =config.server;
var url = 'http://'+SERVER+':32400/'+commande;
console.log(url);
var request = require('request');
request({ 'uri' : url}, function (err, response, body ) {
if (err || response.statusCode != 200){
callback({'tts' : "La connexion à Plex a échoué"});
return;
}
callback(body);
});
}
var output = function ( callback, output ) {
console.log('ACTION: ' + output);
callback({ 'tts' : output});
}