Created
November 5, 2011 00:22
-
-
Save EntityReborn/1340856 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
import os, sys | |
import traceback | |
import mechanize | |
KILOBYTE = 1024 | |
MEGABYTE = KILOBYTE * KILOBYTE | |
BUFFER = MEGABYTE / 2 | |
class Downloader(object): | |
def __init__(self): | |
self._browser = None | |
def browser(self): | |
if not self._browser: | |
self._browser = mechanize.Browser() | |
return self._browser | |
def open(self, url, retry=False, timeout=5): | |
while True: | |
try: | |
self.response = self.browser().open(url, timeout=timeout) | |
except Exception as e: | |
if retry: | |
continue | |
else: | |
return None | |
return self.response | |
def download(self, outfile=None): | |
totalbytes = 0 | |
if not outfile: | |
outfile = os.tmpfile() | |
while True: | |
# Read bit by bit, to keep memory usage down. | |
data = self.response.read(BUFFER) | |
if not data: | |
done = True | |
break | |
outfile.write(data) | |
totalbytes += len(data) | |
print "Got %d bytes, %d total." % (len(data), totalbytes) | |
outfile.seek(0) | |
return outfile |
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
from simpdownloader import Downloader, BUFFER | |
def main(): | |
dl = Downloader() | |
filename = "ubuntu.iso" | |
url = "http://www.ubuntu.com/start-download?distro=desktop&bits=32&release=latest" | |
try: | |
dl.open(url, retry=True) | |
tmpfile = dl.download() | |
except Exception as e: | |
print "Error downloading '%s': %s" % (outfile, e.message) | |
else: | |
if tmpfile: | |
with open(filename, "wb") as newfile: | |
while True: | |
data = tmpfile.read(BUFFER) | |
if not data: | |
break | |
newfile.write(data) | |
tmpfile.close() | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment