add color for hashtag and try catch for quit() in ctrl+c
[rainbowstream.git] / rainbowstream / rainbow.py
index 4a1e3d78c02c6624126aecec5a66724fd48a1dba..a9ee2bab32b421d1d5dcee1dcbcdff27ade09dfe 100644 (file)
@@ -143,6 +143,9 @@ def init(args):
     """
     Init function
     """
+    # Handle Ctrl C
+    ctrl_c_handler = lambda signum, frame: quit()
+    signal.signal(signal.SIGINT, ctrl_c_handler)
     # Get name
     t = Twitter(auth=authen())
     name = '@' + t.account.verify_credentials()['screen_name']
@@ -1468,9 +1471,13 @@ def quit():
     """
     Exit all
     """
-    save_history()
-    os.system('rm -rf rainbow.db')
-    os.kill(g['stream_pid'], signal.SIGKILL)
+    try:
+        save_history()
+        os.system('rm -rf rainbow.db')
+        os.kill(g['stream_pid'], signal.SIGKILL)
+        printNicely(green('See you next time :)'))
+    except:
+        pass
     sys.exit()
 
 
@@ -1619,7 +1626,7 @@ def listen():
         g['cmd'] = cmd
         try:
             # Lock the semaphore
-            db.semaphore_update_flag(True)
+            db.semaphore_update_lock(True)
             # Save cmd to global variable and call process
             g['stuff'] = ' '.join(line.split()[1:])
             # Process the command
@@ -1630,7 +1637,7 @@ def listen():
             else:
                 g['prefix'] = True
             # Release the semaphore lock
-            db.semaphore_update_flag(False)
+            db.semaphore_update_lock(False)
         except Exception:
             printNicely(red('OMG something is wrong with Twitter right now.'))
 
@@ -1689,7 +1696,7 @@ def stream(domain, args, name='Rainbow Stream'):
                     ig=args.ignore,
                 )
             elif tweet.get('direct_message'):
-                print_message(tweet['direct_message'])
+                print_message(tweet['direct_message'],check_semaphore=True)
     except TwitterHTTPError:
         printNicely('')
         printNicely(
@@ -1707,9 +1714,8 @@ def fly():
     except TwitterHTTPError:
         printNicely('')
         printNicely(
-            magenta("Something wrong with Twitter Oauth right now :("))
-        printNicely(
-            magenta("Please delete ~/.rainbow_oauth and try again."))
+            magenta("We have maximum connection problem with twitter'stream API right now :("))
+        printNicely(magenta("Let's try again later."))
         save_history()
         os.system('rm -rf rainbow.db')
         sys.exit()