-def draw(t, iot=False, keyword=None, fil=[], ig=[]):
- """
- Draw the rainbow
- """
-
- # Retrieve tweet
- tid = t['id']
- text = t['text']
- screen_name = t['user']['screen_name']
- name = t['user']['name']
- created_at = t['created_at']
- favorited = t['favorited']
- date = parser.parse(created_at)
- date = date - datetime.timedelta(seconds=time.timezone)
- clock = date.strftime('%Y/%m/%d %H:%M:%S')
-
- # Get expanded url
- try:
- expanded_url = []
- url = []
- urls = t['entities']['urls']
- for u in urls:
- expanded_url.append(u['expanded_url'])
- url.append(u['url'])
- except:
- expanded_url = None
- url = None
-
- # Get media
- try:
- media_url = []
- media = t['entities']['media']
- for m in media:
- media_url.append(m['media_url'])
- except:
- media_url = None
-
- # Filter and ignore
- screen_name = '@' + screen_name
- if fil and screen_name not in fil:
- return
- if ig and screen_name in ig:
- return
-
- # Get rainbow id
- res = db.tweet_to_rainbow_query(tid)
- if not res:
- db.tweet_store(tid)
- res = db.tweet_to_rainbow_query(tid)
- rid = res[0].rainbow_id
-
- # Format info
- user = cycle_color(name) + grey(' ' + screen_name + ' ')
- meta = grey('[' + clock + '] [id=' + str(rid) + '] ')
- if favorited:
- meta = meta + green(u'\u2605')
- tweet = text.split()
- # Replace url
- if expanded_url:
- for index in range(len(expanded_url)):
- tweet = map(
- lambda x: expanded_url[index] if x == url[index] else x,
- tweet)
- # Highlight RT
- tweet = map(lambda x: grey(x) if x == 'RT' else x, tweet)
- # Highlight screen_name
- tweet = map(lambda x: cycle_color(x) if x[0] == '@' else x, tweet)
- # Highlight link
- tweet = map(lambda x: cyan(x) if x[0:7] == 'http://' else x, tweet)
- # Highlight search keyword
- if keyword:
- tweet = map(
- lambda x: on_yellow(x) if
- ''.join(c for c in x if c.isalnum()).lower() == keyword.lower()
- else x,
- tweet
- )
- # Recreate tweet
- tweet = ' '.join(tweet)
-
- # Draw rainbow
- line1 = u"{u:>{uw}}:".format(
- u=user,
- uw=len(user) + 2,
- )
- line2 = u"{c:>{cw}}".format(
- c=meta,
- cw=len(meta) + 2,
- )
- line3 = ' ' + tweet
-
- printNicely('')
- printNicely(line1)
- printNicely(line2)
- printNicely(line3)
-
- # Display Image
- if iot and media_url:
- for mu in media_url:
- response = requests.get(mu)
- image_to_display(StringIO(response.content))
-
-
-def print_message(m):
- """
- Print direct message
- """
- screen_name = '@' + m['sender_screen_name']
- name = m['sender']['name']
- text = m['text']
- mid = m['id']
- date = parser.parse(m['created_at'])
- date = date - datetime.timedelta(seconds=time.timezone)
- clock = date.strftime('%Y/%m/%d %H:%M:%S')
-
- # Get rainbow id
- res = db.message_to_rainbow_query(mid)
- if not res:
- db.message_store(mid)
- res = db.message_to_rainbow_query(mid)
- rid = res[0].rainbow_id
-
- user = cycle_color(name) + grey(' ' + screen_name + ' ')
- meta = grey('[' + clock + '] [message_id=' + str(rid) + '] ')
- text = ''.join(map(lambda x: x+' ' if x=='\n' else x,text))
-
- line1 = u"{u:>{uw}}:".format(
- u=user,
- uw=len(user) + 2,
- )
- line2 = u"{c:>{cw}}".format(
- c=meta,
- cw=len(meta) + 2,
- )
-
- line3 = ' ' + text
-
- printNicely('')
- printNicely(line1)
- printNicely(line2)
- printNicely(line3)
-
-