From 6bb4559d18d9541fa4abcb1611c7996afbc4bcf6 Mon Sep 17 00:00:00 2001 From: John Garland Date: Sat, 8 May 2010 16:24:16 +1000 Subject: [PATCH] Make host_to_url support redirection and add another test --- deluge/ui/tracker_icons.py | 36 ++++++++++++++++++++---------------- tests/publicbt.ico | Bin 0 -> 2550 bytes tests/test_tracker_icons.py | 7 +++++++ 3 files changed, 27 insertions(+), 16 deletions(-) create mode 100644 tests/publicbt.ico diff --git a/deluge/ui/tracker_icons.py b/deluge/ui/tracker_icons.py index e9c23ab05..d4823e5e4 100644 --- a/deluge/ui/tracker_icons.py +++ b/deluge/ui/tracker_icons.py @@ -152,6 +152,7 @@ class TrackerIcons(Component): self.icons[None] = None self.pending = {} + self.redirects = {} def get(self, host): """ @@ -202,7 +203,7 @@ class TrackerIcons(Component): :rtype: Deferred """ if not url: - url = host_to_url(host) + url = self.host_to_url(host) log.debug("Downloading %s", url) return download_file(url, mkstemp()[1], force_filename=True) @@ -235,7 +236,8 @@ class TrackerIcons(Component): d = f if f.check(error.PageRedirect): # Handle redirect errors - location = urljoin(host_to_url(host), error_msg.split(" to ")[1]) + location = urljoin(self.host_to_url(host), error_msg.split(" to ")[1]) + self.redirects[host] = url_to_host(location) d = self.download_page(host, url=location) d.addCallbacks(self.on_download_page_complete, self.on_download_page_fail, errbackArgs=(host,)) @@ -275,7 +277,7 @@ class TrackerIcons(Component): :rtype: list """ log.debug("Got icons for %s: %s", host, icons) - url = host_to_url(host) + url = self.host_to_url(host) icons = [(urljoin(url, icon), mimetype) for icon, mimetype in icons] log.debug("Icon urls: %s", icons) return icons @@ -345,7 +347,7 @@ class TrackerIcons(Component): d = f if f.check(error.PageRedirect): # Handle redirect errors - location = urljoin(host_to_url(host), error_msg.split(" to ")[1]) + location = urljoin(self.host_to_url(host), error_msg.split(" to ")[1]) d = self.download_icon([(location, extension_to_mimetype(location.rpartition('.')[2]))] + icons, host) if not icons: d.addCallbacks(self.on_download_icon_complete, self.on_download_icon_fail, @@ -354,7 +356,7 @@ class TrackerIcons(Component): d = self.download_icon(icons, host) elif f.check(IndexError, HTMLParseError): # No icons, try favicon.ico as an act of desperation - d = self.download_icon([(urljoin(host_to_url(host), "favicon.ico"), extension_to_mimetype("ico"))], host) + d = self.download_icon([(urljoin(self.host_to_url(host), "favicon.ico"), extension_to_mimetype("ico"))], host) d.addCallbacks(self.on_download_icon_complete, self.on_download_icon_fail, callbackArgs=(host,), errbackArgs=(host,)) else: @@ -404,6 +406,19 @@ class TrackerIcons(Component): del self.pending[host] return icon + def host_to_url(self, host): + """ + Given a host, returns the URL to fetch + + :param host: the tracker host + :type host: string + :returns: the url of the tracker + :rtype: string + """ + if host in self.redirects: + host = self.redirects[host] + return "http://%s/" % host + ################################ HELPER CLASSES ############################### class FaviconParser(HTMLParser): @@ -450,17 +465,6 @@ class FaviconParser(HTMLParser): ############################### HELPER FUNCTIONS ############################## -def host_to_url(host): - """ - Given a host, returns the URL to fetch - - :param host: the tracker host - :type host: string - :returns: the url of the tracker - :rtype: string - """ - return "http://%s/" % host - def url_to_host(url): """ Given a URL, returns the host it belongs to diff --git a/tests/publicbt.ico b/tests/publicbt.ico new file mode 100644 index 0000000000000000000000000000000000000000..54b10a810eaa022bb1b33383efa7682f390487ea GIT binary patch literal 2550 zcmZQzU}Ruo5D);-3Je)63=C=v3{XA?Lk24YLmvwRg9b>9fq_AR0iuq9Mj-PhD?{c@ z*1efGS^uNs{M!N;Vo)_8^z(x2>;I)MTmHZQ{q#QweEj|V|K%@R|4)75k8ajp zkO5Hh{^#A|{eS4g%>SSNy!yZPRoVZx2c{tH|9c-f{$Ks7`2WX0Fa96>F#mu4Eq=H; z$oluboACeJ-w*$*?ka-iLFR!BD8DNU=2zU6`~T(7oBs#j&j9m5_QLe{JhK1){qM*B zPk&tgfBoy8|An`O!R~?S2ZaO356^#I|Nr&RyZ;j&yMfJs=|A>y!Tt9!a)6kI*bN|nJ8V2(I|M}13{~!7= z3oO3*O~e1}n;hu+L3sf#2C`@W`z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