-
Notifications
You must be signed in to change notification settings - Fork 355
/
api_leetcode.py
77 lines (68 loc) · 2.73 KB
/
api_leetcode.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
# -*- coding: utf-8 -*-
"""
:author @wangwangit
cron: 10 10 * * *
new Env('LeetCode 每日一题');
"""
import requests
from notify_mtr import send
from utils import get_data
class LeetCode:
@staticmethod
def main():
base_url = "https://leetcode-cn.com"
# 获取今日每日一题的题名(英文)
res = requests.post(
f"{base_url}/graphql",
json={
"operationName": "questionOfToday",
"variables": {},
"query": "query questionOfToday { todayRecord { "
"question { questionFrontendId questionTitleSlug __typename } "
"lastSubmission { id __typename } "
"date userStatus __typename } }",
},
).json()
title_slug = (
res.get("data")
.get("todayRecord")[0]
.get("question")
.get("questionTitleSlug")
)
# 获取今日每日一题的所有信息
url = f"{base_url}/problems/{title_slug}"
res = requests.post(
f"{base_url}/graphql",
json={
"operationName": "questionData",
"variables": {"titleSlug": title_slug},
"query": "query questionData($titleSlug: String!) "
"{ question(titleSlug: $titleSlug) { questionId questionFrontendId "
"boundTopicId title titleSlug content translatedTitle translatedContent"
" isPaidOnly difficulty likes dislikes isLiked similarQuestions "
"contributors { username profileUrl avatarUrl __typename } "
"langToValidPlayground topicTags "
"{ name slug translatedName __typename } "
"companyTagStats codeSnippets { lang langSlug code __typename } "
"stats hints solution { id canSeeDetail __typename } "
"status sampleTestCase metaData judgerAvailable judgeType "
"mysqlSchemas enableRunCode envInfo book "
"{ id bookName pressName source shortDescription fullDescription"
" bookImgUrl pressImgUrl productUrl __typename } "
"isSubscribed isDailyQuestion dailyRecordStatus "
"editorType ugcQuestionId style __typename } }",
},
).json()
# 题目
question = res.get("data").get("question")
# 题号
num = question.get("questionFrontendId")
# 题名(中文)
zh_title = question.get("translatedTitle")
return f'<a href="{url}">{num}. {zh_title}</a>'
if __name__ == "__main__":
_data = get_data()
leetcode = _data.get("LEETCODE")
if leetcode:
result = LeetCode().main()
send("LeetCode 每日一题", result)