-
Notifications
You must be signed in to change notification settings - Fork 1
/
weather.py
67 lines (58 loc) · 1.72 KB
/
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
import requests
import json
TAG = "WEATHER"
WEATHER_URL_3D = 'https://devapi.qweather.com/v7/weather/3d?'
WEATHER_URL_NOW = 'https://devapi.qweather.com/v7/weather/now?'
def DEBUG(str):
if False:
print(str)
def get_weather_3d(uri_query):
data = {}
list_daily = []
# data_daily = {}
url = WEATHER_URL_3D + uri_query
DEBUG(url)
response = requests.get(url)
DEBUG(response.text)
data_json = json.loads(response.text)
code = data_json['code']
data['code'] = code
if code != "200":
data = json.dumps(data, ensure_ascii=False)
return data
daily = data_json['daily']
for d in daily:
data_daily = {}
data_daily['fxDate'] = d['fxDate']
data_daily['tempMin'] = d['tempMin']
data_daily['tempMax'] = d['tempMax']
data_daily['iconDay'] = d['iconDay']
data_daily['textDay'] = d['textDay']
list_daily.append(data_daily)
# data_daily.clear()
data['daily'] = list_daily
data = json.dumps(data, ensure_ascii=False)
return data
def get_weather_now(uri_query):
data = {}
url = WEATHER_URL_NOW + uri_query
DEBUG(url)
response = requests.get(url)
DEBUG(response.text)
data_json = json.loads(response.text)
code = data_json['code']
data['code'] = code
if code != "200":
data = json.dumps(data, ensure_ascii=False)
return data
now = data_json['now']
temp = now['temp'] # 温度
icon = now['icon']
text = now['text'] # 天气状况 -> 晴
humidity = now['humidity'] # 湿度
data['temp'] = temp
data['icon'] = icon
data['text'] = text
data['humidity'] = humidity
data = json.dumps(data, ensure_ascii=False)
return data