]> git.phdru.name Git - bookmarks_db.git/blob - Robots/bkmk_rurllib_py3.py
aca07cd86a09d21845597a3d84f76f10c98a2a48
[bookmarks_db.git] / Robots / bkmk_rurllib_py3.py
1 """Simple, strightforward robot based on urllib
2
3 This file is a part of Bookmarks database and Internet robot.
4
5 """
6
7 __author__ = "Oleg Broytman <phd@phdru.name>"
8 __copyright__ = "Copyright (C) 2000-2023 PhiloSoft Design"
9 __license__ = "GNU GPL"
10
11 __all__ = ['robot_urllib_py3']
12
13
14 import socket
15 import sys
16 import urllib.request
17
18 from Robots.bkmk_robot_base import robot_base, get_error
19
20
21 class RedirectException(Exception):
22     def __init__(self, errcode, newurl):
23         Exception.__init__(self)
24         self.errcode = errcode
25         self.newurl = newurl
26
27
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']
35         else:
36             newurl = "Nowhere"
37         raise RedirectException(errcode, newurl)
38
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
45
46     # Error 401 -- authentication required
47     def http_error_401(self, url, fp, errcode, errmsg, headers, data=None):
48         raise IOError(
49             ('http error', errcode, "Authentication required ", headers))
50
51     def http_error_default(self, url, fp, errcode, errmsg, headers):
52         if fp:
53             fp.read()
54             fp.close()
55         raise IOError(('http error', errcode, errmsg, headers))
56
57     def open(self, fullurl, data=None, timeout=socket._GLOBAL_DEFAULT_TIMEOUT):
58         return urllib.request.URLopener.open(self, fullurl, data)
59
60
61 urllib.request._opener = opener = MyURLopener()
62
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__
70 )
71 opener.addheader('X-User-Agent', _x_user_agent)
72 opener.addheader('Referer', '')
73
74 opener.addheader('Accept', '*/*')
75 opener.addheader('Accept-Language', 'ru,en')
76 opener.addheader('Cache-Control', 'max-age=300')
77 opener.addheader('Connection', 'close')
78
79
80 urllib_ftpwrapper = urllib.request.ftpwrapper
81 ftpcache_key = None
82
83
84 class myftpwrapper(urllib_ftpwrapper):
85     def __init__(self, user, passwd, host, port, dirs):
86         urllib_ftpwrapper.__init__(self, user, passwd, host, port, dirs)
87         global ftpcache_key
88         ftpcache_key = (user, host, port, '/'.join(dirs))
89
90
91 urllib.request.ftpwrapper = myftpwrapper
92
93
94 class robot_urllib_py3(robot_base):
95     def get(self, bookmark, url, accept_charset=False):
96         try:
97             # Set fake referer to the base URL
98             opener.addheaders[2] = ('Referer', url)
99
100             if accept_charset and bookmark.charset:
101                 opener.addheader('Accept-Charset', bookmark.charset)
102             try:
103                 fname, headers = urllib.request.urlretrieve(url)
104             finally:
105                 if accept_charset and bookmark.charset:
106                     # Remove Accept-Charset
107                     del opener.addheaders[-1]
108
109             infile = open(fname, 'rt')
110             content = infile.read()
111             infile.close()
112
113             return None, None, None, headers, content
114
115         except RedirectException as e:
116             return None, e.errcode, e.newurl, None, None
117
118         except IOError as e:
119             if (e[0] == "http error") and (e[1] == -1):
120                 error = None
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)
124             else:
125                 error = get_error(e)
126                 self.log('   Error: %s' % error)
127
128             return error, None, None, None, None
129
130     def get_ftp_welcome(self):
131         global ftpcache_key
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.
135         ftpcache_key = None
136         return _welcome
137
138     def finish_check_url(self, bookmark):
139         robot_base.finish_check_url(self, bookmark)
140         urllib.request.urlcleanup()
141         urllib.request._opener = opener