# # tracker_icons.py # # Copyright (C) 2008 Martijn Voncken # # Deluge is free software. # # You may redistribute it and/or modify it under the terms of the # GNU General Public License, as published by the Free Software # Foundation; either version 3 of the License, or (at your option) # any later version. # # deluge is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. # See the GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with deluge. If not, write to: # The Free Software Foundation, Inc., # 51 Franklin Street, Fifth Floor # Boston, MA 02110-1301, USA. # # In addition, as a special exception, the copyright holders give # permission to link the code of portions of this program with the OpenSSL # library. # You must obey the GNU General Public License in all respects for all of # the code used other than OpenSSL. If you modify file(s) with this # exception, you may extend this exception to your version of the file(s), # but you are not obligated to do so. If you do not wish to do so, delete # this exception statement from your version. If you delete this exception # statement from all source files in the program, then also delete it here. # # import threading from twisted.internet import reactor from urllib import urlopen from deluge.log import LOG as log from deluge.common import get_pixmap import os import deluge.configmanager import deluge.component as component #some servers don't have their favicon at the expected location RENAMES = { "legaltorrents.com":"beta.legaltorrents.com", "aelitis.com":"www.vuze.com" } VALID_ICO_TYPES = ["octet-stream", "x-icon", "image/vnd.microsoft.icon", "vnd.microsoft.icon", "plain"] VALID_PNG_TYPES = ["octet-stream", "png"] def fetch_url(url, valid_subtypes=None): """ returns: data or None """ try: url_file = urlopen(url) data = url_file.read() #validate: if valid_subtypes and (url_file.info().getsubtype() not in valid_subtypes): raise Exception("Unexpected type for %s : %s" % (url, url_file.info().getsubtype())) if not data: raise Exception("No data") except Exception, e: log.debug("%s %s" % (url, e)) return None return data class TrackerIcons(component.Component): def __init__(self): component.Component.__init__(self, "TrackerIcons") #set image cache dir self.image_dir = os.path.join(deluge.configmanager.get_config_dir(), "icons") if not os.path.exists(self.image_dir): os.mkdir(self.image_dir) #self.images : {tracker_host:filename} self.images = {"DHT":get_pixmap("dht16.png" )} #load image-names in cache-dir for icon in os.listdir(self.image_dir): if icon.endswith(".ico"): self.images[icon[:-4]] = os.path.join(self.image_dir, icon) if icon.endswith(".png"): self.images[icon[:-4]] = os.path.join(self.image_dir, icon) def _fetch_icon(self, tracker_host): """ returns (ext, data) """ host_name = RENAMES.get(tracker_host, tracker_host) #HACK! ico = fetch_url("http://%s/favicon.ico" % host_name, VALID_ICO_TYPES) if ico: return ("ico", ico) png = fetch_url("http://%s/favicon.png" % host_name, VALID_PNG_TYPES) if png: return ("png", png) # FIXME: This should be cleaned up and not copy the top code try: html = urlopen("http://%s/" % (host_name,)) except Exception, e: log.debug(e) html = None if html: icon_path = "" line = html.readline() while line: if '