-
Notifications
You must be signed in to change notification settings - Fork 0
/
judge0_compiler.py
74 lines (64 loc) · 2.34 KB
/
judge0_compiler.py
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
import requests, os
import base64
from dotenv import load_dotenv
from colorama import Fore, Style
from pprint import pprint
try:
load_dotenv()
url = "https://judge0-ce.p.rapidapi.com/submissions"
rapidAI_key = os.getenv("RAPIDAPI_KEY")
except Exception as e:
print(f"{Fore.RED}Judge0 Connection Error: {e} \n{Style.RESET_ALL}")
# all rest 7-12 are runtime errors
STATUS_MAP = {
1: "In Queue",
2: "Processing",
3: "Accepted",
4: "Wrong Answer",
5: "Time Limit Exceeded",
6: "Compilation Error",
**dict.fromkeys(range(7, 13), "Runtime Error"),
13: "Internal Error",
}
# Generate a submission and send it to RapidAI's Judge0 Client for compilation
def create_submission(source_code, inputs=""):
querystring = {"base64_encoded": "true", "wait": "true", "fields": "*"}
encoded_source_code = base64.b64encode(source_code.encode("utf-8")).decode("utf-8")
encoded_inputs = base64.b64encode(inputs.encode("utf-8")).decode("utf-8")
payload = {
"language_id": 71,
"source_code": encoded_source_code,
"stdin": encoded_inputs,
}
headers = {
"x-rapidapi-key": rapidAI_key,
"x-rapidapi-host": "judge0-ce.p.rapidapi.com",
"Content-Type": "application/json",
}
try:
response = requests.post(url, json=payload, headers=headers, params=querystring)
pprint(response.json())
return response.json(), response.json()["token"]
except Exception as e:
print(
f"{Fore.RED}Judge0 - Failed to create submission : {e} \n{Style.RESET_ALL}"
)
# Fetch the compiled code using the unqiue token
def get_submission(token, response):
if response["stderr"] is None:
get_url = url + f"/{token}"
querystring = {"base64_encoded": "true", "fields": "*"}
headers = {
"x-rapidapi-key": rapidAI_key,
"x-rapidapi-host": "judge0-ce.p.rapidapi.com",
}
try:
response = requests.get(get_url, headers=headers, params=querystring)
output = base64.b64decode(response.json()["stdout"]).decode("utf-8")
return output
except Exception as e:
print(
f"{Fore.RED}Judge0 - Failed to fetch submission : {e} \n{Style.RESET_ALL}"
)
else:
return base64.b64decode(response["stderr"]).decode("utf-8")