feat:重构代码模块
This commit is contained in:
28
work/daily_stock.py
Normal file
28
work/daily_stock.py
Normal file
@@ -0,0 +1,28 @@
|
||||
import requests
|
||||
|
||||
stock_url = 'http://push2.eastmoney.com/api/qt/stock/get'
|
||||
estun_id = '0.002747'
|
||||
|
||||
|
||||
def get_today_stock(company_id=estun_id):
|
||||
response = requests.get(
|
||||
stock_url,
|
||||
params={'secid': company_id}
|
||||
)
|
||||
|
||||
return response.json()
|
||||
|
||||
|
||||
def get_stock_message(stock) -> str:
|
||||
# 获取真实数据
|
||||
current_price = stock['data']['f43'] / 100
|
||||
prev_close = stock['data']['f60'] / 100
|
||||
|
||||
# 计算涨跌幅
|
||||
change_percent = (current_price - prev_close) / prev_close * 100
|
||||
change_percent_rounded = round(change_percent, 2)
|
||||
|
||||
# 根据涨跌确定颜色
|
||||
color = "warning" if change_percent < 0 else "info"
|
||||
|
||||
return f"📈 **埃斯顿(002747)**\n> 现价: <font color=\"{color}\">{current_price}元</font>\n> 涨跌: <font color=\"{color}\">{change_percent_rounded}%</font>"
|
||||
86
work/daily_weather.py
Normal file
86
work/daily_weather.py
Normal file
@@ -0,0 +1,86 @@
|
||||
import requests
|
||||
from datetime import datetime
|
||||
|
||||
weather_url = 'https://p478kygkjw.re.qweatherapi.com'
|
||||
weather_api_key = '4aa47f183c694aaa812ff018be8270e4'
|
||||
nanjing_location = '101190101'
|
||||
|
||||
|
||||
def get_today_weather(city_location=nanjing_location):
|
||||
url = f'{weather_url}/v7/weather/3d'
|
||||
response = requests.get(
|
||||
url=url,
|
||||
headers={'X-QW-Api-Key': f'{weather_api_key}'},
|
||||
params={'location': city_location}
|
||||
)
|
||||
|
||||
return response.json()
|
||||
|
||||
|
||||
def get_today_indices(city_location=nanjing_location):
|
||||
url = f'{weather_url}/v7/indices/1d'
|
||||
response = requests.get(
|
||||
url=url,
|
||||
headers={'X-QW-Api-Key': f'{weather_api_key}'},
|
||||
params={'location': city_location, 'type': '0'}
|
||||
)
|
||||
|
||||
return response.json()
|
||||
|
||||
|
||||
def get_weather_message(weather, indices) -> str:
|
||||
textDay = weather['daily'][0]['textDay']
|
||||
textNight = weather['daily'][0]['textNight']
|
||||
weather_desc = textDay if textDay == textNight else f"{textDay}转{textNight}"
|
||||
|
||||
# 定义天气数据变量
|
||||
weather_data = {
|
||||
"city": "南京",
|
||||
"date": weather['daily'][0]['fxDate'],
|
||||
"update_time": datetime.now().strftime("%H:%M:%S"),
|
||||
"temp_max": weather['daily'][0]['tempMax'],
|
||||
"temp_min": weather['daily'][0]['tempMin'],
|
||||
"weather": weather_desc,
|
||||
"humidity": weather['daily'][0]['humidity'] + "%",
|
||||
"wind": weather['daily'][0]['windDirDay'] + " " + weather['daily'][0]['windScaleDay'] + "级",
|
||||
"life_index": [
|
||||
{"type": "舒适度指数", "icon": "😊", "level": indices['daily'][7]['category'],
|
||||
"advice": indices['daily'][7]['text']},
|
||||
{"type": "穿衣指数", "icon": "👕", "level": indices['daily'][2]['category'],
|
||||
"advice": indices['daily'][2]['text']},
|
||||
{"type": "运动指数", "icon": "🏃", "level": indices['daily'][0]['category'],
|
||||
"advice": indices['daily'][0]['text']},
|
||||
{"type": "洗车指数", "icon": "🚗", "level": indices['daily'][1]['category'],
|
||||
"advice": indices['daily'][1]['text']}
|
||||
],
|
||||
"data_source": "中央气象台"
|
||||
}
|
||||
|
||||
# 构建markdown内容
|
||||
return f"""# 🌤️ 今日天气播报
|
||||
## 📍 基本信息
|
||||
**城市:** {weather_data["city"]}
|
||||
**日期:** {weather_data["date"]}
|
||||
**更新时间:** {weather_data["update_time"]}
|
||||
|
||||
## 🌡️ 今日天气
|
||||
- **温度:** 最高{weather_data["temp_max"]}℃/最低{weather_data["temp_min"]}℃
|
||||
- **天气:** {weather_data["weather"]}
|
||||
- **湿度:** {weather_data["humidity"]}
|
||||
- **风力:** {weather_data["wind"]}
|
||||
|
||||
## 📊 生活指数
|
||||
😊 **舒适度指数:** {weather_data["life_index"][0]["level"]}
|
||||
建议:{weather_data["life_index"][0]["advice"]}
|
||||
|
||||
👕 **穿衣指数:** {weather_data["life_index"][1]["level"]}
|
||||
建议:{weather_data["life_index"][1]["advice"]}
|
||||
|
||||
🏃 **运动指数:** {weather_data["life_index"][2]["level"]}
|
||||
建议:{weather_data["life_index"][2]["advice"]}
|
||||
|
||||
🚗 **洗车指数:** {weather_data["life_index"][3]["level"]}
|
||||
建议:{weather_data["life_index"][3]["advice"]}
|
||||
|
||||
---
|
||||
*数据来源:{weather_data['data_source']}*"""
|
||||
Reference in New Issue
Block a user