-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackup.py
More file actions
63 lines (52 loc) · 1.9 KB
/
backup.py
File metadata and controls
63 lines (52 loc) · 1.9 KB
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
import os
from autokattis import Kattis
from dotenv import load_dotenv
import shutil
load_dotenv(".env")
extensions = {
"Python 3": ".py",
"C++": ".cpp",
}
# Log in to kattis
kt = Kattis(os.environ.get("K_USER"), os.environ.get("K_PASS"))
# Delete all previous saved problems
folder = './problems'
for filename in os.listdir(folder):
file_path = os.path.join(folder, filename)
try:
if os.path.isfile(file_path) or os.path.islink(file_path):
os.unlink(file_path)
elif os.path.isdir(file_path):
shutil.rmtree(file_path)
except Exception as e:
print('Failed to delete %s. Reason: %s' % (file_path, e))
# Get all successful submissions
stats = kt.stats()
probs = kt.problems()
template = ""
with open("md_template.md") as obj:
template = obj.read()
# print(stats[0])
# print(kt.problem(stats[0]["id"]))
for problem in stats:
if problem["test_case_passed"] != problem["test_case_full"]:
print("Not fully completed, not including", problem["name"])
else:
path = "./problems/"+problem["id"]
os.mkdir(path)
if problem["language"] not in extensions:
print("Not found language", problem["language"], "for", problem["name"])
else:
print(problem["id"])
solutionFilename = problem["id"]+extensions[problem["language"]]
with open(path+"/"+solutionFilename, 'w') as obj:
code = kt.get_soup_response(problem["link"]+"/source/"+solutionFilename)
code = code.find('p').get_text()
obj.write(code)
target = problem["id"]
for i in probs:
id = i["link"].split("/")[-1]
if id == target:
i["percent"] = round(i["acc"] / i["total"] * 100, 2)
with open(path+"/"+problem["id"]+".md", 'w') as obj:
obj.write(template.format(**i))