Merge pull request #250 from MaxyMoos/280-chr-support
authororakaro <orakaro@users.noreply.github.com>
Wed, 14 Mar 2018 01:40:31 +0000 (10:40 +0900)
committerGitHub <noreply@github.com>
Wed, 14 Mar 2018 01:40:31 +0000 (10:40 +0900)
Add 280 character support

rainbowstream/colorset/config
rainbowstream/draw.py
rainbowstream/rainbow.py
rainbowstream/util.py

index 54b76ff..38933ad 100644 (file)
@@ -1,4 +1,6 @@
 {
+    // Turn to 'true' in order to disable extended tweets display (legacy mode)
+    "DISABLE_EXTENDED_TWEETS" : false,
     // After 120 minutes, the stream will automatically hangup
     "HEARTBEAT_TIMEOUT" : 120,
     // Image on term
index 75e7577..62254e6 100644 (file)
@@ -175,6 +175,23 @@ def fallback_humanize(date, fallback_format=None, use_fallback=False):
     return clock
 
 
+def get_full_text(t):
+    """Handle RTs and extended tweets to always display all the available text"""
+
+    if t.get('retweeted_status'):
+        rt_status = t['retweeted_status']
+        if rt_status.get('extended_tweet'):
+            elem = rt_status['extended_tweet']
+        else:
+            elem = rt_status
+        rt_text = elem.get('full_text', elem.get('text'))
+        t['full_text'] = 'RT @' + rt_status['user']['screen_name'] + ': ' + rt_text
+    elif t.get('extended_tweet'):
+        t['full_text'] = t['extended_tweet']['full_text']
+
+    return t.get('full_text', t.get('text'))
+
+
 def draw(t, keyword=None, humanize=True, noti=False, fil=[], ig=[]):
     """
     Draw the rainbow
@@ -184,7 +201,8 @@ def draw(t, keyword=None, humanize=True, noti=False, fil=[], ig=[]):
 
     # Retrieve tweet
     tid = t['id']
-    text = t['text']
+
+    text = get_full_text(t)
     screen_name = t['user']['screen_name']
     name = t['user']['name']
     created_at = t['created_at']
@@ -201,8 +219,6 @@ def draw(t, keyword=None, humanize=True, noti=False, fil=[], ig=[]):
 
     # Pull extended retweet text
     try:
-        text = 'RT @' + t['retweeted_status']['user']['screen_name'] + ': ' +\
-            t['retweeted_status']['text']
         # Display as a notification
         target = t['retweeted_status']['user']['screen_name']
         if all([target == c['original_name'], not noti]):
@@ -1067,7 +1083,7 @@ def format_quote(tweet):
     """
     # Retrieve info
     screen_name = tweet['user']['screen_name']
-    text        = tweet['text']
+    text = get_full_text(tweet)
     tid         = str( tweet['id'] )
 
     # Validate quote format
index a499f95..aded31d 100644 (file)
@@ -330,7 +330,9 @@ def home():
     num = c['HOME_TWEET_NUM']
     if g['stuff'].isdigit():
         num = int(g['stuff'])
-    for tweet in reversed(t.statuses.home_timeline(count=num)):
+    kwargs = {'count': num}
+    kwargs = add_tweetmode_parameter(kwargs)
+    for tweet in reversed(t.statuses.home_timeline(**kwargs)):
         draw(t=tweet)
     printNicely('')
 
@@ -355,7 +357,9 @@ def mentions():
     num = c['HOME_TWEET_NUM']
     if g['stuff'].isdigit():
         num = int(g['stuff'])
-    for tweet in reversed(t.statuses.mentions_timeline(count=num)):
+    kwargs = {'count': num}
+    kwargs = add_tweetmode_parameter(kwargs)
+    for tweet in reversed(t.statuses.mentions_timeline(**kwargs)):
         draw(t=tweet)
     printNicely('')
 
@@ -398,8 +402,9 @@ def view():
             num = int(g['stuff'].split()[1])
         except:
             num = c['HOME_TWEET_NUM']
-        for tweet in reversed(
-                t.statuses.user_timeline(count=num, screen_name=user[1:])):
+        kwargs = {'count': num, 'screen_name': user[1:]}
+        kwargs = add_tweetmode_parameter(kwargs)
+        for tweet in reversed(t.statuses.user_timeline(**kwargs)):
             draw(t=tweet)
         printNicely('')
     else:
@@ -415,8 +420,10 @@ def view_my_tweets():
         num = int(g['stuff'])
     except:
         num = c['HOME_TWEET_NUM']
+    kwargs = {'count': num, 'screen_name': g['original_name']}
+    kwargs = add_tweetmode_parameter(kwargs)
     for tweet in reversed(
-            t.statuses.user_timeline(count=num, screen_name=g['original_name'])):
+            t.statuses.user_timeline(**kwargs)):
         draw(t=tweet)
     printNicely('')
 
