forked from simark/pr0nit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pr0nit.py
460 lines (371 loc) · 14.6 KB
/
pr0nit.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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
#!/usr/bin/env python
"""
pr0nit:
Completely standalone, 1-file, cross platform wallpaper switcher. Obtains
wallpaper from reddit. No dependencies other than python and stdlib.
Suggested subreddits:
/r/earthporn, /r/usaporn, /r/cityporn. Works on any
Author: Tom Dignan <tom@tomdignan.com>
Mac OS X Support: James Clarke
License:
Public Domain
Disclaimer:
Neither the original author nor any contributor is responsible
for the results of your use of this program.
"""
import md5
import argparse
import uuid
import os
import json
import urllib2
import time
import sys
import re
from BaseHTTPServer import HTTPServer, BaseHTTPRequestHandler
from SimpleHTTPServer import SimpleHTTPRequestHandler
import webbrowser
if sys.platform == 'darwin':
from Foundation import NSAppleScript
from Cocoa import NSApplication
DEBUG = False
WALLPAPER_CACHE_DIR = "%s/.wallpaper" % os.getenv("HOME")
DEFAULT_SUBREDDIT = "earthporn" # try also, r/usaporn
DEFAULT_WALLPAPER_CMD = "feh --bg-scale"
DEFAULT_FRAME_SPEED = 300
WEB_INTERFACE_PORT = 8080
STATIC_FILE_PORT = 8082
class WebInterface(BaseHTTPRequestHandler):
def _make_wallpaper_html(self):
html = []
wallpaper_map = wallpaper_setter.all_wallpaper_by_subreddit
all_subreddits = wallpaper_map.keys()
all_subreddits.sort()
for subreddit in all_subreddits:
wallpaper = wallpaper_map[subreddit]
html.append('<section id="%s"><h2>%s</h2><br />' % (subreddit, subreddit))
i = 0
for w in wallpaper:
if i % 3 == 0:
html.append('<div class="row-fluid">')
html.append("""
<div class="span4 frame">
<div class="row-fluid">
<div class="span12">
""")
thumbnail_url = "http://localhost:%d/%s/%s" % (STATIC_FILE_PORT, subreddit,
w['thumbnail'])
wallpaper_url = "http://localhost:%d/%s/%s" % (STATIC_FILE_PORT, subreddit,
w['filename'])
html.append('<a class="fancybox_preview" href="%s"><img src="%s"/></a>' %
(wallpaper_url, thumbnail_url))
html.append("""
</div></div>
<br />
<div class="row-fluid">
<div class="span2"></div>
<div class="span3">
<a class="btn preview">Preview</a>
</div>
<div class="span2"></div>
<div class="span3">
<a href="http://localhost:%d?set=%s" class="btn">Set</a>
</div>
<div class="span2"></div>
</div>
""" % (WEB_INTERFACE_PORT, w['path']))
html.append('</div>')
if i % 3 == 2:
html.append('</div><br />')
i += 1
html.append("</section>")
return "\n".join(html)
def _make_wallpaper_nav_html(self):
html = []
clazz = "active"
for subreddit in wallpaper_setter.all_subreddits:
html.append('<li class="%s navlink"><a href="#%s">%s</a></li>' % (clazz, subreddit, subreddit))
clazz = ""
return "\n".join(html)
def _render_index_html(self):
return """<!doctype html>
<html lang="en">
<head>
<title>Pr0nit: a Reddit-Powered Wallpaper Switcher</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link
href="//netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/css/bootstrap-combined.min.css"
rel="stylesheet">
<link
href="//cdnjs.cloudflare.com/ajax/libs/fancybox/2.1.5/jquery.fancybox.min.css"
rel="stylesheet">
<style type="text/css">
#content {
position: relative;
margin-top: 70px;
}
section {
padding-top: 70px;
margin-top: -70px;
}
.frame {
text-align: center;
padding: 10px;
border: 1px solid #eee;
}
</style>
</head>
<body>
<div class="navbar navbar-inverse navbar-fixed-top">
<div class="navbar-inner">
<div class="container">
<button type="button" class="btn btn-navbar" data-toggle="collapse"
data-target=".nav-collapse">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="brand" href="#">Pr0nit</a>
<div class="nav-collapse collapse">
<ul class="nav">
%s
</ul>
</div>
</div>
</div>
</div>
<div class="container" id="content">
%s
</div>
<script src="http://code.jquery.com/jquery.js"></script>
<script src="//netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/js/bootstrap.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/fancybox/2.1.5/jquery.fancybox.min.js"></script>
<script>
$(document).ready(function() {
$(".navlink").click(function() {
$(".navlink").removeClass("active");
$(this).addClass("active");
});
$(".preview").click(function() {
$(this).closest(".row-fluid").siblings().find(".fancybox_preview").click();
});
$(".fancybox_preview").fancybox();
})
</script>
</body>
</html>
""" % (self._make_wallpaper_nav_html(), self._make_wallpaper_html())
def show_index(self):
self.send_response(200)
self.send_header("Content-Type", "text/html")
self.end_headers()
self.wfile.write(self._render_index_html())
self.wfile.close()
def do_GET(self):
m = re.match("/\?set=(.*)", self.path)
if m is None:
self.show_index()
else:
# set wallpaper
wallpaper_path = m.group(1)
self.wallpaper_setter.set_wallpaper(wallpaper_path)
self.show_index()
class RedditWallpaperSetter(object):
def __init__(self, subreddit, cache_dir, frame_speed, monitors=1,
**kwargs):
"""
Called every time the program is started. Ensures that the directory
structure is intact.
"""
self._create_directory_if_not_exists(cache_dir)
self.cache_root_dir = cache_dir
self.cache_dir = os.path.join(cache_dir, subreddit)
self._create_directory_if_not_exists(self.cache_dir)
self.subreddit = "r/" + subreddit
self.frame_speed = frame_speed
self.monitors = monitors
@property
def all_subreddits(self):
subreddits = []
for filename in os.listdir(self.cache_root_dir):
full_path = os.path.join(self.cache_root_dir, filename)
if os.path.isdir(full_path):
subreddits.append(filename)
subreddits.sort()
return subreddits
@property
def all_wallpaper_by_subreddit(self):
wallpaper = {}
for subreddit in self.all_subreddits:
wallpaper[subreddit] = []
path = os.path.join(self.cache_root_dir, subreddit)
for filename in os.listdir(path):
# Don't try to generate thumbnails for thumbnails
if len(filename) == 37:
continue
thumbnail = filename.replace(".jpg", "m.jpg")
wallpaper[subreddit].append(dict(filename=filename,
thumbnail=thumbnail, path=os.path.join(path,filename)))
return wallpaper
def _create_directory_if_not_exists(self, path):
if not os.path.isdir(path):
os.mkdir(path)
def update_wallpaper(self):
"""
Downloads latest wallpaper and displays one at a time, sleeping
at the specified frame speed.
"""
try:
wallpaper_urls = self._get_wallpaper_urls()
except urllib2.HTTPError as e:
print "Retrying in 10 seconds: %r %d" % (e, e.code)
time.sleep(10)
self.run()
return
for url in wallpaper_urls:
try:
self._cache_wallpaper(url, is_thumb=True)
self._cache_wallpaper(url)
except urllib2.HTTPError, e:
print "%r %r skipping %s" % (e, e.code, url)
def slideshow(self):
for filename in os.listdir(self.cache_dir):
self.set_wallpaper(os.path.join(self.cache_dir, filename))
time.sleep(self.frame_speed)
def _get_wallpaper_urls(self):
"""
Downloads the current URLs from the first page of results for
the target subreddit.
"""
url = "http://www.reddit.com/%s/.json" % self.subreddit
headers = { 'User-Agent' : 'Pr0nit/1.0' }
req = urllib2.Request(url, None, headers)
f = urllib2.urlopen(req)
data = json.loads(f.read())['data']
wallpaper_urls = []
for child in data['children']:
url = child['data']['url']
# If it doesn't end in jpg, it probably goes to another HTML page.
# Easiest to just exclude them.
#
# If it's not hosted on imgur, we don't get a free thumbnail. Just
# skip it.
if url.lower().endswith(".jpg") and "imgur.com" in url:
wallpaper_urls.append(url)
return wallpaper_urls
def _cache_wallpaper(self, url, is_thumb=False):
"""
Saves the wallpaper at the target `url` in `cache_dir` but only if
there is no file in the cache which appears to be the target wallpaper
already. The heuristic used is an md5sum of the target wallpaper's url.
"""
if is_thumb:
file_in = urllib2.urlopen(url.replace(".jpg", "m.jpg"))
else:
file_in = urllib2.urlopen(url)
summer = md5.new()
summer.update(url)
filename = summer.hexdigest() + ".jpg"
if is_thumb:
path = os.path.join(self.cache_dir, filename.replace(".jpg",
"m.jpg"))
else:
path = os.path.join(self.cache_dir, filename)
# only create the file if it does not already exist in the cache.
if not os.path.isfile(path):
file_out = open(path, "w")
file_out.write(file_in.read())
file_out.close()
file_in.close()
else:
print "cache hit: %r %r" % (url, path)
return path
def set_wallpaper(self, path):
raise Exception("Not implemented")
class RedditWallpaperSetterLinux(RedditWallpaperSetter):
def set_wallpaper(self, path):
os.system("%s %s" % (DEFAULT_WALLPAPER_CMD, path))
class RedditWallpaperSetterXFCE4(RedditWallpaperSetter):
def set_wallpaper(self, path):
for i in range(self.monitors):
os.system("xfconf-query -c xfce4-desktop -p "
" /backdrop/screen0/monitor%d/image-path -s %s" % (i, path))
class RedditWallpaperSetterOSX(RedditWallpaperSetter):
def set_wallpaper(self, path):
script = NSAppleScript.alloc().initWithSource_("""
tell app "Finder" to set desktop picture to POSIX file "%s"
""" % path)
script.executeAndReturnError_(None)
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="""Set the wallpaper from
reddit""")
parser.add_argument("--subreddit", metavar="<name>", type=str, nargs=1,
default=DEFAULT_SUBREDDIT, help="Target subreddit to scrape for "
" wallpaper.")
parser.add_argument("--monitors", metavar="<number>", type=int, nargs=1,
default=1, help="Number of monitors in your setup (XFCE4 only)")
parser.add_argument("--frame-speed", metavar="<seconds>", type=int,
default=DEFAULT_FRAME_SPEED, nargs=1,
help="Number of seconds to elapse between switching wallpaper.")
parser.add_argument("--wallpaper-dir", metavar="<path>", type=str, nargs=1,
default=WALLPAPER_CACHE_DIR, help="Directory to read/write wallpaper to"
" and from")
parser.add_argument("--platform", metavar="<string>", type=str, nargs=1,
default=sys.platform, help="Target platform, defaults to %r." %
sys.platform)
parser.add_argument("--use-web-interface", action="store_true",
help="Enable local web interface.")
args = parser.parse_args()
get_arg = lambda x : x[0] if not x is None and not isinstance(x, int) \
and not isinstance(x, basestring) else x
use_web_interface = get_arg(args.use_web_interface)
monitors = get_arg(args.monitors)
platform = get_arg(args.platform)
frame_speed = get_arg(args.frame_speed)
subreddit = get_arg(args.subreddit)
wallpaper_dir = get_arg(args.wallpaper_dir)
if platform == "darwin":
# Set the activation policy to NSApplicationActivationPolicyAccessory
# so we don't show the Python dock icon when using PyObjC.
NSApplication.sharedApplication().setActivationPolicy_(2)
wallpaper_setter = RedditWallpaperSetterOSX(subreddit, wallpaper_dir,
frame_speed, monitors=monitors)
elif platform == "xfce4":
wallpaper_setter = RedditWallpaperSetterXFCE4(subreddit, wallpaper_dir,
frame_speed, monitors=monitors)
else:
wallpaper_setter = RedditWallpaperSetterLinux(subreddit, wallpaper_dir,
frame_speed, monitors=monitors)
print "Updating wallpaper selection, please wait..."
wallpaper_setter.update_wallpaper()
print "Done!"
if use_web_interface:
# Use a separate process to kick off the web browser so that it can be
# delayed.
pid2 = os.fork()
if pid2 == 0:
time.sleep(1)
webbrowser.open("http://localhost:%d" % WEB_INTERFACE_PORT)
sys.exit(0)
print "Launching web interface...."
pid = os.fork()
if pid == 0:
# Serve static files in separate process.
os.chdir(wallpaper_dir)
file_server = HTTPServer(("127.0.0.1", STATIC_FILE_PORT), SimpleHTTPRequestHandler)
sa = file_server.socket.getsockname()
print "Serving static files on:", sa[0], "port", sa[1], "..."
file_server.serve_forever()
else:
WebInterface.wallpaper_setter = wallpaper_setter
server = HTTPServer(("127.0.0.1", WEB_INTERFACE_PORT), WebInterface)
server.serve_forever()
# kill the child so it doesn't run away.
os.kill(pid)
os.kill(pid2)
sys.exit(0)
else:
print "Running infinite slideshow..."
# runs here until cancelled.
while True:
wallpaper_setter.slideshow()