]> git.phdru.name Git - bookmarks_db.git/blob - Robots/bkmk_rurllib.py
067799c737271b5ceeb373eeb321bc12eaf1e7ec
[bookmarks_db.git] / Robots / bkmk_rurllib.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']
12
13
14 import sys
15 import urllib
16
17 from Robots.bkmk_robot_base import robot_base, get_error
18
19
20 class RedirectException(Exception):
21     def __init__(self, errcode, newurl):
22         Exception.__init__(self)
23         self.errcode = errcode
24         self.newurl = newurl
25
26
27 class MyURLopener(urllib.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']
34         else:
35             newurl = "Nowhere"
36         raise RedirectException(errcode, newurl)
37
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
44     # Error 308 -- relocated (permanently)
45     http_error_308 = http_error_301
46
47     # Error 401 -- authentication required
48     def http_error_401(self, url, fp, errcode, errmsg, headers, data=None):
49         raise IOError(
50             ('http error', errcode, "Authentication required ", headers))
51
52     def http_error_default(self, url, fp, errcode, errmsg, headers):
53         if fp:
54             fp.read()
55             fp.close()
56         raise IOError(('http error', errcode, errmsg, headers))
57
58
59 urllib._urlopener = MyURLopener()
60
61 # Fake headers to pretend this is a real browser
62 _user_agent = "Mozilla/5.0 (X11; U; Linux 2.6 i686; en)"
63 " Gecko/20001221 Firefox/2.0.0"
64 urllib._urlopener.addheaders[0] = ('User-Agent', _user_agent)
65 _x_user_agent = "bookmarks_db (Python %d.%d.%d; urllib/%s)" % (
66    sys.version_info[0], sys.version_info[1],
67    sys.version_info[2], urllib.__version__
68 )
69 urllib._urlopener.addheader('X-User-Agent', _x_user_agent)
70 urllib._urlopener.addheader('Referer', '')
71
72 urllib._urlopener.addheader('Accept', '*/*')
73 urllib._urlopener.addheader('Accept-Language', 'ru,en')
74 urllib._urlopener.addheader('Cache-Control', 'max-age=300')
75 urllib._urlopener.addheader('Connection', 'close')
76
77
78 urllib_ftpwrapper = urllib.ftpwrapper
79 ftpcache_key = None
80
81
82 class myftpwrapper(urllib_ftpwrapper):
83     def __init__(self, user, passwd, host, port, dirs):
84         urllib_ftpwrapper.__init__(self, user, passwd, host, port, dirs)
85         global ftpcache_key
86         ftpcache_key = (user, host, port, '/'.join(dirs))
87
88
89 urllib.ftpwrapper = myftpwrapper
90
91
92 class robot_urllib(robot_base):
93     def get(self, bookmark, url, accept_charset=False):
94         try:
95             # Set fake referer to the base URL
96             urllib._urlopener.addheaders[2] = ('Referer', url)
97
98             if accept_charset and bookmark.charset:
99                 urllib._urlopener.addheader('Accept-Charset', bookmark.charset)
100             try:
101                 fname, headers = urllib.urlretrieve(url)
102             finally:
103                 if accept_charset and bookmark.charset:
104                     # Remove Accept-Charset
105                     del urllib._urlopener.addheaders[-1]
106
107             infile = open(fname, 'rt')
108             content = infile.read()
109             infile.close()
110
111             return None, None, None, headers, content
112
113         except RedirectException as e:
114             return None, e.errcode, e.newurl, None, None
115
116         except IOError as e:
117             if (e[0] == "http error") and (e[1] == -1):
118                 error = None
119                 bookmark.no_error = "The server did not return any header - "
120                 "it is not an error, actually"
121                 self.log('   no headers: %s' % bookmark.no_error)
122             else:
123                 error = get_error(e)
124                 self.log('   Error: %s' % error)
125
126             return error, None, None, None, None
127
128     def get_ftp_welcome(self):
129         global ftpcache_key
130         _welcome = urllib._urlopener.ftpcache[ftpcache_key].ftp.welcome
131         # I am assuming there are no duplicate ftp URLs in db.
132         # If there are - ftpcache_key in next line is invalid.
133         ftpcache_key = None
134         return _welcome
135
136     def finish_check_url(self, bookmark):
137         robot_base.finish_check_url(self, bookmark)
138         urllib.urlcleanup()