From e8788bde0818b662c705422baaf9d6276125085a 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 zcmeHJNl2AZ6#g{FCQZvmOS8e|Y_i#GP#esii#9D*Jm87IK=2!e ztzN(cpiB3|R&U@e*CKU|5=PN;e`{9N1&6>x`Z^SK`;jHluQlyl={u z`tMbu!~VTm@|*6<`HxBNzH-!oBj&E60rITjk~DVI$nBbaa>6>-*q^D0m0LA?#NP2) z@)|BnN>PB*Pj~yAx^!vjY>DXTyYV-r8qot{%R-V>h>|uYpB1T#}?NZg2 zF9pqyWOs3}bhp<$=o_lP6-aYOoupUB86S@LFYH(3ZQ~td_n|q*_&HP-Y2qKV&Jt_u z8!2gdE_+Lc%ImK;rzy2sb~U0bTu zwwD{+^cMf9HA-sQOC-DYh%<)c{(4?>bjNnkVCZ>6mSk3KH2J$+y_vYoH0$?j(_}}X zpS!&a{x!~%`g5#z9Abr4rz4*+op<{!*C z$aN5#luWRuVE+KQ3gQps7|2DCiy*(io&|Co;sLz!Ol`+xC&2L9y?B=;ERw!a#N?ok~% zKh6@rU=(Mn`b=LvHpDV4JR!=L3&5nr0r0W(L;tnYR3ExNcA-CYzXi+J4W~XU+J8}O zfMsyZ;=Xg|QC}7Y?>X_7glHd(UP(P^5PRH2%T!Nb;0CW`#AqD#2aH(G{ppjPdgN^C zK}kVf`+-Rz)G=;Vh&dm;g8y68JtGolhFZqVNF2{LXzm|8WyzYQQ-_%Qn+49y;PaHa OxqQf<_F6r!c0U2}z(JM( literal 0 HcmV?d00001 diff --git a/tests/test_tracker_icons.py b/tests/test_tracker_icons.py index 8732ad56e..ae8f7feb2 100644 --- a/tests/test_tracker_icons.py +++ b/tests/test_tracker_icons.py @@ -53,3 +53,10 @@ class TrackerIconsTestCase(unittest.TestCase): d.addCallback(self.assertNotIdentical, None) d.addCallback(self.assertEquals, icon) return d + + def test_get_publicbt_ico(self): + icon = TrackerIcon("../publicbt.ico") + d = icons.get("publicbt.org") + d.addCallback(self.assertNotIdentical, None) + d.addCallback(self.assertEquals, icon) + return d