/
reddit-bg.py
369 lines (287 loc) · 8.85 KB
/
reddit-bg.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
#!/usr/bin/env python2
# encoding: utf-8
"""
Donwload image from Reddit for wallpapers.
"""
from __future__ import division, print_function, unicode_literals
__author__ = "Parantapa Bhattachara <pb [at] parantapa [dot] net>"
import os
from os.path import join, isdir, isfile
import sys
import time
import json
import random
import signal
import hashlib
from ConfigParser import ConfigParser
from subprocess import call, check_output, CalledProcessError
import yaml
import logbook
import requests
from pypb import abspath
import pypb.awriter as aw
from pypb import register_exit_signals
from pbapps_common import get_i3status_rundir, get_logdir, \
dummy_handler, wake_i3status, \
AttrDict, ICONS, COLORS
MODULE = "reddit-bg"
IMAGE_EXTS = "jpg JPG jpeg JPEG png PNG".split()
SYMB_REDDIT = ICONS.fa_reddit
SYMB_SLEEPING = ICONS.fa_bed
SYMB_GET_IMG_LIST = ICONS.fa_refresh
SYMB_DOWNLOAD_IMG = ICONS.fa_download
log = logbook.Logger(MODULE)
def _req_get(*args, **kwargs):
"""
Do a requests.get in a try catch block to catch all exceptions;
"""
kwargs.setdefault("timeout", 60)
try:
r = requests.get(*args, **kwargs)
return r
except Exception: # pylint: disable=broad-except
log.warn("Received unknown exception", exc_info=True)
return None
def state_update(text, fname):
"""
Write text to file atomically. Text may contain unicode.
"""
block = [{
"full_text": "{}: {}".format(SYMB_REDDIT, text),
"color": COLORS.green
}]
with aw.open(fname, "w") as fobj:
json.dump(block, fobj)
wake_i3status()
def _not_over_18(post):
"""
Chcek not NSFW.
"""
return not post["data"]["over_18"]
def _getimg(post):
"""
Return URL.
"""
return post["data"]["url"]
def _transform_imgur(url):
"""
Transform imgur urls.
"""
for ext in IMAGE_EXTS:
if url.endswith("." + ext):
return url
check = {
"http://imgur.com/a/": False,
"https://imgur.com/a/": False,
"http://imgur.com": True,
"https://imgur.com": True,
}
for prefix, code in check.items():
if url.startswith(prefix):
if code:
return url + ".jpg"
else:
return url
return url
def _is_image_url(url):
"""
Check if url is image url.
"""
for ext in IMAGE_EXTS:
if url.endswith("." + ext):
return True
return False
def _getext(url):
"""
From url get file extension.
"""
for ext in IMAGE_EXTS:
if url.endswith("." + ext):
return ext
return "jpg"
def _is_image_file(fname):
"""
Check if given file is an image file.
"""
if not isfile(fname):
return False
cmd = ["file", fname]
try:
out = check_output(cmd)
except CalledProcessError:
return False
if "PNG image data" in out or "JPEG image data" in out:
return True
return False
def get_subreddit_images(subreddit, user_agent):
"""
Get the images posted in a given subreddit.
subreddit - url of the subreddit's json file
user_agent - user agent strint to use for crawling reddit
"""
headers = {"User-Agent": user_agent}
params = {"limit": 100}
log.info("Getting pics from /r/{} ...", subreddit)
urlfmt = "http://www.reddit.com/r/{}/.json".format
posts = _req_get(urlfmt(subreddit), headers=headers, params=params)
if posts is None or posts.status_code != 200:
log.warn("Getting pics from /r/{} failed!", subreddit)
return []
try:
posts = posts.json()["data"]["children"]
posts = filter(_not_over_18, posts)
urls = map(_getimg, posts)
urls = map(_transform_imgur, urls)
urls = filter(_is_image_url, urls)
except (ValueError, KeyError):
return []
return list(urls)
def download_image(url, fname):
"""
Download the image in url to the given file.
"""
log.info("Downloading image from {} ...", url)
resp = _req_get(url)
if resp is not None and resp.status_code == 200:
log.info("Saving to {} ...", fname)
with open(fname, "wb") as fobj:
fobj.write(resp.content)
return True
log.warn("Downloading failed!")
return False
def set_nitrogen_bg(cfname, screen, ifname, mode="auto", bgcolor=None):
"""
Set the nitrogen background image.
cfname - Location for nitrogen's configuration
screen - Where to set the wall paper: fullscreen, screen1, screen2
ifname - Image fname to use as background image
mode - scaled, auto, centered
bgcolor - Background color for the desktop
"""
log.info("Setting background image in nitrogen ...")
# Setup the proper value of screen
screen = {
"fullscreen" : ":0.0",
"screen1" : "xin_0",
"screen2" : "xin_1"
}[screen]
# Setup poper value for mode
mode = {
"scaled" : 0,
"auto" : 4,
"centered" : 2
}[mode]
mode = str(mode)
# Read the config
conf = ConfigParser()
conf.read(cfname)
# Fix in the stuff to fill in
conf.set(screen, "file", ifname)
conf.set(screen, "mode", mode)
if bgcolor is not None:
conf.set(screen, "bgcolor", bgcolor)
# Write back config
with open(cfname, "w") as fobj:
conf.write(fobj)
# Call nitrogen restore to update
call(["nitrogen", "--restore"])
def set_background(screen, mode, cfg, statefile):
"""
Try to set the desktop background.
"""
# Location of the state file
log.info("Getting image list ...")
state_update(SYMB_GET_IMG_LIST, statefile)
urls = []
for sub in cfg.wallpaper_subreddits:
us = get_subreddit_images(sub, cfg.user_agent)
urls.extend(us)
urls = set(urls)
# Load seen urls file
log.info("Checking which urls have already been seen ...")
if isfile(cfg.seenurls_fname):
with open(cfg.seenurls_fname) as fobj:
seen_urls = set(json.load(fobj))
else:
seen_urls = set()
# Create unseen urls list
unseen_urls = urls - seen_urls
if not unseen_urls:
unseen_urls = urls
unseen_urls = list(unseen_urls)
random.shuffle(unseen_urls)
for url in unseen_urls:
seen_urls.add(url)
# Create a unique filename for the url
uhash = hashlib.sha1(url.encode("utf-8")).hexdigest()
fname = "%s.%s" % (uhash, _getext(url))
fname = join(cfg.save_dir, fname)
# Download the image
state_update(SYMB_DOWNLOAD_IMG, statefile)
if not download_image(url, fname):
continue
if not _is_image_file(fname):
continue
# Set background
set_nitrogen_bg(cfg.nitrogen_conf_fname, screen, fname, mode)
break
# Dump updated seen urls
log.info("Writing down list of checked urls ...")
with aw.open(cfg.seenurls_fname, "w") as fobj:
json.dump(sorted(seen_urls), fobj)
def read_cfg(fname):
"""
Read the config.
"""
with open(fname, "r") as fobj:
cfg = yaml.load(fobj)
cfg = AttrDict(cfg)
cfg.save_dir = abspath(cfg.save_dir)
cfg.seenurls_fname = abspath(cfg.seenurls_fname)
cfg.nitrogen_conf_fname = abspath(cfg.nitrogen_conf_fname)
return cfg
def do_main(statefile):
"""
Run the actual code.
"""
try:
_, cfname = sys.argv # pylint: disable=unbalanced-tuple-unpacking
except ValueError:
print("Usage: ./reddit-bg.py <config.yaml>")
sys.exit(1)
cfname = abspath(cfname)
# Load config
cfg = read_cfg(cfname)
# Create the savedir if not exists
if not isdir(cfg.save_dir):
log.info("Creating image saving directory {} ...", cfg.save_dir)
os.makedirs(cfg.save_dir, 0o700)
while True:
# Reload config
cfg = read_cfg(cfname)
# Try to get set background
for screen, mode in cfg.screens:
set_background(screen, mode, cfg, statefile)
# Go to sleep
log.info("Next update after {} minutes.", cfg.update_interval)
state_update(SYMB_SLEEPING, statefile)
time.sleep(cfg.update_interval * 60)
def main():
prio = 30
# Setup logfile
logfile = "%s/%s.log" % (get_logdir(), MODULE)
logbook.FileHandler(logfile).push_application()
with log.catch_exceptions():
# Get the external state directory
extdir = get_i3status_rundir()
# Write out my own pid
pidfile = "%s/%d%s.pid" % (extdir, prio, MODULE)
with open(pidfile, "w") as fobj:
fobj.write(str(os.getpid()))
register_exit_signals()
signal.signal(signal.SIGUSR1, dummy_handler)
# File to write block info
blockfile = "%s/%d%s.block" % (extdir, prio, MODULE)
do_main(blockfile)
if __name__ == '__main__':
main()