forked from OreosLab/checkinpanel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api_leetcode.py
76 lines (68 loc) · 3.06 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
# -*- coding: utf-8 -*-
"""
:author @wangwangit
cron: 10 10 * * *
new Env('LeetCode 每日一题');
"""
import json
import requests
from notify_mtr import send
from utils import get_data
class LeetCode:
@staticmethod
def main():
base_url = "https://leetcode-cn.com"
# 获取今日每日一题的题名(英文)
response = requests.post(
base_url + "/graphql",
json={
"operationName": "questionOfToday",
"variables": {},
"query": "query questionOfToday { todayRecord { question { questionFrontendId questionTitleSlug "
"__typename } lastSubmission { id __typename } date userStatus __typename }} ",
},
)
leetcode_title = (
json.loads(response.text)
.get("data")
.get("todayRecord")[0]
.get("question")
.get("questionTitleSlug")
)
# 获取今日每日一题的所有信息
url = base_url + "/problems/" + leetcode_title
response = requests.post(
base_url + "/graphql",
json={
"operationName": "questionData",
"variables": {"titleSlug": leetcode_title},
"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格式
json_text = json.loads(response.text).get("data").get("question")
# 题目题号
num = jsonText.get("questionFrontendId")
# 题名(中文)
leetcode_title = json_text.get("translatedTitle")
msg = num + "." + leetcode_title
return f"{msg}\n{url}"
if __name__ == "__main__":
data = get_data()
leetcode = data.get("LEETCODE")
if leetcode:
res = LeetCode().main()
send("LeetCode 每日一题", res)