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-2023 PhiloSoft Design"
9 __license__ = "GNU GPL"
11 __all__ = ['robot_urllib_py3']
17 from Robots.bkmk_robot_base import robot_base, get_error
20 class RedirectException(Exception):
21 def __init__(self, errcode, newurl):
22 Exception.__init__(self)
23 self.errcode = errcode
27 class MyURLopener(urllib.request.URLopener):
28 # Error 301 -- relocated (permanently)
29 def http_error_301(self, url, fp, errcode, errmsg, headers, data=None):
30 if 'location' in headers:
31 newurl = headers['location']
32 elif 'uri' in headers:
33 newurl = headers['uri']
36 raise RedirectException(errcode, newurl)
38 # Error 302 -- relocated (temporarily)
39 http_error_302 = http_error_301
40 # Error 303 -- relocated (see other)
41 http_error_303 = http_error_301
42 # Error 307 -- relocated (temporarily)
43 http_error_307 = http_error_301
45 # Error 401 -- authentication required
46 def http_error_401(self, url, fp, errcode, errmsg, headers, data=None):
48 ('http error', errcode, "Authentication required ", headers))
50 def http_error_default(self, url, fp, errcode, errmsg, headers):
54 raise IOError(('http error', errcode, errmsg, headers))
57 urllib.request._opener = MyURLopener()
59 # Fake headers to pretend this is a real browser
60 _user_agent = "Mozilla/5.0 (X11; U; Linux 2.6 i686; en)"
61 " Gecko/20001221 Firefox/2.0.0"
62 urllib.request._opener.addheaders[0] = ('User-Agent', _user_agent)
63 _x_user_agent = "bookmarks_db (Python %d.%d.%d; urllib/%s)" % (
64 sys.version_info[0], sys.version_info[1],
65 sys.version_info[2], urllib.request.__version__
67 urllib.request._opener.addheader('X-User-Agent', _x_user_agent)
68 urllib.request._opener.addheader('Referer', '')
70 urllib.request._opener.addheader('Accept', '*/*')
71 urllib.request._opener.addheader('Accept-Language', 'ru,en')
72 urllib.request._opener.addheader('Cache-Control', 'max-age=300')
73 urllib.request._opener.addheader('Connection', 'close')
76 urllib_ftpwrapper = urllib.request.ftpwrapper
80 class myftpwrapper(urllib_ftpwrapper):
81 def __init__(self, user, passwd, host, port, dirs):
82 urllib_ftpwrapper.__init__(self, user, passwd, host, port, dirs)
84 ftpcache_key = (user, host, port, '/'.join(dirs))
87 urllib.request.ftpwrapper = myftpwrapper
90 class robot_urllib_py3(robot_base):
91 def get(self, bookmark, url, accept_charset=False):
93 # Set fake referer to the base URL
94 urllib.request._opener.addheaders[2] = ('Referer', url)
96 if accept_charset and bookmark.charset:
97 urllib.request._opener.addheader('Accept-Charset', bookmark.charset)
99 fname, headers = urllib.request.urlretrieve(url)
101 if accept_charset and bookmark.charset:
102 # Remove Accept-Charset
103 del urllib.request._opener.addheaders[-1]
105 infile = open(fname, 'rt')
106 content = infile.read()
109 return None, None, None, headers, content
111 except RedirectException as e:
112 return None, e.errcode, e.newurl, None, None
115 if (e[0] == "http error") and (e[1] == -1):
117 bookmark.no_error = "The server did not return any header - "
118 "it is not an error, actually"
119 self.log(' no headers: %s' % bookmark.no_error)
122 self.log(' Error: %s' % error)
124 return error, None, None, None, None
126 def get_ftp_welcome(self):
128 _welcome = urllib.request._opener.ftpcache[ftpcache_key].ftp.welcome
129 # I am assuming there are no duplicate ftp URLs in db.
130 # If there are - ftpcache_key in next line is invalid.
134 def finish_check_url(self, bookmark):
135 robot_base.finish_check_url(self, bookmark)
136 urllib.request.urlcleanup()