新闻优化
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)
|
||||
|
||||
Reference in New Issue
Block a user