Leetcode题目更新提醒小脚本~
Posted wuezs
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Leetcode题目更新提醒小脚本~相关的知识,希望对你有一定的参考价值。
Leetcode缺少一个 出现新题目就发邮件提醒用户的功能,所以写了一个脚本。
脚本有两个函数:检查leet网站是否有新题目,并且题目是能做的、没加锁的;发邮件提醒用户。
网站解析用的是beautifulSoup 所以需要预先安装bs4模块
脚本写完需要上传到云主机上,这里简单起见 我用的是实验室的服务器,上传并运行该脚本。
这里有个小问题要解决,ssh运行python断开终端后会杀死该进程,要用nohup python xxx.py & 命令保证该进程不被杀死 一直保持运行。
#!/usr/bin/env
#coding:utf-8
import urllib,re,time
import smtplib
from email.mime.text import MIMEText
from email.header import Header
from bs4 import BeautifulSoup
def checkUpdate(latest):
url = "https://leetcode.com/problemset/algorithms/" #网页地址
wp = urllib.urlopen(url) #打开连接
soup = BeautifulSoup(wp.read())
count=0
lock,update = False,False
for link in soup.find_all('tr'):
count=count+1
#print(link)
if count==3:
for tds in link.find_all('td'):
if(tds.string==str(latest)):
update = True
if(tds.find('i')!=None):
lock = True
break
if update and not lock:
print "We will send an email to inform users that a new question has arised."
return 0
elif update and lock:
return 1
else:
return 2
def sendMails(recvlist):
sender = '13760274091@163.com'
subject = 'leetcode又有新题目可以做了'
smtpserver = 'smtp.163.com'
username = '13760274091@163.com'
password = 'xxxxx'
msg = MIMEText('<html><h2>leetcode有新题目啦~~ <a href="https://leetcode.com/problemset/algorithms/">题目链接</a></h2></html>','html','utf-8')#中文需参数‘utf-8',单字节字符不需要
msg['Subject'] = Header(subject, 'utf-8')
msg['From'] = '13760274091@163.com'
#msg['To'] = "zhushuai2015@qq.com"
smtp = smtplib.SMTP()
smtp.connect('smtp.163.com')
smtp.login(username, password)
for receiver in recvlist:
msg['To']=receiver
smtp.sendmail(sender, receiver, msg.as_string())
smtp.quit()
if __name__ == '__main__':
latest = 377
recvs =['zhushuai2015@qq.com','xxxxx@qq.com'];
while True:
state = checkUpdate(latest)
if(state==0):#有新题且无锁
latest = latest+1
sendMails(recvs)
elif(state==1):#有新题但有锁
latest=latest+1
time.sleep(60)#1m检查一次
以上是关于Leetcode题目更新提醒小脚本~的主要内容,如果未能解决你的问题,请参考以下文章