Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Downloads all the wallpapers on InterfaceLIFT.
#!/usr/bin/env python
from BeautifulSoup import BeautifulSoup
from urllib2 import urlopen
from re import compile, match
from urllib import urlretrieve
from sys import argv, stdout
from os.path import isfile
from time import sleep
# Get the resolution from the command line
resolution = '{0}x{1}'.format(int(argv[1]), int(argv[2]))
except (IndexError, ValueError):
stdout.write('Usage: X Y\n')
# Set up the URL formats for retrieving content
base_url = ''
page_url = base_url + 'wallpaper/downloads/date/any/index{0}.html'
file_name = '{0:05}_{1}_{2}.jpg'
download_url = base_url + 'wallpaper/grab/{0}'
# Compile a regex for getting IDs and slugs
pattern = compile(r"^javascript:imgload\('([a-z]+)', this,'([0-9]+)'\)$")
# Grab the first page
url = page_url.format('')
doc = urlopen(url)
soup = BeautifulSoup(doc)
# Find out how many pages there are
last_page = -1
for tag in soup.findAll('a', attrs={'class': 'selector'}):
page = int(tag.string)
last_page = max(last_page, page)
except ValueError:
for page in range(1, last_page + 1):
stdout.write('Page {0} of {1}...\n'.format(page, last_page))
# Grab the page
url = page_url.format(page)
doc = urlopen(url)
soup = BeautifulSoup(doc)
# Find each image's <select> tag
for tag in soup.findAll('select', onchange=True):
matches = match(pattern, tag['onchange'])
if not matches:
(slug, id) = matches.groups()
# Build a local file name and remote URL
fn = file_name.format(int(id), slug, resolution)
url = download_url.format(fn)
# Download the image
if isfile(fn):
stdout.write('\tRetrieving {0}...\n'.format(fn))
urlretrieve(url, fn)
# Nap sneakily
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment