-
Notifications
You must be signed in to change notification settings - Fork 0
/
weather_report.py
331 lines (302 loc) · 13.7 KB
/
weather_report.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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# import requests
import urllib.request
import json
import sys
from datetime import datetime, timedelta
base_url = "http://api.worldweatheronline.com/free/v2/weather.ashx"
codes = {'113': 'iconSunny',
'116': 'iconPartlyCloudy',
"119": 'iconCloudy',
'122': 'iconVeryCloudy',
'143': 'iconFog',
'176': 'iconLightShowers',
'179': 'iconLightSleetShowers',
'182': 'iconLightSleet',
'185': 'iconLightSleet',
'200': 'iconThunderyShowers',
'227': 'iconLightSnow',
'230': 'iconHeavySnow',
'248': 'iconFog',
'260': 'iconFog',
'263': 'iconLightShowers',
'266': 'iconLightRain',
'281': 'iconLightSleet',
'284': 'iconLightSleet',
'293': 'iconLightRain',
'296': 'iconLightRain',
'299': 'iconHeavyShowers',
'302': 'iconHeavyRain',
'305': 'iconHeavyShowers',
'308': 'iconHeavyRain',
'311': 'iconLightSleet',
'314': 'iconLightSleet',
'317': 'iconLightSleet',
'320': 'iconLightSnow',
'323': 'iconLightSnowShowers',
'326': 'iconLightSnowShowers',
'329': 'iconHeavySnow',
'332': 'iconHeavySnow',
'335': 'iconHeavySnowShowers',
'338': 'iconHeavySnow',
'350': 'iconLightSleet',
'353': 'iconLightShowers',
'356': 'iconHeavyShowers',
'359': 'iconHeavyRain',
'362': 'iconLightSleetShowers',
'365': 'iconLightSleetShowers',
'368': 'iconLightSnowShowers',
'371': 'iconHeavySnowShowers',
'374': 'iconLightSleetShowers',
'377': 'iconLightSleet',
'386': 'iconThunderyShowers',
'389': 'iconThunderyHeavyRain',
'392': 'iconThunderySnowShowers',
'395': 'iconHeavySnowShowers'
}
icons = {
'iconSunny': [
'\033[38;5;226m \\ / \033[0m',
'\033[38;5;226m .-. \033[0m',
'\033[38;5;226m ― ( ) ― \033[0m',
'\033[38;5;226m `-’ \033[0m',
'\033[38;5;226m / \\ \033[0m'],
'iconPartlyCloudy': [
"\033[38;5;226m \\ /\033[0m ",
"\033[38;5;226m _ /\"\"\033[38;5;250m.-. \033[0m",
"\033[38;5;226m \\_\033[38;5;250m( ). \033[0m",
"\033[38;5;226m /\033[38;5;250m(___(__) \033[0m",
" "],
'iconCloudy': [
" ",
"\033[38;5;250m .--. \033[0m",
"\033[38;5;250m .-( ). \033[0m",
"\033[38;5;250m (___.__)__) \033[0m",
" "],
'iconVeryCloudy': [
" ",
"\033[38;5;240;1m .--. \033[0m",
"\033[38;5;240;1m .-( ). \033[0m",
"\033[38;5;240;1m (___.__)__) \033[0m",
" "],
'iconLightShowers': [
"\033[38;5;226m _`/\"\"\033[38;5;250m.-. \033[0m",
"\033[38;5;226m ,\\_\033[38;5;250m( ). \033[0m",
"\033[38;5;226m /\033[38;5;250m(___(__) \033[0m",
"\033[38;5;111m ‘ ‘ ‘ ‘ \033[0m",
"\033[38;5;111m ‘ ‘ ‘ ‘ \033[0m"],
'iconHeavyShowers': [
"\033[38;5;226m _`/\"\"\033[38;5;240;1m.-. \033[0m",
"\033[38;5;226m ,\\_\033[38;5;240;1m( ). \033[0m",
"\033[38;5;226m /\033[38;5;240;1m(___(__) \033[0m",
"\033[38;5;21;1m ‚‘‚‘‚‘‚‘ \033[0m",
"\033[38;5;21;1m ‚’‚’‚’‚’ \033[0m"],
'iconLightSnowShowers': [
"\033[38;5;226m _`/\"\"\033[38;5;250m.-. \033[0m",
"\033[38;5;226m ,\\_\033[38;5;250m( ). \033[0m",
"\033[38;5;226m /\033[38;5;250m(___(__) \033[0m",
"\033[38;5;255m * * * \033[0m",
"\033[38;5;255m * * * \033[0m"],
'iconHeavySnowShowers': [
"\033[38;5;226m _`/\"\"\033[38;5;240;1m.-. \033[0m",
"\033[38;5;226m ,\\_\033[38;5;240;1m( ). \033[0m",
"\033[38;5;226m /\033[38;5;240;1m(___(__) \033[0m",
"\033[38;5;255;1m * * * * \033[0m",
"\033[38;5;255;1m * * * * \033[0m"],
'iconLightSleetShowers': [
"\033[38;5;226m _`/\"\"\033[38;5;250m.-. \033[0m",
"\033[38;5;226m ,\\_\033[38;5;250m( ). \033[0m",
"\033[38;5;226m /\033[38;5;250m(___(__) \033[0m",
"\033[38;5;111m ‘ \033[38;5;255m*\033[38;5;111m ‘ \033[38;5;255m* \033[0m",
"\033[38;5;255m *\033[38;5;111m ‘ \033[38;5;255m*\033[38;5;111m ‘ \033[0m"],
'iconThunderyShowers': [
"\033[38;5;226m _`/\"\"\033[38;5;250m.-. \033[0m",
"\033[38;5;226m ,\\_\033[38;5;250m( ). \033[0m",
"\033[38;5;226m /\033[38;5;250m(___(__) \033[0m",
"\033[38;5;228;5m ⚡\033[38;5;111;25m‘ ‘\033[38;5;228;5m⚡\033[38;5;111;25m‘ ‘ \033[0m",
"\033[38;5;111m ‘ ‘ ‘ ‘ \033[0m"],
'iconThunderyHeavyRain': [
"\033[38;5;240;1m .-. \033[0m",
"\033[38;5;240;1m ( ). \033[0m",
"\033[38;5;240;1m (___(__) \033[0m",
"\033[38;5;21;1m ‚‘\033[38;5;228;5m⚡\033[38;5;21;25m‘‚\033[38;5;228;5m⚡\033[38;5;21;25m‚‘ \033[0m",
"\033[38;5;21;1m ‚’‚’\033[38;5;228;5m⚡\033[38;5;21;25m’‚’ \033[0m"],
'iconThunderySnowShowers': [
"\033[38;5;226m _`/\"\"\033[38;5;250m.-. \033[0m",
"\033[38;5;226m ,\\_\033[38;5;250m( ). \033[0m",
"\033[38;5;226m /\033[38;5;250m(___(__) \033[0m",
"\033[38;5;255m *\033[38;5;228;5m⚡\033[38;5;255;25m *\033[38;5;228;5m⚡\033[38;5;255;25m * \033[0m",
"\033[38;5;255m * * * \033[0m"],
'iconLightRain': [
"\033[38;5;250m .-. \033[0m",
"\033[38;5;250m ( ). \033[0m",
"\033[38;5;250m (___(__) \033[0m",
"\033[38;5;111m ‘ ‘ ‘ ‘ \033[0m",
"\033[38;5;111m ‘ ‘ ‘ ‘ \033[0m"],
'iconHeavyRain': [
"\033[38;5;240;1m .-. \033[0m",
"\033[38;5;240;1m ( ). \033[0m",
"\033[38;5;240;1m (___(__) \033[0m",
"\033[38;5;21;1m ‚‘‚‘‚‘‚‘ \033[0m",
"\033[38;5;21;1m ‚’‚’‚’‚’ \033[0m"],
'iconLightSnow': [
"\033[38;5;250m .-. \033[0m",
"\033[38;5;250m ( ). \033[0m",
"\033[38;5;250m (___(__) \033[0m",
"\033[38;5;255m * * * \033[0m",
"\033[38;5;255m * * * \033[0m"],
'iconHeavySnow': [
"\033[38;5;240;1m .-. \033[0m",
"\033[38;5;240;1m ( ). \033[0m",
"\033[38;5;240;1m (___(__) \033[0m",
"\033[38;5;255;1m * * * * \033[0m",
"\033[38;5;255;1m * * * * \033[0m"],
'iconLightSleet': [
"\033[38;5;250m .-. \033[0m",
"\033[38;5;250m ( ). \033[0m",
"\033[38;5;250m (___(__) \033[0m",
"\033[38;5;111m ‘ \033[38;5;255m*\033[38;5;111m ‘ \033[38;5;255m* \033[0m",
"\033[38;5;255m *\033[38;5;111m ‘ \033[38;5;255m*\033[38;5;111m ‘ \033[0m"],
'iconFog': [
" ",
"\033[38;5;251m _ - _ - _ - \033[0m",
"\033[38;5;251m _ - _ - _ \033[0m",
"\033[38;5;251m _ - _ - _ - \033[0m",
" "],
'iconUnknown': [
" .-. ",
" __) ",
" ( ",
" `-’ ",
" • "]
}
windDir = {
"N": "\033[1m↓\033[0m",
"NNE": "\033[1m↓\033[0m",
"NE": "\033[1m↙\033[0m",
"ENE": "\033[1m↙\033[0m",
"E": "\033[1m←\033[0m",
"ESE": "\033[1m←\033[0m",
"SE": "\033[1m↖\033[0m",
"SSE": "\033[1m↖\033[0m",
"S": "\033[1m↑\033[0m",
"SSW": "\033[1m↑\033[0m",
"SW": "\033[1m↗\033[0m",
"WSW": "\033[1m↗\033[0m",
"W": "\033[1m→\033[0m",
"WNW": "\033[1m→\033[0m",
"NW": "\033[1m↘\033[0m",
"NNW": "\033[1m↘\033[0m",
}
class Query(object):
def __init__(self, day, city):
self.day = day
self.time = [3, 4, 5, 6]
self.weatherCode = ''
self.weather = ''
self.date = ''
self.hourly = ''
self.tempC = 0
self.winddir16Point = ''
self.windspeedKmph = 0
self.humidity = 0
self.chanceofwater = 0
self.city = city
def query(self):
# 使用resquests
# p={"q":"%s"%self.city, "num_of_days":3, "format":"json", "key":"55f1fdd05fba23be0a18043d0a017", "lang":"zh"}
# response=requests.get(base_url,params=p)
# json_string = response.text
# 使用urllib
url = base_url + "?key=55f1fdd05fba23be0a18043d0a017&q=%s&num_of_days=3&format=json&lang=zh" % self.city
response = urllib.request.urlopen(url)
json_string = response.read()
parsed_json = json.loads(json_string)
data = parsed_json['data'] # 获取所有数据
try:
self.weather = data['weather'][self.day] # 获取天气预报,[]内0代表当天,1代表明天,以此类推。
except KeyError as e:
print("\033[1;31;49m" + "请输入正确的城市或地区!" + "\033[0m")
sys.exit()
self.date = self.weather['date']
def detail(self, time):
self.hourly = self.weather['hourly'][time] # 获取小时数据,time:100-1500
self.weatherCode = self.hourly['weatherCode']
self.tempC = self.hourly['tempC']
self.winddir16Point = self.hourly['winddir16Point']
self.windspeedKmph = self.hourly['windspeedKmph']
self.chanceofrain = self.hourly['chanceofrain']
self.chanceofsnow = self.hourly['chanceofsnow']
self.humidity = self.hourly['humidity']
self.chanceofwater = int(self.chanceofrain) if int(self.chanceofrain) > int(self.chanceofsnow) else int(
self.chanceofsnow)
def printSingle(self):
l1 = l2 = l3 = l4 = l5 = ''
for time in self.time:
self.detail(time)
l1 += '│' + icons[codes[self.weatherCode]][0] + self.hourly['lang_zh'][0]['value'].encode(
"utf-8") + '\t\t' if len(self.hourly['lang_zh'][0]['value'].encode("utf-8")) <= 12 else '│' + icons[
codes[self.weatherCode]][0] + self.hourly['lang_zh'][0]['value'].encode("utf-8") + '\t'
l2 += '│' + icons[codes[self.weatherCode]][1] + temp_color(self.tempC) + "°C" + '\t\t'
l3 += '│' + icons[codes[self.weatherCode]][2] + windDir[self.winddir16Point] + " " + wind_color(
self.windspeedKmph) + "km/h" + '\t\t'
l4 += '│' + icons[codes[self.weatherCode]][3] + "降水概率:" + str(self.chanceofwater) + "%" + '\t'
l5 += '│' + icons[codes[self.weatherCode]][4] + "湿度:" + str(self.humidity) + "%" + '\t\t'
print(
l1 + "│")
print(
l2 + "│")
print(
l3 + "│")
print(
l4 + "│")
print(
l5 + "│")
def printDay(self, delta):
date_time = datetime.strftime(datetime.today() + timedelta(days=delta), "%Y-%m-%d")
line1 = " ┌─────────────┐ "
line2 = "┌───────────────────────────────┬──────────────────────────%s───────────────────────────┬───────────────────────────────┐" % date_time
line3 = "│ Morning │ Noon └──────┬──────┘ Evening │ Night │"
line4 = "├───────────────────────────────┼───────────────────────────────┼───────────────────────────────┼───────────────────────────────┤"
endline = "└───────────────────────────────┴───────────────────────────────┴───────────────────────────────┴───────────────────────────────┘"
print(line1)
print(line2)
print(line3)
print(line4)
self.printSingle()
print(endline)
def temp_color(temp):
if temp >= 35 or temp <= -10:
color = "\033[1;31;49m" + str(temp) + "\033[0m"
elif (temp >= 25 and temp < 35):
color = "\033[1;33;49m" + str(temp) + "\033[0m"
elif temp > 10 and temp < 25:
color = "\033[1;32;49m" + str(temp) + "\033[0m"
elif temp > -10 and temp <= 10:
color = "\033[1;34;49m" + str(temp) + "\033[0m"
return color
def wind_color(windspeed):
if windspeed <= 5:
color = "\033[1;32;49m" + str(windspeed) + "\033[0m"
elif windspeed > 5 and windspeed <= 10:
color = "\033[1;33;49m" + str(windspeed) + "\033[0m"
else:
color = "\033[1;34;49m" + str(windspeed) + "\033[0m"
return color
def main():
try:
city = sys.argv[1]
except IndexError as e:
print(
"\033[1;31;49m" + "请输入您要查询的城市或地区:" + "\033[0m")
city = input()
if city == '':
sys.exit()
day = [0, 1, 2]
for i in day:
query = Query(i, city)
query.query()
query.printDay(i)
if __name__ == "__main__":
main()