forked from bharadwaj-raju/WeatherDesk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWeatherDesk.py
289 lines (187 loc) · 7.13 KB
/
WeatherDesk.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
#!/usr/bin/env python3
# Copyright (c) 2016 Bharadwaj Raju <[email protected]> All Rights Reserved.
# Licensed under the GNU General Public License 3: https://www.gnu.org/licenses/gpl.txt
import urllib.request
import os
import time
import datetime
import json
import sys
import argparse
import Desktop
__author__ = 'Bharadwaj Raju <[email protected]>'
NAMING_RULES = '''
This is how to name files in the wallpaper directory:\n
WEATHER | FILENAME
_________________________|________________
Clear, Calm, Fair: | normal{0}
Thunderstorm: | thunder{0}
Windy, Breeze, Gale: | wind{0}
Drizzle, Rain, Showers: | rain{0}
Snow: | snow{0}
Cloudy: | cloudy{0}
Other: | normal{0}
If using with --time or --time 3, add:
"day-", "night-" or "evening-" in front of filename.
If using with --time 4, add:
"morning-", "day-", "evening-" or "night-"
If using with --time 2, add:
"day-" or "night-"
'''
#-- Arguments
arg_parser = argparse.ArgumentParser(
description='''WeatherDesk - Change the wallpaper based on the weather
(Uses the Yahoo! Weather API)''', formatter_class=argparse.RawTextHelpFormatter)
arg_parser.add_argument('-d', '--dir', metavar='directory', type=str,
help='Specify wallpaper directory. Default: %s' % '~/.weatherdesk_walls',
required=False)
arg_parser.add_argument('-f', '--format', metavar='format', type=str,
help='Specify image file format. Default: %s' % '.jpg',
required=False)
arg_parser.add_argument('-w', '--wait', metavar='seconds', type=int,
help='Specify time (in seconds) to wait before updating. Default: %d' % 600,
required=False)
arg_parser.add_argument('-t', '--time', nargs='?',
help='''Use different backgrounds for different times.\n
Variations:
2 = day/night
3 = day/evening/night [Default]
4 = morning/day/evening/night
See --naming.''',
type=int, choices=[2, 3, 4], const=3, required=False)
arg_parser.add_argument('-n', '--naming', action='store_true',
help='Show the image file-naming rules and exit.',
required=False)
arg_parser.add_argument('-c', '--city', metavar='name', type=str,
help=str('Specify city for weather. If not given, taken from ipinfo.io.'), nargs='+',
required=False)
args = arg_parser.parse_args()
if args.city:
city = ' '.join(args.city).replace(' ', '%20')
else:
try:
city = json.loads(urllib.request.urlopen('http://ipinfo.io/json').read().decode('utf-8'))
city = city['city'].replace(' ', '%20')
except:
pass
use_time = bool(args.time)
if args.dir:
# User provided a directory
walls_dir = os.path.abspath(args.dir)
if not os.path.isdir(walls_dir):
sys.stderr.write('Invalid directory %s.' % walls_dir)
sys.exit(1)
else:
walls_dir = os.path.join(os.path.expanduser('~'), '.weatherdesk_walls')
if not os.path.isdir(walls_dir):
os.mkdir(walls_dir)
fmt = 'No directory specified. Creating in {}... Put files there or specify directory with --dir'
sys.stderr.write(fmt.format(walls_dir))
sys.exit(1)
if args.format:
if not args.format.startswith('.'):
args.format = '.' + args.format
file_format = args.format
else: file_format = '.jpg'
wait_time = args.wait or 600 # ten minutes
if args.naming:
print(NAMING_RULES.format(file_format))
sys.exit(0)
#-- -- Functions
def get_time_of_day(level=3):
'''
For detail level 2:
06 to 20: day
20 to 06: night
'''
'''
For detail level 3:
06 to 17: day
17 to 20: evening
20 to 06: night
'''
'''
For detail level 4:
06 to 08: morning
08 to 17: day
17 to 20: evening
20 to 06: night
'''
current_time = datetime.datetime.now()
if level == 3:
if current_time.hour in range(6, 17):
return 'day'
elif current_time.hour in range(17, 20):
return 'evening'
else:
return 'night'
elif level == 4:
if current_time.hour in range(6, 8):
return 'morning'
elif current_time.hour in range(8, 17):
return 'day'
elif current_time.hour in range(17, 20):
return 'evening'
else:
return 'night'
else:
if current_time.hour in range(6, 20):
return 'day'
else:
return 'night'
def get_file_name(weather_name, time=False):
# For some weird reason, Python evaluates the following as True:
# 'some' or 'the other' or 'nope' in 'string_which_doesnt_have_these'
rain_strs = ('drizzle', 'rain', 'shower')
wind_strs = ('wind', 'breez', 'gale') # breez matches both breeze and breezy
normal_strs = ('calm', 'clear', 'fair', 'sun')
if any(substr in weather_name for substr in rain_strs):
weather_file = 'rain' + file_format
elif 'thunder' in weather_name:
weather_file = 'thunder' + file_format
elif 'snow' in weather_name:
weather_file = 'snow' + file_format
elif any(substr in weather_name for substr in wind_strs):
weather_file = 'wind' + file_format
elif any(substr in weather_name for substr in normal_strs):
weather_file = 'normal' + file_format
elif 'cloud' in weather_name:
weather_file = 'cloudy' + file_format
else:
weather_file = 'normal' + file_format
if time:
return get_time_of_day(args.time) + '-' + weather_file
return weather_file
def check_if_all_files_exist(time=False, level=3):
all_exist = True
required_files = ['rain', 'snow', 'normal', 'cloudy', 'wind', 'thunder']
if time:
if level == 3:
daytime = ['day', 'evening', 'night']
elif level == 4:
daytime = ['morning', 'day', 'evening', 'night']
else: # level 2
daytime = ['day', 'night']
required_files = [moment + '-' + weather
for moment in daytime
for weather in required_files]
for i in required_files:
if not os.path.isfile(os.path.join(walls_dir, (i + file_format))):
all_exist = False
sys.stderr.write(os.path.join(walls_dir, (i + file_format)) + '\n')
return all_exist
if not check_if_all_files_exist(time=use_time, level=args.time):
sys.stderr.write('\nNot all required files were found.\n %s' % NAMING_RULES.format(file_format))
sys.exit(1)
while True:
try:
weather_json_url = r'https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20weather.forecast%20where%20woeid%20in%20(select%20woeid%20from%20geo.places(1)%20where%20text%3D%22' + city + '%22)&format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys'
weather_json = json.loads(urllib.request.urlopen(weather_json_url).read().decode('utf-8'))
weather = str(weather_json['query']['results']['channel']['item']['condition']['text']).lower()
print(weather)
print(city)
print(os.path.join(walls_dir, get_file_name(weather, time=use_time)))
Desktop.set_wallpaper(os.path.join(walls_dir, get_file_name(weather, time=use_time)))
except:
pass
time.sleep(wait_time)