-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
289 lines (219 loc) · 7.69 KB
/
main.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
import json
import requests
import datetime
import sys
icon = {
"50":"陽光充沛",
"51":"間有陽光",
"52":"短暫陽光",
"53":"間有陽光幾陣驟雨",
"54":"短暫陽光有驟雨",
"60":"多雲",
"61":"密雲",
"62":"微雨",
"63":"雨",
"64":"大雨",
"65":"雷暴",
"70":"天色良好",
"71":"天色良好",
"72":"天色良好",
"73":"天色良好",
"74":"天色良好",
"75":"天色良好",
"76":"大致多雲",
"77":"天色大致良好",
"80":"大風",
"81":"乾燥",
"82":"潮濕",
"83":"霧",
"84":"薄霧",
"85":"煙霞",
"90":"熱",
"91":"暖",
"92":"涼",
"93":"冷"
}
heat_stress_warning_level = {
"AMBER" : "黃色工作暑熱警告",
"RED" : "紅色工作暑熱警告",
"BLACK" : "黑色工作暑熱警告",
}
def get_weather_data(datatype):
res = requests.get(f"https://data.weather.gov.hk/weatherAPI/opendata/weather.php?dataType={datatype}&lang=tc")
data=json.loads(res.text)
return data
def dateprocess(str,dash=False,leading_zero = False):
if dash == False:
year = str[0:4]
month = str[4:6]
day = str[6:9]
time = 0
elif dash == True:
year = str[0:4]
month = str[5:7]
day = str[8:10]
time = str[11:16]
if leading_zero == False:
if month[0] == "0":
month = month[1]
if day[0] == "0":
day = day[1]
return year,month,day,time
# 工作暑熱警告
def heat_stress_warning():
res = requests.get(f"https://data.weather.gov.hk/weatherAPI/opendata/hsww.php?lang=tc")
data=json.loads(res.text)
if bool(data) == True: # 判斷是否有值
data = data["hsww"]
if data["actionCode"] == "CANCEL":
time = dateprocess(data["effectiveTime"],True)[3]
print(f'工作暑熱警告已於 {time} 取消。')
elif data["actionCode"] == "ISSUE":
print("工作暑熱警告:")
print(f'\t警告級別:{heat_stress_warning_level[data["warningLevel"]]}')
effective_time = dateprocess(data["effectiveTime"],True)[3]
issue_time = dateprocess(data["issueTime"],True)[3]
print(f'\t生效時間:{effective_time}')
print(f'\t發出時間:{issue_time}')
def weather_report_info():
data = get_weather_data("rhrread")
# 天氣圖標
print("天氣概況:",end="")
for i in range(len(data["icon"])):
print(icon[str(data["icon"][i])],end=" ")
print("\n")
heat_stress_warning()
# 特別天氣提示
if "specialWxTips" in data:
if data["specialWxTips"] != "":
for i in range(len(data["specialWxTips"])):
print(f'特別天氣提示:{data["specialWxTips"][i]}\n')
# 熱帶氣旋位置
if "tcmessage" in data:
if data["tcmessage"] != "":
for i in range(len(data["tcmessage"])):
print(data["tcmessage"][i],'\n')
# 警告信息
if data["warningMessage"] != "": # 判斷是否爲空字串
for i in range(len(data["warningMessage"])):
print(f'警告信息:{data["warningMessage"][i]}')
print()
# 溫度
print("溫度:")
for i in range(26):
print(f'\t{data["temperature"]["data"][i]["place"]}:{data["temperature"]["data"][i]["value"]}°C')
print("-"*20,"\n")
# 濕度
print(f'濕度:{data["humidity"]["data"][0]["value"]}%\n')
# 雨量
start_time_rain = dateprocess(data["rainfall"]["startTime"],True)
end_time_rain = dateprocess(data["rainfall"]["endTime"],True)
rain = False
for k in range(0,18): # 判断全港是否有雨
if data["rainfall"]["data"][k]["main"] == "FALSE" and data["rainfall"]["data"][k]["max"] != 0 :
rain = True
if rain == True:
#暴雨警告提醒
if "rainstormReminder" in data:
if data["rainstormReminder"] != "":
print(f'暴雨警告提醒:{data["rainstormReminder"]}\n')
print(f"雨量(時間:{start_time_rain[3]} - {end_time_rain[3]}):")
for k in range(0,18):
if data["rainfall"]["data"][k]["main"] == "FALSE" and data["rainfall"]["data"][k]["max"] != 0 :
if "min" in data["rainfall"]["data"][k]:
print(f'\t{data["rainfall"]["data"][k]["place"]}:{data["rainfall"]["data"][k]["min"]}mm - {data["rainfall"]["data"][k]["max"]}mm')
else:
print(f'\t{data["rainfall"]["data"][k]["place"]}:{data["rainfall"]["data"][k]["max"]}mm')
else:
print(f'\t{data["rainfall"]["data"][k]["place"]}:無雨')
print("-"*20,"\n")
else:
print("全港無雨")
# 紫外線指數
if data["uvindex"] != "":
print(f'紫外線指數:{data["uvindex"]["data"][0]["value"]}({data["uvindex"]["data"][0]["desc"]})\n')
# 閃電
if "lightning" in data:
if data["lightning"] != "":
start_time_lightning = dateprocess(data["lightning"]["startTime"],True)[3]
end_time_lightning = dateprocess(data["lightning"]["endTime"],True)[3]
print(f"閃電(時間:{start_time_lightning} - {end_time_lightning}):")
for i in range(len(data["lightning"]["data"])):
if data["lightning"]["data"][i]["occur"] == "true":
print(f'\t{data["lightning"]["data"][i]["place"]}')
print("\n","-"*20,"\n")
update_time = dateprocess(data["updateTime"],True)
print(f"\n更新時間:{update_time[0]} 年 {update_time[1]} 月 {update_time[2]} 日 {update_time[3]}")
print('\n','='*20,'\n')
def weather_forecast_info(data,date,full=0):
if full == 0: # 判斷是否獲取九天天氣預報,避免重複輸出
update_time = dateprocess(data["updateTime"],True)
print(f'更新時間:{update_time[0]} 年 {update_time[1]} 月 {update_time[2]} 日 {update_time[3]}\n')
print("天氣概況:",data['generalSituation'],"\n")
weather_forecast = data["weatherForecast"][date]
# 轉換時間戳
year = int(dateprocess(weather_forecast["forecastDate"])[0])
month = int(dateprocess(weather_forecast["forecastDate"])[1])
day = int(dateprocess(weather_forecast["forecastDate"])[2])
print(f'{year} 年 {month} 月 {day} 日 ({weather_forecast["week"]})')
print(f'風:{weather_forecast["forecastWind"]}')
print(f'天氣:{weather_forecast["forecastWeather"]} ({icon[str(weather_forecast["ForecastIcon"])]})')
print(f'最高溫度:{weather_forecast["forecastMaxtemp"]["value"]}°C')
print(f'最低溫度:{weather_forecast["forecastMintemp"]["value"]}°C')
print(f'相對濕度:{weather_forecast["forecastMinrh"]["value"]}% - {weather_forecast["forecastMaxrh"]["value"]}% ')
print(f'降雨概率:{weather_forecast["PSR"]}')
print("-"*20,"\n")
def all_freocast_info(data):
for i in range(0,9):
weather_forecast_info(data,i,i)
while True:
print("""
選項:\n
[0]: 退出程序;\n
[1]: 天氣報告;\n
[2]: 天氣預報;\n
提示:只需輸入數字部分。
""")
try:
command = int(input("指令:"))
except:
print("請按指令輸入。")
continue
print('='*20,"\n")
if command == 0:
sys.exit()
elif command == 1:
print("天氣報告:\n")
weather_report_info()
elif command == 2:
data = get_weather_data("fnd")
print("""
天氣預報:\n
[0]: 返回上一頁;\n
[1]: 獲取 9 天全部天氣預報;\n
[2]: 獲取九天内指定日期的天氣預報;\n
""")
try:
command = int(input("指令:"))
except:
print("請按指令輸入。")
continue
print()
if command == 0:
continue
elif command == 1:
all_freocast_info(data)
elif command == 2:
try:
date = int(input("第幾天:"))
while date <= 0 or date > 9:
print("請輸入 1-9\n")
date = int(input("第幾天:"))
except:
print("請輸入 1-9\n")
continue
print("\n九天天氣預報:\n")
weather_forecast_info(data,date-1)
else:
print("請按指令輸入。")
continue