]> git.phdru.name Git - bookmarks_db.git/blob - Robots/bkmk_rurllib2.py
1233c70aee1912d9047864f385e1140fd157ac5c
[bookmarks_db.git] / Robots / bkmk_rurllib2.py
1 """Robot based on urllib2
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) 2014-2023 PhiloSoft Design"
9 __license__ = "GNU GPL"
10
11 __all__ = ['robot_urllib2']
12
13
14 import sys
15 import httplib
16 import urllib2
17 from Robots.bkmk_robot_base import robot_base, get_error
18
19
20 _fw = None
21
22
23 class FTPHandler(urllib2.FTPHandler):
24     def connect_ftp(self, user, passwd, host, port, dirs, timeout):
25         global _fw
26         _fw = urllib2.FTPHandler.connect_ftp(self, user, passwd, host, port,
27                                              dirs, timeout)
28         return _fw
29
30
31 opener = urllib2.OpenerDirector()
32 default_classes = [urllib2.HTTPHandler, urllib2.HTTPDefaultErrorHandler,
33                    FTPHandler, urllib2.HTTPErrorProcessor]
34 if hasattr(httplib, 'HTTPS'):
35     default_classes.insert(0, urllib2.HTTPSHandler)
36 for klass in default_classes:
37     opener.add_handler(klass())
38
39 urllib2.install_opener(opener)
40
41
42 # Fake headers to pretend this is a real browser
43 _user_agent = "Mozilla/5.0 (X11; Linux i686; rv:30.0)"
44 " Gecko/20100101 Firefox/30.0"
45 _x_user_agent = "bookmarks_db (Python %d.%d.%d; urllib2)" % (
46     sys.version_info[0], sys.version_info[1], sys.version_info[2])
47
48
49 class robot_urllib2(robot_base):
50     def get(self, bookmark, url, accept_charset=False):
51         request = urllib2.Request(url)
52         request.add_header('Accept', '*/*')
53         if accept_charset and bookmark.charset:
54             request.add_header('Accept-Charset', bookmark.charset)
55         request.add_header('Accept-Language', 'ru,en')
56         request.add_header('Cache-Control', 'max-age=300')
57         request.add_header('Connection', 'close')
58         request.add_header('Referer', url)
59         request.add_header('User-agent', _user_agent)
60         request.add_header('X-User-Agent', _x_user_agent)
61
62         global _fw
63         _fw = None
64
65         try:
66             response = urllib2.urlopen(request)
67
68         except urllib2.HTTPError as e:
69             if e.code in (301, 302, 303, 307, 308):
70                 return None, e.code, e.hdrs['Location'], None, None
71             else:
72                 self.log('   HTTP Error %s: %s' % (e.code, e.msg))
73                 return ("HTTP Error %s: %s" % (e.code, e.msg),
74                         None, None, None, None)
75
76         except urllib2.URLError as e:
77             self.log('   URL Error: %s' % e.reason)
78             return "URL Error: %s" % e.reason, None, None, None, None
79
80         except httplib.HTTPException as e:
81             error = get_error(e)
82             self.log('   HTTP Exception: %s' % error)
83             return "HTTP Exception: %s" % error, None, None, None, None
84
85         except IOError as e:
86             error = get_error(e)
87             self.log('   I/O Error: %s' % error)
88             return "I/O Error: %s" % error, None, None, None, None
89
90         else:
91             return None, None, None, response.info(), response.read()
92
93     def get_ftp_welcome(self):
94         if _fw is None:
95             return ''
96         return _fw.ftp.welcome