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