-
Notifications
You must be signed in to change notification settings - Fork 99
/
script.js
80 lines (69 loc) · 1.87 KB
/
script.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
const axios = require('axios');
const cheerio = require('cheerio');
var fs = require('fs');
url = 'http://codeforces.com/contest/1256/problem/C'
let getTestCaseFromProblemHtml = (dir, html) => {
fs.copyFileSync(`${dir}/../template.cpp`, `${dir}/sol.cpp`);
data = [];
const $ = cheerio.load(html);
$('div.input pre').each((i, elem) => {
data[i] = {
...data[i],
input: $(elem).text()
};
});
$('div.output pre').each((i, elem) => {
data[i] = ({
...data[i],
output: $(elem).text()
});
});
console.log(data);
data.forEach((test, i) => {
fs.writeFile(`${dir}/in${i}.txt`, test.input, function(err) {
if(err) {
console.log(err);
}
console.log(`The file ${dir}/in${i}.txt was saved!`);
});
fs.writeFile(`${dir}/out${i}.txt`, test.output, function(err) {
if(err) {
console.log(err);
}
console.log(`The file ${dir}/out${i}.txt was saved!`);
});
})
console.log(data);
}
function getTestCaseFromProblemUrl(url) {
var dir = `./${url.substring(url.lastIndexOf('/')+1)}`;
if (!fs.existsSync(dir)){
fs.mkdirSync(dir);
}
axios.get(url)
.then(response => {
// console.log(response);
getTestCaseFromProblemHtml(dir, response.data);
}
)
.catch(err => console.log(err));
}
// getTestCaseFromProblemUrl(url);
contest_url = 'http://codeforces.com/contest/1256';
// ''
let getTotalProblemsFromContestHtml = (html) => {
data = [];
const $ = cheerio.load(html);
console.log('parsing');
$('tr td.id a').each((i, elem) => {
problem_url = 'https://codeforces.com/' + $(elem).attr('href')
console.log(problem_url);
getTestCaseFromProblemUrl(problem_url);
});
}
const contestUrl = process.argv[2];
axios.get(contestUrl)
.then(response => {
// console.log(response);
getTotalProblemsFromContestHtml(response.data);
});