forked from seveas/hacks
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwallpaper.py
More file actions
executable file
·66 lines (58 loc) · 2.24 KB
/
wallpaper.py
File metadata and controls
executable file
·66 lines (58 loc) · 2.24 KB
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
#!/usr/bin/python
#
# Grab a random desktop background and set it as background. Requirements: xrandr, gnome, PIL
#
# (c) 2010-2013 Dennis Kaarsemaker
import cStringIO as stringio
import glob
import os
from PIL import Image
import random
import re
import stat
import stealenv
import sys
import requests
from whelk import shell
requests.utils.default_user_agent = lambda: 'Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:20.0) Gecko/20100101 Firefox/20.0'
download_path = '/home/dennis/Pictures/wallpapers'
class DownloadError(Exception):
pass
class IfLift(object):
index = "aHR0cDovL2ludGVyZmFjZWxpZnQuY29tL3dhbGxwYXBlci9kb3dubG9hZHMvZGF0ZS9hbnkv==".decode('base64')
download_base = "aHR0cDovL2ludGVyZmFjZWxpZnQuY29tL3dhbGxwYXBlci83eXo0bWExLw==".decode('base64')
def random_image(self, resolution):
html = re.sub('<.*?>', '', requests.get(self.index).text)
pages = re.search("page \d+ of (\d+)", html).group(1)
rand = random.randint(1, int(pages))
html = requests.get("%sindex%d.html" % (self.index, rand)).text
images = re.findall('<img[^>]*previews/(.*?)"', html)
name,ext = random.choice(images).rsplit('.', 1)
name = '%s_%s.%s' % (name, resolution, ext)
dpath = os.path.join(download_path, name)
if not os.path.exists(dpath):
img = requests.get(self.download_base + name).content
Image.open(stringio.StringIO(img))
with open(dpath, 'w') as fd:
fd.write(img)
return dpath
def main():
uid = stealenv.from_name('/usr/bin/nautilus')
resolution = re.search(r"current\s+(\d+ x \d+)", shell.xrandr().stdout).group(1).replace(' ', '')
path = None
if len(sys.argv) == 2:
path = sys.argv[1]
else:
klass = IfLift # In the future there could be more classes
try:
path = klass().random_image(resolution)
except DownloadError:
pictures = glob.glob(os.path.join(download_path, '*_%s.*' % resolution))
if pictures:
path = random.choice(pictures)
if path:
shell.gsettings("set", "org.gnome.desktop.background", "picture-uri", "file://" + os.path.abspath(path))
else:
print "No image found"
if __name__ == '__main__':
main()