move tracker_icons

This commit is contained in:
Martijn Voncken 2008-10-27 00:26:39 +00:00
parent 0f94cf2316
commit 1c121a8c17
1 changed files with 76 additions and 21 deletions

View File

@ -45,7 +45,27 @@ RENAMES = {
"aelitis.com":"www.vuze.com" "aelitis.com":"www.vuze.com"
} }
VALID_TYPES = ["octet-stream","x-icon"] VALID_ICO_TYPES = ["octet-stream","x-icon"]
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 %s" % (url, e, e.message))
return None
return data
class TrackerIcons(object): class TrackerIcons(object):
def __init__(self): def __init__(self):
@ -61,6 +81,31 @@ class TrackerIcons(object):
for icon in os.listdir(self.image_dir): for icon in os.listdir(self.image_dir):
if icon.endswith(".ico"): if icon.endswith(".ico"):
self.images[icon[:-4]] = os.path.join(self.image_dir, icon) 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)
"""
TODO: need a test-site first...
html = fetch_url("http://%s/" % (host_name,))
if html:
for line in html:
print line
"""
return (None, None)
def _fetch_icon_thread(self, tracker_host, callback): def _fetch_icon_thread(self, tracker_host, callback):
""" """
@ -69,22 +114,12 @@ class TrackerIcons(object):
calls callback on sucess calls callback on sucess
assumes dicts,urllib and logging are threadsafe. assumes dicts,urllib and logging are threadsafe.
""" """
try:
host_name = RENAMES.get(tracker_host, tracker_host)
icon = urlopen("http://%s/favicon.ico" % host_name)
icon_data = icon.read()
#validate icon: ext, icon_data = self._fetch_icon(tracker_host)
if icon.info().getsubtype() not in VALID_TYPES:
raise Exception("Unexpected type: %s" % icon.info().getsubtype())
if not icon_data:
raise Exception("No data")
except Exception, e:
log.debug("%s %s %s" % (tracker_host, e, e.message))
return False
filename = os.path.join(get_default_config_dir("icons"),"%s.ico" % tracker_host)
if icon_data:
filename = os.path.join(get_default_config_dir("icons"),"%s.%s" % (tracker_host, ext))
print filename
f = open(filename,"wb") f = open(filename,"wb")
f.write(icon_data) f.write(icon_data)
f.close() f.close()
@ -111,4 +146,24 @@ class TrackerIcons(object):
self.get_async(tracker_host, None) self.get_async(tracker_host, None)
return None return None
if __name__ == "__main__":
import time
def del_old():
filename = os.path.join(get_default_config_dir("icons"),"legaltorrents.com.ico")
if os.path.exists(filename):
os.remove(filename)
def test_get():
del_old()
trackericons = TrackerIcons()
print trackericons.images
print trackericons.get("unknown2")
print trackericons.get("ntorrents.net")
print trackericons.get("google.com")
print trackericons.get("legaltorrents.com")
time.sleep(5.0)
print trackericons.get("legaltorrents.com")
test_get()
#test_async()