weather_report/weather.py

89 lines
5 KiB
Python
Raw Permalink Normal View History

2024-02-20 03:28:29 +00:00
# 桂林市天气数据
# 数据来源: http://www.nmc.cn
# API: http://www.nmc.cn/rest/weather?stationid=57957
# Response: json
import requests
from datetime import datetime
API_URL = "http://www.nmc.cn/rest/weather"
CITY = "桂林市"
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:122.0) Gecko/20100101 Firefox/122.0',
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8',
'Accept-Language': 'en-US,en;q=0.5',
# 'Accept-Encoding': 'gzip, deflate',
'DNT': '1',
'Sec-GPC': '1',
'Connection': 'keep-alive',
'Upgrade-Insecure-Requests': '1',
}
params = {
'stationid': '57957',
}
def make_get_request():
response = requests.get(API_URL, params=params, headers=headers)
response.raise_for_status()
return response.json()
def get_weather_report() -> str:
try:
response = make_get_request()
if "success" != response["msg"]:
raise ValueError
current_date = response['data']['predict']['detail'][0]['date']
current_date_object = datetime.strptime(current_date, "%Y-%m-%d")
year = current_date_object.year
month = current_date_object.month
day = current_date_object.day
# 当天白天天气信息
current_date_day_weather_info = response['data']['predict']['detail'][0]['day']['weather']['info']
current_date_day_weather_temperature = response['data']['predict']['detail'][0]['day']['weather']['temperature']
current_date_day_wind_direct = response['data']['predict']['detail'][0]['day']['wind']['direct']
current_date_day_wind_power = response['data']['predict']['detail'][0]['day']['wind']['power']
# 当天夜晚天气信息
current_date_night_weather_info = response['data']['predict']['detail'][0]['night']['weather']['info']
current_date_night_weather_temperature = response['data']['predict']['detail'][0]['night']['weather']['temperature']
current_date_night_wind_direct = response['data']['predict']['detail'][0]['night']['wind']['direct']
current_date_night_wind_power = response['data']['predict']['detail'][0]['night']['wind']['power']
# 明天白天天气信息
next_date_day_weather_info = response['data']['predict']['detail'][1]['day']['weather']['info']
next_date_day_weather_temperature = response['data']['predict']['detail'][1]['day']['weather']['temperature']
next_date_day_wind_direct = response['data']['predict']['detail'][1]['day']['wind']['direct']
next_date_day_wind_power = response['data']['predict']['detail'][1]['day']['wind']['power']
# 明天夜晚天气信息
next_date_night_weather_info = response['data']['predict']['detail'][1]['night']['weather']['info']
next_date_night_weather_temperature = response['data']['predict']['detail'][1]['night']['weather']['temperature']
next_date_night_wind_direct = response['data']['predict']['detail'][1]['night']['wind']['direct']
next_date_night_wind_power = response['data']['predict']['detail'][1]['night']['wind']['power']
# 后天白天天气信息
next_next_date_day_weather_info = response['data']['predict']['detail'][2]['day']['weather']['info']
next_next_date_day_weather_temperature = response['data']['predict']['detail'][2]['day']['weather']['temperature']
next_next_date_day_wind_direct = response['data']['predict']['detail'][2]['day']['wind']['direct']
next_next_date_day_wind_power = response['data']['predict']['detail'][2]['day']['wind']['power']
# 后天夜晚天气信息
next_next_date_night_weather_info = response['data']['predict']['detail'][2]['night']['weather']['info']
next_next_date_night_weather_temperature = response['data']['predict']['detail'][2]['night']['weather']['temperature']
next_next_date_night_wind_direct = response['data']['predict']['detail'][2]['night']['wind']['direct']
next_next_date_night_wind_power = response['data']['predict']['detail'][2]['night']['wind']['power']
weather_report_text = f"{CITY}{month}{day}号天气预报,今天{current_date_night_weather_temperature}{current_date_day_weather_temperature}度," + \
f"白天{current_date_day_weather_info}{current_date_day_wind_direct}{current_date_day_wind_power}" + \
f"晚上{current_date_night_weather_info}{current_date_night_wind_direct}{current_date_night_wind_power}" + \
f"预计明天{next_date_night_weather_temperature}{next_date_day_weather_temperature}度,{next_date_day_weather_info}{next_date_day_wind_direct}{next_date_day_wind_power}" + \
f"后天{next_next_date_night_weather_temperature}{next_next_date_day_weather_temperature}度,{next_next_date_day_weather_info}{next_next_date_day_wind_direct}{next_next_date_day_wind_power}"
return weather_report_text
except requests.HTTPError as e:
print(f"网络错误: {e}")
except ValueError as e:
print(f"出错了: {e}")