-
Notifications
You must be signed in to change notification settings - Fork 4
/
node_helper.js
71 lines (67 loc) · 2.5 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
67
68
69
70
71
/* global Module */
/* node_helper.js
*
* Magic Mirror
* Module: MMM-RottenTomatoes
*
* Magic Mirror By Michael Teeuw http://michaelteeuw.nl
* MIT Licensed.
*
* Module MMM-RottenTomatoes By Adam Moses http://adammoses.com
* MIT Licensed.
*/
// call in the required classes
var RTS = require("rt-scraper");
// the main module helper create
module.exports = NodeHelper.create({
// subclass start method, clears the initial config array
start: function() {
this.moduleConfigs = [];
this.timers = [];
this.errorCount = 0;
// this value controls number of calls fails before giving up
this.errorFailLimit = 10;
},
// subclass socketNotificationReceived, received notification from module
socketNotificationReceived: function(notification, payload) {
if (notification === "ROTTEN_TOMATOES_REGISTER_CONFIG") {
// add the current config to an array of all configs used by the helper
this.moduleConfigs[this.moduleConfigs.length] = payload;
// this to self
var self = this;
// call the initial update now
this.updateRTData(payload);
// schedule the updates
this.timers[this.timers.length] = setInterval(
function() { self.updateRTData(payload); }, payload.refreshRate);
}
},
// increment error count, if passed limit send notice to module and stop updates
processError: function() {
this.errorCount += 1;
if (this.errorCount >= this.errorFailLimit)
{
this.sendSocketNotification('ROTTEN_TOMATOES_TOO_MANY_ERRORS', {} );
for (var cIndex = 0; cIndex < this.timers.length; cIndex++)
clearTimeout(this.timers[cIndex]);
this.timers = [];
}
},
// main helper function to get the rotten tomatoes information
updateRTData: function(theConfig) {
// this to self
var self = this;
// otherwise get a named list
RTS.getRottenTomatoesScraperData( function(error, data) {
if (!error) {
var returnPayload = {identifier: theConfig.identifier
, rtData: data};
self.sendSocketNotification('ROTTEN_TOMATOES_UPDATE', returnPayload );
}
else {
this.processError();
}
});
},
});
//------------ end -------------