This repository has been archived by the owner on Jul 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
73 lines (56 loc) · 2.15 KB
/
index.ts
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
import axios from 'axios';
import { parse } from 'node-html-parser';
import * as fs from 'fs';
import { fields, regex } from './mappings';
import Element from './element';
import he from 'he';
const url = "https://en.wikipedia.org/wiki/Element_";
let head: string;
let body: string;
if (fs.existsSync("result/")) fs.rmdirSync("result/", { recursive: true });
fs.mkdir("result/", err => {
if (err) throw err;
});
getData().then(s =>
fs.writeFile("result/elements.json", JSON.stringify(s.sort(j => j.AtomicNumber), null, 4), err => {
if (err) throw err;
})
);
async function getData(): Promise<Element[]> {
const obj: Element[] = []
for (let i = 1; i <= 118; i++) {
obj.push(await requestData(i));
}
return obj;
}
function decode(input: string | undefined): string {
return he.decode(input).replace(/(<([^>]+)>)/gi, "").trim();
}
async function requestData(i: number): Promise<Element> {
const data = await axios.get(url + i).then(res => res.data);
const element = new Element();
const root = parse(data);
const infobox = root.querySelector("table.infobox");
const caption = infobox.querySelector("caption");
const captionParts = caption.rawText.split(", ");
element.Name = captionParts[0];
element.Symbol = captionParts[1].replace(/\d*/, "")
element.AtomicNumber = i;
const tbody = infobox.querySelector("tbody");
tbody.childNodes.forEach(s => {
const tr = (s as any) as HTMLElement;
if (tr.querySelector("th")?.hasAttribute("scope")) {
const td = tr.querySelector("td");
const th = tr.querySelector("th");
head = decode(th?.innerHTML)
body = decode(td?.innerHTML);
if (head.includes("atomic weight") || head.includes("Mass number")) head = "AtomicWeight";
if (fields.has(head)) {
head = fields.get(head) as string;
body = body.match(regex.get(head) != null ? regex.get(head) as RegExp : /.+/g)?.values().next().value as string;
(element as any)[head] = (isNaN(+body) ? body : Number.parseFloat(body));
}
}
});
return element;
}