-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutil.js
executable file
·87 lines (81 loc) · 2.09 KB
/
util.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
/**
* util.js
*
* Utilities shared by spam-finding scripts.
*/
import {readFile, writeFile} from 'fs/promises';
import {CookieJar} from 'tough-cookie';
import got from 'got';
/**
* HTTP client.
*/
const http = got.extend({
cookieJar: new CookieJar(),
headers: {
'User-Agent': 'Fandom Global Content Review Lookup, ' +
'contact KockaAdmiralac through [email protected] ' +
'for specific information.'
},
resolveBodyOnly: true,
retry: {
limit: 0
},
timeout: {
request: 20 * 1000
}
});
/**
* Makes a GET request to a JSON endpoint.
* @param {String} url URL to query
* @param {Object} searchParams Query string parameters
* @returns {Promise} Promise to listen on for response
*/
export function getJSON(url, searchParams) {
return http.get(url, {
responseType: 'json',
searchParams
});
}
/**
* Queries the MediaWiki API.
* @param {String} url Wiki URL
* @param {Object} params Parameters to supply in the query
* @returns {Promise} Promise to listen on for response
*/
export function apiQuery(url, params) {
params.action = 'query';
params.format = 'json';
return getJSON(`${url}/api.php`, params);
}
/**
* Reads a JSON file.
* @param {string} filename File to read JSON from
* @returns {*} Parsed JSON file
*/
export async function readJSON(filename) {
return JSON.parse(await readFile(filename, {
encoding: 'utf-8'
}));
}
/**
* Writes data to a JSON file.
* @param {string} filename File to read JSON from
* @param {object} data Data to write to the file
* @returns {Promise<void>} Promise to listen on for completion
*/
export async function writeJSON(filename, data) {
await writeFile(filename, JSON.stringify(data));
}
/**
* Posts to a Discord webhook, if any is specified through environment
* variables.
* @param {string} content Content to post
*/
export async function notify(content) {
const webhookUrl = process.env.DISCORD_WEBHOOK;
if (webhookUrl) {
await http.post(webhookUrl, {
json: {content}
});
}
}