forked from andrecarlucci/MMM-DailyDilbert
-
Notifications
You must be signed in to change notification settings - Fork 3
/
node_helper.js
executable file
·264 lines (246 loc) · 8.22 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
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
/****************
Node helper for MMM-Comics module
****************/
var request = require("request");
var NodeHelper = require("node_helper");
var cheerio = require("cheerio");
var moment = require("moment");
module.exports = NodeHelper.create({
config: [],
start: function () {
console.log("Starting node helper: " + this.name);
},
socketNotificationReceived: function (notification, payload) {
if (notification === "GET_COMIC") {
this.config = payload.config;
var comic = payload.comic.toLowerCase();
var dailyTime = payload.config.timeForDaily;
var dailyStart = moment(dailyTime[0], "k");
var dailyEnd = moment(dailyTime[1], "k");
this.log("Daily time frame: " + dailyStart.format("HH:mm") + " - " + dailyEnd.format("HH:mm"));
var random;
if (moment().isBetween(dailyStart,dailyEnd)) {
this.log("Time for the daily comic!");
random = false
} else {
this.log("Time for a random comic!")
random = true
}
this.log("Notification: " + notification + " Payload: " + comic);
switch (comic) {
case "dilbert":
this.getDilbert("en", random);
break;
case "dilbert_de":
this.getDilbert("de", random);
break;
case "garfield":
this.getGarfield(random);
break;
case "xkcd":
this.getXkcd(random);
break;
case "licd":
this.getLICD(random);
break;
case "peanuts":
this.getPeanuts(random);
break;
case "calvin+hobbes":
this.getCalvinandHobbes(random);
break;
/*case "nichtlustig":
this.getNichtLustig(random);
break;*/
case "ruthe":
this.getRuthe(random);
break;
default:
this.log("Comic not found!");
}
}
},
getGarfield: function (random) {
var self = this;
var url = "https://garfield.com/comic/";
this.log("-> Garfield request");
var start = new Date (1979, 1, 1);
var end = new Date();
var comicDate = (random) ? new Date(start.getTime() + Math.random() * (end.getTime() - start.getTime())) : end;
url += moment(comicDate).format("YYYY/MM/DD") + "/";
this.log("Trying url: "+url);
request(url, function (error, response, body) {
var $ = cheerio.load(body);
var src = $(".img-responsive").attr('src');
self.log("Garfield -> " + src);
self.sendSocketNotification("COMIC", {
img : src
});
});
return;
},
getPeanuts: function (random) {
var self = this;
var url = "https://www.gocomics.com/peanuts/";
this.log("-> Peanuts request");
var start = new Date (1952, 1, 1);
var end = new Date();
var comicDate = (random) ? new Date(start.getTime() + Math.random() * (end.getTime() - start.getTime())) : end;
url += moment(comicDate).format("YYYY/MM/DD") + "/";
this.log("Trying url: "+url);
request(url, function (error, response, body) {
var $ = cheerio.load(body);
var src = $(".item-comic-image .img-fluid").attr('src');
self.log("Peanuts -> " + src);
self.sendSocketNotification("COMIC", {
img : src
});
});
return;
},
getLICD: function (random) {
var self = this;
var url = "https://www.leasticoulddo.com/comic/";
this.log("-> LICD request");
var start = new Date (2003, 2, 10);
var end = new Date();
var comicDate = (random) ? new Date(start.getTime() + Math.random() * (end.getTime() - start.getTime())) : end;
url += moment(comicDate).format("YYYYMMDD") + "/";
this.log("Trying url: "+url);
request(url, function (error, response, body) {
if (error) {
self.log("Error fetching comic. Trying again... Message: " + error);
self.getLICD(random);
} else {
var $ = cheerio.load(body);
var src = $(".comic-wrap .comic").attr('src');
self.log("LICD comic source -> " + src);
self.sendSocketNotification("COMIC", {
img : src
});
}
});
return;
},
getCalvinandHobbes: function (random) {
var self = this;
var url = "https://www.gocomics.com/calvinandhobbes/";
this.log("-> Calvin and Hobbes request");
var start = new Date (1985, 11, 18);
var end = new Date(1995, 12, 31);
var comicDate = (random) ? new Date(start.getTime() + Math.random() * (end.getTime() - start.getTime())) : end;
url += moment(comicDate).format("YYYY/MM/DD") + "/";
this.log("Trying url: "+url);
request(url, function (error, response, body) {
var $ = cheerio.load(body);
//console.log($(".img-fluid"));
var src = $(".item-comic-image .img-fluid").attr('src');
self.log("Calvin and Hobbes -> " + src);
self.sendSocketNotification("COMIC", {
img : src
});
});
return;
},
getDilbert: function (lang, random) {
var self = this;
var url = (lang == "en") ? "https://dilbert.com/" : "https://www.ingenieur.de/unterhaltung/dilbert/" ;
this.log("-> Dilbert request");
if (random && lang == "en") {
var start = new Date (2000, 1, 1);
var end = new Date();
var randomDate = new Date(start.getTime() + Math.random() * (end.getTime() - start.getTime()));
this.log("Random Date: "+randomDate);
url += moment(randomDate).format("YYYY-MM-DD") + "/";
} else {
url += moment().format("YYYY-MM-DD") + "/";
}
request(url, function (error, response, body) {
var $ = cheerio.load(body);
var imgClass = (lang == "en") ? ".img-comic" : ".size-large";
var src = $(imgClass).attr('src');
self.log("Dilbert -> " + src);
self.sendSocketNotification("COMIC", {
img : src
});
});
return;
},
getRuthe: function (random) {
var self = this;
var url = "http://ruthe.de/cartoon/";
this.log("Ruthe request");
if (random) {
var randomNr = Math.floor((Math.random() * 3233) + 1);
url += randomNr;
} else {
url += "3233";
}
this.log("Ruthe URL: "+url);
request(url, function (error, response, body) {
var $ = cheerio.load(body);
var src = $("#link_archive > img").attr('src');
self.log("Ruthe -> " + src);
self.sendSocketNotification("COMIC", {
img : "http://ruthe.de" + src
});
});
return;
},
/*getNichtLustig: function (random) {
var self = this;
var url = "http://static.nichtlustig.de/toondb";
this.log("Nichtlustig request");
var min = 501;
var max = 150422;
var randomNr = Math.floor((Math.random() * 149922) + 501);
url += randomNr.padStart(6, '0') + ".html";
this.log("Nichtlustig Comic URL: "+url);
request(url, function (error, response, body) {
var $ = cheerio.load(body);
var src = $("").attr('');
self.log("Nichtlustig comic -> " + src);
self.sendSocketNotification("COMIC", {
img : ""
});
});
return;
},*/
getXkcd: function (random) {
var self = this;
var baseUrl = "http://xkcd.com/";
request(baseUrl + "info.0.json", function (error, response, body) {
if (!error && response.statusCode == 200) {
var comic = JSON.parse(body);
var current = comic.num;
var date = new Date();
var dayOfWeek = date.getDay();
var url = "http://xkcd.com/";
if (!random && [1,3,5].includes(dayOfWeek)) {
url = url + current;
self.log("Its Monwedfriday! Showing current xkcd comic!");
} else {
self.log("It's not Monwedfriday! Parsing random comic");
var randomNumber = Math.floor((Math.random() * current) + 1);
url = "http://xkcd.com/" + randomNumber;
}
self.log(url);
request(url, function (error, response, body) {
if (!error && response.statusCode == 200) {
var $ = cheerio.load(body);
var src = $("#comic img").attr('src');
self.log("xkcd src -> " + src);
self.sendSocketNotification("COMIC", {img : src});
} else {
self.log("Error "+error+": Comic could not be fetched");
}
});
}
});
},
log: function (msg) {
if (this.config && this.config.debug) {
console.log(this.name + ":", JSON.stringify(msg));
}
},
});