Changeset 6bb4559


Ignore:
Timestamp:
05/08/2010 06:26:08 AM (15 years ago)
Author:
John Garland <johnnybg+deluge@gmail.com>
Children:
8f021c
Parents:
7c9eea
git-author:
John Garland <johnnybg+deluge@gmail.com> (05/08/2010 06:24:16 AM)
git-committer:
John Garland <johnnybg+deluge@gmail.com> (05/08/2010 06:26:08 AM)
Message:

Make host_to_url support redirection and add another test

Files:
1 added
2 edited

Legend:

Unmodified
Added
Removed
  • deluge/ui/tracker_icons.py

    r7c9eea r6bb4559  
    153153
    154154        self.pending = {}
     155        self.redirects = {}
    155156
    156157    def get(self, host):
     
    203204        """
    204205        if not url:
    205             url = host_to_url(host)
     206            url = self.host_to_url(host)
    206207        log.debug("Downloading %s", url)
    207208        return download_file(url, mkstemp()[1], force_filename=True)
     
    236237        if f.check(error.PageRedirect):
    237238            # Handle redirect errors
    238             location = urljoin(host_to_url(host), error_msg.split(" to ")[1])
     239            location = urljoin(self.host_to_url(host), error_msg.split(" to ")[1])
     240            self.redirects[host] = url_to_host(location)
    239241            d = self.download_page(host, url=location)
    240242            d.addCallbacks(self.on_download_page_complete, self.on_download_page_fail,
     
    276278        """
    277279        log.debug("Got icons for %s: %s", host, icons)
    278         url = host_to_url(host)
     280        url = self.host_to_url(host)
    279281        icons = [(urljoin(url, icon), mimetype) for icon, mimetype in icons]
    280282        log.debug("Icon urls: %s", icons)
     
    346348        if f.check(error.PageRedirect):
    347349            # Handle redirect errors
    348             location = urljoin(host_to_url(host), error_msg.split(" to ")[1])
     350            location = urljoin(self.host_to_url(host), error_msg.split(" to ")[1])
    349351            d = self.download_icon([(location, extension_to_mimetype(location.rpartition('.')[2]))] + icons, host)
    350352            if not icons:
     
    355357        elif f.check(IndexError, HTMLParseError):
    356358            # No icons, try favicon.ico as an act of desperation
    357             d = self.download_icon([(urljoin(host_to_url(host), "favicon.ico"), extension_to_mimetype("ico"))], host)
     359            d = self.download_icon([(urljoin(self.host_to_url(host), "favicon.ico"), extension_to_mimetype("ico"))], host)
    358360            d.addCallbacks(self.on_download_icon_complete, self.on_download_icon_fail,
    359361                           callbackArgs=(host,), errbackArgs=(host,))
     
    404406        del self.pending[host]
    405407        return icon
     408
     409    def host_to_url(self, host):
     410        """
     411        Given a host, returns the URL to fetch
     412
     413        :param host: the tracker host
     414        :type host: string
     415        :returns: the url of the tracker
     416        :rtype: string
     417        """
     418        if host in self.redirects:
     419            host = self.redirects[host]
     420        return "http://%s/" % host
    406421
    407422################################ HELPER CLASSES ###############################
     
    451466############################### HELPER FUNCTIONS ##############################
    452467
    453 def host_to_url(host):
    454     """
    455     Given a host, returns the URL to fetch
    456 
    457     :param host: the tracker host
    458     :type host: string
    459     :returns: the url of the tracker
    460     :rtype: string
    461     """
    462     return "http://%s/" % host
    463 
    464468def url_to_host(url):
    465469    """
  • tests/test_tracker_icons.py

    r7c9eea r6bb4559  
    5353        d.addCallback(self.assertEquals, icon)
    5454        return d
     55
     56    def test_get_publicbt_ico(self):
     57        icon = TrackerIcon("../publicbt.ico")
     58        d = icons.get("publicbt.org")
     59        d.addCallback(self.assertNotIdentical, None)
     60        d.addCallback(self.assertEquals, icon)
     61        return d
Note: See TracChangeset for help on using the changeset viewer.