forked from vsp4/codechef-rating-predictor
-
Notifications
You must be signed in to change notification settings - Fork 1
/
verify.js
113 lines (93 loc) · 2.78 KB
/
verify.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
var http = require("http");
var https = require("https");
var util = require("util");
var cheerio = require("cheerio");
var async = require("async");
var path = require("path");
var fs = require("fs");
var dataList = {};
var originalList = {};
var cacheDir = "cache";
require("./helper.js")();
var type = "all";
function grabPage(pageno, callback)
{
var url = "https://www.codechef.com/api/ratings/" + type + "?sortBy=global_rank&order=asc&itemsPerPage=40&page=" + pageno;
var filepath = path.resolve(path.join(cacheDir, pageno + ".json"));
execHttps(url, function(source)
{
if (source.indexOf("availablePages") == -1)
{
grabPage(pageno, callback);
return;
}
fs.writeFile(filepath, source, 'utf8');
setImmediate(callback);
}, 4);
}
function readPage(pageno, callback)
{
var filepath = path.resolve(path.join(cacheDir, pageno + ".json"));
var source = fs.readFileSync(filepath).toString();
var obj = JSON.parse(source);
obj.list.forEach(function(data)
{
originalList[data.username] = parseInt(data.all_rating);
});
setImmediate(callback);
}
function savePages(start, end, callback)
{
var arr = [];
for (var i = start; i <= end; i++)
{
arr.push(i);
}
async.eachLimit(arr, 5, grabPage, function(err)
{
setImmediate(callback);
});
}
function getMatching(url, start, end, callback)
{
var url = util.format(url);
execHttps(url, function(source)
{
var $ = cheerio.load(source);
var lastpage = parseInt($('.pageinfo').text().split(' ')[2]) - 1;
$('tbody>tr').each(function(i, data)
{
var username = $($(data).children()[1]).text();
var predicted = parseInt($($(data).children()[3]).text());
dataList[username] = {user: username, predicted: predicted};
});
var arr = [];
for (var i = start; i <= end; i++)
{
arr.push(i);
}
async.eachLimit(arr, 5, readPage, function(err)
{
var limit = 5000;
var countError = new Array(limit).fill(0);
Object.keys(dataList).forEach(function(username)
{
var diff = Math.abs(dataList[username].predicted - originalList[username]);
if (diff != undefined)
{
countError[diff]++;
}
console.log(username, dataList[username].predicted, originalList[username], diff);
});
for (var i = 0; i < limit; i++)
{
if (countError[i])
{
console.log(i, countError[i]);
}
}
});
}, 4);
}
//savePages(1, 1003);
getMatching("http://127.0.0.1:8080/contest/JUNE17/all", 1, 1003);