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