庆祝中华人民共和国成立70周年

高举旗帜 团结一致 锐意进取

bilibili up主更新邮件提醒

代码是很久之前写的(可能得有2年了)
那是一个不知道pymysql美味的年代
数据库查询都是用php api实现,现在看来略有些愚蠢。
涉及的两个api
bilibili/api.php(命名不规范,没想到后续又写了第二个api)
bilibili/get_user_api.php
前者是查询数据库中是否有记录的,没有的话插入,有的话返回1
后者是返回用户列表的,因为这个是多用户的程序,主要包含邮箱和uid
上代码:

import os, io, sys, re, time, base64, json
import webbrowser, urllib.request, urllib.parse
import smtplib
import math
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
#mailto_list= ['0@qq.com']
mail_host = "smtp.qq.com"      #设置服务器
mail_user = "0@qq.com"      #用户名
mail_pass = "******"      #口令
mail_postfix = "qq.com"   #发件箱的后缀
#发送邮件
#produced by:LiangHao
def send_mail(to_list, sub, content):
	me="up主更新提醒"+"<"+mail_user+">" 
	msg = MIMEText(content, _subtype='html', _charset='GBK')
	msg['Subject'] = sub
	msg['From'] = me
	msg['To'] = ";".join(to_list)
	try:
		server = smtplib.SMTP()
		server.connect(mail_host)
		server.login(mail_user, mail_pass)
		server.sendmail(me,to_list,msg.as_string())
		server.close()
		return True
	except Exception as e:
		print (str(e))
		return False
#检查是否更新
#produced by:LiangHao
def check_update(mid,num,mail,uid):
	try:
		url = "http://space.bilibili.com/ajax/member/getSubmitVideos?mid="+str(mid)+"&pagesize=10&page=1"
		stdout=urllib.request.urlopen(url)
		videoInfo= stdout.read().decode('utf-8')
		jsonData = json.loads(videoInfo)
		send_url = "http://127.0.0.1/bilibili/api.php?uid="+str(uid)
		postdata = urllib.parse.urlencode(jsonData["data"]["vlist"][num])
		postdata = postdata.encode('utf-8')  
		res = urllib.request.urlopen(send_url,postdata)
		msg=res.read()
		info=jsonData["data"]["vlist"][num]
		cont="<h3>您关注的up主:【"+info["author"]+"】更新啦!</h3><br />【作品标题】:"+info["title"]+"<br />【作品 i d 】:"+str(info["aid"])+"<br />【作品简介】:"+info["description"].replace('\r\n','')+"<br />【作品时长】:"+info["length"]+"<br />【发布时间】:"+info["created"]+"<br />【视频链接】:http://www.bilibili.com/video/av"+str(info["aid"])+"<br />【标题图片】:<br /><img style='max-width:300px' src='"+info["pic"]+"'>"
		if(msg.decode('utf-8')=="3"):
			print("数据错误")
			return False
		if(msg.decode('utf-8')=="2"):
			print("存在")
			return True
		if(msg.decode('utf-8')=="1"):
			print("不存在")
			if send_mail(mail,"【"+info["author"]+"】更新提醒",cont):
				print("发送成功")
				return True
			else:
				print("发送失败")
				return False
		else:
			print(msg.decode('utf-8'))
	except:
		return False
#根据用户的id获取关注列表
#produced by:LiangHao
def get_mid_list(uid,mail):
	try:
		u_url = "http://space.bilibili.com/ajax/friend/GetAttentionList?mid="+str(uid)+"&pagesize=10&page=1"
		u_stdout=urllib.request.urlopen(u_url)
		u_videoInfo= u_stdout.read().decode('utf-8')
		u_jsonData = json.loads(u_videoInfo)
		fid=[]
		for i in range(0, len(u_jsonData["data"]["list"])-1 + 1):
			fid.append(u_jsonData["data"]["list"][i]["fid"])
		for f_id in fid:
			check_update(f_id,0,mail,uid)
	except:
		print("获取失败")
		return False
#获取用户列表
#produced by:LiangHao
def get_user_list():
	url = "http://127.0.0.1/bilibili/get_user_api.php"
	stdout=urllib.request.urlopen(url)
	userInfo= stdout.read().decode('utf-8')
	#print(videoInfo)
	if(userInfo=="2"):
		print("未获取到用户")
		exit()
	jsonData = json.loads(userInfo)
	num=len(jsonData["data"])
	for i in jsonData["data"]:
		#print(i["uid"])
		umail=[i["mail"]];
		get_mid_list(i["uid"],umail)
 
if __name__ == '__main__':
	get_user_list()

if __name__ == ‘__main__’:
get_user_list()

点赞

发表评论

电子邮件地址不会被公开。 必填项已用*标注