前段时间一直在刷leetcode的每日一题,每天都是用电脑打开leetcode的网站查看,显得特别的麻烦,想在手机上查看,这样就可以利用空闲时间刷题。于是去下载leetcode的app,发现上面竟然没有每日一题的推送(建议官方及时更新)。
有需求就有服务!!!
怎么解决这个问题呢,一想到我可是个程序猿(老搬砖了)呀,当然得用编程的思想和方式去解决了,于是就有这篇文章😂
一,大致思路
二,具体实现
1)抓取leetcode每日一题数据(python真好用)
# coding=<encoding name> : # coding=utf-8
from datetime import datetime
import requests
import json
import smtplib
from email.mime.text import MIMEText
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 }}"
})
leetcodeTitle = json.loads(response.text).get('data').get('todayRecord')[0].get("question").get('questionTitleSlug')
# 获取今日每日一题的所有信息
url = base_url + "/problems/" + leetcodeTitle
response = requests.post(base_url + "/graphql",
json={"operationName": "questionData", "variables": {"titleSlug": leetcodeTitle},
"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格式
jsonText = json.loads(response.text).get('data').get("question")
# 题目题号
no = jsonText.get('questionFrontendId')
# 题名(中文)
leetcodeTitle = jsonText.get('translatedTitle')
# 题目难度级别
level = jsonText.get('difficulty')
# 题目内容
context = jsonText.get('translatedContent')
# print(leetcodeTitle)
# print(context)
# print(level)
# print(no)
# 早安语录接口(天行数据API,自行申请免费))
response = requests.get("")
json = json.loads(response.text)
# 得到语录数据
ana = json.get('newslist')[0].get('content')
# 表情链接
face_url = 'http://wx3.sinaimg.cn/large/007hyfXLly1g0uj7x5jpaj301o02a0sw.jpg'
# 开始运行时间(可通过配置文件解耦)
begin_time = datetime(2020, 12, 23)
# 脚本运行时间计算
info = "<span style='color:cornflowerblue'>本脚本已运行{0}天<span>".format(
(datetime.today() - begin_time).days.__str__())
# 数据全部HTML化
htmlText = """ <head>
<meta charset=UTF-8>
<link rel="stylesheet">
<style>
code {
color: blue;
font-size: larger;
}
</style>
</link>
</head>
<body>
<div> </B><BR></B><FONT
style="FONT-SIZE: 12pt; FILTER: shadow(color=#af2dco); WIDTH: 100%; COLOR: #730404; LINE-HEIGHT: 100%; FONT-FAMILY: 华文行楷"
size=6><span style="COLOR: cornflowerblue">早安语录:</span>""" + ana + """</FONT><img width="40px" src=""" + face_url + """">
<div>
<h3>Leetcode-每日一题</h3>
<h4>""" + no + '.' + leetcodeTitle + '.' + level + """</h4>""" + context + '本题连接:<a href=' + url + ">" + url + "</a></div>" + info
2)配置邮箱并发送
这里需要让邮箱开启STMP得到密码请自行百度
# 邮箱类
class SendEmail:
def __init__(self, show_name, send_user, email_host, email_port, password, user_list, title, message):
self.show_name = show_name
self.send_user = send_user
self.email_host = email_host
self.email_port = email_port
self.password = password
self.user_list = user_list
self.message = message
self.title = title
def send_email(self):
try:
user = self.show_name + "<" + self.send_user + ">"
message = MIMEText(self.message, _subtype='html', _charset='utf-8')
message['Subject'] = self.title
message['From'] = user
message['To'] = ";".join(self.user_list)
server = smtplib.SMTP_SSL(self.email_host)
server.connect(self.email_host, self.email_port)
server.login(self.send_user, self.password)
server.sendmail(user, self.user_list, message.as_string())
server.close()
print("success!!!")
except Exception as e:
print("error:", e)
if __name__ == '__main__':
# 发件人邮箱
send_user = ""
# 邮箱对应的host(QQ为例可其他)
email_host = "smtp.qq.com"
email_port = 465
# 开启SMTP时的密码
password = ""
# 邮件上显示的昵称
show_name = "小灿智能助手"
# 收件人邮箱账户(可多人)
user_list = ["","",""]
# 邮件标题
title = "每日信息推送"
message = htmlText
send = SendEmail(show_name, send_user, email_host, email_port, password, user_list, title, message)
send.send_email()
3)把文件上传到服务器并配置定时任务
- 确保crontab的安装详细可查看这篇博客(crontab安装及操作)
- 在linux环境下用pip安装我们的脚本所需要用到模块如:pip install requests
- 查看python的安装目录:which python
- 配置定时任务:crontab -e
- 输入:格式:分 时 日 月 星期几 python安装目录 脚本目录
如:00 8 * * * /usr/bin/python /root/download/init.py (每天早上8点运行脚本) - 查看任务列表:crontab -l
- 静候佳音
完整代码:https://github.com/Wchert/leetcode-every-day-auto-push.git
三,效果如下
启发:通过这个案例我们可以发现其实我们每天都有很多类似且重复的操作去查看一些信息,那我们可以通过代码的方式去收集信息并汇总分析然后展现,当然每个人所需的信息各有千秋,那么私人定制,个性化设计等功能岂不是也应当应运而生。😁