-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathscraper.py
More file actions
executable file
·172 lines (134 loc) · 5.12 KB
/
scraper.py
File metadata and controls
executable file
·172 lines (134 loc) · 5.12 KB
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#!/usr/bin/env python3
import argparse
import concurrent.futures
import html.entities
import html.parser
import os
import re
import sys
import urllib.parse
import urllib.request
class ContestHTMLParser(html.parser.HTMLParser):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.problems = set()
self.re = re.compile('contest/\d+/problem/(\w+)')
def handle_starttag(self, tag, attrs):
if tag != 'a':
return
href = dict(attrs).get('href')
if not href:
return
match = self.re.search(href)
if not match:
return
self.problems.add(match.group(1))
def getProblems(self):
return sorted(list(self.problems))
class ProblemHTMLParser(html.parser.HTMLParser):
class Node:
def __init__(self, tag, attrs):
self.tag = tag
self.attrs = attrs
self.children = []
self.data = ''
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.stack = []
self.sample_input_nodes = []
self.recording = False
def handle_starttag(self, tag, attrs):
attrs = dict(attrs)
sample_input_div = tag == 'div' and attrs.get('class', '').find('sample') != -1
if self.recording or sample_input_div:
self.recording = True
node = ProblemHTMLParser.Node(tag, attrs)
if self.stack:
if tag == 'br':
self.stack[-1].data += '\n'
else:
self.stack[-1].children.append(node)
self.stack.append(node)
def handle_endtag(self, tag):
if self.recording:
node = self.stack.pop()
if not self.stack:
self.sample_input_nodes.append(node)
self.recording = False
def handle_data(self, data):
if self.recording:
self.stack[-1].data += data
def handle_entityref(self, name):
code = html.entities.name2codepoint.get(name)
assert code is not None, 'Unrecognized HTML entity &{};'.format(name)
self.handle_data(chr(code))
def handle_charref(self, name):
if name[0] == 'x':
self.handle_data(chr(int(name[1:], 16)))
else:
self.handle_data(chr(int(name)))
def walkNodes(self, node, output_pre_datas):
if node.tag == 'pre':
output_pre_datas.append(node.data)
else:
for child in node.children:
self.walkNodes(child, output_pre_datas)
def getExamples(self):
if not self.sample_input_nodes:
return []
assert len(self.sample_input_nodes) == 1
pre_datas = []
self.walkNodes(self.sample_input_nodes[0], pre_datas)
assert len(pre_datas) % 2 == 0
examples = []
for i in range(0, len(pre_datas), 2):
examples.append((pre_datas[i], pre_datas[i+1]))
return examples
def download_problem(contest_uri, problem):
problem_uri = contest_uri + '/problem/' + problem
print('Retrieving', problem_uri, '...')
sys.stdout.flush()
problem_html = urllib.request.urlopen(problem_uri).read().decode('utf-8')
print('Retrieved problem {} ({} bytes).'.format(problem, len(problem_html)))
# Hack for codeforces HTML errors
problem_html = problem_html.replace('<p</p>', '<p></p>')
problem_html = problem_html.replace('<ul</ul>', '<ul></ul>')
problem_html = problem_html.replace('<div class="sample-test"<', '<div class="sample-test"><')
parser = ProblemHTMLParser()
try:
parser.feed(problem_html)
except:
print(problem_html, file=sys.stderr)
raise
examples = parser.getExamples()
problem_dir = problem.lower()
if not os.path.isdir(problem_dir):
os.mkdir(problem_dir)
for i, example in enumerate(examples, 1):
input_path = os.path.join(problem_dir, 'in{}'.format(i))
with open(input_path, 'w') as f:
f.write(example[0])
output_path = os.path.join(problem_dir, 'out{}'.format(i))
with open(output_path, 'w') as f:
f.write(example[1])
print('Wrote {} examples for problem {}.'.format(len(examples), problem))
parser = argparse.ArgumentParser(description='Codeforces scraper. https://github.com/lovrop/codeforces-scraper')
parser.add_argument('contest', help='URI or numerical ID of contest to scrape')
args = parser.parse_args()
# See if it was just a numeric ID
try:
contest_id = int(args.contest)
contest_uri = 'http://codeforces.com/contest/{}'.format(contest_id)
except ValueError:
contest_uri = args.contest
print('Retrieving ', contest_uri, '... ', sep='', end='')
sys.stdout.flush()
contest_html = urllib.request.urlopen(contest_uri).read().decode('utf-8')
print('OK ({} bytes).'.format(len(contest_html)))
parser = ContestHTMLParser()
parser.feed(contest_html)
problems = parser.getProblems()
print('Found', len(problems), 'problems.')
with concurrent.futures.ThreadPoolExecutor(max_workers=5) as executor:
for problem in problems:
executor.submit(download_problem, contest_uri, problem)