Skip to content

Instantly share code, notes, and snippets.

@tfausak
Created March 14, 2011 15:48
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save tfausak/869349 to your computer and use it in GitHub Desktop.
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
try:
resolution = '{0}x{1}'.format(int(argv[1]), int(argv[2]))
except (IndexError, ValueError):
stdout.write('Usage: update.py X Y\n')
exit(1)
# Set up the URL formats for retrieving content
base_url = 'http://interfacelift.com/'
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'}):
try:
page = int(tag.string)
last_page = max(last_page, page)
except ValueError:
pass
for page in range(1, last_page + 1):
stdout.write('Page {0} of {1}...\n'.format(page, last_page))
stdout.flush()
# 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:
continue
(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):
continue
stdout.write('\tRetrieving {0}...\n'.format(fn))
stdout.flush()
urlretrieve(url, fn)
# Nap sneakily
sleep(1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment