This repository was archived by the owner on Oct 27, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsubmit.js
72 lines (45 loc) · 1.5 KB
/
submit.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
'use strict'
const request = require('request').defaults({jar:true});
const options = require('./http_options');
const Promise = require('bluebird');
const fs = require('fs');
const path = require('path');
const cheerio = require('cheerio');
function submit(file_path,problemId,eventEmitter){
return function(data){
return new Promise(function(resolve,reject){
let problem=problemId.substring(0,problemId.length-1);
let problemIndex=problemId[problemId.length-1];
let http_options=options();
http_options.method='POST';
http_options.url='http://codeforces.com/problemset/problem/'+problem+'/'+problemIndex+'?csrf_token='+data.csrf_token;
let formData = {
csrf_token:data.csrf_token,
action:'submitSolutionFormSubmitted',
submittedProblemIndex:problemIndex,
source:'',
programTypeId:'42',
sourceFile: {
value: fs.createReadStream(file_path),
options: {
filename: path.basename(file_path),
contentType: 'text/x-c++src'
}
}
}
http_options.formData=formData;
request(http_options,function(err,res,body){
if(err){
return reject(err);
}
if(res.statusCode!='302'){
return reject(new Error('You have submitted exactly the same code before'));
}
eventEmitter.emit('submit',true);
let $=cheerio.load(body);
resolve(data);
})
})
}
}
module.exports=submit;