-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
110 lines (94 loc) · 3.29 KB
/
main.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
import time
import requests
import json
import os
s = requests.session()
username = ""
password = ""
record_list = []
suffixMap = {
"C++": ".cpp",
"Python3": ".py",
"Python": ".py",
"MySQL": ".sql",
"Go": ".go",
"Java": ".java",
"C": ".c",
"JavaScript": ".js",
"PHP": ".php",
"C#": ".cs",
"Ruby": ".rb",
"Swift": ".swift",
"Scala": ".scl",
"Kotlin": ".kt",
"Rust": ".rs",
}
def read_config():
with open("config.json", "r") as f:
conf = json.load(f)
global username, password
username = conf["username"]
password = conf["password"]
def login():
s.post(
"https://leetcode.cn/accounts/login/",
data={"login": username, "password": password},
headers={"Referer": "https://leetcode.cn/accounts/login/"},
)
def fetch_list():
cur_page = 1
while True:
print("获取范围 " + str(cur_page * 40 - 40) + "-" + str(cur_page * 40))
res = s.get(
"https://leetcode.cn/api/submissions/",
params={"offset": (cur_page - 1) * 40, "limit": 40},
).json()
if len(res["submissions_dump"]) == 0:
print("结束")
return
global record_list
record_list += res["submissions_dump"]
cur_page = cur_page + 1
time.sleep(1)
def fetch_record(record_id):
post_data = {
"operationName": "mySubmissionDetail",
"variables": {"id": str(record_id)},
"query": "query mySubmissionDetail($id: ID!) {\n submissionDetail(submissionId: $id) {\n id\n code\n "
" runtime\n memory\n rawMemory\n statusDisplay\n timestamp\n lang\n "
"passedTestCaseCnt\n totalTestCaseCnt\n sourceUrl\n question {\n titleSlug\n "
"title\n translatedTitle\n questionId\n __typename\n }\n ... on "
"GeneralSubmissionNode {\n outputDetail {\n codeOutput\n expectedOutput\n "
"input\n compileError\n runtimeError\n lastTestcase\n __typename\n "
"}\n __typename\n }\n submissionComment {\n comment\n flagType\n "
"__typename\n }\n __typename\n }\n}\n",
}
res = s.post(
"https://leetcode.cn/graphql/",
data=json.dumps(post_data),
headers={"Content-Type": "application/json"},
).json()
return res["data"]["submissionDetail"]
def main():
read_config()
login()
fetch_list()
for i in record_list:
r = fetch_record(i["id"])
print(i)
code_dir = os.path.join("out", r["question"]["questionId"])
if not os.path.exists(code_dir):
os.mkdir(code_dir)
code_file = str(r["id"])
code_file += "_" + str(r["statusDisplay"]).lower().replace(" ", "_")
if str(r["statusDisplay"]) == "Accepted":
code_file += "_" + str(r["runtime"]).replace(" ", "")
code_file += "_" + str(r["rawMemory"])
code_file += suffixMap[i["lang"]]
with open(os.path.join(code_dir, code_file), "w") as f:
f.write(r["code"])
with open(os.path.join(code_dir, "record.json"), "w") as f:
json.dump({"simple": i, "full": r}, f)
time.sleep(1)
if __name__ == "__main__":
main()