-
Notifications
You must be signed in to change notification settings - Fork 0
/
clock.mjs
32 lines (25 loc) · 1.17 KB
/
clock.mjs
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
"use strict"
import got from "got"
import * as cheerio from "cheerio"
import fs from "fs";
const getDoomsDay = async () => {
const { body } = await got("https://thebulletin.org/doomsday-clock/current-time/")
const $ = cheerio.load(body);
const timeSentence = await $(".fl-rich-text h2").first();
const textTime = await timeSentence.text().replace("\xa0", " ");
let sentenceResult = '';
const { groups: parsed } = textTime.match(/(?<sentence>.*: )?IT IS(?: STILL)? (?<time>\d+)(?: AND A (?<half>HALF))? (?<type>MINUTES|MINUTE|SECONDS|SECOND) TO MIDNIGHT/i)
if (parsed.sentence) {
sentenceResult = parsed.sentence;
} else {
const sentence = await $("h2.fl-heading .fl-heading-text").last();
sentenceResult = await sentence.text().replace("\xa0", " ");
}
const seconds = parsed.type === "seconds" ? Number(parsed.time) : (Number(parsed.time) * 60) + (parsed.half ? 30 : 0)
return {
type: 'seconds',
time: seconds,
sentence: sentenceResult.replace(': ', '').replace(':', '') ?? ''
};
};
fs.writeFile('doomsday.json', await getDoomsDay().then((data) => JSON.stringify(data)), (err) => {console.log(err)});