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']
18 from Robots.bkmk_robot_base import robot_base, get_error
21 class RedirectException(Exception):
22 def __init__(self, errcode, newurl):
23 Exception.__init__(self)
24 self.errcode = errcode
28 class MyURLopener(urllib.request.URLopener):
29 # Error 301 -- relocated (permanently)
30 def http_error_301(self, url, fp, errcode, errmsg, headers, data=None):
31 if 'location' in headers:
32 newurl = headers['location']
33 elif 'uri' in headers:
34 newurl = headers['uri']
37 raise RedirectException(errcode, newurl)
39 # Error 302 -- relocated (temporarily)
40 http_error_302 = http_error_301
41 # Error 303 -- relocated (see other)
42 http_error_303 = http_error_301
43 # Error 307 -- relocated (temporarily)
44 http_error_307 = http_error_301
46 # Error 401 -- authentication required
47 def http_error_401(self, url, fp, errcode, errmsg, headers, data=None):
49 ('http error', errcode, "Authentication required ", headers))
51 def http_error_default(self, url, fp, errcode, errmsg, headers):
55 raise IOError(('http error', errcode, errmsg, headers))
57 def open(self, fullurl, data=None, timeout=socket._GLOBAL_DEFAULT_TIMEOUT):
58 return urllib.request.URLopener.open(self, fullurl, data)
61 urllib.request._opener = opener = MyURLopener()
63 # Fake headers to pretend this is a real browser
64 _user_agent = "Mozilla/5.0 (X11; U; Linux 2.6 i686; en)"
65 " Gecko/20001221 Firefox/2.0.0"
66 opener.addheaders[0] = ('User-Agent', _user_agent)
67 _x_user_agent = "bookmarks_db (Python %d.%d.%d; urllib/%s)" % (
68 sys.version_info[0], sys.version_info[1],
69 sys.version_info[2], urllib.request.__version__
71 opener.addheader('X-User-Agent', _x_user_agent)
72 opener.addheader('Referer', '')
74 opener.addheader('Accept', '*/*')
75 opener.addheader('Accept-Language', 'ru,en')
76 opener.addheader('Cache-Control', 'max-age=300')
77 opener.addheader('Connection', 'close')
80 urllib_ftpwrapper = urllib.request.ftpwrapper
84 class myftpwrapper(urllib_ftpwrapper):
85 def __init__(self, user, passwd, host, port, dirs):
86 urllib_ftpwrapper.__init__(self, user, passwd, host, port, dirs)
88 ftpcache_key = (user, host, port, '/'.join(dirs))
91 urllib.request.ftpwrapper = myftpwrapper
94 class robot_urllib_py3(robot_base):
95 def get(self, bookmark, url, accept_charset=False):
97 # Set fake referer to the base URL
98 opener.addheaders[2] = ('Referer', url)
100 if accept_charset and bookmark.charset:
101 opener.addheader('Accept-Charset', bookmark.charset)
103 fname, headers = urllib.request.urlretrieve(url)
105 if accept_charset and bookmark.charset:
106 # Remove Accept-Charset
107 del opener.addheaders[-1]
109 infile = open(fname, 'rt')
110 content = infile.read()
113 return None, None, None, headers, content
115 except RedirectException as e:
116 return None, e.errcode, e.newurl, None, None
119 if (e[0] == "http error") and (e[1] == -1):
121 bookmark.no_error = "The server did not return any header - "
122 "it is not an error, actually"
123 self.log(' no headers: %s' % bookmark.no_error)
126 self.log(' Error: %s' % error)
128 return error, None, None, None, None
130 def get_ftp_welcome(self):
132 _welcome = opener.ftpcache[ftpcache_key].ftp.welcome
133 # I am assuming there are no duplicate ftp URLs in db.
134 # If there are - ftpcache_key in next line is invalid.
138 def finish_check_url(self, bookmark):
139 robot_base.finish_check_url(self, bookmark)
140 urllib.request.urlcleanup()
141 urllib.request._opener = opener