import random
import itertools
import requests
-import time
import locale
import arrow
import re
+import os
from twitter.util import printNicely
from functools import wraps
printNicely(red('Sorry, image link is broken'))
+def print_threads(d):
+ """
+ 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
+
+ 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 + '|'
+ printNicely(screen_line)
+ printNicely(dotline)
+
+
+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
+
+ 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
+ printNicely(screen_line)
+ printNicely(dotline)
+
+
def print_message(m):
"""
Print direct message
signal.signal(signal.SIGINT, ctrl_c_handler)
# Get name
t = Twitter(auth=authen())
- name = '@' + t.account.verify_credentials()['screen_name']
+ credential = t.account.verify_credentials()
+ screen_name = '@' + credential['screen_name']
+ name = credential['name']
if not get_config('PREFIX'):
- set_config('PREFIX', name)
- g['original_name'] = name[1:]
+ set_config('PREFIX', screen_name)
+ g['original_name'] = screen_name[1:]
+ g['full_name'] = name
g['decorated_name'] = lambda x: color_func(
c['DECORATED_NAME'])('[' + x + ']: ')
# Theme init
themes = [f.split('.')[0] for f in files if f.split('.')[-1] == 'json']
g['themes'] = themes
g['pause'] = False
+ g['message_threads'] = {}
# Startup cmd
g['cmd'] = ''
# Semaphore init
def inbox():
"""
- Inbox direct messages
+ Inbox threads
"""
t = Twitter(auth=authen())
num = c['MESSAGES_DISPLAY']
- rel = []
if g['stuff'].isdigit():
num = g['stuff']
+ # Get inbox messages
cur_page = 1
- # Max message per page is 20 so we have to loop
+ inbox = []
while num > 20:
- rel = rel + t.direct_messages(
+ inbox = inbox + t.direct_messages(
count=20,
page=cur_page,
include_entities=False,
)
num -= 20
cur_page += 1
- rel = rel + t.direct_messages(
+ inbox = inbox + t.direct_messages(
count=num,
page=cur_page,
include_entities=False,
skip_status=False
)
- # Display
- printNicely('Inbox: newest ' + str(len(rel)) + ' messages.')
- for m in reversed(rel):
- print_message(m)
- printNicely('')
-
-
-def sent():
- """
- Sent direct messages
- """
- t = Twitter(auth=authen())
+ # Get sent messages
num = c['MESSAGES_DISPLAY']
- rel = []
if g['stuff'].isdigit():
- num = int(g['stuff'])
+ num = g['stuff']
cur_page = 1
- # Max message per page is 20 so we have to loop
+ sent = []
while num > 20:
- rel = rel + t.direct_messages.sent(
+ sent = sent + t.direct_messages.sent(
count=20,
page=cur_page,
include_entities=False,
)
num -= 20
cur_page += 1
- rel = rel + t.direct_messages.sent(
+ sent = sent + t.direct_messages.sent(
count=num,
page=cur_page,
include_entities=False,
skip_status=False
)
- # Display
- printNicely('Sent: newest ' + str(len(rel)) + ' messages.')
- for m in reversed(rel):
- print_message(m)
- printNicely('')
+
+ d = {}
+ uniq_inbox = list(set(
+ [(m['sender_screen_name'],m['sender']['name']) for m in inbox]
+ ))
+ uniq_sent= list(set(
+ [(m['recipient_screen_name'],m['recipient']['name']) for m in sent]
+ ))
+ for partner in uniq_inbox:
+ inbox_ary = [m for m in inbox if m['sender_screen_name'] == partner[0]]
+ sent_ary = [m for m in sent if m['recipient_screen_name'] == partner[0]]
+ d[partner] = inbox_ary + sent_ary
+ for partner in uniq_sent:
+ if partner not in d:
+ d[partner] = [m for m in sent if m['recipient_screen_name'] == partner[0]]
+ g['message_threads'] = print_threads(d)
+
+
+def thread():
+ """
+ View a thread of message
+ """
+ try:
+ thread_id = int(g['stuff'])
+ print_thread(g['message_threads'][thread_id],g['original_name'],g['full_name'])
+ except Exception as e:
+ print(e)
+ import traceback
+ print(traceback.format_exc())
+ printNicely(red('No such thread.'))
def message():
'open',
'ls',
'inbox',
- 'sent',
+ 'thread',
'trash',
'whois',
'fl',
urlopen,
ls,
inbox,
- sent,
+ thread,
trash,
whois,
follow,
[''], # open url
['fl', 'fr'], # list
[], # inbox
- [], # sent
+ [i for i in g['message_threads']], #sent
[], # trash
['@'], # whois
['@'], # follow