From 5eb218cf652e4780d067b1e9684b41880cdbe4f4 Mon Sep 17 00:00:00 2001 From: xiaoyan-5800X Date: Mon, 25 Sep 2023 00:24:10 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=AE=8C=E6=88=90=E5=BE=AE=E5=A4=B4?= =?UTF-8?q?=E6=9D=A1=E4=B8=AA=E6=95=B0=E7=BB=9F=E8=AE=A1=E5=87=BD=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/plugins/zhibo8/plugins/TouTiao.py | 72 +++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 src/plugins/zhibo8/plugins/TouTiao.py diff --git a/src/plugins/zhibo8/plugins/TouTiao.py b/src/plugins/zhibo8/plugins/TouTiao.py new file mode 100644 index 0000000..1575e7d --- /dev/null +++ b/src/plugins/zhibo8/plugins/TouTiao.py @@ -0,0 +1,72 @@ +import datetime +from datetime import timedelta +from nonebot.adapters.onebot.v11 import Bot, Event +from nonebot.typing import T_State +from nonebot.params import CommandArg, Command + +from nonebot import on_command +from nonebot.rule import to_me +from requests_html import HTMLSession, HTML +import json + +max_behot_time = '0' +# 初始化map对象,分别记录每个人的微头条个数 +weitoutiaoMap = {"太能喵": 0, "小小": 0, "大帝强": 0, "叶小欢": 0} + +""" +获取微头条数据信息 +""" +def getWeiToutiaoInfo(): + global max_behot_time + # 获取上一周的第一天和最后一天的时间戳 + lastWeekStartTime, lastWeekEndTime = getLastWeekFirstDayTimeStamp() + session = HTMLSession() + weitoutiaoGet = session.get(f"https://www.toutiao.com/api/pc/list/user/feed?category=pc_profile_ugc&token=MS4wLjABAAAA7lHc4sBPuZaQ85qdIrwVvWm8Ps5O1kPMpuh5lTJAwII&max_behot_time={max_behot_time}&aid=24&app_name=toutiao_web", verify=False, proxies=None) + # print(weitoutiaoGet.text) + # 使用Json解析返回的数据 + resultJson = json.loads(weitoutiaoGet.text) + # 先获取下个max_behot_time时间戳,如果需要翻页查询需要使用该参数 + max_behot_time = resultJson['next']['max_behot_time'] + # 开始循环解析data数据,获取微头条的内容和发布人员 + dataList = resultJson['data'] + # 获取每一条消息的发布时间戳,如果发布时间出现早于上周一0点的数据,结束统计,函数返回,否则递归调用当前函数 + for data in dataList: + # 获取本条新闻的发布时间 + publishTime = data["publish_time"] + if int(publishTime) > int(lastWeekEndTime): + continue + elif int(publishTime) < int(lastWeekStartTime): + return + else: + # 如果是比分预测类的微头条,则跳过 + content = str(data['content']) + if content.find("比分预测】")>0: + continue + else: + # 获取该微头条的发布人和发布日期 + author = None + if content.find("太能喵")>0: + weitoutiaoMap["太能喵"] = weitoutiaoMap["太能喵"]+1 + elif content.find("小小")>0: + weitoutiaoMap["小小"] = weitoutiaoMap["小小"]+1 + elif content.find("大帝强") > 0: + weitoutiaoMap["大帝强"] = weitoutiaoMap["大帝强"] + 1 + elif content.find("叶小欢") > 0: + weitoutiaoMap["叶小欢"] = weitoutiaoMap["叶小欢"] + 1 + getWeiToutiaoInfo() + +""" +获取当前日期上一周的第一天和最后一天的时间戳 +""" +def getLastWeekFirstDayTimeStamp(): + now = datetime.datetime.now() + # 上周第一天和最后一天 + last_week_start = now - timedelta(days=now.weekday() + 7, hours=now.hour, minutes=now.minute, seconds=now.second, microseconds=now.microsecond) + last_week_end = last_week_start - timedelta(days=-7) + print(f"上周第一天的日期是:{last_week_start},最后一天的日期是:{last_week_end}") + print(f"上周第一天的时间戳是:{last_week_start.timestamp()},最后一天的时间戳是:{last_week_end.timestamp()}") + return last_week_start.timestamp(), last_week_end.timestamp() + +if __name__ == '__main__': + getWeiToutiaoInfo() + print(weitoutiaoMap) \ No newline at end of file