-
Notifications
You must be signed in to change notification settings - Fork 0
/
csde_clubs.js
executable file
·70 lines (59 loc) · 1.84 KB
/
csde_clubs.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
#!/usr/bin/env node
const assert = require('assert');
const argparse = require('argparse');
const fetch = require('node-fetch');
const fs = require('fs');
const path = require('path');
const utils = require('./src/utils');
const {unify_team_name} = require('./src/eventutils.js');
const TARGET_FILE = path.join(__dirname, 'src', 'csde_map.js');
async function main() {
const parser = new argparse.ArgumentParser({
description: 'Generate and write club IDs for CourtSpot',
});
parser.addArgument('--display', {action: 'storeTrue', help: `Do not write to ${TARGET_FILE}, display instead`});
const args = parser.parseArgs();
const lines = [];
for (const league of [1, 2, 3, 4, 5, 6, 7, 11, 12, 14]) {
const response = await fetch(`https://courtspot.de/php__Skripte/getVereine.php?liga=${league}`);
assert.equal(response.status, 200);
const xml = await response.text();
const [err_msg, doc] = utils.parseXML(xml);
assert(!err_msg, `Failed to parse XML: ${err_msg}`);
for (const el of Array.from(doc.getElementsByTagName('Verein'))) {
let name = el.getElementsByTagName('Lang')[0].textContent;
const name_m = /^(.*)\ 1$/.exec(name);
if (name_m) {
name = name_m[1];
}
name = unify_team_name(name);
const id = el.getElementsByTagName('Nummer')[0].textContent;
lines.push(` '${league}-${id}': '${name}',`);
}
}
if (args.display) {
for (const line of lines) {
console.log(line.trim());
}
} else {
const js = (
"'use strict';\n\n" +
'const TEAM_NAMES = {\n' +
lines.join('\n') +
'\n};\n\n' +
'module.exports = {\n' +
' TEAM_NAMES,\n' +
'};\n'
);
await fs.promises.writeFile(TARGET_FILE, js, {encoding: 'utf-8'});
console.log(`Updated ${TARGET_FILE}`);
}
}
(async () => {
try {
await main();
} catch (e) {
console.error(e.stack);
process.exit(2);
}
})();