Added `tweet_mode='extended'` option to `t.statuses` calls
[rainbowstream.git] / rainbowstream / draw.py
index 429bd279220512ef7e353c2d8253941e187b6777..a8bd803436c227c55d98ffcf8c1c87c979caa3cb 100644 (file)
@@ -7,6 +7,7 @@ import arrow
 import re
 import os
 
+from io import BytesIO
 from twitter.util import printNicely
 from functools import wraps
 from pyfiglet import figlet_format
@@ -174,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
@@ -183,13 +201,15 @@ 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']
     favorited = t['favorited']
     retweet_count = t['retweet_count']
     favorite_count = t['favorite_count']
+    client = t['source']
     date = parser.parse(created_at)
     try:
         clock_format = c['FORMAT']['TWEET']['CLOCK_FORMAT']
@@ -199,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]):
@@ -215,6 +233,12 @@ def draw(t, keyword=None, humanize=True, noti=False, fil=[], ig=[]):
     # Unescape HTML character
     text = unescape(text)
 
+    # Get client name
+    try:
+        client = client.split('>')[-2].split('<')[0]
+    except:
+        client = None
+
     # Get expanded url
     try:
         expanded_url = []
@@ -265,7 +289,8 @@ def draw(t, keyword=None, humanize=True, noti=False, fil=[], ig=[]):
     if favorited:
         fav = color_func(c['TWEET']['favorited'])(u'\u2605')
 
-    tweet = text.split()
+    tweet = text.split(' ')
+    tweet = [x for x in tweet if x != '']
     # Replace url
     if expanded_url:
         for index in xrange(len(expanded_url)):
@@ -281,17 +306,18 @@ def draw(t, keyword=None, humanize=True, noti=False, fil=[], ig=[]):
         else x,
         tweet)
     # Highlight screen_name
-    tweet = lmap(lambda x: cycle_color(x) if x[0] == '@' else x, tweet)
+    tweet = lmap(
+        lambda x: cycle_color(x) if x.lstrip().startswith('@') else x, tweet)
     # Highlight link
     tweet = lmap(
         lambda x: color_func(c['TWEET']['link'])(x)
-        if x.startswith('http')
+        if x.lstrip().startswith('http')
         else x,
         tweet)
     # Highlight hashtag
     tweet = lmap(
         lambda x: color_func(c['TWEET']['hashtag'])(x)
-        if x.startswith('#')
+        if x.lstrip().startswith('#')
         else x,
         tweet)
     # Highlight my tweet
@@ -300,11 +326,12 @@ def draw(t, keyword=None, humanize=True, noti=False, fil=[], ig=[]):
                  for x in tweet
                  if not any([
                      x == 'RT',
-                     x.startswith('http'),
-                     x.startswith('#')])
+                     x.lstrip().startswith('http'),
+                     x.lstrip().startswith('#')])
                  ]
     # Highlight keyword
     tweet = ' '.join(tweet)
+    tweet = '\n  '.join(tweet.split('\n'))
     if keyword:
         roj = re.search(keyword, tweet, re.IGNORECASE)
         if roj:
@@ -321,6 +348,7 @@ def draw(t, keyword=None, humanize=True, noti=False, fil=[], ig=[]):
         formater = nick.join(formater.split('#nick'))
         formater = fav.join(formater.split('#fav'))
         formater = tweet.join(formater.split('#tweet'))
+        formater = emojize(formater)
         # Change clock word
         word = [wo for wo in formater.split() if '#clock' in wo][0]
         delimiter = color_func(c['TWEET']['clock'])(
@@ -340,7 +368,11 @@ def draw(t, keyword=None, humanize=True, noti=False, fil=[], ig=[]):
         delimiter = color_func(c['TWEET']['favorite_count'])(
             str(favorite_count).join(word.split('#fa_count')))
         formater = delimiter.join(formater.split(word))
-        formater = emojize(formater)
+        # Change client word
+        word = [wo for wo in formater.split() if '#client' in wo][0]
+        delimiter = color_func(c['TWEET']['client'])(
+            client.join(word.split('#client')))
+        formater = delimiter.join(formater.split(word))
     except:
         pass
 
@@ -924,7 +956,7 @@ def show_profile(u):
     url = 'URL : ' + (color_func(c['PROFILE']['url'])(url) if url else '')
     date = parser.parse(created_at)
     clock = fallback_humanize(date)
-    clock = 'Join at ' + color_func(c['PROFILE']['clock'])(clock)
+    clock = 'Joined ' + color_func(c['PROFILE']['clock'])(clock)
 
     # Format
     line1 = u"{u:>{uw}}".format(
@@ -1050,8 +1082,10 @@ def format_quote(tweet):
     Quoting format
     """
     # Retrieve info
-    screen_name = '@' + tweet['user']['screen_name']
-    text = tweet['text']
+    screen_name = tweet['user']['screen_name']
+    text = get_full_text(t)
+    tid         = str( tweet['id'] )
+
     # Validate quote format
     if '#owner' not in c['QUOTE_FORMAT']:
         printNicely(light_magenta('Quote should contains #owner'))
@@ -1059,13 +1093,16 @@ def format_quote(tweet):
     if '#comment' not in c['QUOTE_FORMAT']:
         printNicely(light_magenta('Quote format should have #comment'))
         return False
+
     # Build formater
     formater = ''
     try:
         formater = c['QUOTE_FORMAT']
-        formater = screen_name.join(formater.split('#owner'))
-        formater = text.join(formater.split('#tweet'))
-        formater = u2str(formater)
+
+        formater = formater.replace('#owner', screen_name)
+        formater = formater.replace('#tweet', text)
+        formater = formater.replace('#tid',   tid)
+
         formater = emojize(formater)
     except:
         pass