forked from OreosLab/checkinpanel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi_weather.py
87 lines (79 loc) · 2.81 KB
/
api_weather.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# -*- coding: utf-8 -*-
"""
cron: 30 7 * * *
new Env('天气预报');
"""
import json
import os
from datetime import datetime
import requests
from notify_mtr import send
from utils import get_data
class Weather:
def __init__(self, check_items):
self.check_items = check_items
def main(self):
"""
获取天气信息。网址:https://www.sojson.com/blog/305.html
:return:
"""
try:
with open(
os.path.join(os.path.dirname(__file__), "city.json"),
"r",
encoding="utf-8",
) as city_file:
city_map = json.loads(city_file.read())
except Exception:
with open(
"/ql/repo/Oreomeow_checkinpanel_master/city.json", "r", encoding="utf-8"
) as city_file:
city_map = json.loads(city_file.read())
msg_all = ""
for city_name in self.check_items:
city_code = city_map.get(city_name, "101020100")
weather_url = f"http://t.weather.itboy.net/api/weather/city/{city_code}"
resp = requests.get(url=weather_url)
if resp.status_code == 200 and resp.json().get("status") == 200:
d = resp.json()
msg = (
"\n城市:"
+ d["cityInfo"]["parent"]
+ " "
+ d["cityInfo"]["city"]
+ "\n日期:"
+ d["data"]["forecast"][0]["ymd"]
+ " "
+ d["data"]["forecast"][0]["week"]
+ "\n天气:"
+ d["data"]["forecast"][0]["type"]
+ "\n温度:"
+ d["data"]["forecast"][0]["high"]
+ " "
+ d["data"]["forecast"][0]["low"]
+ "\n湿度:"
+ d["data"]["shidu"]
+ "\n空气质量:"
+ d["data"]["quality"]
+ "\nPM2.5:"
+ str(d["data"]["pm25"])
+ "\nPM10:"
+ str(d["data"]["pm10"])
+ "\n风力风向:"
+ d["data"]["forecast"][0]["fx"]
+ " "
+ d["data"]["forecast"][0]["fl"]
+ "\n感冒指数:"
+ d["data"]["ganmao"]
+ "\n温馨提示:"
+ d["data"]["forecast"][0]["notice"]
+ "\n更新时间:"
+ d["time"]
)
msg_all += msg + "\n\n"
return msg_all
if __name__ == "__main__":
data = get_data()
_check_items = data.get("CITY", [])
res = Weather(check_items=_check_items).main()
send("天气预报", res)