-
Notifications
You must be signed in to change notification settings - Fork 0
/
xkcdlock.py
executable file
·402 lines (341 loc) · 12.8 KB
/
xkcdlock.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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
#!/usr/bin/python
import argparse
from datetime import datetime, timedelta
import os
import random
import re
import subprocess
import sys
import time
import urllib.error
import urllib.request
#from wand.display import display
from wand.font import Font
from wand.image import Image
MODES=['latest', 'random', 'index']
DEFAULT_LOCK_BIN=["swaylock", "i3lock"]
DEFAULT_LOCK_ARGS=['-c', '#000000', '-s', 'fit']
LOG=False
URL='https://xkcd.com/'
LATEST_URL=URL
RANDOM_URL='https://c.xkcd.com/random/comic'
COOLDOWN_PREFIX='last-query'
DEFAULT_COOLDOWN_TIME=timedelta(hours=1)
TIME_FORMAT='%Y-%m-%d %H:%M:%S'
URL_FONT="https://github.com/ipython/xkcd-font/raw/master/xkcd-script/font/xkcd-script.ttf"
IMG_FONT_FILE='xkcd-script.ttf'
IMG_FONT_DIR=f'~/.local/share/fonts'
IMG_BACKGROUND_COLOR='#000000'
IMG_CAPTION_COLOR='#b0b0b0'
IMG_CAPTION_SIZE=18
IMG_TITLE_COLOR='#ffffff'
IMG_TITLE_SIZE=22
def check_font(path):
font_dir = path
font_path = os.path.join(font_dir, IMG_FONT_FILE)
try:
if not os.path.isfile(font_path):
log(f'Font {font_path} not found.')
url=URL_FONT
log(f'Downloading font {url}...')
try:
response = urllib.request.urlopen(url)
log('Download succeeded.')
font = response.read()
except:
log(f'Failed querying font from {url}.')
return None
os.makedirs(font_dir, exist_ok = True)
with open(font_path, 'wb') as f:
f.write(font)
except:
log(f'Failed reading font file')
return None
return font_path
def download(path, url):
log(f'Downloading comic from {url}...')
try:
response = urllib.request.urlopen(url)
html = response.read().decode()
except:
log(f'Failed querying comic from {url}.')
return None
title_match = re.search(r'<div[^>]*id="ctitle"[^>]*>([^<]*)</div>', html)
img_tag_match = re.search(r'<div[^>]*id="comic"[^>]*>\s(.+)\s<\/div>', html)
index_match = re.search(r'Permanent link .*\".*xkcd.com\/(\d+)\"', html)
if title_match is None or img_tag_match is None or index_match is None:
return None
img_tag = img_tag_match[1]
img_url_match = re.search(r'src="([^"]*)"', img_tag)
img_tagline_match = re.search(r'title="([^"]*)"', img_tag)
if img_url_match is None or img_tagline_match is None:
return None
title = title_match[1]
img_url = img_url_match[1]
img_tagline = img_tagline_match[1]
#manual char replacement
img_tagline = img_tagline.replace(''', '\'').replace('&', '&')
index_str = index_match[1]
try:
index = int(index_str)
index_str = str(index)
except:
log(f'Could not parse index {index_str}.')
return None
# pad url with protocol
if img_url[:2] == '//':
img_url = 'https:' + img_url
try:
response = urllib.request.urlopen(img_url)
img = response.read()
except:
log(f'Failed querying image from {img_url}.')
return None
# Write information to file
try:
img_path = os.path.join(path, index_str + '.png')
with open(img_path, 'wb') as f:
f.write(img)
with open(os.path.join(path, index_str + '.txt'), 'w') as f:
f.write(title + '\n')
f.write(img_tagline + '\n')
except:
log(f'Failed writing to {path}, are permissions correct?')
return None
log('Download completed.')
return (index, img_path, title, img_tagline)
def read_cooldown_cache(path, mode, cooldown=DEFAULT_COOLDOWN_TIME):
cooldown_path = os.path.join(path, COOLDOWN_PREFIX + '-' + mode)
now = datetime.now()
cached_prefix = None
try:
if os.path.isfile(cooldown_path):
with open(cooldown_path, 'r') as f:
cached_time = datetime.strptime(f.readline().strip(), TIME_FORMAT)
cached_prefix = f.readline().strip()
if now - cached_time <= cooldown:
log(f'Found cached value for mode {mode}: {cached_prefix}')
return cached_prefix
except:
log(f'Failed reading cooldown')
return None
def write_cooldown_cache(path, mode, prefix):
cooldown_path = os.path.join(path, COOLDOWN_PREFIX + '-' + mode)
now = datetime.now()
try:
with open(cooldown_path, 'w') as f:
f.write(now.strftime(TIME_FORMAT))
f.write('\n')
f.write(prefix)
f.write('\n')
except:
log(f'Failed writing new cooldown time')
def download_latest(path, cooldown):
cached = read_cooldown_cache(path, 'latest', cooldown=cooldown)
if not cached is None:
res = load(path, cached)
if cached is None or res is None:
res = download(path, LATEST_URL)
if not res is None:
(index, _, _, _) = res
write_cooldown_cache(path, 'latest', str(index))
return res
def download_random(path, cooldown):
cached = read_cooldown_cache(path, 'random', cooldown=cooldown)
if not cached is None:
res = load(path, cached)
if cached is None or res is None:
res = download(path, RANDOM_URL)
if not res is None:
(index, _, _, _) = res
write_cooldown_cache(path, 'random', str(index))
return res
def download_index(path, index, cooldown):
res = load(path, cached)
if res is None:
res = download(path, url + str(index))
return res
def load(path, prefix):
index = int(prefix)
try:
img_path = os.path.join(path, prefix + '.png')
if not os.path.isfile(img_path):
log(f'Could not find image file {path}/{prefix}.png')
return None
#with open(img_path, 'rb') as f:
# img = f.readall()
with open(os.path.join(path, prefix + '.txt'), 'r') as f:
title = f.readline().strip()
tagline = f.readline().strip()
except:
log(f'Failed reading metadata from {path}/{prefix}.txt.')
return None
return (index, img_path, title, tagline)
def load_index(path, index):
return load(path, str(index))
def load_random(path):
# not the most optimal, but functional
try:
filenames = list(filter(lambda f: f.endswith('.png'), os.listdir(path)))
except:
log(f'Failed reading directory {path}, are permissions correct?')
return None
i = random.randint(0, len(filenames) - 1)
return load(path, filenames[i].split('.')[0])
def load_latest(path):
# use highest index through natural sorting
try:
filenames = list(filter(lambda f: f.endswith('.png'), os.listdir(path)))
except:
log(f'Failed reading directory {path}, are permissions correct?')
return None
return load(path, filenames[-1].split('.')[0])
def screen_resolution():
try:
xrandr_output = subprocess.run(['xrandr', '-q'], capture_output=True, encoding='ascii')
match = re.search(r'(\d+)x(\d+)*', xrandr_output.stdout)
if not match is None:
return (int(match[1]), int(match[2]))
except:
log('Failed to obtain screen resolution with xrandr')
return None
log('Failed to obtain screen resolution with xrandr')
return None
def lock(path, img_path, args):
cmd = [path, '-i', img_path]
cmd.extend(args)
subprocess.Popen(cmd)
# No return
exit()
def main():
global LOG
parser = argparse.ArgumentParser(
prog = 'xkcdlock-py',
description = 'Launches a locking program with the XKCD comic embedded',
epilog = 'Use at your own risk.')
parser.add_argument('-l', '--lock',
help='Lock program')
parser.add_argument('-d', '--dir',
help='Search directory for downloaded comics')
parser.add_argument('mode',
metavar='MODE',
choices=MODES,
help=f"Which loading strategy to use. Choose between '{MODES[0]}', '{MODES[1]}' and '{MODES[2]}'")
parser.add_argument('-i', '--index',
type=int,
help='In index mode, which index to show. If this option is not given in index mode, the program will default to latest mode.')
parser.add_argument('-f', '--fallback',
metavar='MODE',
choices=MODES,
help='If no internet connection is available, which mode to use instead')
parser.add_argument('-c', '--cooldown',
help='Cooldown time in format "HH:MM" for which no new comics should be downloaded for the given mode')
parser.add_argument('-o', '--offline',
help='Use offline mode',
action='store_true')
parser.add_argument('-v', '--verbose',
help='Verbose logging',
action='store_true')
# Parse arguments
args = parser.parse_args()
if args.verbose:
LOG=True
offline = args.offline
mode = args.mode
path = args.dir
if path is None:
path = '/tmp/xkcdlock-py'
log(f"No directory provided, choosing default directory '{path}'")
else:
path = os.path.expanduser(path)
lock_bin = args.lock
if lock_bin is None: # TODO
lock_bin = DEFAULT_LOCK_BIN[0]
index = args.index
if index is None and mode == 'index':
mode = 'latest'
log('No index provided in \'index\' mode, switching to \'latest\' mode')
fallback = args.fallback
if fallback is None:
fallback = mode
cooldown = DEFAULT_COOLDOWN_TIME
if not args.cooldown is None:
cooldown_match = re.search(r'(\d\d):(\d\d)', args.cooldown)
if cooldown_match is None:
log(f'Failed parsing cooldown {args.cooldown}')
else:
hours = int(cooldown_match[1])
minutes = int(cooldown_match[2])
cooldown = datetime.timedelta(hours=hours, minutes=minutes)
# Create directory
os.makedirs(path, exist_ok = True)
img = None
# Download comic
if not offline:
if mode == 'index':
res = download_index(path, index, cooldown)
elif mode == 'random':
res = download_random(path, cooldown)
else:
res = download_latest(path, cooldown)
if res is None:
log("Download failed, falling back to offline mode '{fallback}'")
mode = fallback
offline = True
else:
(index, img_path, title, tagline) = res
log(f'Successfully loaded comic {index}')
# Load comic in offline mode
if offline:
res = None
if mode == 'index':
res = load_index(path, index)
# fallback if index fails
if (mode == 'index' and res is None) or mode == 'random':
res = load_random(path)
else:
res = load_latest(path)
if res is None:
log('Offline loading failed, falling back to no image')
else:
(index, img_path, title, tagline) = res
log(f'Successfully loaded comic {index}')
# Check Font
font_dir = os.path.expanduser(IMG_FONT_DIR)
font_path = check_font(font_dir)
# Prepare image
resolution = screen_resolution()
if img_path is not None and not resolution is None:
try:
(x, y) = resolution
with Image(filename=img_path) as img:
img.background_color = IMG_BACKGROUND_COLOR
#img.extent(x, y, gravity='center')
# caption title directly above
if not (font_path is None):
try:
img.extent(x, img.height + int(IMG_TITLE_SIZE * 1.5), gravity='south')
font = Font(font_path, color=IMG_TITLE_COLOR, size=IMG_TITLE_SIZE)
img.caption(title, font=font, gravity='north')
except:
log('Error loading font, is the path correct?')
img.extent(x, y, gravity='center')
if not (font_path is None):
try:
font = Font(font_path, color=IMG_CAPTION_COLOR, size=IMG_CAPTION_SIZE)
img.caption(tagline, font=font, gravity='south')
#font = Font(IMG_FONT_PATH, color=IMG_TITLE_COLOR, size=IMG_TITLE_SIZE)
#img.caption(title, font=font, gravity='north')
except:
log('Error loading font, is the path correct?')
#display(img)
img_path = os.path.join(path, 'lock.png')
img.save(filename=img_path)
except:
log('Failed processing image')
lock(lock_bin, img_path, DEFAULT_LOCK_ARGS)
def log(msg):
if LOG:
print(msg, file=sys.stderr)
if __name__ == "__main__":
main()