]> git.phdru.name Git - bookmarks_db.git/blobdiff - Robots/bkmk_rurllib.py
Minor refactoring: reorder return values
[bookmarks_db.git] / Robots / bkmk_rurllib.py
index f0a614e3799e1ccae9750bd57fbbf5c4f99ccd90..d9908b83292ab92fcfd107710446555cd3aab712 100644 (file)
@@ -17,8 +17,8 @@ from Robots.bkmk_robot_base import robot_base, RedirectException, get_error
 
 
 class MyURLopener(urllib.URLopener):
-   # Error 302 -- relocated (temporarily)
-   def http_error_302(self, url, fp, errcode, errmsg, headers, data=None): 
+   # Error 301 -- relocated (permanently)
+   def http_error_301(self, url, fp, errcode, errmsg, headers, data=None): 
       if headers.has_key('location'):
          newurl = headers['location']
       elif headers.has_key('uri'):
@@ -27,10 +27,12 @@ class MyURLopener(urllib.URLopener):
          newurl = "Nowhere"
       raise RedirectException(errcode, newurl)
 
-   # Error 301 -- also relocated (permanently)
-   http_error_301 = http_error_302
-   # Error 307 -- also relocated (temporary)
-   http_error_307 = http_error_302
+   # Error 302 -- relocated (temporarily)
+   http_error_302 = http_error_301
+   # Error 303 -- relocated (see other)
+   http_error_303 = http_error_301
+   # Error 307 -- relocated (temporarily)
+   http_error_307 = http_error_301
 
    # Error 401 -- authentication required
    def http_error_401(self, url, fp, errcode, errmsg, headers, data=None): 
@@ -72,32 +74,35 @@ urllib.ftpwrapper = myftpwrapper
 
 
 class robot_urllib(robot_base):
-   def urlretrieve(self, bookmark, url, accept_charset=False):
+   def get(self, bookmark, url, accept_charset=False):
       try:
          # Set fake referer to the base URL
          urllib._urlopener.addheaders[2] = ('Referer', url)
 
          if accept_charset and bookmark.charset:
             urllib._urlopener.addheader('Accept-Charset', bookmark.charset)
-         fname, headers = urllib.urlretrieve(url)
-         if accept_charset and bookmark.charset:
-            del urllib._urlopener.addheaders[-1]
+         try:
+            fname, headers = urllib.urlretrieve(url)
+         finally:
+            if accept_charset and bookmark.charset:
+               del urllib._urlopener.addheaders[-1] # Remove Accept-Charset
 
          infile = open(fname, 'rb')
          content = infile.read()
          infile.close()
 
-         return headers, content
+         return None, headers, content
 
       except IOError, msg:
          if (msg[0] == "http error") and (msg[1] == -1):
+            error = None
             bookmark.no_error = "The server did not return any header - it is not an error, actually"
             self.log('   no headers: %s' % bookmark.no_error)
          else:
-            bookmark.error = get_error(msg)
-            self.log('   Error: %s' % bookmark.error)
+            error = get_error(msg)
+            self.log('   Error: %s' % error)
 
-         return None, None
+         return error, None, None
 
    def get_ftp_welcome(self):
       global ftpcache_key