Created
June 25, 2020 12:02
-
-
Save Mukundan314/2032d5fc59b3365a5b6034cb85174c21 to your computer and use it in GitHub Desktop.
Tool to do custom tests on codeforces form cli
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
import argparse | |
import getpass | |
import pathlib | |
import sys | |
import time | |
import lxml.html | |
import requests | |
LANG_TO_ID = { | |
"GNU GCC C11 5.1.0": 43, | |
"Clang++17 Diagnostics": 52, | |
"GNU G++11 5.1.0": 42, | |
"GNU G++14 6.4.0": 50, | |
"GNU G++17 7.3.0": 54, | |
"Microsoft Visual C++ 2010": 2, | |
"Microsoft Visual C++ 2017": 59, | |
"GNU G++17 9.2.0 (64 bit, msys 2)": 61, | |
"C# Mono 5.18": 9, | |
"D DMD32 v2.091.0": 28, | |
"Go 1.14": 32, | |
"Haskell GHC 8.6.3": 12, | |
"Java 11.0.5": 60, | |
"Java 1.8.0_162": 36, | |
"Kotlin 1.3.70": 48, | |
"OCaml 4.02.1": 19, | |
"Delphi 7": 3, | |
"Free Pascal 3.0.2": 4, | |
"PascalABC.NET 3.4.2": 51, | |
"Perl 5.20.1": 13, | |
"PHP 7.2.13": 6, | |
"Python 2.7.15": 7, | |
"Python 3.7.2": 31, | |
"PyPy 2.7 (7.2.0)": 40, | |
"PyPy 3.6 (7.2.0)": 41, | |
"Ruby 2.0.0p645": 8, | |
"Rust 1.42.0": 49, | |
"Scala 2.12.8": 20, | |
"JavaScript V8 4.8.0": 34, | |
"Node.js 9.4.0": 55, | |
"ActiveTcl 8.5": 14, | |
"Io-2008-01-07 (Win32)": 15, | |
"Pike 7.8": 17, | |
"Befunge": 18, | |
"OpenCobol 1.0": 22, | |
"Factor": 25, | |
"Secret_171": 26, | |
"Roco": 27, | |
"Ada GNAT 4": 33, | |
"Mysterious Language": 38, | |
"FALSE": 39, | |
"Picat 0.9": 44, | |
"J": 47, | |
"Microsoft Q#": 56, | |
"Text": 57, | |
"UnknownX": 62, | |
} | |
def main(argv): | |
parser = argparse.ArgumentParser(argv[0]) | |
parser.add_argument("-i", "--input", type=pathlib.Path, help="Input file") | |
parser.add_argument("-p", "--poll", type=float, help="Poll duration", default=0.5) | |
parser.add_argument("program", type=pathlib.Path, help="The program to run") | |
parser.add_argument("language", type=str, help="Programing language to run with") | |
parser.add_argument("handle", nargs="?", type=str, help="Codeforces handle or email") | |
parser.add_argument("password", nargs="?", type=str, help="Codeforces Password") | |
args = parser.parse_args(argv[1:]) | |
if args.handle is None: | |
args.handle = input("Handle/Email: ") | |
if args.password is None: | |
args.password = getpass.getpass() | |
programTypeId = LANG_TO_ID[args.language] | |
sourceCode = args.program.read_bytes() | |
inp = "" if args.input is None else args.input.read_text() | |
with requests.Session() as sess: | |
res = sess.get("https://codeforces.com") | |
parsed = lxml.html.fromstring(res.text) | |
csrf_token = parsed.find_class("csrf-token")[0].get("data-csrf") | |
res = sess.post( | |
"https://codeforces.com/enter", | |
data={ | |
"action": "enter", | |
"csrf_token": csrf_token, | |
"handleOrEmail": args.handle, | |
"password": args.password, | |
}, | |
) | |
parsed = lxml.html.fromstring(res.text) | |
csrf_token = parsed.find_class("csrf-token")[0].get("data-csrf") | |
res = sess.post( | |
"https://codeforces.com/data/customtest", | |
data={ | |
"action": "submitSourceCode", | |
"csrf_token": csrf_token, | |
"input": inp, | |
"programTypeId": programTypeId, | |
"sourceCode": sourceCode, | |
}, | |
) | |
submit_id = res.json()["customTestSubmitId"] | |
output = stat = None | |
while output is None: | |
res = sess.post( | |
"https://codeforces.com/data/customtest", | |
data={ | |
"action": "getVerdict", | |
"customTestSubmitId": submit_id, | |
"csrf_token": csrf_token, | |
}, | |
) | |
output = res.json().get("output", None) | |
stat = res.json().get("stat", None) | |
time.sleep(args.poll) | |
print(output) | |
print("=====") | |
print("Used:", stat) | |
if __name__ == "__main__": | |
main(sys.argv) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment