XZZ/worker/weather.py

56 lines
2.1 KiB
Python
Raw Normal View History

2020-08-28 21:32:41 +08:00
import random
import requests
import datetime
2020-08-28 21:43:15 +08:00
from zzcore import StdAns
2020-08-28 21:32:41 +08:00
2020-08-28 21:43:15 +08:00
class Ans(StdAns):
2020-08-28 21:32:41 +08:00
"""docstring for Ans"""
def GETMSG(self):
try:
return self.get_weather()
except Exception as e:
return "啊哦,出错了"
def get_weather(self):
ran = random.randint(0, 9999999999)
url = "https://api.caiyunapp.com/v2/Y2FpeXVuIGFuZHJpb2QgYXBp/120.127164,36.000129/weather?lang=zh_CN&device_id={}".format(ran)
res = requests.get(url).json()
msg = "XZZ为您播报近期天气\n-------------------------------------\n"
msg += "日期 | 温度 | 湿度 | 天气 |\n-------------------------------------"
result = res["result"]["daily"]
now = datetime.datetime.now()
weather_summary = {
"CLEAR_DAY": "",
"CLEAR_NIGHT": "",
"CLOUD_DAY_WIDGET": "多云转晴",
"CLOUDY": "",
"HAZE": "",
"HAZE_WIDGET": "",
"PARTLY_CLOUD_NIGHT": "多云转晴",
"PARTLY_CLOUD_NIGHT_WIDGET": "多云转晴",
"PARTLY_CLOUD_WIDGET": "",
"PARTLY_CLOUDY_DAY": "多云转晴",
"PARTLY_CLOUDY_NIGHT": "多云转晴",
"RAIN": "",
"RAIN_HEAVY": "大雨",
"RAIN_LIGHT": "小雨",
"RAIN_MIDDLE": "中雨",
"RAIN_NORMAL_WIDGET": "大雨",
"SNOW": "",
"SNOW_WIDGET": "",
"SUNSHINE_NIGHT_WIDGET": "",
"SUNSHINE_WIDGET": "",
"WIND": "",
}
for i in range(5):
msg += "\n"
msg += "{} | ".format((now + datetime.timedelta(days=i)).strftime("%Y-%m-%d"))
msg += "{} | ".format(format(result["temperature"][i]["avg"], ".2f"))
msg += "{} | ".format(format(result["humidity"][i]["avg"], ".2f"))
msg += "{} | ".format(weather_summary[result["skycon"][i]["value"]])
return msg
if __name__ == "__main__":
Ans().GETMSG()