-
Notifications
You must be signed in to change notification settings - Fork 1
/
tramwaj.js
48 lines (39 loc) · 1.23 KB
/
tramwaj.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
var fs = require('fs'),
bot = require('nodemw'),
client = new bot('config.js');
var lines = [];
client.getPagesInCategory('Linie tramwajowe', function(pages) {
pages.forEach(function(page) {
if (page.ns !== 0) {
return;
}
client.getArticle(page.title, function(content) {
client.expandTemplates(content, page.title, function(tmpl) {
var from = client.getTemplateParamFromXml(tmpl, 'uruchomiona') || '',
to = client.getTemplateParamFromXml(tmpl, 'zlikwidowana') || '',
num = client.getTemplateParamFromXml(tmpl, 'numer') || '';
from = parseInt(from.split(' ').pop().replace(/\[|\]/g, ''), 10) || false;
to = parseInt(to.split(' ').pop().replace(/\[|\]/g, ''), 10) || 2012;
if (from && num) {
console.log('* [[' + page.title + ']] (' + from + '-' + to + ')');
lines.push({
name: num.trim(),
from: from,
to: to
});
lines.sort(function(a,b) {
var lineA = parseInt(a.name, 10),
lineB = parseInt(b.name, 10);
if (!isNaN(lineA) && !isNaN(lineB)) {
return lineA - lineB;
}
else {
return !isNaN(lineA) ? -1 : 1;
}
});
fs.writeFileSync('db/tramwaj.json', JSON.stringify(lines, null, ' '));
}
});
});
});
});