X-Git-Url: https://git.phdru.name/?a=blobdiff_plain;f=Robots%2Fbkmk_rsimple.py;h=7de63185a6c8800e2d41a04d0ead795475531616;hb=3e5f3edd3d9fb7fcfa2d56ff3794716a4bf4d4cb;hp=69636d9ef09044905962a3930fc8789e32b45ebc;hpb=7fd1132c4ab530b7b34e846ca45f99b8737dd127;p=bookmarks_db.git diff --git a/Robots/bkmk_rsimple.py b/Robots/bkmk_rsimple.py index 69636d9..7de6318 100644 --- a/Robots/bkmk_rsimple.py +++ b/Robots/bkmk_rsimple.py @@ -1,7 +1,7 @@ """ Simple, strightforward robot - Written by Oleg Broytman. Copyright (C) 2000-2010 PhiloSoft Design. + Written by Oleg Broytman. Copyright (C) 2000-2011 PhiloSoft Design. """ import sys, os @@ -105,6 +105,7 @@ icons = {} # Icon cache; maps URL to a tuple (content type, data) class robot_simple(Robot): def check_url(self, bookmark): + fname = None try: self.start = int(time.time()) bookmark.icon = None @@ -175,24 +176,6 @@ class robot_simple(Robot): parser = parse_html(fname, charset, self.log) if parser: bookmark.real_title = parser.title - if parser.refresh: - refresh = parser.refresh - try: - url = refresh.split('=', 1)[1] - except IndexError: - url = "self" - try: - timeout = float(refresh.split(';')[0]) - except (IndexError, ValueError): - raise RedirectException("html", "Bad redirect to %s (%s)" % (url, refresh)) - else: - try: - timeout = int(refresh.split(';')[0]) - except ValueError: - pass # float timeout - raise RedirectException("html", "%s (%s sec)" % (url, timeout)) - - # Get favicon.ico icon = parser.icon else: icon = None @@ -223,24 +206,43 @@ class robot_simple(Robot): except: etype, emsg, tb = sys.exc_info() self.log(" no icon : %s %s" % (etype, emsg)) - etype = None - emsg = None - tb = None + etype = emsg = tb = None icons[icon] = None else: content_type = headers["Content-Type"] - if content_type.startswith("image/"): + if content_type.startswith("application/") \ + or content_type.startswith("image/") \ + or content_type.startswith("text/plain"): icon_file = open(icon_fname, "rb") icon_data = icon_file.read() icon_file.close() bookmark.icon_href = icon - bookmark.icon = "data:%s;base64,%s" % (content_type, b64encode(icon_data)) self.log(" got icon : %s" % content_type) + if content_type.startswith("application/") \ + or content_type.startswith("text/plain"): + self.log(" non-image content type, assume x-icon") + content_type = 'image/x-icon' + bookmark.icon = "data:%s;base64,%s" % (content_type, b64encode(icon_data)) icons[icon] = (content_type, bookmark.icon) else: self.log(" no icon : bad content type '%s'" % content_type) icons[icon] = None - os.remove(icon_fname) + if parser and parser.refresh: + refresh = parser.refresh + try: + url = refresh.split('=', 1)[1] + except IndexError: + url = "self" + try: + timeout = float(refresh.split(';')[0]) + except (IndexError, ValueError): + raise RedirectException("html", "Bad redirect to %s (%s)" % (url, refresh)) + else: + try: + timeout = int(refresh.split(';')[0]) + except ValueError: + pass # float timeout + raise RedirectException("html", "%s (%s sec)" % (url, timeout)) except KeyError, key: self.log(" no header: %s" % key) @@ -291,3 +293,4 @@ class robot_simple(Robot): now = int(time.time()) bookmark.test_time = str(now - start) + urllib.urlcleanup()