+ icon = urljoin("%s://%s%s" % (url_type, url_host, url_path), icon)
+ self.log(" looking for icon at: %s" % icon)
+ if icon in icons:
+ if icons[icon]:
+ content_type, bookmark.icon = icons[icon]
+ self.log(" cached icon: %s" % content_type)
+ else:
+ self.log(" cached icon: no icon")
+ else:
+ 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:
+ 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)
+