1 """Simple, strightforward robot based on urllib
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_urllib']
16 from Robots.bkmk_robot_base import robot_base, get_error
19 class RedirectException(Exception):
20 def __init__(self, errcode, newurl):
21 Exception.__init__(self)
22 self.errcode = errcode
25 class MyURLopener(urllib.URLopener):
26 # Error 301 -- relocated (permanently)
27 def http_error_301(self, url, fp, errcode, errmsg, headers, data=None):
28 if headers.has_key('location'):
29 newurl = headers['location']
30 elif headers.has_key('uri'):
31 newurl = headers['uri']
34 raise RedirectException(errcode, newurl)
36 # Error 302 -- relocated (temporarily)
37 http_error_302 = http_error_301
38 # Error 303 -- relocated (see other)
39 http_error_303 = http_error_301
40 # Error 307 -- relocated (temporarily)
41 http_error_307 = http_error_301
43 # Error 401 -- authentication required
44 def http_error_401(self, url, fp, errcode, errmsg, headers, data=None):
45 raise IOError, ('http error', errcode, "Authentication required ", headers)
47 def http_error_default(self, url, fp, errcode, errmsg, headers):
51 raise IOError, ('http error', errcode, errmsg, headers)
54 urllib._urlopener = MyURLopener()
56 # Fake headers to pretend this is a real browser
57 _user_agent = "Mozilla/5.0 (X11; U; Linux 2.6 i686; en) Gecko/20001221 Firefox/2.0.0"
58 urllib._urlopener.addheaders[0] = ('User-Agent', _user_agent)
59 _x_user_agent = "bookmarks_db (Python %d.%d.%d; urllib/%s)" % (
60 sys.version_info[0], sys.version_info[1], sys.version_info[2], urllib.__version__)
61 urllib._urlopener.addheader('X-User-Agent', _x_user_agent)
62 urllib._urlopener.addheader('Referer', '')
64 urllib._urlopener.addheader('Accept', '*/*')
65 urllib._urlopener.addheader('Accept-Language', 'ru,en')
66 urllib._urlopener.addheader('Cache-Control', 'max-age=300')
67 urllib._urlopener.addheader('Connection', 'close')
70 urllib_ftpwrapper = urllib.ftpwrapper
73 class myftpwrapper(urllib_ftpwrapper):
74 def __init__(self, user, passwd, host, port, dirs):
75 urllib_ftpwrapper.__init__(self, user, passwd, host, port, dirs)
77 ftpcache_key = (user, host, port, '/'.join(dirs))
79 urllib.ftpwrapper = myftpwrapper
82 class robot_urllib(robot_base):
83 def get(self, bookmark, url, accept_charset=False):
85 # Set fake referer to the base URL
86 urllib._urlopener.addheaders[2] = ('Referer', url)
88 if accept_charset and bookmark.charset:
89 urllib._urlopener.addheader('Accept-Charset', bookmark.charset)
91 fname, headers = urllib.urlretrieve(url)
93 if accept_charset and bookmark.charset:
94 del urllib._urlopener.addheaders[-1] # Remove Accept-Charset
96 infile = open(fname, 'rb')
97 content = infile.read()
100 return None, None, None, headers, content
102 except RedirectException, e:
103 return None, e.errcode, e.newurl, None, None
106 if (e[0] == "http error") and (e[1] == -1):
108 bookmark.no_error = "The server did not return any header - it is not an error, actually"
109 self.log(' no headers: %s' % bookmark.no_error)
112 self.log(' Error: %s' % error)
114 return error, None, None, None, None
116 def get_ftp_welcome(self):
118 _welcome = urllib._urlopener.ftpcache[ftpcache_key].ftp.welcome
119 ftpcache_key = None # I am assuming there are no duplicate ftp URLs in db.
120 # If there are - ftpcache_key in prev line is invalid.
123 def finish_check_url(self, bookmark):
124 robot_base.finish_check_url(self, bookmark)