-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathnode_helper.js
More file actions
59 lines (47 loc) · 1.67 KB
/
Copy pathnode_helper.js
File metadata and controls
59 lines (47 loc) · 1.67 KB
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
// Node 18+ ships a global fetch, so no external dependency is needed.
var NodeHelper = require("node_helper");
// Dilbert ended on 2023-03-12 and dilbert.com is gone, so we pull a random
// strip from the public archive maintained at github.com/jvarn/dilbert-archive.
// Each year has a JSON file keyed by date; every entry carries an
// "originalimageurl" that points to a copy of the strip on the Wayback Machine.
const ARCHIVE_BASE = "https://raw.githubusercontent.com/jvarn/dilbert-archive/main/public/comics-data";
const FIRST_YEAR = 1989;
const LAST_YEAR = 2023;
module.exports = NodeHelper.create({
start: function() {
console.log("Starting node helper: " + this.name);
},
socketNotificationReceived: function(notification, payload) {
var self = this;
if(notification === "GET_COMIC") {
self.getRandomComic();
return;
}
},
getRandomComic: function() {
var self = this;
var year = FIRST_YEAR + Math.floor(Math.random() * (LAST_YEAR - FIRST_YEAR + 1));
var url = ARCHIVE_BASE + "/" + year + ".json";
console.log("Dilbert -> fetching archive for " + year + ": " + url);
fetch(url)
.then(response => response.json())
.then(comics => {
var dates = Object.keys(comics);
if (dates.length === 0) {
throw new Error("No comics found for " + year);
}
var date = dates[Math.floor(Math.random() * dates.length)];
var comic = comics[date];
var img = comic.originalimageurl;
console.log("Dilbert -> " + date + " -> " + img);
self.sendSocketNotification("COMIC", {
img: img,
date: date,
title: comic.title || ""
});
})
.catch((error) => {
console.log("Dilbert Fetch Error -> " + error);
});
},
});