]> jfr.im git - z_archive/twitter.git/blobdiff - twitter/util.py
bandaid unicode/str.encode-related crash bug
[z_archive/twitter.git] / twitter / util.py
index 8d66c4e8e8e56e58bd4bd468e2708405072e9602..5a5f50710f4d8ed8d7d5a0c65f4bc97671f5f072 100644 (file)
@@ -10,7 +10,11 @@ from __future__ import print_function
 import contextlib
 import re
 import sys
+import textwrap
 import time
+import socket
+
+PY_3_OR_HIGHER = sys.version_info >= (3, 0)
 
 try:
     from html.entities import name2codepoint
@@ -47,14 +51,32 @@ def printNicely(string):
     if hasattr(sys.stdout, 'buffer'):
         sys.stdout.buffer.write(string.encode('utf8'))
         print()
+        sys.stdout.buffer.flush()
+        sys.stdout.flush()
     else:
         print(string.encode('utf8'))
 
-__all__ = ["htmlentitydecode", "smrt_input"]
+def actually_bytes(stringy):
+    if PY_3_OR_HIGHER:
+        if type(stringy) == bytes:
+            pass
+        elif type(stringy) != str:
+            stringy = str(stringy)
+        if type(stringy) == str:
+            stringy = stringy.encode("utf-8")
+    else:
+        if type(stringy) == str:
+            pass
+        elif type(stringy) != unicode:
+            stringy = str(stringy)
+        if type(stringy) == unicode:
+            stringy = stringy.encode("utf-8")
+    return stringy
 
 def err(msg=""):
     print(msg, file=sys.stderr)
 
+
 class Fail(object):
     """A class to count fails during a repetitive task.
 
@@ -116,17 +138,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."""
@@ -134,3 +163,15 @@ def parse_host_list(list_of_hosts):
         m.group(1) for m in re.finditer("\s*([^,\s]+)\s*,?\s*", list_of_hosts))
     return p
 
+
+def align_text(text, left_margin=17, max_width=160):
+    lines = []
+    for line in text.split('\n'):
+        temp_lines = textwrap.wrap(line, max_width - left_margin)
+        temp_lines = [(' ' * left_margin + line) for line in temp_lines]
+        lines.append('\n'.join(temp_lines))
+    ret = '\n'.join(lines)
+    return ret.lstrip()
+
+
+__all__ = ["htmlentitydecode", "smrt_input"]