From fc46012423806779f53872ce82288a5827050fbb Mon Sep 17 00:00:00 2001 From: Bradley Jones Date: Wed, 8 Apr 2015 16:46:35 +0100 Subject: [PATCH] Check tweet length 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 | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/rainbowstream/rainbow.py b/rainbowstream/rainbow.py index 53fcd63..4b1ae25 100644 --- a/rainbowstream/rainbow.py +++ b/rainbowstream/rainbow.py @@ -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 -- 2.25.1