-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathapi.py
executable file
·52 lines (40 loc) · 1.48 KB
/
api.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
#!/usr/bin/env python
"""
Weather data is provided by https://www.seniverse.com/,
below code are modified from https://github.com/seniverse/seniverse-api-demos
NOTE: user need provide shell environment `SENIVERSE_KEY` and `SENIVERSE_UID`\
to using this API
"""
import os
import requests
import json
KEY = os.getenv('SENIVERSE_KEY', '') # API key
UID = "" # 用户ID, TODO: 当前并没有使用这个值,签名验证方式将使用到这个值
LOCATION = 'beijing' # 所查询的位置,可以使用城市拼音、v3 ID、经纬度等
API = 'https://api.seniverse.com/v3/weather/daily.json' # API URL,可替换为其他 URL
UNIT = 'c' # 单位
LANGUAGE = 'zh-Hans' # 查询结果的返回语言
def fetch_weather(location, start=0, days=15):
result = requests.get(API, params={
'key': KEY,
'location': location,
'language': LANGUAGE,
'unit': UNIT,
'start': start,
'days': days
}, timeout=2)
return result.json()
def get_weather_by_day(location, day=1):
result = fetch_weather(location)
normal_result = {
"location": result["results"][0]["location"],
"result": result["results"][0]["daily"][day]
}
return normal_result
if __name__ == '__main__':
default_location = "合肥"
result = fetch_weather(default_location)
print(json.dumps(result, ensure_ascii=False))
default_location = "合肥"
result = get_weather_by_day(default_location)
print(json.dumps(result, ensure_ascii=False))