This repository has been archived by the owner on Oct 9, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
codeforces_downloader.cpp
150 lines (138 loc) · 4.24 KB
/
codeforces_downloader.cpp
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
//https://github.com/boleynsu/acmicpc-codes/blob/master/templates/Core.hpp
#include <Core>
//http://www.boost.org/
#include <boost/filesystem.hpp>
//http://curl.haxx.se/
#include <curl/curl.h>
#include <curl/easy.h>
str USER,PASS;
size_t write_data(char *data,size_t size,size_t num,string* buffer)
{
buffer->append(data,size*num);
return size*num;
}
str page_url;
str page_content;
size_t page_count;
str contest;
str file_url;
str file_content;
void save_file()
{
cerr<<"save()"<<endl;
const str getid("\"/contest/"+contest+"/problem/");
static const str begincode("<pre class=\"prettyprint\" style=\"padding:0.5em;\">");
static const str endcode("</pre>");
str id=contest+file_content.substr(file_content.find(getid)+sz(getid),1);
size_t p=file_content.find(getid);
p=file_content.find("<td>",p);
size_t q=file_content.find("</td>",p);
str lang=file_content.substr(p,q-p);
str type;
if (lang.find("C++")!=str::npos) type=".cpp";
else if (file_content.find("Java")!=str::npos) type=".java";
else if (file_content.find("PHP")!=str::npos) type=".php";
else type=".txt";
if (!boost::filesystem::exists(id+".java")
&&!boost::filesystem::exists(id+".cpp")
&&!boost::filesystem::exists(id+".txt")
&&!boost::filesystem::exists(id+".php"))
{
str output;
size_t p=file_content.find(begincode)+sz(begincode);
size_t q=file_content.find(endcode,p);
repf(i,p,q) if (file_content[i]!='\r') output.pb(file_content[i]);
output.pb('\n');
ofstream file(id+type);
rep(i,sz(output))
{
if (output[i]!='&') file<<output[i];
else
{
int j=i;
whl(output[++j]!=';') ;
if (output[i+1]=='a') file<<'&';
else if (output[i+1]=='q') file<<'\"';
else if (output[i+1]=='l') file<<'<';
else if (output[i+1]=='g') file<<'>';
i=j;
}
}
}
cerr<<"/save()"<<endl;
}
void download_file()
{
cerr<<"download_file()"<<endl;
string buffer;
CURL *curl=curl_easy_init();
curl_easy_setopt(curl,CURLOPT_URL,file_url.c_str());
curl_easy_setopt(curl,CURLOPT_FOLLOWLOCATION,1);
curl_easy_setopt(curl,CURLOPT_WRITEFUNCTION,write_data);
curl_easy_setopt(curl,CURLOPT_WRITEDATA,&buffer);
curl_easy_perform(curl);
curl_easy_cleanup(curl);
buffer.swap(file_content);
save_file();
cerr<<"/download_file()"<<endl;
}
void get_file_url()
{
static const str subbegin(" data-submission-id=\""),subend("\"");
static const str idbegin("<a href=\"/contest/"),idend("/");
static const str gym("<a href=\"/problemset/gymProblem");
size_t p=0;
lp
{
p=page_content.find(subbegin,p);
if (p==str::npos) break;
p+=sz(subbegin);
size_t q=page_content.find(subend,p);
str sub=page_content.substr(p,q-p);
if (page_content.find(idbegin,p)>=page_content.find(gym,p)) continue;
p=page_content.find(idbegin,p)+sz(idbegin);
q=page_content.find(idend,p);
contest=page_content.substr(p,q-p);
file_url="http://codeforces.com/contest/"+contest+"/submission/"+sub;
size_t x=page_content.find(subbegin,p);
size_t y=page_content.find("<span class='verdict-accepted'>Accepted</span>",p);
if (y<x) download_file();
}
}
void download_page()
{
cerr<<"Page "<<++page_count<<": "<<page_url<<endl;
cerr<<"download_page()"<<endl;
string buffer;
CURL *curl=curl_easy_init();
curl_easy_setopt(curl,CURLOPT_URL,page_url.c_str());
curl_easy_setopt(curl,CURLOPT_FOLLOWLOCATION,1);
curl_easy_setopt(curl,CURLOPT_WRITEFUNCTION,write_data);
curl_easy_setopt(curl,CURLOPT_WRITEDATA,&buffer);
curl_easy_perform(curl);
curl_easy_cleanup(curl);
buffer.swap(page_content);
get_file_url();
cerr<<"/download_page()"<<endl;
}
bool has_next_page()
{
static const str beginpage("href=\""),endpage("\"");
size_t p=page_content.find("page-index",page_content.find("page-index active")+1);
if (p==str::npos) rtn false;
p=page_content.find(beginpage,p)+sz(beginpage);
size_t q=page_content.find(endpage,p);
page_url="http://codeforces.com"+page_content.substr(p,q-p);
rtn true;
}
int main()
{
cout<<"Username:"<<flush;
cin>>USER;
curl_global_init(CURL_GLOBAL_ALL);
page_url="http://codeforces.com/submissions/"+USER+"/page/1";
do download_page();
whl(has_next_page());
cout<<"Enter 'q' to quit."<<endl<<flush;
whl(cin.get()!='q');
}