新闻优化
This commit is contained in:
@@ -3,6 +3,8 @@
|
||||
|
||||
import json
|
||||
import re
|
||||
from typing import Optional
|
||||
|
||||
from loguru import logger
|
||||
import time
|
||||
from datetime import datetime
|
||||
@@ -65,7 +67,7 @@ class News(object):
|
||||
try:
|
||||
response = requests.get(url, headers=self.headers, timeout=10)
|
||||
response.raise_for_status() # 检查请求是否成功
|
||||
|
||||
print(response.json())
|
||||
if response.status_code == 200:
|
||||
post = response.json()
|
||||
# 提取content列表 - 避免使用str作为变量名
|
||||
@@ -104,10 +106,32 @@ class News(object):
|
||||
elif website == 'bbc':
|
||||
return func_english_news.bbc()
|
||||
|
||||
def get_news_60s(self) -> Optional[str]:
|
||||
"""
|
||||
调用 60s 接口并提取 image 字段
|
||||
:return: image url 或 None
|
||||
"""
|
||||
|
||||
API_URL = "http://192.168.2.32:4399/v2/60s"
|
||||
TIMEOUT = 5 # 秒
|
||||
try:
|
||||
resp = requests.get(API_URL, timeout=TIMEOUT)
|
||||
resp.raise_for_status() # HTTP 非 200 会抛异常
|
||||
|
||||
data = resp.json()
|
||||
return data.get("data", {}).get("image")
|
||||
|
||||
except requests.RequestException as e:
|
||||
print(f"请求接口失败: {e}")
|
||||
except ValueError as e:
|
||||
print(f"JSON 解析失败: {e}")
|
||||
|
||||
return None
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
news = News()
|
||||
print(news.get_news_60s())
|
||||
# # msg = "@水牛-分身 今日百度新闻"
|
||||
# # q = re.sub(r"@.*?[\u2005|\s]", "", msg).replace(" ", "")
|
||||
# # print(q)
|
||||
|
||||
27
robot.py
27
robot.py
@@ -1,10 +1,12 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
import asyncio
|
||||
import base64
|
||||
import threading
|
||||
import time
|
||||
import tomllib
|
||||
from collections import deque
|
||||
|
||||
import requests
|
||||
import toml
|
||||
from loguru import logger
|
||||
|
||||
@@ -495,6 +497,24 @@ class Robot:
|
||||
except Exception as e:
|
||||
self.LOG.error(f"send_group_txt_message:{feature.description} error:{e}")
|
||||
|
||||
async def send_group_image_message(self, msg: str, feature: Feature):
|
||||
"""向所有启用了特定功能的群发送文本消息"""
|
||||
try:
|
||||
receivers = self.gbm.get_group_list()
|
||||
if not receivers:
|
||||
return
|
||||
|
||||
resp = requests.get(msg, timeout=10)
|
||||
resp.raise_for_status()
|
||||
|
||||
base64_str = base64.b64encode(resp.content).decode("utf-8")
|
||||
|
||||
for r in receivers:
|
||||
if self.gbm.get_group_permission(r, feature) == PermissionStatus.ENABLED:
|
||||
await self.ipad_bot.send_image_message(r, base64_str)
|
||||
except Exception as e:
|
||||
self.LOG.error(f"send_group_image_message:{feature.description} error:{e}")
|
||||
|
||||
async def send_group_file_message(self, path: str, feature: Feature):
|
||||
try:
|
||||
receivers = self.gbm.get_group_list()
|
||||
@@ -669,8 +689,11 @@ class Robot:
|
||||
|
||||
async def news_baidu_report_auto(self) -> None:
|
||||
try:
|
||||
news = News().get_baidu_news()
|
||||
await self.send_group_txt_message(news, Feature.DAILY_NEWS)
|
||||
# news = News().get_baidu_news()
|
||||
# await self.send_group_txt_message(news, Feature.DAILY_NEWS)
|
||||
path = News().get_news_60s()
|
||||
|
||||
await self.send_group_image_message(path, Feature.DAILY_NEWS)
|
||||
except Exception as e:
|
||||
self.LOG.error(f"newsBaiduReportAuto error:{e}")
|
||||
|
||||
|
||||
Reference in New Issue
Block a user