|
| 1 | +import argparse |
| 2 | +import os.path |
| 3 | +import time |
| 4 | + |
| 5 | +import requests |
| 6 | +from urllib import request |
| 7 | +import json |
| 8 | +import bs4 |
| 9 | +import javalang |
| 10 | + |
| 11 | +from codeforces import CodeforcesAPI |
| 12 | + |
| 13 | + |
| 14 | +def get_args(): |
| 15 | + parser = argparse.ArgumentParser() |
| 16 | + parser.add_argument("--problem_count", dest='problem_count', type=int, default=100) |
| 17 | + parser.add_argument("--submission_count", dest='submission_count', type=int, default=10) |
| 18 | + parser.add_argument("--min_rating", dest='min_rating', type=int, default=0) |
| 19 | + parser.add_argument("--max_rating", dest="max_rating", type=int, default=1500) |
| 20 | + parser.add_argument("--output_dir", dest="output_dir", type=str, default=".") |
| 21 | + return parser.parse_args() |
| 22 | + |
| 23 | + |
| 24 | +args = get_args() |
| 25 | + |
| 26 | + |
| 27 | +def check_json(answer): |
| 28 | + values = json.loads(answer) |
| 29 | + |
| 30 | + if values['status'] == 'OK': |
| 31 | + return values['result'] |
| 32 | + |
| 33 | + |
| 34 | +def get_main_name(tree): |
| 35 | + """ |
| 36 | + :return: class name with main method |
| 37 | + """ |
| 38 | + return next(klass.name for klass in tree.types |
| 39 | + if isinstance(klass, javalang.tree.ClassDeclaration) |
| 40 | + for m in klass.methods |
| 41 | + if m.name == 'main' and m.modifiers.issuperset({'public', 'static'})) |
| 42 | + |
| 43 | + |
| 44 | +def save_source_code(contest_id, submission_id): |
| 45 | + """ |
| 46 | + Parse html page to find source code of submission and save it in some unique package p${contest_id}.p${submission_id}. |
| 47 | + If we reach api request bound, then we try to sleep for 5 minutes. |
| 48 | + """ |
| 49 | + url = request.Request(f"http://codeforces.com/contest/{contest_id}/submission/{submission_id}") |
| 50 | + with request.urlopen(url) as req: |
| 51 | + soup = bs4.BeautifulSoup(req.read(), "html.parser") |
| 52 | + path = os.path.join(args.output_dir, f"p{contest_id}", f"p{submission_id}") |
| 53 | + if not os.path.exists(path): |
| 54 | + os.makedirs(path) |
| 55 | + code = "" |
| 56 | + for p in soup.find_all("pre", {"class": "program-source"}): |
| 57 | + code += p.get_text() |
| 58 | + tree = javalang.parse.parse(code) |
| 59 | + try: |
| 60 | + name = get_main_name(tree) |
| 61 | + with open(os.path.join(path, f"{name}.java"), 'w') as f: |
| 62 | + print(f"package p{contest_id}.p{submission_id};", file=f) |
| 63 | + f.write(code) |
| 64 | + except StopIteration: |
| 65 | + print("Sleeping, because we reach request bound") |
| 66 | + time.sleep(300) |
| 67 | + |
| 68 | + |
| 69 | +def main(): |
| 70 | + codeforces = "http://codeforces.com/api/" |
| 71 | + api = CodeforcesAPI() |
| 72 | + |
| 73 | + with request.urlopen(f"{codeforces}problemset.problems") as req: |
| 74 | + all_problems = check_json(req.read().decode('utf-8')) |
| 75 | + |
| 76 | + problems = [] |
| 77 | + cur_problem = 0 |
| 78 | + for p in all_problems['problems']: |
| 79 | + if cur_problem >= args.problem_count: |
| 80 | + break |
| 81 | + if p.get('rating') is None: |
| 82 | + continue |
| 83 | + if p['rating'] < args.min_rating or p['rating'] > args.max_rating: |
| 84 | + continue |
| 85 | + cur_problem += 1 |
| 86 | + problems.append({'contest_id': p['contestId'], 'index': p['index']}) |
| 87 | + |
| 88 | + print(f"Get {len(problems)} problems: {problems[0]}") |
| 89 | + |
| 90 | + """ |
| 91 | + For each problem try to take submission_count submissions. |
| 92 | + """ |
| 93 | + all_submission = 0 |
| 94 | + for i, p in enumerate(problems): |
| 95 | + cur_submission = 0 |
| 96 | + iteration = 0 |
| 97 | + page_size = 1000 |
| 98 | + while cur_submission < args.submission_count: |
| 99 | + length = 0 |
| 100 | + for s in api.contest_status(contest_id=p['contest_id'], from_=page_size * iteration + 1, count=page_size): |
| 101 | + if cur_submission >= args.submission_count: |
| 102 | + break |
| 103 | + length += 1 |
| 104 | + if s.problem.contest_id != p['contest_id'] or s.problem.index != p['index']: |
| 105 | + continue |
| 106 | + if s.programming_language != "Java 8": |
| 107 | + continue |
| 108 | + if s.verdict.name != "ok": |
| 109 | + continue |
| 110 | + save_source_code(p['contest_id'], s.id) |
| 111 | + cur_submission += 1 |
| 112 | + all_submission += 1 |
| 113 | + print(f"Get new {all_submission} program") |
| 114 | + iteration += 1 |
| 115 | + if length == 0: |
| 116 | + break |
| 117 | + |
| 118 | + |
| 119 | +if __name__ == "__main__": |
| 120 | + main() |
0 commit comments