Check tweet length
authorBradley Jones <jones.bradley@me.com>
Wed, 8 Apr 2015 15:46:35 +0000 (16:46 +0100)
committerBradley Jones <jones.bradley@me.com>
Wed, 8 Apr 2015 16:22:20 +0000 (17:22 +0100)
Check the length of a tweet before sending to twitter and display error message
so user can change the message without having to wait for the twitter exception.

rainbowstream/rainbow.py

index 53fcd63..4b1ae25 100644 (file)
@@ -1941,13 +1941,15 @@ def listen():
             c['lock'] = True
             # Save cmd to global variable and call process
             g['stuff'] = ' '.join(line.split()[1:])
-            # Process the command
-            process(cmd)()
-            # Not re-display
-            if cmd in ['switch', 't', 'rt', 'rep']:
-                g['prefix'] = False
-            else:
-                g['prefix'] = True
+            # Check tweet length
+            if check_tweet_length():
+                # Process the command
+                process(cmd)()
+                # Not re-display
+                if cmd in ['switch', 't', 'rt', 'rep']:
+                    g['prefix'] = False
+                else:
+                    g['prefix'] = True
             # Release the semaphore lock
             c['lock'] = False
         except EOFError:
@@ -1957,6 +1959,18 @@ def listen():
             printNicely(red('OMG something is wrong with Twitter right now.'))
 
 
+def check_tweet_length():
+    """
+    Check tweet length (should be <= 140 chars)
+    """
+    length = len(g['stuff'])
+    if length <= 140:
+        return True
+
+    printNicely(red("Message is too long: %s chars" % length))
+    return False
+
+
 def reconn_notice():
     """
     Notice when Hangup or Timeout