@@ -436,12 +443,14 @@ def search():
         type = 'mixed'
     max_record = c['SEARCH_MAX_RECORD']
     count = min(max_record, 100)
+    kwargs = {
+        'q': query,
+        'type': type,
+        'count': count,
+    }
+    kwargs = add_tweetmode_parameter(kwargs)
     # Perform search
-    rel = t.search.tweets(
-        q=query,
-        type=type,
-        count=count
-    )['statuses']
+    rel = t.search.tweets(**kwargs)['statuses']
     # Return results
     if rel:
         printNicely('Newest tweets:')
@@ -535,7 +544,9 @@ def quote():
         printNicely(red('Sorry I can\'t understand.'))
         return
     tid = c['tweet_dict'][id]
-    tweet = t.statuses.show(id=tid)
+    kwargs = {'id': tid}
+    kwargs = add_tweetmode_parameter(kwargs)
+    tweet = t.statuses.show(**kwargs)
     # Get formater
     formater = format_quote(tweet)
     if not formater:
@@ -569,7 +580,9 @@ def allretweet():
     except:
         num = c['RETWEETS_SHOW_NUM']
     # Get result and display
-    rt_ary = t.statuses.retweets(id=tid, count=num)
+    kwargs = {'id': tid, 'count': num}
+    kwargs = add_tweetmode_parameter(kwargs)
+    rt_ary = t.statuses.retweets(**kwargs)
     if not rt_ary:
         printNicely(magenta('This tweet has no retweet.'))
         return
@@ -589,14 +602,17 @@ def conversation():
         printNicely(red('Sorry I can\'t understand.'))
         return
     tid = c['tweet_dict'][id]
-    tweet = t.statuses.show(id=tid)
+    kwargs = {'id': tid}
+    kwargs = add_tweetmode_parameter(kwargs)
+    tweet = t.statuses.show(**kwargs)
     limit = c['CONVERSATION_MAX']
     thread_ref = []
     thread_ref.append(tweet)
     prev_tid = tweet['in_reply_to_status_id']
     while prev_tid and limit:
         limit -= 1
-        tweet = t.statuses.show(id=prev_tid)
+        kwargs['id'] = prev_tid
+        tweet = t.statuses.show(**kwargs)
         prev_tid = tweet['in_reply_to_status_id']
         thread_ref.append(tweet)
 
@@ -663,7 +679,9 @@ def favorite():
     tid = c['tweet_dict'][id]
     t.favorites.create(_id=tid, include_entities=False)
     printNicely(green('Favorited.'))
-    draw(t.statuses.show(id=tid))
+    kwargs = {'id': tid}
+    kwargs = add_tweetmode_parameter(kwargs)
+    draw(t.statuses.show(**kwargs))
     printNicely('')
 
 
@@ -680,7 +698,9 @@ def unfavorite():
     tid = c['tweet_dict'][id]
     t.favorites.destroy(_id=tid)
     printNicely(green('Okay it\'s unfavorited.'))
-    draw(t.statuses.show(id=tid))
+    kwargs = {'id': tid}
+    kwargs = add_tweetmode_parameter(kwargs)
+    draw(t.statuses.show(**kwargs))
     printNicely('')
 
 
@@ -695,7 +715,9 @@ def share():
     except:
         printNicely(red('Tweet id is not valid.'))
         return
-    tweet = t.statuses.show(id=tid)
+    kwargs = {'id': tid}
+    kwargs = add_tweetmode_parameter(kwargs)
+    tweet = t.statuses.show(**kwargs)
     url = 'https://twitter.com/' + \
         tweet['user']['screen_name'] + '/status/' + str(tid)
     import platform
index d69cf88..a1a2a44 100644 (file)
@@ -13,7 +13,7 @@ def detail_twitter_error(twitterException):
     try:
         for m in data.get('errors', dict()):
             printNicely(yellow(m.get('message')))
-    except: 
+    except:
         printNicely(yellow(data))
 
 
@@ -47,3 +47,12 @@ def format_prefix(listname='', keyword=''):
     formattedPrefix = formattedPrefix.replace('#me', '@' + c['original_name'])
 
     return formattedPrefix
+
+
+def add_tweetmode_parameter(kwargs):
+    """
+    Add support for extended mode to Twitter API calls unless explicitly stated in config
+    """
+    if not c.get('DISABLE_EXTENDED_TWEETS'):
+        kwargs['tweet_mode'] = 'extended'
+    return kwargs