|
|
|
|
@ -2,6 +2,7 @@
|
|
|
|
|
import requests
|
|
|
|
|
import json
|
|
|
|
|
import os
|
|
|
|
|
import time
|
|
|
|
|
from typing import Optional, Dict, Any
|
|
|
|
|
|
|
|
|
|
class NetworkService:
|
|
|
|
|
@ -11,138 +12,354 @@ class NetworkService:
|
|
|
|
|
self.api_key = None
|
|
|
|
|
self.cache = {}
|
|
|
|
|
self.session = requests.Session()
|
|
|
|
|
# 天气缓存相关属性
|
|
|
|
|
self._cached_weather_data = None # 缓存的天气数据
|
|
|
|
|
self._cached_location = None # 缓存的定位信息
|
|
|
|
|
self._weather_cache_timestamp = None # 缓存时间戳
|
|
|
|
|
|
|
|
|
|
def get_weather_info(self) -> Optional[Dict[str, Any]]:
|
|
|
|
|
def get_cached_weather_data(self):
|
|
|
|
|
"""获取缓存的天气数据"""
|
|
|
|
|
return self._cached_weather_data
|
|
|
|
|
|
|
|
|
|
def get_cached_location(self):
|
|
|
|
|
"""获取缓存的定位信息"""
|
|
|
|
|
return self._cached_location
|
|
|
|
|
|
|
|
|
|
def set_weather_cache(self, weather_data, location):
|
|
|
|
|
"""设置天气缓存"""
|
|
|
|
|
self._cached_weather_data = weather_data
|
|
|
|
|
self._cached_location = location
|
|
|
|
|
self._weather_cache_timestamp = time.time()
|
|
|
|
|
|
|
|
|
|
def clear_weather_cache(self):
|
|
|
|
|
"""清除天气缓存"""
|
|
|
|
|
self._cached_weather_data = None
|
|
|
|
|
self._cached_location = None
|
|
|
|
|
self._weather_cache_timestamp = None
|
|
|
|
|
|
|
|
|
|
def is_weather_cache_valid(self):
|
|
|
|
|
"""检查天气缓存是否有效(30分钟内)"""
|
|
|
|
|
if self._weather_cache_timestamp is None:
|
|
|
|
|
return False
|
|
|
|
|
return (time.time() - self._weather_cache_timestamp) < 1800 # 30分钟
|
|
|
|
|
|
|
|
|
|
def get_user_ip(self):
|
|
|
|
|
"""获取用户IP地址 - 使用多个备用服务"""
|
|
|
|
|
# 首先尝试获取本地IP
|
|
|
|
|
try:
|
|
|
|
|
import socket
|
|
|
|
|
hostname = socket.gethostname()
|
|
|
|
|
local_ip = socket.gethostbyname(hostname)
|
|
|
|
|
if local_ip and not local_ip.startswith("127."):
|
|
|
|
|
print(f"获取到本地IP: {local_ip}")
|
|
|
|
|
return local_ip
|
|
|
|
|
except Exception as e:
|
|
|
|
|
print(f"获取本地IP失败: {e}")
|
|
|
|
|
|
|
|
|
|
# 如果本地IP获取失败,使用备用外部服务
|
|
|
|
|
ip_services = [
|
|
|
|
|
"https://httpbin.org/ip",
|
|
|
|
|
"https://api.ipify.org?format=json",
|
|
|
|
|
"https://ipapi.co/json/"
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
for service in ip_services:
|
|
|
|
|
try:
|
|
|
|
|
print(f"尝试从 {service} 获取IP地址...")
|
|
|
|
|
response = self.session.get(service, timeout=3, verify=False)
|
|
|
|
|
if response.status_code == 200:
|
|
|
|
|
data = response.json()
|
|
|
|
|
ip = data.get("origin") or data.get("ip") or data.get("ip_address")
|
|
|
|
|
if ip:
|
|
|
|
|
print(f"成功从 {service} 获取IP: {ip}")
|
|
|
|
|
return ip
|
|
|
|
|
except Exception as e:
|
|
|
|
|
print(f"从 {service} 获取IP失败: {e}")
|
|
|
|
|
continue
|
|
|
|
|
|
|
|
|
|
print("所有IP获取服务都失败了,使用默认IP")
|
|
|
|
|
return "8.8.8.8" # 使用Google DNS作为默认IP
|
|
|
|
|
|
|
|
|
|
def get_default_weather(self):
|
|
|
|
|
"""获取默认天气数据"""
|
|
|
|
|
return {
|
|
|
|
|
"city": "北京",
|
|
|
|
|
"temperature": 20,
|
|
|
|
|
"description": "晴天",
|
|
|
|
|
"humidity": 60,
|
|
|
|
|
"wind_speed": 3.5
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
def clear_weather_cache(self):
|
|
|
|
|
"""清除天气缓存"""
|
|
|
|
|
self._cached_weather_data = None
|
|
|
|
|
self._cached_location = None
|
|
|
|
|
self._weather_cache_time = None
|
|
|
|
|
print("天气缓存已清除")
|
|
|
|
|
|
|
|
|
|
def get_weather_info(self, use_cache: bool = True) -> Optional[Dict[str, Any]]:
|
|
|
|
|
"""获取天气信息,支持缓存机制"""
|
|
|
|
|
|
|
|
|
|
# 如果启用缓存且缓存有效,直接返回缓存数据
|
|
|
|
|
if use_cache and self.is_weather_cache_valid():
|
|
|
|
|
print("使用缓存的天气数据")
|
|
|
|
|
return self._cached_weather_data
|
|
|
|
|
|
|
|
|
|
# 如果没有指定城市,使用自动定位
|
|
|
|
|
return self.get_weather_info_by_city(None, use_cache)
|
|
|
|
|
|
|
|
|
|
def get_weather_info_by_city(self, city_name: Optional[str] = None, use_cache: bool = True) -> Optional[Dict[str, Any]]:
|
|
|
|
|
"""根据城市名获取天气信息"""
|
|
|
|
|
|
|
|
|
|
# 如果启用缓存且缓存有效,直接返回缓存数据
|
|
|
|
|
if use_cache and self.is_weather_cache_valid() and not city_name:
|
|
|
|
|
print("使用缓存的天气数据")
|
|
|
|
|
return self._cached_weather_data
|
|
|
|
|
|
|
|
|
|
# 实现天气信息获取逻辑
|
|
|
|
|
# 1. 获取用户IP地址
|
|
|
|
|
try:
|
|
|
|
|
ip_response = self.session.get("https://httpbin.org/ip", timeout=5, verify=False)
|
|
|
|
|
ip_data = ip_response.json()
|
|
|
|
|
ip = ip_data.get("origin", "")
|
|
|
|
|
# 如果指定了城市名,直接使用该城市,否则通过IP定位
|
|
|
|
|
if city_name:
|
|
|
|
|
print(f"使用指定城市: {city_name}")
|
|
|
|
|
city = city_name
|
|
|
|
|
# 清除缓存以确保获取新数据
|
|
|
|
|
if hasattr(self, 'clear_weather_cache'):
|
|
|
|
|
self.clear_weather_cache()
|
|
|
|
|
else:
|
|
|
|
|
# 1. 获取用户IP地址 - 使用多个备用服务
|
|
|
|
|
print("开始获取天气信息...")
|
|
|
|
|
ip = self.get_user_ip()
|
|
|
|
|
print(f"获取到的IP地址: {ip}")
|
|
|
|
|
|
|
|
|
|
if not ip:
|
|
|
|
|
print("无法获取IP地址,使用默认天气数据")
|
|
|
|
|
return self.get_default_weather()
|
|
|
|
|
|
|
|
|
|
# 2. 根据IP获取地理位置
|
|
|
|
|
# 注意:这里使用免费的IP地理位置API,实际应用中可能需要更精确的服务
|
|
|
|
|
location_response = self.session.get(f"http://ip-api.com/json/{ip}", timeout=5, verify=False)
|
|
|
|
|
location_url = f"http://ip-api.com/json/{ip}"
|
|
|
|
|
print(f"请求地理位置: {location_url}")
|
|
|
|
|
location_response = self.session.get(location_url, timeout=5, verify=False)
|
|
|
|
|
location_data = location_response.json()
|
|
|
|
|
print(f"地理位置响应: {location_data}")
|
|
|
|
|
|
|
|
|
|
if location_data.get("status") != "success":
|
|
|
|
|
return None
|
|
|
|
|
print("地理位置获取失败,使用默认天气数据")
|
|
|
|
|
return self.get_default_weather()
|
|
|
|
|
|
|
|
|
|
city = location_data.get("city", "Unknown")
|
|
|
|
|
print(f"获取到的城市: {city}")
|
|
|
|
|
|
|
|
|
|
if not city:
|
|
|
|
|
print("无法获取城市名称,使用默认天气数据")
|
|
|
|
|
return self.get_default_weather()
|
|
|
|
|
|
|
|
|
|
# 保存定位信息到缓存
|
|
|
|
|
self._cached_location = {
|
|
|
|
|
"ip": ip,
|
|
|
|
|
"city": city,
|
|
|
|
|
"country": location_data.get("country", "Unknown"),
|
|
|
|
|
"region": location_data.get("regionName", "Unknown")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# 3. 调用天气API获取天气数据
|
|
|
|
|
# 注意:这里使用OpenWeatherMap API作为示例,需要API密钥
|
|
|
|
|
# 在实际应用中,需要设置有效的API密钥
|
|
|
|
|
if self.api_key:
|
|
|
|
|
weather_url = f"http://api.openweathermap.org/data/2.5/weather?q={city}&appid={self.api_key}&units=metric&lang=zh_cn"
|
|
|
|
|
weather_response = self.session.get(weather_url, timeout=5, verify=False)
|
|
|
|
|
weather_data = weather_response.json()
|
|
|
|
|
|
|
|
|
|
# 4. 解析并格式化数据
|
|
|
|
|
if weather_response.status_code == 200:
|
|
|
|
|
formatted_weather = {
|
|
|
|
|
"city": city,
|
|
|
|
|
"temperature": weather_data["main"]["temp"],
|
|
|
|
|
"description": weather_data["weather"][0]["description"],
|
|
|
|
|
"humidity": weather_data["main"]["humidity"],
|
|
|
|
|
"wind_speed": weather_data["wind"]["speed"]
|
|
|
|
|
}
|
|
|
|
|
# 5. 返回天气信息字典
|
|
|
|
|
return formatted_weather
|
|
|
|
|
else:
|
|
|
|
|
# 当没有API密钥时,使用免费的天气API获取真实数据
|
|
|
|
|
# 首先尝试获取城市ID(需要映射城市名到ID)
|
|
|
|
|
city_id_map = {
|
|
|
|
|
"Beijing": "101010100",
|
|
|
|
|
"Shanghai": "101020100",
|
|
|
|
|
"Tianjin": "101030100",
|
|
|
|
|
"Chongqing": "101040100",
|
|
|
|
|
"Hong Kong": "101320101",
|
|
|
|
|
"Macau": "101330101",
|
|
|
|
|
# 添加中文城市名映射
|
|
|
|
|
"北京": "101010100",
|
|
|
|
|
"上海": "101020100",
|
|
|
|
|
"天津": "101030100",
|
|
|
|
|
"重庆": "101040100",
|
|
|
|
|
"香港": "101320101",
|
|
|
|
|
"澳门": "101330101",
|
|
|
|
|
# 添加更多主要城市
|
|
|
|
|
"广州": "101280101",
|
|
|
|
|
"深圳": "101280601",
|
|
|
|
|
"杭州": "101210101",
|
|
|
|
|
"南京": "101190101",
|
|
|
|
|
"成都": "101270101",
|
|
|
|
|
"武汉": "101200101",
|
|
|
|
|
"西安": "101110101",
|
|
|
|
|
"沈阳": "101070101",
|
|
|
|
|
"青岛": "101120201",
|
|
|
|
|
"大连": "101070201",
|
|
|
|
|
"苏州": "101190401",
|
|
|
|
|
"无锡": "101190201"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# 尝试映射英文城市名到ID
|
|
|
|
|
city_id = city_id_map.get(city)
|
|
|
|
|
|
|
|
|
|
# 3. 调用天气API获取天气数据
|
|
|
|
|
# 注意:这里使用OpenWeatherMap API作为示例,需要API密钥
|
|
|
|
|
# 在实际应用中,需要设置有效的API密钥
|
|
|
|
|
if self.api_key:
|
|
|
|
|
weather_url = f"http://api.openweathermap.org/data/2.5/weather?q={city}&appid={self.api_key}&units=metric&lang=zh_cn"
|
|
|
|
|
# 如果找到城市ID,使用该ID获取天气,否则使用默认北京ID
|
|
|
|
|
if city_id:
|
|
|
|
|
weather_city_id = city_id
|
|
|
|
|
print(f"使用城市ID获取天气: {city} -> {weather_city_id}")
|
|
|
|
|
else:
|
|
|
|
|
# 对于中国主要城市,直接使用拼音映射
|
|
|
|
|
city_pinyin_map = {
|
|
|
|
|
"Beijing": "北京",
|
|
|
|
|
"Shanghai": "上海",
|
|
|
|
|
"Tianjin": "天津",
|
|
|
|
|
"Chongqing": "重庆",
|
|
|
|
|
"Guangzhou": "广州",
|
|
|
|
|
"Shenzhen": "深圳",
|
|
|
|
|
"Hangzhou": "杭州",
|
|
|
|
|
"Nanjing": "南京",
|
|
|
|
|
"Chengdu": "成都",
|
|
|
|
|
"Wuhan": "武汉",
|
|
|
|
|
"Xi\'an": "西安",
|
|
|
|
|
"Shenyang": "沈阳"
|
|
|
|
|
}
|
|
|
|
|
chinese_city = city_pinyin_map.get(city, city)
|
|
|
|
|
weather_city_id = "101010100" # 默认北京ID
|
|
|
|
|
print(f"使用默认城市ID获取天气: {city} -> {weather_city_id}")
|
|
|
|
|
|
|
|
|
|
# 使用免费天气API获取天气数据
|
|
|
|
|
try:
|
|
|
|
|
# 使用和风天气免费API的替代方案 - sojson天气API
|
|
|
|
|
weather_url = f"http://t.weather.sojson.com/api/weather/city/{weather_city_id}"
|
|
|
|
|
print(f"请求天气数据: {weather_url}")
|
|
|
|
|
weather_response = self.session.get(weather_url, timeout=5, verify=False)
|
|
|
|
|
print(f"天气响应状态码: {weather_response.status_code}")
|
|
|
|
|
weather_data = weather_response.json()
|
|
|
|
|
print(f"天气数据响应: {weather_data}")
|
|
|
|
|
|
|
|
|
|
# 4. 解析并格式化数据
|
|
|
|
|
if weather_response.status_code == 200:
|
|
|
|
|
if weather_data.get("status") == 200:
|
|
|
|
|
# 解析天气数据
|
|
|
|
|
current_data = weather_data.get("data", {})
|
|
|
|
|
wendu = current_data.get("wendu", "N/A")
|
|
|
|
|
shidu = current_data.get("shidu", "N/A")
|
|
|
|
|
forecast = current_data.get("forecast", [])
|
|
|
|
|
|
|
|
|
|
# 获取第一个预报项作为当前天气
|
|
|
|
|
current_weather = forecast[0] if forecast else {}
|
|
|
|
|
weather_type = current_weather.get("type", "晴")
|
|
|
|
|
|
|
|
|
|
# 获取生活指数信息
|
|
|
|
|
lifetips = []
|
|
|
|
|
if current_weather:
|
|
|
|
|
# 从预报数据中提取生活提示
|
|
|
|
|
ganmao = current_weather.get("ganmao", "")
|
|
|
|
|
if ganmao:
|
|
|
|
|
lifetips.append(f"感冒指数: {ganmao}")
|
|
|
|
|
|
|
|
|
|
# 添加其他生活指数(基于天气类型推断)
|
|
|
|
|
if "雨" in weather_type:
|
|
|
|
|
lifetips.append("出行建议: 记得带伞")
|
|
|
|
|
elif "晴" in weather_type:
|
|
|
|
|
lifetips.append("出行建议: 适合户外活动")
|
|
|
|
|
elif "雪" in weather_type:
|
|
|
|
|
lifetips.append("出行建议: 注意防滑保暖")
|
|
|
|
|
elif "雾" in weather_type or "霾" in weather_type:
|
|
|
|
|
lifetips.append("健康提醒: 减少户外运动")
|
|
|
|
|
|
|
|
|
|
# 温度相关建议
|
|
|
|
|
temp = float(wendu) if wendu != "N/A" else 20
|
|
|
|
|
if temp > 30:
|
|
|
|
|
lifetips.append("穿衣建议: 注意防暑降温")
|
|
|
|
|
elif temp < 5:
|
|
|
|
|
lifetips.append("穿衣建议: 注意保暖防寒")
|
|
|
|
|
elif temp < 15:
|
|
|
|
|
lifetips.append("穿衣建议: 适当添加衣物")
|
|
|
|
|
else:
|
|
|
|
|
lifetips.append("穿衣建议: 天气舒适")
|
|
|
|
|
|
|
|
|
|
formatted_weather = {
|
|
|
|
|
"city": city,
|
|
|
|
|
"temperature": weather_data["main"]["temp"],
|
|
|
|
|
"description": weather_data["weather"][0]["description"],
|
|
|
|
|
"humidity": weather_data["main"]["humidity"],
|
|
|
|
|
"wind_speed": weather_data["wind"]["speed"]
|
|
|
|
|
"temperature": float(wendu) if wendu != "N/A" else 20,
|
|
|
|
|
"description": weather_type,
|
|
|
|
|
"humidity": shidu.replace("%", "") if shidu != "N/A" else "60",
|
|
|
|
|
"wind_speed": "3.5", # 默认风速
|
|
|
|
|
"lifetips": lifetips # 生活提示列表
|
|
|
|
|
}
|
|
|
|
|
# 5. 返回天气信息字典
|
|
|
|
|
print(f"成功获取天气数据: {formatted_weather}")
|
|
|
|
|
|
|
|
|
|
# 缓存天气数据
|
|
|
|
|
self.set_weather_cache(formatted_weather, self._cached_location)
|
|
|
|
|
return formatted_weather
|
|
|
|
|
else:
|
|
|
|
|
# 当没有API密钥时,使用免费的天气API获取真实数据
|
|
|
|
|
# 首先尝试获取城市ID(需要映射城市名到ID)
|
|
|
|
|
city_id_map = {
|
|
|
|
|
"Beijing": "101010100",
|
|
|
|
|
"Shanghai": "101020100",
|
|
|
|
|
"Tianjin": "101030100",
|
|
|
|
|
"Chongqing": "101040100",
|
|
|
|
|
"Hong Kong": "101320101",
|
|
|
|
|
"Macau": "101330101"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# 尝试映射英文城市名到ID
|
|
|
|
|
city_id = city_id_map.get(city)
|
|
|
|
|
|
|
|
|
|
# 如果找不到映射,尝试直接使用城市名
|
|
|
|
|
if not city_id:
|
|
|
|
|
# 对于中国主要城市,直接使用拼音映射
|
|
|
|
|
city_pinyin_map = {
|
|
|
|
|
"Beijing": "北京",
|
|
|
|
|
"Shanghai": "上海",
|
|
|
|
|
"Tianjin": "天津",
|
|
|
|
|
"Chongqing": "重庆"
|
|
|
|
|
}
|
|
|
|
|
chinese_city = city_pinyin_map.get(city, city)
|
|
|
|
|
else:
|
|
|
|
|
print(f"天气API返回错误状态: {weather_data.get('status')}")
|
|
|
|
|
|
|
|
|
|
# 使用免费天气API
|
|
|
|
|
try:
|
|
|
|
|
# 使用和风天气免费API的替代方案 - sojson天气API
|
|
|
|
|
weather_url = f"http://t.weather.sojson.com/api/weather/city/101010100" # 默认北京
|
|
|
|
|
weather_response = self.session.get(weather_url, timeout=5, verify=False)
|
|
|
|
|
weather_data = weather_response.json()
|
|
|
|
|
|
|
|
|
|
if weather_data.get("status") == 200:
|
|
|
|
|
# 解析天气数据
|
|
|
|
|
current_data = weather_data.get("data", {})
|
|
|
|
|
wendu = current_data.get("wendu", "N/A")
|
|
|
|
|
shidu = current_data.get("shidu", "N/A")
|
|
|
|
|
forecast = current_data.get("forecast", [])
|
|
|
|
|
|
|
|
|
|
# 获取第一个预报项作为当前天气
|
|
|
|
|
current_weather = forecast[0] if forecast else {}
|
|
|
|
|
weather_type = current_weather.get("type", "晴")
|
|
|
|
|
|
|
|
|
|
formatted_weather = {
|
|
|
|
|
"city": city,
|
|
|
|
|
"temperature": float(wendu) if wendu != "N/A" else 20,
|
|
|
|
|
"description": weather_type,
|
|
|
|
|
"humidity": shidu.replace("%", "") if shidu != "N/A" else "60",
|
|
|
|
|
"wind_speed": "3.5" # 默认风速
|
|
|
|
|
}
|
|
|
|
|
return formatted_weather
|
|
|
|
|
except Exception as e:
|
|
|
|
|
print(f"获取免费天气数据时出错: {e}")
|
|
|
|
|
|
|
|
|
|
# 如果以上都失败,返回默认数据
|
|
|
|
|
return {
|
|
|
|
|
"city": city,
|
|
|
|
|
"temperature": 20,
|
|
|
|
|
"description": "晴天",
|
|
|
|
|
"humidity": 60,
|
|
|
|
|
"wind_speed": 3.5
|
|
|
|
|
}
|
|
|
|
|
except Exception as e:
|
|
|
|
|
print(f"获取天气信息时出错: {e}")
|
|
|
|
|
return None
|
|
|
|
|
except Exception as e:
|
|
|
|
|
print(f"获取免费天气数据时出错: {e}")
|
|
|
|
|
|
|
|
|
|
# 如果以上都失败,返回默认数据
|
|
|
|
|
default_weather = {
|
|
|
|
|
"city": city,
|
|
|
|
|
"temperature": 20,
|
|
|
|
|
"description": "晴天",
|
|
|
|
|
"humidity": 60,
|
|
|
|
|
"wind_speed": 3.5,
|
|
|
|
|
"lifetips": [
|
|
|
|
|
"穿衣建议: 天气舒适",
|
|
|
|
|
"出行建议: 适合户外活动",
|
|
|
|
|
"健康提醒: 保持良好心情"
|
|
|
|
|
]
|
|
|
|
|
}
|
|
|
|
|
print(f"使用默认天气数据 for {city}")
|
|
|
|
|
|
|
|
|
|
# 缓存默认天气数据
|
|
|
|
|
self.set_weather_cache(default_weather, self._cached_location)
|
|
|
|
|
return default_weather
|
|
|
|
|
|
|
|
|
|
def get_daily_quote(self) -> Optional[str]:
|
|
|
|
|
|
|
|
|
|
# 实现每日一句获取逻辑
|
|
|
|
|
# 1. 调用名言API
|
|
|
|
|
# 实现每日一句获取逻辑 - 使用古诗词API
|
|
|
|
|
try:
|
|
|
|
|
# 使用一个免费的名言API,禁用SSL验证以避免证书问题
|
|
|
|
|
response = self.session.get("https://api.quotable.io/random", timeout=5, verify=False)
|
|
|
|
|
# 使用古诗词·一言API - 每次返回随机不同的诗词
|
|
|
|
|
response = self.session.get("https://v1.jinrishici.com/all.json", timeout=5, verify=False)
|
|
|
|
|
|
|
|
|
|
# 2. 解析返回的名言数据
|
|
|
|
|
# 2. 解析返回的古诗词数据
|
|
|
|
|
if response.status_code == 200:
|
|
|
|
|
quote_data = response.json()
|
|
|
|
|
content = quote_data.get("content", "")
|
|
|
|
|
author = quote_data.get("author", "")
|
|
|
|
|
poetry_data = response.json()
|
|
|
|
|
content = poetry_data.get('content', '')
|
|
|
|
|
author = poetry_data.get('author', '')
|
|
|
|
|
title = poetry_data.get('origin', '')
|
|
|
|
|
|
|
|
|
|
# 3. 格式化名言文本
|
|
|
|
|
formatted_quote = f'"{content}" - {author}'
|
|
|
|
|
# 3. 格式化古诗词文本
|
|
|
|
|
if content and author and title:
|
|
|
|
|
formatted_poetry = f"{content} — {author}《{title}》"
|
|
|
|
|
elif content and author:
|
|
|
|
|
formatted_poetry = f"{content} — {author}"
|
|
|
|
|
elif content:
|
|
|
|
|
formatted_poetry = content
|
|
|
|
|
else:
|
|
|
|
|
formatted_poetry = "暂无古诗词"
|
|
|
|
|
|
|
|
|
|
# 4. 返回名言字符串
|
|
|
|
|
return formatted_quote
|
|
|
|
|
# 4. 返回古诗词字符串
|
|
|
|
|
return formatted_poetry
|
|
|
|
|
else:
|
|
|
|
|
# 如果API调用失败,返回默认名言
|
|
|
|
|
return "书山有路勤为径,学海无涯苦作舟。"
|
|
|
|
|
# 如果API调用失败,返回默认古诗词
|
|
|
|
|
return "山重水复疑无路,柳暗花明又一村。"
|
|
|
|
|
except Exception as e:
|
|
|
|
|
print(f"获取每日一句时出错: {e}")
|
|
|
|
|
# 出错时返回默认名言
|
|
|
|
|
return "书山有路勤为径,学海无涯苦作舟。"
|
|
|
|
|
print(f"获取古诗词时出错: {e}")
|
|
|
|
|
# 出错时返回默认古诗词
|
|
|
|
|
return "山重水复疑无路,柳暗花明又一村。"
|
|
|
|
|
|
|
|
|
|
def download_image(self, url: str) -> Optional[bytes]:
|
|
|
|
|
|
|
|
|
|
|