diff --git a/download.py b/download.py index 60ec62a..930d27f 100644 --- a/download.py +++ b/download.py @@ -1,5 +1,3 @@ -#!/usr/bin/env python - ###################################################################### # Copyright 2009, 2010 ryuslash # @@ -49,6 +47,18 @@ class Downloader(object): def set_on_downloaded(self, on_downloaded): self.on_downloaded = on_downloaded + def download(self, url): + f = None + tries = 10 + while tries > 0: + try: + f = urllib.urlopen(url) + break + except IOError: + tries -= 1 + write("\rTry of %s failed, %d tries left" % (url, tries)) + return f + def get_thread_links(self, baseurl): myparser = htmlparser.MyParser() i = 0 @@ -57,14 +67,8 @@ class Downloader(object): while code != 404: url = baseurl + str(i) - tries = 10 - while tries > 0: - try: - f = urllib.urlopen(url) - break - except IOError: - tries -= 1 - write("\rTry of %s failed, %d tries left" % (url, tries)) + f = self.download(url) + if not f is None: code = f.getcode() if code == 404: @@ -95,15 +99,8 @@ class Downloader(object): progress.show_progress(i) img_url = baseurl + link - tries = 10 - while tries > 0: - try: - f = urllib.urlopen(img_url) - break - except IOError: - tries -= 1 - write("\rTry of %s failed, %d tries left" \ - % (img_url, tries)) + f = self.download(img_url) + if not f is None: s = f.read() f.close()