]> jfr.im git - z_archive/twitter.git/blobdiff - twitter/util.py
Merge pull request #226 from hugovk/master
[z_archive/twitter.git] / twitter / util.py
index 3d9a4bb514dbb1188692139d67d3ea51b2e36c2e..7831939a2c1e2a24b2e40573cefe5a92c16d72de 100644 (file)
@@ -12,6 +12,7 @@ import re
 import sys
 import textwrap
 import time
+import socket
 
 try:
     from html.entities import name2codepoint
@@ -117,17 +118,24 @@ def follow_redirects(link, sites= None):
     req = urllib2.Request(link)
     req.get_method = lambda : 'HEAD'
     try:
-        with contextlib.closing(opener.open(req)) as site:
+        with contextlib.closing(opener.open(req,timeout=1)) as site:
             return site.url
-    except (urllib2.HTTPError, urllib2.URLError):
+    except:
         return redirect_handler.last_url if redirect_handler.last_url else link
 
 def expand_line(line, sites):
     """Expand the links in the line for the given sites."""
-    l = line.strip()
-    msg_format, links = find_links(l)
-    args = tuple(follow_redirects(l, sites) for l in links)
-    return msg_format % args
+    try:
+        l = line.strip()
+        msg_format, links = find_links(l)
+        args = tuple(follow_redirects(l, sites) for l in links)
+        line = msg_format % args
+    except Exception as e:
+        try:
+            err("expanding line %s failed due to %s" % (line, unicode(e)))
+        except:
+            pass
+    return line
 
 def parse_host_list(list_of_hosts):
     """Parse the comma separated list of hosts."""