1 """Base class for robots
3 This file is a part of Bookmarks database and Internet robot.
7 __author__ = "Oleg Broytman <phd@phdru.name>"
8 __copyright__ = "Copyright (C) 2000-2014 PhiloSoft Design"
9 __license__ = "GNU GPL"
11 __all__ = ['robot_base', 'get_error']
16 from base64 import b64encode
17 from urlparse import urljoin
19 from m_lib.net.www.util import parse_time
20 from m_lib.md5wrapper import md5wrapper
22 from bkmk_objects import Robot
23 from parse_html import parse_html
26 class RedirectException(Exception):
34 def __init__(self, errcode, newurl):
35 Exception.__init__(self, "(%s) to %s" % (self.reloc_dict[errcode], newurl))
40 if isinstance(msg, str):
46 s.append("'%s'" % str(i).replace('\n', "\\n"))
47 return "(%s)" % ' '.join(s)
50 icons = {} # Icon cache; maps URL to a tuple (content type, data)
51 # or None if there is no icon.
53 class robot_base(Robot):
54 def check_url(self, bookmark):
56 self.start = int(time.time())
59 url_type, url_rest = urllib.splittype(bookmark.href)
60 url_host, url_path = urllib.splithost(url_rest)
61 url_path, url_tag = urllib.splittag(url_path)
63 url = "%s://%s%s" % (url_type, url_host, url_path)
64 headers, content = self.urlretrieve(bookmark, url, True)
74 size = headers["Content-Length"]
79 last_modified = headers["Last-Modified"]
84 last_modified = parse_time(last_modified)
89 last_modified = str(int(last_modified))
91 last_modified = bookmark.last_visit
94 bookmark.last_modified = last_modified
97 if urllib._urlopener.type == "ftp": # Pass welcome message through MD5
98 md5.update(self.get_ftp_welcome())
101 bookmark.md5 = str(md5)
105 content_type = headers["Content-Type"]
106 self.log(" Content-Type: %s" % content_type)
108 # extract charset from "text/html; foo; charset=UTF-8, bar; baz;"
109 content_type, charset = content_type.split(';', 1)
110 content_type = content_type.strip()
111 charset = charset.split('=')[1].strip().split(',')[0]
112 self.log(" HTTP charset : %s" % charset)
113 except (ValueError, IndexError):
115 self.log(" no charset in Content-Type header")
116 for ctype in ("text/html", "application/xhtml+xml"):
117 if content_type.startswith(ctype):
123 parser = parse_html(content, charset, self.log)
125 bookmark.real_title = parser.title
130 icon = "/favicon.ico"
131 icon_url = urljoin("%s://%s%s" % (url_type, url_host, url_path), icon)
132 self.log(" looking for icon at: %s" % icon_url)
133 if icon_url in icons:
135 bookmark.icon_href = icon_url
136 content_type, bookmark.icon = icons[icon_url]
137 self.log(" cached icon: %s" % content_type)
139 self.log(" cached icon: no icon")
145 icon_headers, icon_data = self.urlretrieve(bookmark, _icon_url)
146 except RedirectException, e:
148 self.log(" redirect to : %s" % _icon_url)
150 if icon_data is None:
151 raise IOError("No icon")
154 raise IOError("Too many redirects")
156 etype, emsg, tb = sys.exc_info()
157 self.log(" no icon : %s %s" % (etype, emsg))
158 etype = emsg = tb = None
159 icons[icon_url] = None
161 content_type = icon_headers["Content-Type"]
162 if content_type.startswith("application/") \
163 or content_type.startswith("image/") \
164 or content_type.startswith("text/plain"):
165 bookmark.icon_href = icon_url
166 self.log(" got icon : %s" % content_type)
167 if content_type.startswith("application/") \
168 or content_type.startswith("text/plain"):
169 self.log(" non-image content type, assume x-icon")
170 content_type = 'image/x-icon'
171 bookmark.icon = "data:%s;base64,%s" % (content_type, b64encode(icon_data))
172 icons[icon_url] = (content_type, bookmark.icon)
174 self.log(" no icon : bad content type '%s'" % content_type)
175 icons[icon_url] = None
176 if parser and parser.refresh:
177 refresh = parser.refresh
179 url = refresh.split('=', 1)[1]
183 timeout = float(refresh.split(';')[0])
184 except (IndexError, ValueError):
185 raise RedirectException("html", "Bad redirect to %s (%s)" % (url, refresh))
188 timeout = int(refresh.split(';')[0])
191 raise RedirectException("html", "%s (%s sec)" % (url, timeout))
193 except KeyError, key:
194 self.log(" no header: %s" % key)
197 bookmark.error = "Unexpected EOF (FTP server closed connection)"
198 self.log(' EOF: %s' % bookmark.error)
200 except RedirectException, msg:
201 bookmark.moved = str(msg)
202 self.log(' Moved: %s' % bookmark.moved)
204 except KeyboardInterrupt:
205 self.log("Keyboard interrupt (^C)")
210 traceback.print_exc()
211 bookmark.error = "Exception!"
212 self.log(' Exception: %s' % bookmark.error)
215 self.finish_check_url(bookmark)
220 def finish_check_url(self, bookmark):
222 bookmark.last_tested = str(start)
224 now = int(time.time())
225 bookmark.test_time = str(now - start)