-
Notifications
You must be signed in to change notification settings - Fork 2
/
fetch_issues.js
78 lines (66 loc) · 2.32 KB
/
fetch_issues.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
const cf = require('cross-fetch');
const fs = require('fs-extra');
const WIKI_URL = 'https://wiki.nexusmods.com';
const REPO_URL = 'https://api.github.com/repos/Nexus-Mods/Vortex';
async function fetchWikiCategory(name) {
const res = await cf(`${WIKI_URL}/api.php?action=query&list=categorymembers&cmtitle=Category:${name}&cmlimit=500&format=json`)
const data = await res.json();
return await data.query.categorymembers.reduce(async (prev, member) => {
if (member.title.startsWith('File:')) {
return await prev;
} else if (member.title.startsWith('Category:')) {
return [].concat(await prev, await fetchWikiCategory(member.title.split(':')[1]));
} else {
return [].concat(await prev, [member.title]);
}
}, []);
}
async function fetchFAQ() {
const res = await cf(`${WIKI_URL}/api.php?action=parse&page=Frequently_Asked_Questions&format=json&prop=sections`)
const data = await res.json();
return data.parse.sections.filter(faq => faq.line !== undefined);
}
async function fetchIssues() {
const res = await cf(`${REPO_URL}/issues?filter=all&labels=reference`);
return await res.json();
}
function transformWikiPages(page, keywords) {
return {
type: 'wiki',
title: page,
url: `${WIKI_URL}/index.php/${page.replace(/ /g, '_')}`,
keywords: keywords[page] || [],
};
}
function transformFAQ(faq, keywords) {
return {
type: 'faq',
title: faq.line,
url: `${WIKI_URL}/index.php/Frequently_Asked_Questions#${faq.anchor}`,
keywords: keywords[faq.line] || [],
};
}
function transformIssue(issue, keywords) {
return {
type: 'issue',
title: issue.title,
url: issue.html_url,
keywords: keywords[issue.title] || [],
};
}
function makeUniqueByKey(input, key) {
return Object.values(input.reduce((prev, item) => { prev[key(item)] = item; return prev; }, {}));
}
async function main() {
const keywords = JSON.parse(await fs.readFile('keywords.json'));
const wiki = makeUniqueByKey((await fetchWikiCategory('Vortex'))
.map(w => transformWikiPages(w, keywords)),
w => w.title);
const faq = (await fetchFAQ())
.map(f => transformFAQ(f, keywords));
const issues = (await fetchIssues())
.map(i => transformIssue(i, keywords))
await fs.writeFile('issues.json',
JSON.stringify([...wiki, ...faq, ...issues], undefined, 2));
}
main();