-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweather.py
139 lines (108 loc) · 3.89 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import requests
import datetime
API_KEY = 'ddc79cf1bf6f60224b43cbebbc5ac509'
# 緯度、経度
lat = 35.44
lon = 139.44
BASE_URL = 'http://api.openweathermap.org/data/2.5/weather'
FORECAST_URL = 'http://api.openweathermap.org/data/2.5/forecast'
PARAMS = {
'q': 'Tokyo,JP', # 東京都
'units': 'metric', # 温度を摂氏で取得
'appid': API_KEY,
'lang': 'jp'
}
# PARAMS_FORECAST = {
# 'q': 'Tokyo,JP', # 東京都
# 'units': 'metric', # 温度を摂氏で取得
# 'appid': API_KEY
# }
weather_en_to_ja = {
'Clear':'🌞快晴',
'Clouds':'☁️曇り',
'Thunderstorm':'🌩️雷',
'Drizzle':'🌧️小雨',
'Rain':'🌧️雨',
'Snow':'雪',
'Mist':'🌫️霧',
'Smoke':'🌫️煙',
'Haze':'🌫️煙霧',
'Dust':'🌫️霧',
'Fog':'🌫️霧',
'Sand':'砂',
'Ash':'灰',
'Squall':'スコール',
'Tornado':'竜巻',
}
# 確認用
def weather_print():
# 現在
response = requests.get(BASE_URL, params=PARAMS)
data = response.json()
if response.status_code == 200:
main_data = data['main']
weather_data = data['weather'][0]
# print(f"都市: {data['name']}")
print(f"気温: {main_data['temp']}℃")
print(f"最高気温: {main_data['temp_max']}℃")
print(f"最低気温: {main_data['temp_min']}℃")
# print(f"気圧: {main_data['pressure']} hPa")
print(f"湿度: {main_data['humidity']}%")
print(f"天気: {weather_data['main']}")
else:
print(f"エラー: {data['message']}")
# 予報
response_fore = requests.get(FORECAST_URL, params=PARAMS)
data_fore = response_fore.json()
if response_fore.status_code == 200:
list_data_fore = data_fore['list'][0]
main_data_fore = list_data_fore['main']
weather_data_fore = list_data_fore['weather'][0]
temp_max = -100
temp_min = 100
for i in range(4):
list_tmp = data_fore['list'][i]
temp_max = max(temp_max,list_tmp['main']['temp_max'])
temp_min = min(temp_min,list_tmp['main']['temp_min'])
print('予報')
# print(list_data_fore['dt'])
# print(f"気温: {main_data_fore['temp']}℃")
# print(f"最高気温: {main_data_fore['temp_max']}℃")
# print(f"最低気温: {main_data_fore['temp_min']}℃")
print(f"最高気温: {temp_max}℃")
print(f"最低気温: {temp_min}℃")
print(f"天気: {weather_data_fore['main']}")
else:
print(f"エラー: {data['message']}")
def get_now_weather():
# 現在
response = requests.get(BASE_URL, params=PARAMS)
data = response.json()
if response.status_code == 200:
main_data = data['main']
weather_data = data['weather'][0]
# print(f"都市: {data['name']}")
now_temp = main_data['temp']
now_max_temp = main_data['temp_max']
now_min_temp = main_data['temp_min']
now_humidity = str(main_data['humidity'])
now_weather = weather_data['main']
return [now_temp,now_max_temp,now_min_temp,now_humidity,weather_en_to_ja[now_weather]]
def get_for_weather():
# 予報
response_fore = requests.get(FORECAST_URL, params=PARAMS)
data_fore = response_fore.json()
if response_fore.status_code == 200:
list_data_fore = data_fore['list'][0]
# main_data_fore = list_data_fore['main']
weather_data_fore = list_data_fore['weather'][0]
temp_max = -100
temp_min = 100
for i in range(4):
list_tmp = data_fore['list'][i]
temp_max = max(temp_max,list_tmp['main']['temp_max'])
temp_min = min(temp_min,list_tmp['main']['temp_min'])
for_max_temp = temp_max
for_min_temp = temp_min
for_weather = weather_data_fore['main']
return [for_max_temp,for_min_temp,weather_en_to_ja[for_weather]]