-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnode_helper.js
66 lines (60 loc) · 2.28 KB
/
node_helper.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
var NodeHelper = require("node_helper");
var https = require("https");
module.exports = NodeHelper.create({
socketNotificationReceived: function (notification, payload) {
if (notification === "GET_MOVIES") {
var nextyear, nextmonth;
nextyear = payload.year;
nextmonth = parseInt(payload.month, 10) + 1;
if (nextmonth >= 13) {
nextmonth = 1;
nextyear = (parseInt(nextyear, 10) + 1).toString();
}
nextmonth = ("0" + nextmonth).slice(-2);
this.getData(payload.id, payload.apikey, payload.year, payload.month);
this.getData(payload.id, payload.apikey, nextyear, nextmonth);
}
},
getData: function (id, apikey, year, month) {
var host = "www.myapifilms.com";
var path = "/imdb/comingSoon?token=" + apikey + "&format=json&language=en-gb&date=" + year + "-" + month;
var self = this;
https.get({
host: host,
path: path
}, function (responce) {
var body = '';
responce.on('data', function (data) {
body += data;
});
responce.on('end', function () {
self.processList(id, JSON.parse(body))
});
});
},
processList: function (id, list) {
var i, j, movieList = [], payload = {};
if (list.error !== undefined) {
payload.error = true;
payload.errorMessage += "Code: " + list.error.code + " Message: " + list.error.message;
}
if (list.data !== undefined) {
payload.error = false;
var cs = list.data.comingSoon;
if (cs.length === 0) {
payload.error = true;
payload.errorMessage += "Empty data array - probable date error";
}
for (i = 0; i < cs.length; i += 1) {
for (j = 0; j < cs[i].movies.length; j += 1) {
if (cs[i].movies[j].languages.indexOf("English") !== -1) {
movieList.push(cs[i].movies[j]);
}
}
}
payload.movieList = movieList;
payload.id = id;
this.sendSocketNotification("MOVIE_LIST", payload)
}
}
});