-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
87 lines (77 loc) · 2.41 KB
/
main.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
79
80
81
82
83
84
85
86
87
import Wikiapi from 'wikiapi';
import fetch from 'node-fetch';
import * as dotenv from 'dotenv';
dotenv.config();
/* Underlay & Wikipedia parameters */
/* ------------------------------- */
const ulCollection = 'reliable-sources/vaccine-topics-xmsdk95byp';
const ulCollectionVersion = '0.0.2';
const wikiPageDestination = 'Wikipedia:Vaccine safety/Sources/Assessments';
const ulCollectionUrl = `https://www.underlay.org/api/${ulCollection}/download?version=${ulCollectionVersion}`;
/* ------------------------------- */
/* Set edit summary */
const editsummary = `Updated from ${ulCollectionUrl}`;
/* Load the Underlay collection data and parse it into Wikitable formatting */
const parseJson = async () => {
/* Fetch UL collection data */
const response = await fetch(ulCollectionUrl, { json: true });
const body = await response.text();
const data = JSON.parse(body);
/* Iterate over each Source and format it into Wikitable formatting */
const sources = data.data.Source;
const tableText = sources.reduce((prev, curr) => {
const split = curr.discussionForum.split('(');
const forumPrefix = split[0];
const forumSuffix = split[1];
const discussionForumText =
split.length > 1 ? `[${forumSuffix} ${forumPrefix.trim()}]` : forumPrefix;
return `${prev}
|-
|${curr.type}
|${curr.name}
|${curr.assessment}
|${discussionForumText}
|${curr.discussionLastDate}
|
|[${curr.url}]
|${curr.uses}
`.trim();
}, '');
return tableText;
};
const run = async () => {
/* Login to wikiAPI */
const wiki = new Wikiapi();
const login_options = {
user_name: process.env.username,
password: process.env.password,
};
await wiki.login(login_options);
/* Set page that the table will be written to. */
await wiki.page(wikiPageDestination);
/* Load values in Wikitable text format from Underlay collection */
const dataValues = await parseJson();
/* Call wiki.edit function to write to the destination Wiki page */
/* Note, this will overwrite the entire content of the page, if */
/* you want to append or edit, manipulate the `pageData` value. */
await wiki.edit(
(_pageData) => {
return `{| class="wikitable sortable"
|-
! rowspan=2 | Type
! rowspan=2 width=30% | Source name
! rowspan=2 width=2% | Assess<br/>ment
! colspan=3 | Discussions
! rowspan=2 | URL
! rowspan=2 | Uses
|-
! Forums
! width=2% | Date (last)
! width=30% | Discussion summary
${dataValues}
|}`;
},
{ bot: 1, summary: `${editsummary}` }
);
};
run();