X-Git-Url: https://git.phdru.name/?a=blobdiff_plain;f=Robots%2Fbkmk_rurllib.py;h=6c10148af5688b26fc4eba60c7f34247a8d581bc;hb=c19fbf714f025012d089aa86e4fa8e3b46f3a01e;hp=efade79a328d9c8724877cf1819b5b86570c66e3;hpb=8fedded7827ae1d79b31ec38d8e3146bb1a7bea1;p=bookmarks_db.git diff --git a/Robots/bkmk_rurllib.py b/Robots/bkmk_rurllib.py index efade79..6c10148 100644 --- a/Robots/bkmk_rurllib.py +++ b/Robots/bkmk_rurllib.py @@ -13,9 +13,15 @@ __all__ = ['robot_urllib'] import sys, os import time, urllib -from Robots.bkmk_robot_base import robot_base, RedirectException, get_error +from Robots.bkmk_robot_base import robot_base, get_error +class RedirectException(Exception): + def __init__(self, errcode, newurl): + Exception.__init__(self) + self.errcode = errcode + self.newurl = newurl + class MyURLopener(urllib.URLopener): # Error 301 -- relocated (permanently) def http_error_301(self, url, fp, errcode, errmsg, headers, data=None): @@ -74,22 +80,27 @@ 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, None + return None, None, None, headers, content + + except RedirectException, e: + return None, e.errcode, e.newurl, None, None except IOError, msg: if (msg[0] == "http error") and (msg[1] == -1): @@ -100,7 +111,7 @@ class robot_urllib(robot_base): error = get_error(msg) self.log(' Error: %s' % error) - return None, None, error + return error, None, None, None, None def get_ftp_welcome(self): global ftpcache_key