]> git.phdru.name Git - bookmarks_db.git/blobdiff - Robots/bkmk_rurllib.py
Feat(robots): Handle HTTP redirect 308
[bookmarks_db.git] / Robots / bkmk_rurllib.py
index 20b74239b8fb55fc184b08e2cf686ce8ca150983..067799c737271b5ceeb373eeb321bc12eaf1e7ec 100644 (file)
@@ -11,9 +11,7 @@ __license__ = "GNU GPL"
 __all__ = ['robot_urllib']
 
 
-import os
 import sys
-import time
 import urllib
 
 from Robots.bkmk_robot_base import robot_base, get_error
@@ -29,9 +27,9 @@ class RedirectException(Exception):
 class MyURLopener(urllib.URLopener):
     # Error 301 -- relocated (permanently)
     def http_error_301(self, url, fp, errcode, errmsg, headers, data=None):
-        if headers.has_key('location'):
+        if 'location' in headers:
             newurl = headers['location']
-        elif headers.has_key('uri'):
+        elif 'uri' in headers:
             newurl = headers['uri']
         else:
             newurl = "Nowhere"
@@ -43,6 +41,8 @@ class MyURLopener(urllib.URLopener):
     http_error_303 = http_error_301
     # Error 307 -- relocated (temporarily)
     http_error_307 = http_error_301
+    # Error 308 -- relocated (permanently)
+    http_error_308 = http_error_301
 
     # Error 401 -- authentication required
     def http_error_401(self, url, fp, errcode, errmsg, headers, data=None):
@@ -51,7 +51,7 @@ class MyURLopener(urllib.URLopener):
 
     def http_error_default(self, url, fp, errcode, errmsg, headers):
         if fp:
-            void = fp.read()
+            fp.read()
             fp.close()
         raise IOError(('http error', errcode, errmsg, headers))
 
@@ -104,7 +104,7 @@ class robot_urllib(robot_base):
                     # Remove Accept-Charset
                     del urllib._urlopener.addheaders[-1]
 
-            infile = open(fname, 'rb')
+            infile = open(fname, 'rt')
             content = infile.read()
             infile.close()
 
@@ -117,7 +117,7 @@ class robot_urllib(robot_base):
             if (e[0] == "http error") and (e[1] == -1):
                 error = None
                 bookmark.no_error = "The server did not return any header - "
-                    "it is not an error, actually"
+                "it is not an error, actually"
                 self.log('   no headers: %s' % bookmark.no_error)
             else:
                 error = get_error(e)