-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
395 lines (366 loc) · 10.6 KB
/
index.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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
import MediaWikiBot from "mwbot";
import got from "got";
import sizeOf from "image-size";
import { stripIndent } from "common-tags";
function getFirstItem(object) {
return object[Object.keys(object)[0]];
}
const [, , username, password] = process.argv;
const API_URL = "https://explainxkcd.com/wiki/api.php";
const USER_AGENT =
"Netscape Navigator/4.0 (Apple IIGS; 1024x1; x64) Pentium 4 (JavaScript, with Ad Blockers) Boat mode, HIGH-HEAT DRYING DISABLED, explainxkcdBot";
const CURRENT_COMIC_PAGE_ID = "1923";
const REVISIONS_PAGE_ID = "27987";
const CHECK_INTERVAL = 120e3;
const NOT_EXPECTED_CHECK_INTERVAL = 9e5; // 15 minute intervals on days which are not Monday, Wednesday, Friday
const MAX_LOGIN_TIME = 6048e5; // 1 week, to be safe
const MONTHS = [
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December",
];
const REQUEST_OPTION = {
headers: {
"User-Agent": USER_AGENT,
},
};
const EDIT_SUMMARY = "Created by theusafBOT: ";
const CHANGE_SUMMARY = "Changed by theusafBOT: ";
const LOGIN_DATA = {
apiUrl: API_URL,
username,
password,
};
let expectedComicNumber = null;
let loginTimestamp = 0;
let dateChecked = new Date(0);
let bot = new MediaWikiBot();
function log(message) {
console.log(`[${new Date().toISOString()}] - ${message}`);
}
function getInterval() {
const d = new Date();
const day = d.getDay();
if (
(day === 1 || day === 3 || day === 5) &&
d.getDate() !== dateChecked.getDate()
) {
return CHECK_INTERVAL;
}
return NOT_EXPECTED_CHECK_INTERVAL;
}
/**
* login - logs in and starts the updateWiki loop
*/
function login() {
if (!username || !password) {
console.log("[ERR] - Usage: node index.js <username> <password>");
process.exit(0);
} else {
bot
.loginGetEditToken(LOGIN_DATA)
.then(() => {
loginTimestamp = Date.now();
updateWiki();
})
.catch((err) => {
console.error("[ERR] - Failed to log in");
console.error(err);
process.exit(0);
});
}
}
/**
* updateWiki
* - Fetches information from xkcd and if there is a new comic, create a new page
* - Runs at an interval of 2 minutes
*/
async function updateWiki() {
// log in again after a certain amount of time
if (Date.now() - loginTimestamp > MAX_LOGIN_TIME) {
log("[INFO] - Logging in again");
bot = new MediaWikiBot();
login();
return;
}
try {
// Fetch latest xkcd information
log("[INFO] - Fetching information from xkcd");
const { body } = await got("https://xkcd.com/info.0.json", REQUEST_OPTION);
const comicData = JSON.parse(body);
const { num, img, day, month, year } = comicData;
const date = `${MONTHS[+month - 1]} ${day}, ${year}`;
// if expected number is already set, but current number is lower, no need to re-poll explainxkcd, ignore.
if (expectedComicNumber !== null && expectedComicNumber > num) {
log("[INFO] - No new comic found.");
setTimeout(updateWiki, getInterval());
return;
}
// Fetching expected xkcd number from explain xkcd.
log("[INFO] - Fetching latest comic on explainxkcd");
const currentWikiTemplate = await bot.read("Template:LATESTCOMIC");
const currentRevision = (
currentWikiTemplate.query.pages[CURRENT_COMIC_PAGE_ID] ??
getFirstItem(currentWikiTemplate.query.pages)
).revisions[0]["*"];
const expectedNumber = +currentRevision.match(/\d+$/)[0] + 1;
expectedComicNumber = expectedNumber;
// if expected number is already set, but current number is lower, no need to create new posts.
if (expectedComicNumber > num) {
log("[INFO] - No new comic found.");
setTimeout(updateWiki, getInterval());
return;
}
// Fetch images
log("[INFO] - Fetching images");
const baseImage = await got(img, REQUEST_OPTION).buffer();
const imageExtension = comicData.img.match(/(?<=\.)[a-z]+$/)[0];
const largeImage = await got(
`${img.match(/.*?(?=\.[a-z]+$)/)[0]}_2x.${imageExtension}`,
REQUEST_OPTION,
)
.buffer()
.catch(() => null);
const baseImageSize = sizeOf(baseImage);
const largeImageSize = largeImage ? sizeOf(largeImage) : null;
const imageTitle =
comicData.img.match(/(?<=\/comics\/).*?(?=\.[a-z]+$)/)[0] +
(largeImage ? "_2x" : "");
createNewExplanation({
date,
image: largeImage ?? baseImage,
comicData,
imageTitle,
imageExtension,
baseImageSize,
largeImageSize,
is2x: (largeImage ?? baseImage) === largeImage,
});
} catch (err) {
console.error(
"[ERR] - Failed to fetch xkcd information. See below for details:",
);
console.error(err);
setTimeout(updateWiki, getInterval() * 2);
}
}
async function isInteractiveComic(number) {
try {
const { body } = await got(`https://xkcd.com/${number}`, REQUEST_OPTION);
return /<script src=".*?\/\d+\/[\/\w\d\s\-_]+?\.js/.test(body);
} catch (err) {
return false;
}
}
/**
* createNewExplanation - Creates and updates the various pages
*/
async function createNewExplanation(info) {
try {
const {
imageTitle,
comicData,
imageExtension,
image,
date,
baseImageSize,
largeImageSize,
is2x,
} = info;
const {
safe_title: comicTitle,
title: alternateTitle,
alt,
num: comicNum,
transcript,
} = comicData;
const isInteractiveComicResult = await isInteractiveComic(comicNum);
const title = alternateTitle.includes("&") ? alternateTitle : comicTitle;
// Refresh the edit token to edit/create pages
bot.editToken = null;
await bot.getEditToken();
// upload the image
log("[INFO] - Uploading image to explainxkcd");
await bot.upload(
`${imageTitle}.${imageExtension}`,
image,
stripIndent`
== Summary ==
${
is2x
? `Small size can be found at ${
comicData.img.match(/.*?(?=\.[a-z]+$)/)[0]
}.${imageExtension}`
: ""
}
== Licensing ==
{{XKCD file}}
`,
);
// create/edit redirects
log("[INFO] - Creating redirects");
// If the comic title is a number underneath the current comic number, do not create this redirect
if (!/^\d+$/.test(title) || +title > comicNum) {
const comicTitleRedirect = `#REDIRECT [[${comicNum}: ${title}]]\n`;
await bot.edit(
title,
comicTitleRedirect,
`${EDIT_SUMMARY}${comicTitleRedirect}`,
);
} else {
log(
`[WARN] - Skipped creation of '${title}' due to lower numerical title`,
);
}
const comicNumRedirect = `#REDIRECT [[${comicNum}: ${title}]]\n`;
await bot.edit(
`${comicNum}`,
comicNumRedirect,
`${EDIT_SUMMARY}${comicNumRedirect}`,
);
// create main page
log("[INFO] - Creating main page");
let sizeString = `${baseImageSize.width}x${baseImageSize.height}px`;
// Note 2022-02-03
// If both the 'standard' and '2x' size seem to be the same size
const isSameSize =
largeImageSize &&
baseImageSize.width === largeImageSize.width &&
baseImageSize.height === largeImageSize.height;
if (isSameSize) {
sizeString = `${Math.floor(baseImageSize.width / 2)}x${Math.floor(
baseImageSize.height / 2,
)}px`;
}
// If the base image size is larger than the large image size, use the large image size / 2
const isSmallImageLarger =
largeImageSize &&
baseImageSize.width > largeImageSize.width &&
baseImageSize.height > largeImageSize.height;
if (isSmallImageLarger) {
sizeString = `${Math.floor(largeImageSize.width / 2)}x${Math.floor(
largeImageSize.height / 2,
)}px`;
}
await bot.edit(
`${comicNum}: ${title}`,
stripIndent`
{{comic
| number = ${comicNum}
| date = ${date}
| title = ${title}
${
sizeString === ""
? `| image = ${imageTitle}.${imageExtension}`
: `| image = ${imageTitle}.${imageExtension}
| imagesize = ${sizeString}
| noexpand = true`
}
| titletext = ${alt.replace(/\n/g, "<br>")}
}}${
isInteractiveComicResult
? stripIndent`
* To experience the interactivity, visit the [https://xkcd.com/${comicNum}/ original comic].
`
: ""
}
==Explanation==
{{incomplete|Created by a BOT - Please change this comment when editing this page. Do NOT delete this tag too soon.}}
==Transcript==
{{incomplete transcript|Do NOT delete this tag too soon.}}
${transcript ? `${transcript}\n` : ""}
{{comic discussion}}${
isInteractiveComicResult
? stripIndent`
[[Category:Interactive comics]]
`
: ""
}
`,
`${EDIT_SUMMARY}${comicNum}`,
);
// create talk page
log("[INFO] - Creating talk page");
await bot.edit(
`Talk:${comicNum}: ${title}`,
stripIndent`
<!--Please sign your posts with ~~~~ and don't delete this text. New comments should be added at the bottom.-->
${
isSameSize || isSmallImageLarger
? `The 'standard' and '2x' sized images had unexpected sizes, so an imagesize parameter has been added to render the image consistently with other comics on this website. See the web [https://web.archive.org/web/*/${imageTitle.replace(
/_2x$/,
"",
)}.${imageExtension} archive] for more details. --~~~~`
: ""
}
`,
`${EDIT_SUMMARY} talk page for ${comicNum}`,
);
// update latest comic
log("[INFO] - Updating latest comic");
await bot.edit(
"Template:LATESTCOMIC",
`<noinclude>The latest [[xkcd]] comic is number: </noinclude>${comicNum}`,
`${CHANGE_SUMMARY}${comicNum}`,
);
// update list of all comics
log("[INFO] - Updating list of comics");
const allComicsRead = await bot.read("List of all comics");
const allComicsContent = (
allComicsRead.query.pages[REVISIONS_PAGE_ID] ??
getFirstItem(allComicsRead.query.pages)
).revisions[0]["*"].split("\n"); // .slots.main["*"].split("\n");
for (let i = 0; i < allComicsContent.length; i++) {
if (allComicsContent[i] === "!Date<onlyinclude>") {
const isoDate = new Date(date).toISOString().slice(0, 10);
allComicsContent.splice(
i + 1,
0,
`{{comicsrow|${comicNum}|${isoDate}|${title}|${imageTitle.replace(
/_/g,
" ",
)}.${imageExtension}}}`,
);
break;
}
}
await bot.edit(
"List of all comics",
allComicsContent.join("\n"),
`${CHANGE_SUMMARY}${allComicsContent.length}`,
);
dateChecked = new Date();
setTimeout(updateWiki, getInterval());
// Archive it!
try {
const urls = [
"https://xkcd.com",
"https://explainxkcd.com",
`https://xkcd.com/${comicNum}`,
`https://explainxkcd.com/${comicNum - 1}`,
`https://explainxkcd.com/${comicNum}`,
];
for (const url of urls) {
await got(`https://web.archive.org/save/${url}`);
}
} catch (err) {
/* Ignored */
}
} catch (err) {
console.error(
"[ERR] - Failed to create explanation. See below for details:",
);
console.error(err);
setTimeout(updateWiki, getInterval() * 2);
}
}
login();