This repository has been archived by the owner on Jul 20, 2024. It is now read-only.
generated from YashTotale/boilerplate-react-with-redux
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetTerms.ts
62 lines (54 loc) · 1.5 KB
/
getTerms.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
//@ts-ignore
import reader from "g-sheets-api";
import { writeData, baseOptions } from "./index";
import { getPageData } from "./getWikipedia";
import { TermProps } from "../Utils/interfaces";
interface TermObject {
name: string;
link: string;
}
const termsRequest = () => {
return new Promise<TermObject[]>((resolve, reject) => {
const options = { ...baseOptions, sheetNumber: 4 };
reader(options, resolve);
});
};
const cleanTermData = async (terms: TermObject[]): Promise<TermProps[]> => {
const cleanedTerms: TermProps[] = [];
await Promise.all(
terms.map(async ({ name, link }) => {
let summary: string;
let summarySource: string;
const isWikipedia = link.includes("en.wikipedia.org");
if (isWikipedia) {
summarySource = "Wikipedia";
const title = link.split("/").pop();
if (title) {
try {
const data = await getPageData(title);
summary = data.summary;
link = data.url.toString();
} catch (e) {
console.log(e);
process.exit(1);
}
}
}
const termArray = name.split("; ");
termArray.forEach((split) =>
cleanedTerms.push({
name: split,
link,
summary,
summarySource,
})
);
})
);
return cleanedTerms;
};
export const getTerms = async () => {
const terms = await termsRequest();
const cleanedTerms = await cleanTermData(terms);
await writeData("Terms", cleanedTerms);
};