Created
July 29, 2010 15:05
-
-
Save myles/498353 to your computer and use it in GitHub Desktop.
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 python | |
""" | |
A simple script that downloads the first page wallpapers from Simple | |
Desktops <http://simpledesktops.com/browse/>. | |
Copyright (c) 2010, Myles Braithwaite <[email protected]> | |
All rights reserved. | |
Redistribution and use in source and binary forms, with or without | |
modification, are permitted provided that the following conditions | |
are met: | |
* Redistributions of source code must retain the above copyright | |
notice, this list of conditions and the following disclaimer. | |
* Redistributions in binary form must reproduce the above copyright | |
notice, this list of conditions and the following disclaimer in | |
the documentation and/or other materials provided with the | |
distribution. | |
* Neither the name of the Monkey in your Soul nor the names of its | |
contributors may be used to endorse or promote products derived | |
from this software without specific prior written permission. | |
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | |
HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, | |
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, | |
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS | |
OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED | |
AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY | |
WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | |
POSSIBILITY OF SUCH DAMAGE. | |
""" | |
import os | |
import sys | |
import urllib | |
import urllib2 | |
from BeautifulSoup import BeautifulSoup, SoupStrainer | |
__version__ = '0.1' | |
__project_name__ = 'SimpleDesktopDownloader' | |
__project_link__ = 'http://gist.github.com/498353' | |
SIMPLEDESKTOPS_URL = 'http://simpledesktops.com/browse/' | |
divs = SoupStrainer('div') | |
def get_html(url): | |
request = urllib2.Request(url) | |
request.add_header('User-Agent', '%s/%s +%s' % ( | |
__project_name__, __version__, __project_link__ | |
)) | |
opener = urllib2.build_opener() | |
return opener.open(request).read() | |
def get_pages(url): | |
page_num = 1 | |
page_num_urls = [ ] | |
while True: | |
page_url = url + str(page_num) + '/' | |
try: | |
html = get_html(page_url) | |
except urllib2.HTTPError: | |
break | |
page_num = page_num + 1 | |
page_num_urls += [ page_url, ] | |
return page_num_urls | |
def get_wallpaper_urls(html): | |
soup = BeautifulSoup(html, parseOnlyThese=divs) | |
desktops = soup.findAll('div', attrs={ 'class': "desktop" }) | |
wallpaper_urls = [] | |
for desktop in desktops: | |
wallpaper_urls += [ desktop.find('a')['href'], ] | |
return wallpaper_urls | |
def get_wallpaper(wallpaper_url, output_path): | |
filename = wallpaper_url.split("/")[-1] | |
outpath = os.path.join(output_path, filename) | |
urllib.urlretrieve(wallpaper_url, outpath) | |
def main(output_path): | |
wallpaper_urls = [] | |
for page_url in get_pages(SIMPLEDESKTOPS_URL): | |
html = get_html(page_url) | |
wallpaper_urls += get_wallpaper_urls(html) | |
for url in wallpaper_urls: | |
get_wallpaper(url, output_path) | |
if __name__ == "__main__": | |
output_path = sys.argv[-1] | |
main(output_path) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment