2 Simple, strightforward robot; guaranteed to has problems with timeouts :)
4 Written by BroytMann, Mar 2000 - Aug 2002. Copyright (C) 2000-2002 PhiloSoft Design
8 class RedirectException(Exception):
14 def __init__(self, errcode, newurl):
15 Exception.__init__(self, "(%s) to %s" % (self.reloc_dict[errcode], newurl))
20 from m_lib.net.www.util import parse_time
21 from m_lib.md5wrapper import md5wrapper
24 class MyURLopener(urllib.URLopener):
25 # Error 302 -- relocated (temporarily)
26 def http_error_302(self, url, fp, errcode, errmsg, headers, data=None):
27 if headers.has_key('location'):
28 newurl = headers['location']
29 elif headers.has_key('uri'):
30 newurl = headers['uri']
33 raise RedirectException(errcode, newurl)
35 # Error 301 -- also relocated (permanently)
36 http_error_301 = http_error_302
38 # Error 401 -- authentication required
39 def http_error_401(self, url, fp, errcode, errmsg, headers, data=None):
40 raise IOError, ('http error', errcode, "Authentication required ", headers)
43 urllib._urlopener = MyURLopener()
45 # Some sites allow only Mozilla-compatible browsers; way to stop robots?
46 server_version = "Mozilla/3.0 (compatible; Python-urllib/%s)" % urllib.__version__
47 urllib._urlopener.addheaders[0] = ('User-agent', server_version)
51 if type(msg) == type(""):
57 s.append("'%s'" % string.join(string.split(str(i), "\n"), "\\n"))
58 return "(%s)" % string.join(s)
61 urllib_ftpwrapper = urllib.ftpwrapper
64 class myftpwrapper(urllib_ftpwrapper):
65 def __init__(self, user, passwd, host, port, dirs):
66 urllib_ftpwrapper.__init__(self, user, passwd, host, port, dirs)
68 ftpcache_key = (user, host, port, string.join(dirs, '/'))
70 urllib.ftpwrapper = myftpwrapper
74 _welcome = urllib._urlopener.ftpcache[ftpcache_key].ftp.welcome
75 ftpcache_key = None # I am assuming there are no duplicate ftp URLs in db.
76 # If there are - ftpcache_key in prev line is invalid.
80 from bkmk_objects import Robot
81 from parse_html import parse_html
83 class robot_simple(Robot):
84 def check_url(self, bookmark, url_type, url_rest):
85 if not self.tempfname:
86 self.tempfname = bookmark.tempfname
90 self.start = int(time.time())
91 url_host, url_path = urllib.splithost(url_rest)
92 url_path, url_tag = urllib.splittag(url_path)
94 fname, headers = urllib.urlretrieve("%s://%s%s" % (url_type, url_host, url_path), self.tempfname)
101 size = headers["Content-Length"]
106 last_modified = headers["Last-Modified"]
111 last_modified = parse_time(last_modified)
114 last_modified = str(int(last_modified))
116 last_modified = bookmark.last_visit
119 bookmark.last_modified = last_modified
122 if urllib._urlopener.type == "ftp": # Pass welcome message through MD5
123 md5.update(get_welcome())
125 md5.md5file(self.tempfname)
126 bookmark.md5 = str(md5)
130 content_type = headers["Content-Type"]
132 content_type, charset = content_type.split(';')
133 content_type = content_type.strip()
134 charset = charset.split('=')[1].strip()
135 if self.log: self.log(" HTTP charset : %s" % charset)
136 except (ValueError, IndexError):
138 if self.log: self.log(" no charset in Content-Type header")
139 if content_type == "text/html":
140 parser = parse_html(fname, charset, self.log)
141 title = parser.title.replace('\r', '').replace('\n', ' ').strip()
142 bookmark.real_title = parser.unescape(title)
143 if self.log: self.log(" final title : %s" % bookmark.real_title)
145 refresh = parser.refresh
147 timeout = int(refresh.split(';')[0])
148 except (IndexError, ValueError):
151 url = refresh.split('=', 1)[1]
154 raise RedirectException("html", "%s (%d sec)" % (url, timeout))
159 if (msg[0] == "http error") and (msg[1] == -1):
160 bookmark.no_error = "The server did not return any header - it is not an error, actually"
162 bookmark.error = get_error(msg)
165 bookmark.error = "Unexpected EOF (FTP server closed connection)"
167 except RedirectException, msg:
168 bookmark.moved = str(msg)
170 except KeyboardInterrupt:
174 self.finish_check_url(bookmark)
180 def finish_check_url(self, bookmark):
181 # Calculate these attributes even in case of an error
182 if os.path.exists(self.tempfname):
183 size = str(os.path.getsize(self.tempfname))
189 bookmark.last_tested = str(start)
191 now = int(time.time())
192 bookmark.test_time = str(now - start)