]> git.phdru.name Git - bookmarks_db.git/blob - Robots/bkmk_rurllib2.py
Handle HTTPException and IOError (socket errors)
[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 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 opener = urllib2.OpenerDirector()
21 default_classes = [urllib2.HTTPHandler, urllib2.HTTPDefaultErrorHandler,
22                    urllib2.FTPHandler, urllib2.HTTPErrorProcessor]
23 if hasattr(httplib, 'HTTPS'):
24     default_classes.insert(0, urllib2.HTTPSHandler)
25 for klass in default_classes:
26     opener.add_handler(klass())
27
28 urllib2.install_opener(opener)
29
30
31 # Fake headers to pretend this is a real browser
32 _user_agent = "Mozilla/5.0 (X11; Linux i686; rv:30.0) Gecko/20100101 Firefox/30.0"
33 _x_user_agent = "bookmarks_db (Python %d.%d.%d; urllib2)" % (
34     sys.version_info[0], sys.version_info[1], sys.version_info[2])
35
36
37 class robot_urllib2(robot_base):
38    def get(self, bookmark, url, accept_charset=False):
39         request = urllib2.Request(url)
40         request.add_header('Accept', '*/*')
41         if accept_charset and bookmark.charset:
42             request.add_header('Accept-Charset', bookmark.charset)
43         request.add_header('Accept-Language', 'ru,en')
44         request.add_header('Cache-Control', 'max-age=300')
45         request.add_header('Connection', 'close')
46         request.add_header('Referer', url)
47         request.add_header('User-agent', _user_agent)
48         request.add_header('X-User-Agent', _x_user_agent)
49
50         try:
51             response = urllib2.urlopen(request)
52         except urllib2.HTTPError, e:
53             if e.code in (301, 302, 303, 307):
54                 return None, e.code, e.hdrs['Location'], None, None
55             else:
56                 self.log('   HTTP Error %s: %s' % (e.code, e.msg))
57                 return "HTTP Error %s: %s" % (e.code, e.msg), None, None, None, None
58         except urllib2.URLError, e:
59             self.log('   URL Error: %s' % e.reason)
60             return "URL Error: %s" % e.reason, None, None, None, None
61         except httplib.HTTPException, e:
62             error = get_error(e)
63             self.log('   HTTP Exception: %s' % error)
64             return "HTTP Exception: %s" % error, None, None, None, None
65         except IOError, e:
66             error = get_error(e)
67             self.log('   I/O Error: %s' % error)
68             return "I/O Error: %s" % error, None, None, None, None
69         else:
70             return None, None, None, response.info(), response.read()
71
72    def get_ftp_welcome(self):
73       return ''