X-Git-Url: https://vcs.fsf.org/?p=rainbowstream.git;a=blobdiff_plain;f=rainbowstream%2Fdraw.py;h=747a68b4d8d9eb1c4136878a47f89a2a67f59f61;hp=262fe4f492c39f51386ef2e2652c5bd1e494e40a;hb=223d2e05c222e9023fdbf5c31db7d032668a0f96;hpb=71dc8644aa63543527c5f303c1a0c93fdb120d21 diff --git a/rainbowstream/draw.py b/rainbowstream/draw.py index 262fe4f..747a68b 100644 --- a/rainbowstream/draw.py +++ b/rainbowstream/draw.py @@ -1,15 +1,15 @@ import random import itertools import requests -import datetime -import time +import locale +import arrow import re +import os from twitter.util import printNicely from functools import wraps from pyfiglet import figlet_format from dateutil import parser -from tzlocal import get_localzone from .c_image import * from .colors import * from .config import * @@ -144,18 +144,10 @@ def color_func(func_name): return globals()[func_name] -def draw(t, keyword=None, check_semaphore=False, fil=[], ig=[]): +def draw(t, keyword=None, humanize=True, fil=[], ig=[]): """ Draw the rainbow """ - - # Check the semaphore pause and lock (stream process only) - if check_semaphore: - if c['pause']: - return - while c['lock']: - time.sleep(0.5) - # Check config check_config() @@ -169,13 +161,16 @@ def draw(t, keyword=None, check_semaphore=False, fil=[], ig=[]): retweet_count = t['retweet_count'] favorite_count = t['favorite_count'] date = parser.parse(created_at) - date = date.astimezone(get_localzone()) - clock_format = '%Y/%m/%d %H:%M:%S' - try: - clock_format = c['FORMAT']['TWEET']['CLOCK_FORMAT'] - except: - pass - clock = date.strftime(clock_format) + date = arrow.get(date).to('local') + if humanize: + lang, encode = locale.getdefaultlocale() + clock = arrow.get(date).to('local').humanize(locale=lang) + else: + try: + clock_format = c['FORMAT']['TWEET']['CLOCK_FORMAT'] + except: + clock_format = '%Y/%m/%d %H:%M:%S' + clock = date.datetime.strftime(clock_format) # Pull extended retweet text try: @@ -315,18 +310,160 @@ def draw(t, keyword=None, check_semaphore=False, fil=[], ig=[]): printNicely(red('Sorry, image link is broken')) -def print_message(m, check_semaphore=False): +def print_threads(d): """ - Print direct message + Print threads of messages """ + id = 1 + rel = {} + for partner in d: + messages = d[partner] + count = len(messages) + screen_name = '@' + partner[0] + name = partner[1] + screen_name = color_func(c['MESSAGE']['partner'])(screen_name) + name = cycle_color(name) + thread_id = color_func(c['MESSAGE']['id'])('thread id:'+str(id)) + line = ' '*2 + name + ' ' + screen_name + \ + ' (' + str(count) + ' message) ' + thread_id + printNicely(line) + rel[id] = partner + id += 1 + dg['thread'] = d + return rel + + +def print_thread(partner, me_nick, me_name): + """ + Print a thread of messages + """ + # Sort messages by time + messages = dg['thread'][partner] + messages.sort(key = lambda x:parser.parse(x['created_at'])) + # Print the 1st line + dg['message_thread_margin'] = margin = 2 + left_size = len(partner[0])+len(partner[1]) + margin + right_size = len(me_nick) + len(me_name) + margin + partner_screen_name = color_func(c['MESSAGE']['partner'])('@' + partner[0]) + partner_name = cycle_color(partner[1]) + me_screen_name = color_func(c['MESSAGE']['me'])('@' + me_nick) + me_name = cycle_color(me_name) + left = ' ' * margin + partner_name + ' ' + partner_screen_name + right = me_name + ' ' + me_screen_name + ' ' * margin + h, w = os.popen('stty size', 'r').read().split() + w = int(w) + line = '{}{}{}'.format(left, ' '*(w - left_size - right_size - 2 * margin), right) + printNicely('') + printNicely(line) + printNicely('') + # Print messages + for m in messages: + if m['sender_screen_name'] == me_nick: + print_right_message(m) + elif m['recipient_screen_name'] == me_nick: + print_left_message(m) + + +def print_right_message(m): + """ + Print a message on the right of screen + """ + h, w = os.popen('stty size', 'r').read().split() + w = int(w) + frame_width = w //3 - dg['message_thread_margin'] + step = frame_width - 2 * dg['message_thread_margin'] + slicing = [m['text'][i:i+step] for i in range(0, len(m['text']), step)] + spaces = w - frame_width - dg['message_thread_margin'] + dotline = ' ' * spaces + '-' * frame_width + dotline = color_func(c['MESSAGE']['me_bg'])(dotline) + # Draw the frame + printNicely(dotline) + for line in slicing: + fill = step - len(line) + screen_line = ' ' * spaces + '| ' + line + ' ' * fill + ' ' + if slicing[-1] == line: + screen_line = screen_line + ' >' + else: + screen_line = screen_line + '|' + screen_line = color_func(c['MESSAGE']['me_bg'])(screen_line) + printNicely(screen_line) + printNicely(dotline) + # Print clock + date = parser.parse(m['created_at']) + date = arrow.get(date).to('local').datetime + clock_format = '%Y/%m/%d %H:%M:%S' + try: + clock_format = c['FORMAT']['MESSAGE']['CLOCK_FORMAT'] + except: + pass + clock = date.strftime(clock_format) + # Get rainbow id + if m['id'] not in c['message_dict']: + c['message_dict'].append(m['id']) + rid = len(c['message_dict']) - 1 + else: + rid = c['message_dict'].index(m['id']) + rid = str(rid) + # Create line and print + meta = color_func(c['MESSAGE']['clock'])(clock) + \ + color_func(c['MESSAGE']['id'])(' ('+rid+')') + line = ' ' * (w - len(clock + rid) - 3 - dg['message_thread_margin']) + \ + meta + printNicely(line) + + +def print_left_message(m): + """ + Print a message on the left of screen + """ + h, w = os.popen('stty size', 'r').read().split() + w = int(w) + frame_width = w //3 - dg['message_thread_margin'] + step = frame_width - 2 * dg['message_thread_margin'] + slicing = [m['text'][i:i+step] for i in range(0, len(m['text']), step)] + spaces = dg['message_thread_margin'] + dotline = ' ' * spaces + '-' * frame_width + dotline = color_func(c['MESSAGE']['partner_bg'])(dotline) + # Draw the frame + printNicely(dotline) + for line in slicing: + fill = step - len(line) + screen_line = ' ' + line + ' ' * fill + ' |' + if slicing[-1] == line: + screen_line = ' ' * (spaces-1) + '< ' + screen_line + else: + screen_line = ' ' * spaces + '|' + screen_line + screen_line = color_func(c['MESSAGE']['partner_bg'])(screen_line) + printNicely(screen_line) + printNicely(dotline) + # Print clock + date = parser.parse(m['created_at']) + date = arrow.get(date).to('local').datetime + clock_format = '%Y/%m/%d %H:%M:%S' + try: + clock_format = c['FORMAT']['MESSAGE']['CLOCK_FORMAT'] + except: + pass + clock = date.strftime(clock_format) + # Get rainbow id + if m['id'] not in c['message_dict']: + c['message_dict'].append(m['id']) + rid = len(c['message_dict']) - 1 + else: + rid = c['message_dict'].index(m['id']) + rid = str(rid) + # Create line and print + meta = color_func(c['MESSAGE']['clock'])(clock) + \ + color_func(c['MESSAGE']['id'])(' ('+rid+')') + line = ' ' * dg['message_thread_margin'] + \ + meta + printNicely(line) - # Check the semaphore pause and lock (stream process only) - if check_semaphore: - if c['pause']: - return - while c['lock']: - time.sleep(0.5) +def print_message(m): + """ + Print direct message + """ # Retrieve message sender_screen_name = '@' + m['sender_screen_name'] sender_name = m['sender']['name'] @@ -335,7 +472,7 @@ def print_message(m, check_semaphore=False): recipient_name = m['recipient']['name'] mid = m['id'] date = parser.parse(m['created_at']) - date = date.astimezone(get_localzone()) + date = arrow.get(date).to('local').datetime clock_format = '%Y/%m/%d %H:%M:%S' try: clock_format = c['FORMAT']['MESSAGE']['CLOCK_FORMAT'] @@ -428,8 +565,8 @@ def show_profile(u): location = 'Location : ' + color_func(c['PROFILE']['location'])(location) url = 'URL : ' + (color_func(c['PROFILE']['url'])(url) if url else '') date = parser.parse(created_at) - date = date.astimezone(get_localzone()) - clock = date.strftime('%Y/%m/%d %H:%M:%S') + lang, encode = locale.getdefaultlocale() + clock = arrow.get(date).to('local').humanize(locale=lang) clock = 'Join at ' + color_func(c['PROFILE']['clock'])(clock) # Format @@ -507,8 +644,8 @@ def print_list(group): mode = color_func(c['GROUP']['mode'])('Type: ' + mode) created_at = grp['created_at'] date = parser.parse(created_at) - date = date.astimezone(get_localzone()) - clock = date.strftime('%Y/%m/%d %H:%M:%S') + lang, encode = locale.getdefaultlocale() + clock = arrow.get(date).to('local').humanize(locale=lang) clock = 'Created at ' + color_func(c['GROUP']['clock'])(clock) # Create lines @@ -571,31 +708,31 @@ def format_quote(tweet): except: pass # Highlight like a tweet - formater = formater.split() - formater = lmap( + notice = formater.split() + notice = lmap( lambda x: light_green(x) if x == '#comment' else x, - formater) - formater = lmap( + notice) + notice = lmap( lambda x: color_func(c['TWEET']['rt'])(x) if x == 'RT' else x, - formater) - formater = lmap(lambda x: cycle_color(x) if x[0] == '@' else x, formater) - formater = lmap( + notice) + notice = lmap(lambda x: cycle_color(x) if x[0] == '@' else x, notice) + notice = lmap( lambda x: color_func(c['TWEET']['link'])(x) if x[0:4] == 'http' else x, - formater) - formater = lmap( + notice) + notice = lmap( lambda x: color_func(c['TWEET']['hashtag'])(x) if x.startswith('#') else x, - formater) - formater = ' '.join(formater) + notice) + notice = ' '.join(notice) # Notice - notice = light_magenta('Quoting: "') + formater + light_magenta('"') + notice = light_magenta('Quoting: "') + notice + light_magenta('"') printNicely(notice) return formater