-
Notifications
You must be signed in to change notification settings - Fork 0
/
crawler.js
97 lines (84 loc) · 2.99 KB
/
crawler.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
88
89
90
91
92
93
94
95
96
97
var fs = require('fs');
var os = require('os');
var request = require('request');
var cheerio = require('cheerio');
var xmlBuilder = require('xmlbuilder');
request('http://wiki.vg/index.php?title=Protocol_version_numbers', function(err, response, body) {
var $ = cheerio.load(body);
var header = $('h2:contains("Versions after the Netty rewrite")');
if (header.length < 1) {
console.error('Doesn\'t found h2 "Versions after the Netty rewrite"');
process.exit(1);
return;
}
var table = header.nextAll('table').first();
if (table.length < 1) {
console.error('Doesn\'t found table after header');
process.exit(1);
return;
}
var element, temp, first, second, version, type;
var result = [];
table.find('tr').each(function(index) {
element = $(this);
temp = element.find('td, th');
first = temp.eq(0).text().trim().toLowerCase();
second = temp.eq(1).text().trim().toLowerCase();
// Validate first row
if (index === 0) {
if (first !== 'release name' || second !== 'version number') {
console.error('Invalid tables columns: ' + first + ', ' + second);
process.exit(1);
}
} else {
if (/^[0-9]+w[0-9]+[a-z]*$/.test(first)) {
type = 'SNAPSHOT';
} else if (/^[0-9]+\.[0-9]+(\.[0-9]+)?-pre[0-9]*$/.test(first)) {
type = 'PRE_RELEASE';
} else if (/^[0-9]+\.[0-9]+(\.[0-9]+)?$/.test(first)) {
type = 'RELEASE';
} else if (first === '1.rv-pre1') {
type = 'JOKE';
} else {
console.warn('Unknown version expression: ' + first);
return;
}
if (second.length > 0)
version = second;
result.push({
version_name: first,
version_type: type,
protocol_version: parseInt(version)
});
}
});
if (result.length < 1) {
console.error('No data parsed');
process.exit(1);
return;
}
writeResult(result);
});
function writeResult(data) {
if (!fs.existsSync('data'))
fs.mkdirSync('data');
if (fs.existsSync('data/versions.json'))
fs.truncateSync('data/versions.json', 0);
fs.writeFileSync('data/versions.json', JSON.stringify(data, null, 2) + os.EOL);
if (fs.existsSync('data/versions.xml'))
fs.truncateSync('data/versions.xml', 0);
fs.writeFileSync('data/versions.xml', createXml(data) + os.EOL);
}
function createXml(data) {
var builder = xmlBuilder.create('minecraftVersions');
var current;
for (var i = 0; i < data.length; i++) {
current = data[i];
builder.ele('minecraftVersion', {
versionName: current.version_name,
versionType: current.version_type,
protocolVersion: current.protocol_version
});
}
return builder.end({ pretty: true });
}