This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python3 | |
import re | |
from pathlib import Path | |
import datetime | |
import requests | |
import os | |
import time | |
from sys import argv | |
from bs4 import BeautifulSoup | |
extensions = { | |
"GNU C11": ".c", | |
"Clang++17 Diagnostics": ".cpp", | |
"GNU C++11": ".cpp", | |
"GNU C++14": ".cpp", | |
"GNU C++17": ".cpp", | |
"MS C++": ".cpp", | |
"MS C++ 2017": ".cpp", | |
"GNU C++17 (64)": ".cpp", | |
"Mono C#": ".cs", | |
"D": ".d", | |
"Go": ".go", | |
"Haskell": ".hs", | |
"Java 11": ".java", | |
"Java 8": ".java", | |
"Kotlin": ".kt", | |
"Ocaml": ".ml", | |
"Delphi": ".dpr", | |
"FPC": ".pp", | |
"PascalABC.NET": ".pas", | |
"Perl": ".pl", | |
"PHP": ".php", | |
"Python 2": ".py", | |
"Python 3": ".py", | |
"PyPy 2": ".py", | |
"PyPy 3": ".py", | |
"Ruby": ".rb", | |
"Rust": ".rs", | |
"Scala": ".scala", | |
"JavaScript": ".js", | |
"Node.js": ".js", | |
} | |
s = requests.session() | |
for row in [ | |
row.find_all("td") | |
for page in [ | |
BeautifulSoup( | |
s.get(f"https://codeforces.com/submissions/{argv[1]}/page/{pageNo}").text, | |
"html.parser", | |
).find_all("tr", attrs={"data-submission-id": True}) | |
for pageNo in range( | |
1, | |
int( | |
(lambda lastPage: 1 if lastPage is None else lastPage.text)( | |
BeautifulSoup( | |
s.get(f"https://codeforces.com/submissions/{argv[1]}/").text, | |
"html.parser", | |
).select_one("div.pagination > ul > li:nth-last-child(2)") | |
) | |
) | |
+ 1, | |
) | |
] | |
for row in page | |
if row.find("a", attrs={"href": re.compile("^/contest/")}) is not None | |
]: | |
submission, when, who, problem, lang, verdict, duration, mem = [ | |
cell.get_text(strip=True) for cell in row | |
] | |
contest = ( | |
row[3].find("a", attrs={"href": re.compile("^/contest/")})["href"].split("/")[2] | |
) | |
epoch = time.mktime(datetime.datetime.strptime(when, "%b/%d/%Y %H:%M").timetuple()) | |
( | |
filename := Path( | |
f"./{argv[1]}/{contest}/{problem}/{submission} - {verdict}{extensions.get(lang, ' ' + lang)}" | |
) | |
).parent.mkdir(parents=True, exist_ok=True) | |
print(filename) | |
with open(filename, "w+") as f: | |
url = f"https://codeforces.com/contest/{contest}/submission/{submission}" | |
while ( | |
not (resp := s.get(url)).ok | |
or ( | |
source := BeautifulSoup(resp.text, "html.parser").find( | |
"pre", id="program-source-text" | |
) | |
) | |
is None | |
): | |
print(f"Retrying {url}...") | |
time.sleep(30) | |
f.write(source.text) | |
os.utime(filename, (epoch, epoch)) | |
# os.sleep(1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment