Skip to content

Instantly share code, notes, and snippets.

@tkcranny
Created June 6, 2013 11:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tkcranny/5720842 to your computer and use it in GitHub Desktop.
Save tkcranny/5720842 to your computer and use it in GitHub Desktop.
Simple script to retreive the highest rated (1080p) image from reddit.com/r/wallpapers and apply it as the windows desktop image.
import urllib2, ctypes
import os, re
from time import sleep
__author__ = "Thomas Cranny"
__version__ = "1.0"
def CheckConnection(checks=5*60, delay=1):
for i in range(checks):
try:
response=urllib2.urlopen("http://google.com",timeout=1)
return True
except urllib2.URLError as err:
pass
return False
def GetContent():
site = "http://www.reddit.com/r/wallpapers"
req = urllib2.urlopen(site)
data = ""
try:
data = req.read()
except Exception, e:
print("Error: Could not open site > {0}".format(site))
return data
def DownloadImage(link):
fname = "dl" + re.search("\.\w*$",link).group(0)
f = open(fname, "wb")
f.write(urllib2.urlopen(link).read())
f.close()
return fname
def FindFirstImageLink(data):
link = re.search("(http://i\.imgur\.com/\w{4,10}\.(?:jpg|png))\" >.{5,100}\[1920x1080\]", data)
if link:
link = link.group(0)
link = re.search("http://i\.imgur\.com/\w{4,10}\.(?:jpg|png)", link)
link = link.group(0)
else:
print("Error: No Imgur links found")
return link
def SetDesktopBackground(image, rem=True):
SPI_SETDESKWALLPAPER = 20
ctypes.windll.user32.SystemParametersInfoA(SPI_SETDESKWALLPAPER, 0, image, 0)
if rem: os.remove(image)
def Run():
if not CheckConnection():
print("Could not establish a connection, terminating")
return
print("Connecting to Reddit.com...")
content = GetContent()
print("Extacting best 1080x1920 image...")
link = FindFirstImageLink(content)
print("Retreiving image...")
image = DownloadImage(link)
print("Applying Desktop...")
SetDesktopBackground(image)
print("Done!")
Run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment