-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathforecast.py
64 lines (57 loc) · 2.78 KB
/
forecast.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
import requests
import json
import haku.config
import data.log
from handlers.message import Message
HEKEY = haku.config.Config().get_key('heweather')
def run(message: Message):
KEY = HEKEY # 和风天气key
if KEY:
helpMsg = '小白会试着搜索指定地区天气~\nforecast 城市 地区 n日后\n0>=n>=2'
req = list(message.message.split())
for i in range(0, len(req)):
req[i] = req[i].strip()
url1 = 'https://geoapi.heweather.net/v2/city/lookup'
url2 = 'https://devapi.heweather.net/v7/weather/3d'
ans = ''
params = {'key': KEY}
try:
days = int(req[3])
except:
days = 100
if days >= 0 and days <= 2 and len(req) == 4:
params.update({'location': req[2], 'adm': req[1]})
try:
resp = requests.get(url=url1, params=params, timeout=5)
if resp.status_code == 200:
rejson = json.loads(resp.text)
data.log.get_logger().debug(rejson)
cityId = rejson['location'][0]['id']
province = rejson['location'][0]['adm1']
city = rejson['location'][0]['adm2']
resp = requests.get(url=url2, params={'key': KEY, 'location': cityId}, timeout=5)
if resp.status_code == 200:
rejson = json.loads(resp.text)
data.log.get_logger().debug(rejson)
ans = province + '-' + city + ' ' + rejson['daily'][days]['textDay'] \
+ '\n' + rejson['daily'][days]['fxDate'] \
+ '\n气温:' + rejson['daily'][days]['tempMin'] + '-' + rejson['daily'][days][
'tempMax'] + '℃' \
+ '\n风向:' + rejson['daily'][days]['windDirDay'] + ' 风力:' + rejson['daily'][days][
'windScaleDay'] + '级' \
+ '\n风速:' + rejson['daily'][days]['windSpeedDay'] + 'km/h 气压:' + rejson['daily'][days][
'pressure'] + 'hPa'
else:
ans = '好像返回了奇怪的东西: ' + str(resp.status_code)
elif resp.status_code == 404:
ans = '真的有这个地方咩,别骗小白!'
else:
ans = '好像返回了奇怪的东西: ' + str(resp.status_code)
except Exception as e:
data.log.get_logger().exception(f'RuntimeError in plugin forecast: {e}')
ans = '啊嘞嘞好像出错了,一定是和风炸了不关小白!'
else:
ans = helpMsg
else:
ans = '好像和风不让查诶...'
return ans