This repository has been archived by the owner on Jan 22, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 35
/
toJSON.mjs
147 lines (137 loc) · 4 KB
/
toJSON.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
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
import * as fs from "fs";
import * as path from "path";
import dateFns from "date-fns";
import fetch from "node-fetch";
import jsdom from "jsdom";
const markdown = fs.readFileSync(
path.join(process.cwd(), "README.md"),
"utf-8"
);
const tableMarkdown = markdown.substr(
markdown.indexOf("|"),
markdown.lastIndexOf("|") - markdown.indexOf("|") + 1
);
const padZero = s => `0${s}`.substr(-2);
const undefinedIfZeroLength = s => {
if (s === undefined) {
return undefined
}
const t = s.trim();
return t.length === 0 ? undefined : t;
};
const metaFromUrl = async url => {
try {
const html = await fetch(url);
const dom = new jsdom.JSDOM(await html.text());
const ogImage = dom.window.document.querySelector(
'meta[property="og:image"]'
);
const twitterImage = dom.window.document.querySelector(
'meta[name="twitter:image"]'
);
const imageEl = twitterImage || ogImage;
const twitterSite = dom.window.document.querySelector(
'meta[name="twitter:site"]'
);
const keywords = dom.window.document.querySelector('meta[name="keywords"]');
const description = dom.window.document.querySelector(
'meta[name="description"]'
);
return {
image: imageEl ? imageEl.getAttribute("content") : undefined,
twitter: twitterSite ? twitterSite.getAttribute("content") : undefined,
description: description
? description.getAttribute("content")
: undefined,
keywords: keywords
? keywords
.getAttribute("content")
.split(",")
.map(undefinedIfZeroLength)
: undefined
};
} catch (error) {
return undefined;
}
};
const toJSON = async () => {
const { homeferences } = tableMarkdown
.split("\n")
.map(s => s.trim())
.map(s =>
s
.substr(1, s.length - 2)
.split("|")
.map(s => s.trim())
)
.reduce(
({ homeferences, baseYear, baseMonth }, line) => {
const [date, nameAndURL, price, topic] = line;
const dateTime = /<time datetime="(?:(2[0-9]{3})-([0-9]{2})-01T00:00:00Z)">/.exec(
date
);
const dayRange = /^[0-9]{1,2}(?:[–-]([0-9]{1,2}))?$/.exec(date.trim());
if (dateTime) {
baseYear = parseInt(dateTime[1]);
baseMonth = parseInt(dateTime[2]);
}
if (baseYear && baseMonth && dayRange) {
const startDay = parseInt(dayRange[0], 10);
const endDay = dayRange[1] && parseInt(dayRange[1], 10);
const startDate = new Date(
`${baseYear}-${padZero(baseMonth)}-${padZero(startDay)}T00:00:00Z`
);
const endDate = dateFns.subSeconds(
dateFns.addDays(
endDay
? new Date(
`${baseYear}-${padZero(
baseMonth + (endDay && endDay < startDay ? 1 : 0)
)}-${padZero(endDay)}T00:00:00Z` // FIXME: Rollover to January
)
: startDate,
1
),
1
);
const [_, name, url] = /\[([^\]]+)\]\(([^)]+)\)/.exec(nameAndURL);
return {
homeferences: [
...homeferences,
{
name,
url,
topic: undefinedIfZeroLength(topic),
price: undefinedIfZeroLength(price.replace(/\\/g, "")),
startDay: startDate.toISOString().substr(0, 10),
endDay: endDate.toISOString().substr(0, 10)
}
],
baseYear,
baseMonth
};
}
return {
homeferences,
baseYear,
baseMonth
};
},
{
baseDate: null,
homeferences: []
}
);
return Promise.all(
homeferences.map(async homeference => {
const meta = await metaFromUrl(homeference.url);
return {
...homeference,
...meta
};
})
);
};
toJSON().then(homeferences =>
console.log(JSON.stringify(homeferences, null, 2))
);