微博检测脚本
前言
之前做了一款基于tkinter的图形界面微博监测器,有人留言说想要监测多个博主,最近我也有这个需求,因此做了一个可以同时监测多个博主的脚本,用起来很简单,只需要修改配置文件即可,其它地方都不用改,运行命令行输入python main.py即可运行,也可以修改一下在云函数上运行。但此贴目的主要是发源码进行技术交流,所以用法我就不多讲解用法了。
这是之前那个贴子:https://www.52pojie.cn/thread-1204652-1-1.html
效果展示
配置文件
可以检测多个多个博主,只需改变配置文件config.ini即可
配置文件例子如下:
[default]
# 检测目标uid,列表格式
uid = [25*****95, 63***44982]
# 发件箱
username = t******p@163.com
# 授权码
password = TQV********UCK
# 收件箱
rsvmail = 96******@qq.com
# 对方是否有置顶微博True or False
topweibo = True
# 检测时间间隔(单位/秒)
timespan = 300
# 下方配置不用设置,程序根据上方的uid自动生成,但请不要删除[weibo]这一行
[weibo]
item_2532310695 = 1076032532310695_-_4526975157117527
item_6304244882 = 1076036304244982_-_4525153692654528
主文件
主文件是程序入口,理论上把if name == 'main':改为def main():就可以在云函数上配置了,但是实际操作时出现OS错误,咨询坛友后知道云函数不支持文件读写,所以需要用数据库来代替config,ini。这里有个包叫mod_config,这个是我自己写的包,为了方便对配置文件进行操作。
import requests
import json
import smtplib #smtp服务器
import time
import mod_config
from email.mime.text import MIMEText #邮件文本
from threading import Thread
def thread_it(uid, sender, pwd, user, choice, time_stop):
t = Thread(target=start, args=[uid, sender, pwd, user, choice, time_stop])
t.setDaemon(True) #守护!
t.start()
return t
def Get_json(uid): #监测用户的id
url = 'https://m.weibo.cn/api/container/getIndex?uid={}&containerid=107603{}'.format(uid, uid)
rsp = requests.get(url)
content = json.loads(rsp.content)
one = content['data']['cards']
return content
def Compare(choice, content, uid): #是否选中置顶, json数据
if not choice: #如果没有置顶
itemid = content['data']['cards'][1]['itemid']
config = mod_config.read_config_item()
oldid = config['item_'+str(uid)]
print("旧微博id:", oldid, '新微博id:', itemid)
if itemid == oldid:
print("微博id:", oldid, '暂未更新')
return None
else:
print("uid:"+str(uid)+"已更新", "旧微博id:", oldid, '新微博id:', itemid)
mod_config.set_config_item("item_"+str(uid), itemid)
return 1
else:
itemid = content['data']['cards'][2]['itemid']
config = mod_config.read_config_item()
oldid = config['item_'+str(uid)]
if itemid == oldid:
print("微博id:", oldid, '暂未更新')
return None
else:
print("uid:"+str(uid)+"已更新", "旧微博id:", oldid, '新微博id:', itemid)
mod_config.set_config_item("item_"+str(uid), itemid)
return 1
def Sendmail(recver, sender, password, text): #接收方,发送方,密码,邮件内容
print('正在发送邮件')
subject = ("您关注的博主微博已更新,快来看看吧!") # 邮件标题
content = text
message = MIMEText(content, "html", "utf-8")
# content 发送内容 "plain"文本格式 utf-8 编码格式
message['Subject'] = subject # 邮件标题
message['To'] = recver # 收件人
message['From'] = '博主动态小助手' # 发件人
smtp = smtplib.SMTP_SSL("smtp.163.com", 994) # 实例化smtp服务器
smtp.login(sender, password) # 发件人登录
smtp.sendmail(sender, [recver], message.as_string()) # as_string 对 message 的消息进行了封装
def start(uid, sender, pwd, user, choice, time_stop):
while True:
content = Get_json(uid)
if Compare(choice, content, uid):
Sendmail(user, sender, pwd, '您关注的博主已更新微博,快去看看吧!https://m.weibo.cn/u/{}'.format(uid))
print(time.ctime())
time.sleep(int(time_stop))
else:
print(time.ctime())
time.sleep(int(time_stop))
if __name__ == '__main__':
uid_list, sender, pwd, user, choice, time_stop = mod_config.read_config_default()
mod_config.init_config(uid_list)
t = []
for uid in uid_list:
t.append(thread_it(uid, sender, pwd, user, choice, time_stop))
for i in t:
i.join()
mod_config包
import configparser
# uid, sender, pwd, user, choice, time_stop
def read_config_default():
print("正在读取配置文件")
config = configparser.ConfigParser()
config.read("config.ini", encoding='utf-8')
uid = config['default']['uid']
uid_list = eval(uid)
sender = config['default']['username']
pwd = config['default']['password']
user = config['default']['rsvmail']
choice = config['default']['topweibo']
if choice == "True":
choice = True
elif choice == "False":
choice = False
else:
raise Exception("配置文件topweibo参数出错")
time_stop = int(config['default']['timespan'])
print("配置文件读取完毕\n", uid_list, sender, pwd, user, choice, time_stop)
return uid_list, sender, pwd, user, choice, time_stop
def read_config_item():
config = configparser.ConfigParser()
config.read("config.ini", encoding='utf-8')
return config['weibo']
def set_config_item(item_uid, item_id):
config = configparser.ConfigParser()
config.read("config.ini", encoding='utf-8')
config.set('weibo', item_uid, item_id)
with open('config.ini', 'w') as f:
config.write(f)
def init_config(uid_list):
print('初始化项目配置')
config = configparser.ConfigParser()
config.read("config.ini", encoding='utf-8')
for uid in uid_list:
try:
print('已存在:item_{}:'.format(uid)+config.get('weibo', "item_"+str(uid)))
except configparser.NoOptionError:
set_config_item("item_"+str(uid), '')
print('item_{}不存在, 已自动配置'.format(uid) + config.get('weibo', "item_" + str(uid)))
结语
- 此代码仅作技术交流,禁止商用
- 欢迎大家提出自己的意见和问题,我们一起交流学习
- 如果你觉得有用不要忘了免费评分哦