-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcodechef.py
More file actions
29 lines (28 loc) · 858 Bytes
/
codechef.py
File metadata and controls
29 lines (28 loc) · 858 Bytes
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
import os
import sys
import urllib2
import re
import HTMLParser
f = open('codechef.txt')
url_base = 'https://www.codechef.com'
username = 'nemausus'
for line in f.readlines():
url = ""
try:
url = url_base + line.rstrip()
parts = re.split('(/|,)', line)
filename = parts[4] if parts[2] == 'status' else parts[2] + "_" + parts[6]
text = urllib2.urlopen(url).read()
match = re.search('>(\d{5,7})<', text)
sol_url = url_base + '/viewplaintext/' + match.group(1)
code = urllib2.urlopen(sol_url).read()
code = code[5:-6]
code = HTMLParser.HTMLParser().unescape(code)
out = open(filename + '.cpp', 'w')
out.write('// ' + url + '\n')
out.write('// ' + sol_url + '\n')
out.write(code)
out.close()
except:
print "ERROR", url
f.close()