|
|
|
|
@ -228,22 +228,39 @@ class WordRibbon(QFrame):
|
|
|
|
|
quote_group = self.create_ribbon_group("每日一言")
|
|
|
|
|
quote_layout = QVBoxLayout()
|
|
|
|
|
|
|
|
|
|
# 每日一言显示标签
|
|
|
|
|
self.quote_label = QLabel("暂无")
|
|
|
|
|
self.quote_label.setStyleSheet("QLabel { color: #666666; font-style: italic; font-size: 10px; }")
|
|
|
|
|
self.quote_label.setWordWrap(True)
|
|
|
|
|
self.quote_label.setFixedWidth(150)
|
|
|
|
|
# 创建第一行:类型选择下拉框和刷新按钮
|
|
|
|
|
top_row_layout = QHBoxLayout()
|
|
|
|
|
|
|
|
|
|
# 类型选择下拉框
|
|
|
|
|
self.quote_type_combo = QComboBox()
|
|
|
|
|
self.quote_type_combo.addItems(["普通箴言", "古诗词句"])
|
|
|
|
|
self.quote_type_combo.setFixedSize(120, 25)
|
|
|
|
|
self.quote_type_combo.currentTextChanged.connect(self.on_quote_type_changed)
|
|
|
|
|
|
|
|
|
|
# 刷新按钮
|
|
|
|
|
self.refresh_quote_btn = QPushButton("刷新箴言")
|
|
|
|
|
self.refresh_quote_btn.clicked.connect(self.on_refresh_quote)
|
|
|
|
|
self.refresh_quote_btn.setFixedSize(80, 25)
|
|
|
|
|
|
|
|
|
|
# 添加到第一行布局
|
|
|
|
|
top_row_layout.addWidget(self.quote_type_combo)
|
|
|
|
|
top_row_layout.addWidget(self.refresh_quote_btn)
|
|
|
|
|
top_row_layout.addStretch() # 添加弹性空间,使控件靠左对齐
|
|
|
|
|
|
|
|
|
|
# 每日一言显示标签 - 增大尺寸
|
|
|
|
|
self.quote_label = QLabel("暂无")
|
|
|
|
|
self.quote_label.setStyleSheet("QLabel { color: #666666; font-style: italic; font-size: 10px; }")
|
|
|
|
|
self.quote_label.setWordWrap(True)
|
|
|
|
|
self.quote_label.setFixedWidth(250) # 增加到250像素宽度
|
|
|
|
|
self.quote_label.setMinimumHeight(40) # 设置最小高度,增加显示空间
|
|
|
|
|
|
|
|
|
|
# 添加到主布局
|
|
|
|
|
quote_layout.addLayout(top_row_layout)
|
|
|
|
|
quote_layout.addWidget(self.quote_label)
|
|
|
|
|
quote_layout.addWidget(self.refresh_quote_btn)
|
|
|
|
|
quote_group.setLayout(quote_layout)
|
|
|
|
|
|
|
|
|
|
self.quote_group = quote_group
|
|
|
|
|
self.current_quote_type = "普通箴言" # 默认类型
|
|
|
|
|
|
|
|
|
|
# 组件创建完成后自动获取每日一言
|
|
|
|
|
self.load_daily_quote()
|
|
|
|
|
@ -303,44 +320,63 @@ class WordRibbon(QFrame):
|
|
|
|
|
def load_daily_quote(self):
|
|
|
|
|
"""加载每日一言"""
|
|
|
|
|
try:
|
|
|
|
|
# 创建每日一言API实例
|
|
|
|
|
quote_api = daily_sentence_API("https://api.nxvav.cn/api/yiyan")
|
|
|
|
|
|
|
|
|
|
# 获取每日一言数据
|
|
|
|
|
quote_data = quote_api.get_sentence('json')
|
|
|
|
|
|
|
|
|
|
if quote_data and isinstance(quote_data, dict):
|
|
|
|
|
# 从返回的数据中提取每日一言文本
|
|
|
|
|
quote_text = quote_data.get('yiyan', '暂无每日一言')
|
|
|
|
|
self.update_quote_display(quote_text)
|
|
|
|
|
# 根据当前选择的类型获取不同的内容
|
|
|
|
|
if self.current_quote_type == "古诗词句":
|
|
|
|
|
# 获取古诗词
|
|
|
|
|
quote_text = self.get_chinese_poetry()
|
|
|
|
|
else:
|
|
|
|
|
# 如果API返回空或格式不正确,显示默认文本
|
|
|
|
|
self.update_quote_display("暂无每日一言")
|
|
|
|
|
# 获取普通箴言
|
|
|
|
|
quote_api = daily_sentence_API("https://api.nxvav.cn/api/yiyan")
|
|
|
|
|
quote_data = quote_api.get_sentence('json')
|
|
|
|
|
|
|
|
|
|
if quote_data and isinstance(quote_data, dict):
|
|
|
|
|
quote_text = quote_data.get('yiyan', '暂无每日一言')
|
|
|
|
|
else:
|
|
|
|
|
quote_text = "暂无每日一言"
|
|
|
|
|
|
|
|
|
|
self.update_quote_display(quote_text)
|
|
|
|
|
|
|
|
|
|
except Exception as e:
|
|
|
|
|
print(f"加载每日一言失败: {e}")
|
|
|
|
|
self.update_quote_display("暂无每日一言")
|
|
|
|
|
self.update_quote_display("获取失败")
|
|
|
|
|
|
|
|
|
|
def on_refresh_quote(self):
|
|
|
|
|
"""刷新每日一言按钮点击处理"""
|
|
|
|
|
self.load_daily_quote()
|
|
|
|
|
|
|
|
|
|
def on_quote_type_changed(self, quote_type):
|
|
|
|
|
"""每日一言类型切换处理"""
|
|
|
|
|
self.current_quote_type = quote_type
|
|
|
|
|
# 类型切换时自动刷新内容
|
|
|
|
|
self.load_daily_quote()
|
|
|
|
|
|
|
|
|
|
def get_chinese_poetry(self):
|
|
|
|
|
"""获取古诗词 - 使用古诗词·一言API(随机返回不同诗词)"""
|
|
|
|
|
try:
|
|
|
|
|
# 创建每日一言API实例
|
|
|
|
|
quote_api = daily_sentence_API("https://api.nxvav.cn/api/yiyan")
|
|
|
|
|
|
|
|
|
|
# 获取每日一言数据
|
|
|
|
|
quote_data = quote_api.get_sentence('json')
|
|
|
|
|
# 使用古诗词·一言API - 每次返回随机不同的诗词
|
|
|
|
|
response = requests.get("https://v1.jinrishici.com/all.json", timeout=5)
|
|
|
|
|
|
|
|
|
|
if quote_data and isinstance(quote_data, dict):
|
|
|
|
|
# 从返回的数据中提取每日一言文本
|
|
|
|
|
quote_text = quote_data.get('yiyan', '暂无每日一言')
|
|
|
|
|
self.update_quote_display(quote_text)
|
|
|
|
|
if response.status_code == 200:
|
|
|
|
|
data = response.json()
|
|
|
|
|
content = data.get('content', '')
|
|
|
|
|
author = data.get('author', '')
|
|
|
|
|
title = data.get('origin', '')
|
|
|
|
|
|
|
|
|
|
# 格式化显示文本
|
|
|
|
|
if content and author and title:
|
|
|
|
|
return f"{content} — {author}《{title}》"
|
|
|
|
|
elif content and author:
|
|
|
|
|
return f"{content} — {author}"
|
|
|
|
|
elif content:
|
|
|
|
|
return content
|
|
|
|
|
else:
|
|
|
|
|
return "暂无古诗词"
|
|
|
|
|
else:
|
|
|
|
|
# 如果API返回空或格式不正确,显示默认文本
|
|
|
|
|
self.update_quote_display("获取每日一言失败")
|
|
|
|
|
return "获取古诗词失败"
|
|
|
|
|
|
|
|
|
|
except Exception as e:
|
|
|
|
|
print(f"获取每日一言失败: {e}")
|
|
|
|
|
self.update_quote_display("获取每日一言失败")
|
|
|
|
|
print(f"获取古诗词失败: {e}")
|
|
|
|
|
return "获取古诗词失败"
|
|
|
|
|
|
|
|
|
|
def update_quote_display(self, quote_text):
|
|
|
|
|
"""更新每日一言显示"""
|
|
|
|
|
@ -643,6 +679,28 @@ class WeatherAPI:
|
|
|
|
|
city_info = data['cityInfo']
|
|
|
|
|
current_data = data['data']
|
|
|
|
|
|
|
|
|
|
# 获取生活提示信息
|
|
|
|
|
life_tips = []
|
|
|
|
|
forecast = current_data.get('forecast', [])
|
|
|
|
|
if forecast:
|
|
|
|
|
# 从预报中提取提示信息
|
|
|
|
|
for day in forecast[:3]: # 取前3天的提示
|
|
|
|
|
notice = day.get('notice', '')
|
|
|
|
|
if notice:
|
|
|
|
|
life_tips.append(notice)
|
|
|
|
|
|
|
|
|
|
# 如果没有获取到足够的提示,添加一些默认的
|
|
|
|
|
default_tips = [
|
|
|
|
|
"愿你拥有比阳光明媚的心情",
|
|
|
|
|
"雾霾来袭,戴好口罩再出门",
|
|
|
|
|
"今天天气不错,适合出去走走",
|
|
|
|
|
"记得多喝水,保持身体健康"
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
# 补充到至少3个提示
|
|
|
|
|
while len(life_tips) < 3 and default_tips:
|
|
|
|
|
life_tips.append(default_tips.pop(0))
|
|
|
|
|
|
|
|
|
|
weather_info = {
|
|
|
|
|
'temp': current_data['wendu'],
|
|
|
|
|
'feels_like': current_data['wendu'], # 没有体感温度,用实际温度代替
|
|
|
|
|
@ -651,7 +709,8 @@ class WeatherAPI:
|
|
|
|
|
'wind_dir': current_data['forecast'][0]['fx'],
|
|
|
|
|
'wind_scale': '1', # 没有风力等级,用默认值
|
|
|
|
|
'vis': current_data['forecast'][0]['high'], # 用最高温作为可见度
|
|
|
|
|
'pressure': '1013' # 没有气压,用默认值
|
|
|
|
|
'pressure': '1013', # 没有气压,用默认值
|
|
|
|
|
'life_tips': life_tips # 添加生活提示信息
|
|
|
|
|
}
|
|
|
|
|
print(f"解析后的天气信息: {weather_info}")
|
|
|
|
|
return weather_info
|
|
|
|
|
@ -699,7 +758,8 @@ class WeatherAPI:
|
|
|
|
|
weather_data = {
|
|
|
|
|
'city': city_name,
|
|
|
|
|
'current': current,
|
|
|
|
|
'forecast': forecast
|
|
|
|
|
'forecast': forecast,
|
|
|
|
|
'life_tips': current.get('life_tips', []) # 添加生活提示信息
|
|
|
|
|
}
|
|
|
|
|
return weather_data
|
|
|
|
|
else:
|
|
|
|
|
@ -1153,7 +1213,8 @@ class WeatherAPI:
|
|
|
|
|
'humidity': weather_info['humidity'],
|
|
|
|
|
'wind_scale': weather_info['wind_scale']
|
|
|
|
|
},
|
|
|
|
|
'forecast': forecast
|
|
|
|
|
'forecast': forecast,
|
|
|
|
|
'life_tips': weather_info.get('life_tips', []) # 添加生活提示信息
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
print(f"无法获取城市ID: {original_city_name}")
|
|
|
|
|
|