Created
December 3, 2016 09:03
-
-
Save jvanmelckebeke/206e0bcd43911310cce9a32795d4c33d to your computer and use it in GitHub Desktop.
small script to update wallpaper with thepaperwall.com wallpaper of the day
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import urllib.request as urllib2 | |
from lxml import etree | |
import sys | |
import getpass | |
def downl(): | |
url = "http://www.thepaperwall.com/" | |
response = urllib2.urlopen(url) | |
print("response code: " + str(response.getcode())) | |
if response.getcode() != 200: | |
raise urllib2.URLError | |
htmlparser = etree.HTMLParser() | |
tree = etree.parse(response, htmlparser) | |
a = (tree.xpath('//*[@id="main_leftcol"]/div[2]/div/div[1]/a/@href')[0]) | |
url += a[0:]; | |
response = urllib2.urlopen(url) | |
tree = etree.parse(response, htmlparser) | |
b = tree.xpath('/html/body/div[3]/div[1]/a/img[1]/@src')[0] | |
url = "http://www.thepaperwall.com"+b | |
u = urllib2.urlopen(url) | |
f = open("wallpaper.jpg", 'wb') | |
meta = u.info() | |
print(meta) | |
file_size = int(meta["Content-Length"]) | |
print("Downloading: {0} Bytes: {1}".format(url, file_size)) | |
file_size_dl = 0 | |
block_sz = 8192 | |
while True: | |
buffer = u.read(block_sz) | |
if not buffer: | |
break | |
file_size_dl += len(buffer) | |
f.write(buffer) | |
p = float(file_size_dl) / file_size | |
status = r"[{0:.2%}]".format(p) | |
status = status + chr(8)*(len(status)+1) | |
sys.stdout.write(status) | |
f.close() | |
def connected(): | |
try: | |
urllib2.urlopen('http://216.58.192.142', timeout=1) | |
return True | |
except urllib2.URLError as err: | |
return False | |
while not connected(): | |
try: | |
downl() | |
except urllib2.URLError as err2: | |
continue | |
downl() | |
print("\ndone") | |
import os | |
os.system("gsettings set org.gnome.desktop.background picture-uri file:///home/" + getpass.getuser() + "/programs/wallpaper.jpg") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment