From 33b4d2a832edf09a5ee26f6508d6e762eefaa119 Mon Sep 17 00:00:00 2001 From: Oleg Broytman Date: Mon, 31 Dec 2007 11:12:35 +0000 Subject: [PATCH] Cache icons. git-svn-id: file:///home/phd/archive/SVN/bookmarks_db/trunk@153 fdd5c36f-1aea-0310-aeeb-c58d7e2b6c23 --- Robots/bkmk_rsimple.py | 64 +++++++++++++++++++++++++----------------- 1 file changed, 39 insertions(+), 25 deletions(-) diff --git a/Robots/bkmk_rsimple.py b/Robots/bkmk_rsimple.py index f1d8bb3..9613582 100644 --- a/Robots/bkmk_rsimple.py +++ b/Robots/bkmk_rsimple.py @@ -92,6 +92,9 @@ def get_welcome(): return _welcome +icons = {} # Icon cache; maps URL to a tuple (content type, data) + # or None if there is no icon. + class robot_simple(Robot): def check_url(self, bookmark): if not self.tempfname: @@ -182,34 +185,45 @@ class robot_simple(Robot): icon = "/favicon.ico" icon = urljoin("%s://%s%s" % (url_type, url_host, url_path), icon) self.log(" looking for icon at: %s" % icon) - try: - for i in range(8): - try: - fname, headers = urllib.urlretrieve(icon) - except RedirectException, e: - icon = e.url - self.log(" redirect to : %s" % icon) - else: - break + if icon in icons: + if icons[icon]: + content_type, bookmark.icon = icons[icon] + self.log(" cached icon: %s" % content_type) else: - raise IOError("Too many redirects") - except: - etype, emsg, tb = sys.exc_info() - self.log(" no icon : %s %s" % (etype, emsg)) - etype = None - emsg = None - tb = None + self.log(" cached icon: no icon") else: - content_type = headers["Content-Type"] - if content_type.startswith("image/"): - icon_file = open(fname, "rb") - icon = icon_file.read() - icon_file.close() - bookmark.icon = "data:%s;base64,%s" % (content_type, b64encode(icon)) - self.log(" got icon : %s" % content_type) + try: + _icon = icon + for i in range(8): + try: + fname, headers = urllib.urlretrieve(_icon) + except RedirectException, e: + _icon = e.url + self.log(" redirect to : %s" % _icon) + else: + break + else: + raise IOError("Too many redirects") + except: + etype, emsg, tb = sys.exc_info() + self.log(" no icon : %s %s" % (etype, emsg)) + etype = None + emsg = None + tb = None + icons[icon] = None else: - self.log(" no icon : bad content type '%s'" % content_type) - os.remove(fname) + content_type = headers["Content-Type"] + if content_type.startswith("image/"): + icon_file = open(fname, "rb") + icon = icon_file.read() + icon_file.close() + bookmark.icon = "data:%s;base64,%s" % (content_type, b64encode(icon)) + self.log(" got icon : %s" % content_type) + icons[icon] = (content_type, bookmark.icon) + else: + self.log(" no icon : bad content type '%s'" % content_type) + icons[icon] = None + os.remove(fname) except KeyError: pass -- 2.39.2