fix image IOError
[rainbowstream.git] / rainbowstream / draw.py
index 60d0ef624bf2928c6108a70e4508666acf480b23..57e7867497265c4aea9d7ffea54c3ffbac7e045f 100644 (file)
@@ -28,13 +28,15 @@ def init_cycle():
         for i in c['CYCLE_COLOR']]
     return itertools.cycle(colors_shuffle)
 g['cyc'] = init_cycle()
+g['cache'] = {}
 
 
-def notify_cycle():
+def reset_cycle():
     """
     Notify from rainbow
     """
     g['cyc'] = init_cycle()
+    g['cache'] = {}
 
 
 def order_rainbow(s):
@@ -65,13 +67,11 @@ def Memoize(func):
     """
     Memoize decorator
     """
-    cache = {}
-
     @wraps(func)
     def wrapper(*args):
-        if args not in cache:
-            cache[args] = func(*args)
-        return cache[args]
+        if args not in g['cache']:
+            g['cache'][args] = func(*args)
+        return g['cache'][args]
     return wrapper
 
 
@@ -256,8 +256,11 @@ def draw(t, iot=False, keyword=None, fil=[], ig=[]):
     # Display Image
     if iot and media_url:
         for mu in media_url:
-            response = requests.get(mu)
-            image_to_display(StringIO(response.content))
+            try:
+                response = requests.get(mu)
+                image_to_display(StringIO(response.content))
+            except:
+                printNicely(red('Sorry, image link is broken'))
 
 
 def print_message(m):
@@ -386,8 +389,11 @@ def show_profile(u, iot=False):
     printNicely('')
     printNicely(line1)
     if iot:
-        response = requests.get(profile_image_url)
-        image_to_display(StringIO(response.content), 2, 20)
+        try:
+            response = requests.get(profile_image_url)
+            image_to_display(StringIO(response.content), 2, 20)
+        except:
+            pass
     else:
         printNicely(line2)
     for line in [line3, line4, line5, line6]: