-
Notifications
You must be signed in to change notification settings - Fork 1
/
contest.py
122 lines (110 loc) · 3.98 KB
/
contest.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
from ContestParser import ContestParser
from CFDownloader import CFDownloader
import codecs
import json
import time
from os import system, name, makedirs, path, chdir
settings = json.load(open("settings.json"))
apiKey = settings["api"]["key"]
apiSecret = settings["api"]["secret"]
groupId = settings["groupId"]
contestId = settings["contestId"]
def clear():
if name == 'nt':
_ = system('cls')
else:
_ = system('clear')
def displayMenu():
clear()
print("=========Codeforces Contest Downloader=========")
print("=============Created by LordierClaw============")
handle = settings["login"]["handle"]
print(f"Your handle: {handle}")
print(f"Group ID: {groupId} \t Contest ID: {contestId}")
print("-----------------------------------------------")
print("1. Get contest's information - generate database.json")
print("2. Download all submission (only the first solved)")
print("3. Run Dolos from download folder (dolos required)")
print("4. Let me do everything (1->2->3)")
print("0. Exit")
print("-----------------------------------------------")
return input("Select an option: ")
def displayReturn():
print("-----------------------------------------------")
input("Press Enter to continue...")
def getContestInformation(returnable=True):
clear()
cp = ContestParser(groupId, contestId, apiKey, apiSecret)
data = cp.get()
print("Saving database.json ...", end="")
try:
with codecs.open(f"database.json", "w", encoding="utf-8") as output:
json.dump(data, output, indent=2)
print("Completed")
if returnable == False:
return
except:
print("Failed")
displayReturn()
def downloadAllSubmission(returnable=True):
clear()
print("Start scraping and saving code...")
#checking download folder and database.json exists
makedirs("./download/", exist_ok=True)
if path.exists("database.json") == False:
print("database.json not found! Please return to the menu and get contest's information first!")
displayReturn()
return
data = json.load(codecs.open("database.json", "r", encoding="utf-8"))
print("database.json is loaded successfully.")
cf = CFDownloader()
cf.login(settings["login"]["handle"], settings["login"]["password"])
completedCount = 0
for user in data["solved"]:
handle = user["handle"]
print(f"Working on handle: {handle}")
for sub in user["submission"]:
subId = user["submission"][sub]
if (subId != 0):
name = f"./download/{sub}_{handle}_{subId}.cpp"
if path.exists(name): continue
with codecs.open(name, "w", encoding="utf-8") as output:
output.write(cf.getSourceCode(groupId, contestId, subId))
completedCount += 1
print(f"Saved: {name}, {completedCount} files downloaded and saved, waiting 1 second to avoid being banned")
time.sleep(1)
print(f"Completed: {completedCount} files downloaded and saved")
if (returnable == True):
displayReturn()
def runDolos():
clear()
print("Running dolos ...")
lang = settings["dolos"]["language"]
fileExt = settings["dolos"]["file_ext"]
command = f"dolos -f web -l {lang} *.{fileExt}"
chdir(path.abspath("download"))
system(command)
def doEverything():
getContestInformation(returnable=False)
downloadAllSubmission(returnable=False)
runDolos()
def main():
while(True):
option = displayMenu()
if option == '1':
getContestInformation()
elif option == '2':
downloadAllSubmission()
elif option == '3':
runDolos()
return
elif option == '4':
doEverything()
return
elif option == '0':
return
else:
print("Invalid selection. Please try again.")
displayReturn()
if __name__ == "__main__":
main()