Skip to content

Commit be6aa15

Browse files
Update leetcode.py
1 parent 04abb17 commit be6aa15

File tree

1 file changed

+11
-15
lines changed

1 file changed

+11
-15
lines changed

leetcode.py

+11-15
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,8 @@ def login(LEETCODE_SESSION):
2424
session.mount('http://', HTTPAdapter(max_retries=6)) # 超时重试次数
2525
session.mount('https://', HTTPAdapter(max_retries=6)) # 超时重试次数
2626
session.encoding = "utf-8" # 编码格式
27-
2827
session.cookies.set('LEETCODE_SESSION', LEETCODE_SESSION)
29-
is_login = session.cookies.get('LEETCODE_SESSION') != None
30-
if is_login:
31-
print("登录成功!")
32-
return session
33-
else:
34-
raise Exception("登录失败, 请检查COOKIE是否正确或过期!")
28+
return session
3529

3630

3731
# 获取某个题目的提交记录
@@ -90,16 +84,16 @@ def get_accepted_problems(session):
9084
r = session.post(url, data=payload, headers=headers, verify=False)
9185
response_data = json.loads(r.text)
9286

93-
return response_data
87+
return response_data['data']['userProfileQuestions']
9488

9589

9690

9791
# 生成Markdown文本
9892
def generate_markdown_text(response_data, session):
9993

100-
94+
response_data = response_data['questions']
95+
10196
# 相关介绍
102-
response_data = response_data['data']['userProfileQuestions']['questions']
10397
markdown_text = "## 相关介绍\n\n"
10498
markdown_text += "这是一个 [LeetCode](https://leetcode.cn/problemset/all/) 题目自动统计及分析程序,可自动统计所有提交通过的题目,并以 Markdown 的形式展示。\n\n"
10599
markdown_text += "根据个人需求,目前只考虑获取**提交次数**和**重刷次数**这两个指标,以便更好地进行刷题。\n\n"
@@ -168,8 +162,10 @@ def generate_markdown_text(response_data, session):
168162
if __name__ == '__main__':
169163
session = login(sys.argv[1]) # 登录,参数为已登录 LeetCode 账号的浏览器中 Cookie 名为 LEETCODE_SESSION 的值
170164
response_data = get_accepted_problems(session) # 获取所有通过的题目列表
171-
markdown_text = generate_markdown_text(response_data, session) # 生成Markdown文本
172-
173-
# 更新README.md文件
174-
with open("README.md", "w") as f:
175-
f.write(markdown_text)
165+
if response_data:
166+
markdown_text = generate_markdown_text(response_data, session) # 生成Markdown文本
167+
# 更新README.md文件
168+
with open("README.md", "w") as f:
169+
f.write(markdown_text)
170+
else:
171+
raise Exception("Cookies 已过期,请更新 LEETCODE_SESSION")

0 commit comments

Comments
 (0)