Skip to content

Instantly share code, notes, and snippets.

@pao
Created January 20, 2012 06:17
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pao/1645701 to your computer and use it in GitHub Desktop.
Save pao/1645701 to your computer and use it in GitHub Desktop.
Script to batch download additional digital materials for Artificial Heart Level 4.
'''
Usage: python getHeart.py YOURCODE your@email.com someregex
The "someregex" matches against the link text.
For instance:
* "flac" will get all the FLAC downloads
* "flac|VIDEO" will get FLAC and video
and so on.
'''
import os
import re
import sys
from urllib import url2pathname
import mechanize
CHUNK = 16 * 1024
br = mechanize.Browser()
br.open('http://www.atozmedia.com/soundcard')
br.select_form([f for f in br.forms()][0].name)
br['downloadCode'] = sys.argv[1]
br['email'] = sys.argv[2]
br.submit()
for link in list(br.links(text_regex=sys.argv[3])):
# Run `watch ls -l' in the folder you're downloading to for status
resp = br.follow_link(link)
filename = url2pathname(resp.geturl().split('/')[-1])
if os.path.exists(filename) and os.path.getsize(filename) == int(resp.info()['content-length']):
br.back()
continue
with open(filename, 'wb') as dlfile:
for chunk in iter(lambda: resp.read(CHUNK), ''):
if not chunk: break
dlfile.write(chunk)
br.back()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment