From d58d0292023287167e6b03a997edd2258c2a9834 Mon Sep 17 00:00:00 2001 From: ryuslash Date: Tue, 30 Mar 2010 15:59:15 +0200 Subject: [PATCH] Seperate download function download function has been seperated from get_thread_links and get_image_links --- download.py | 35 ++++++++++++++++------------------- 1 file changed, 16 insertions(+), 19 deletions(-) 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()