post_process.redirect: Log exceptions as warnings
authorW. Trevor King <wking@tremily.us>
Sun, 28 Sep 2014 17:47:15 +0000 (10:47 -0700)
committerW. Trevor King <wking@tremily.us>
Sun, 28 Sep 2014 17:47:15 +0000 (10:47 -0700)
When we hit an exception trying to unwind any redirects, just print
the exception and leave the link with its original value.

This commit also alphabetizes the standard library imports, and splits
off the local import following PEP 8's suggested groupings [1].

[1]: http://legacy.python.org/dev/peps/pep-0008/#imports

Signed-off-by: W. Trevor King <wking@tremily.us>
rss2email/post_process/redirect.py

index 289fc65839b6ea9593fe9d406f2f5f047655d338..b237fbcba324ef8857de4d62184489d047b9a1ef 100644 (file)
@@ -22,10 +22,16 @@ statistics. You may want to avoid this for privacy or for durability.
 This hook finds and uses the real url behind redirects.
 """
 
 This hook finds and uses the real url behind redirects.
 """
 
-import urllib
+import logging
 import re
 import re
+import urllib
+
 import rss2email
 
 import rss2email
 
+
+LOG = _logging.getLogger(__name__)
+
+
 def process(feed, parsed, entry, guid, message):
     # decode message
     encoding = message.get_charsets()[0]
 def process(feed, parsed, entry, guid, message):
     # decode message
     encoding = message.get_charsets()[0]
@@ -52,7 +58,9 @@ def process(feed, parsed, entry, guid, message):
             request = urllib.request.Request(link)
             request.add_header('User-agent', rss2email.feed._USER_AGENT)
             direct_link = urllib.request.urlopen(request).geturl()
             request = urllib.request.Request(link)
             request.add_header('User-agent', rss2email.feed._USER_AGENT)
             direct_link = urllib.request.urlopen(request).geturl()
-        except:
+        except Exception as e:
+            LOG.warning('could not follow redirect for {}: {}'.format(
+                linke, e))
             continue
         content = re.sub(re.escape(link), direct_link, content, re.MULTILINE)
 
             continue
         content = re.sub(re.escape(link), direct_link, content, re.MULTILINE)