10 from twitter
.util
import printNicely
11 from functools
import wraps
12 from pyfiglet
import figlet_format
13 from dateutil
import parser
14 from .c_image
import *
17 from .py3patch
import *
19 # Draw global variables
27 colors_shuffle
= [globals()[i
.encode('utf8')]
28 if not str(i
).isdigit()
29 else term_color(int(i
))
30 for i
in c
['CYCLE_COLOR']]
31 return itertools
.cycle(colors_shuffle
)
38 dg
['cyc'] = init_cycle()
44 Print a string with ordered color with each character
46 colors_shuffle
= [globals()[i
.encode('utf8')]
47 if not str(i
).isdigit()
48 else term_color(int(i
))
49 for i
in c
['CYCLE_COLOR']]
50 colored
= [colors_shuffle
[i
% 7](s
[i
]) for i
in xrange(len(s
))]
51 return ''.join(colored
)
54 def random_rainbow(s
):
56 Print a string with random color with each character
58 colors_shuffle
= [globals()[i
.encode('utf8')]
59 if not str(i
).isdigit()
60 else term_color(int(i
))
61 for i
in c
['CYCLE_COLOR']]
62 colored
= [random
.choice(colors_shuffle
)(i
) for i
in s
]
63 return ''.join(colored
)
72 if args
not in dg
['cache']:
73 dg
['cache'][args
] = func(*args
)
74 return dg
['cache'][args
]
81 Cycle the colors_shuffle
83 return next(dg
['cyc'])(s
)
90 fi
= figlet_format(text
, font
='doom')
92 [next(dg
['cyc'])(i
) for i
in fi
.split('\n')]
98 Check if config is changed
101 data
= get_all_config()
104 if data
[key
] != c
[key
]:
110 def validate_theme(theme
):
112 Validate a theme exists or not
114 # Theme changed check
115 files
= os
.listdir(os
.path
.dirname(__file__
) + '/colorset')
116 themes
= [f
.split('.')[0] for f
in files
if f
.split('.')[-1] == 'json']
117 return theme
in themes
120 def reload_theme(value
, prev
):
122 Check current theme and update if necessary
125 config
= os
.path
.dirname(
126 __file__
) + '/colorset/' + value
+ '.json'
128 data
= load_config(config
)
132 # Restart color cycle and update config
134 set_config('THEME', value
)
139 def color_func(func_name
):
141 Call color function base on name
143 if str(func_name
).isdigit():
144 return term_color(int(func_name
))
145 return globals()[func_name
]
148 def draw(t
, keyword
=None, humanize
=True, fil
=[], ig
=[]):
158 screen_name
= t
['user']['screen_name']
159 name
= t
['user']['name']
160 created_at
= t
['created_at']
161 favorited
= t
['favorited']
162 retweet_count
= t
['retweet_count']
163 favorite_count
= t
['favorite_count']
164 date
= parser
.parse(created_at
)
165 date
= arrow
.get(date
).to('local')
167 lang
, encode
= locale
.getdefaultlocale()
168 clock
= arrow
.get(date
).to('local').humanize(locale
=lang
)
171 clock_format
= c
['FORMAT']['TWEET']['CLOCK_FORMAT']
173 clock_format
= '%Y/%m/%d %H:%M:%S'
174 clock
= date
.datetime
.strftime(clock_format
)
176 # Pull extended retweet text
178 text
= 'RT @' + t
['retweeted_status']['user']['screen_name'] + ': ' +\
179 t
['retweeted_status']['text']
183 # Unescape HTML character
184 text
= unescape(text
)
190 urls
= t
['entities']['urls']
192 expanded_url
.append(u
['expanded_url'])
201 media
= t
['entities']['media']
203 media_url
.append(m
['media_url'])
208 mytweet
= screen_name
== c
['original_name']
209 screen_name
= '@' + screen_name
210 fil
= list(set((fil
or []) + c
['ONLY_LIST']))
211 ig
= list(set((ig
or []) + c
['IGNORE_LIST']))
212 if fil
and screen_name
not in fil
:
214 if ig
and screen_name
in ig
:
218 if tid
not in c
['tweet_dict']:
219 c
['tweet_dict'].append(tid
)
220 rid
= len(c
['tweet_dict']) - 1
222 rid
= c
['tweet_dict'].index(tid
)
225 name
= cycle_color(name
)
226 nick
= color_func(c
['TWEET']['nick'])(screen_name
)
231 fav
= color_func(c
['TWEET']['favorited'])(u
'\u2605')
236 for index
in xrange(len(expanded_url
)):
238 lambda x
: expanded_url
[index
]
244 lambda x
: color_func(c
['TWEET']['rt'])(x
)
248 # Highlight screen_name
249 tweet
= lmap(lambda x
: cycle_color(x
) if x
[0] == '@' else x
, tweet
)
252 lambda x
: color_func(c
['TWEET']['link'])(x
)
253 if x
.startswith('http')
258 lambda x
: color_func(c
['TWEET']['hashtag'])(x
)
264 tweet
= [color_func(c
['TWEET']['mytweet'])(x
)
268 x
.startswith('http'),
272 tweet
= ' '.join(tweet
)
274 roj
= re
.search(keyword
, tweet
, re
.IGNORECASE
)
277 ary
= tweet
.split(occur
)
278 delimiter
= color_func(c
['TWEET']['keyword'])(occur
)
279 tweet
= delimiter
.join(ary
)
281 # Load config formater
284 formater
= c
['FORMAT']['TWEET']['DISPLAY']
285 formater
= name
.join(formater
.split('#name'))
286 formater
= nick
.join(formater
.split('#nick'))
287 formater
= fav
.join(formater
.split('#fav'))
288 formater
= tweet
.join(formater
.split('#tweet'))
290 word
= [wo
for wo
in formater
.split() if '#clock' in wo
][0]
291 delimiter
= color_func(c
['TWEET']['clock'])(
292 clock
.join(word
.split('#clock')))
293 formater
= delimiter
.join(formater
.split(word
))
295 word
= [wo
for wo
in formater
.split() if '#id' in wo
][0]
296 delimiter
= color_func(c
['TWEET']['id'])(id.join(word
.split('#id')))
297 formater
= delimiter
.join(formater
.split(word
))
298 # Change retweet count word
299 word
= [wo
for wo
in formater
.split() if '#rt_count' in wo
][0]
300 delimiter
= color_func(c
['TWEET']['retweet_count'])(
301 str(retweet_count
).join(word
.split('#rt_count')))
302 formater
= delimiter
.join(formater
.split(word
))
303 # Change favorites count word
304 word
= [wo
for wo
in formater
.split() if '#fa_count' in wo
][0]
305 delimiter
= color_func(c
['TWEET']['favorite_count'])(
306 str(favorite_count
).join(word
.split('#fa_count')))
307 formater
= delimiter
.join(formater
.split(word
))
312 printNicely(formater
)
315 if c
['IMAGE_ON_TERM'] and media_url
:
318 response
= requests
.get(mu
)
319 image_to_display(BytesIO(response
.content
))
321 printNicely(red('Sorry, image link is broken'))
324 def print_threads(d
):
326 Print threads of messages
331 messages
= d
[partner
]
332 count
= len(messages
)
333 screen_name
= '@' + partner
[0]
335 screen_name
= color_func(c
['MESSAGE']['partner'])(screen_name
)
336 name
= cycle_color(name
)
337 thread_id
= color_func(c
['MESSAGE']['id'])('thread_id:' + str(id))
338 line
= ' ' * 2 + name
+ ' ' + screen_name
+ \
339 ' (' + str(count
) + ' message) ' + thread_id
347 def print_thread(partner
, me_nick
, me_name
):
349 Print a thread of messages
351 # Sort messages by time
352 messages
= dg
['thread'][partner
]
353 messages
.sort(key
=lambda x
: parser
.parse(x
['created_at']))
354 # Use legacy display on non-ascii text message
355 ms
= [m
['text'] for m
in messages
]
356 ums
= [m
for m
in ms
if not all(ord(c
) < 128 for c
in m
)]
362 # Print the first line
363 dg
['frame_margin'] = margin
= 2
364 partner_nick
= partner
[0]
365 partner_name
= partner
[1]
366 left_size
= len(partner_nick
) + len(partner_name
) + 2
367 right_size
= len(me_nick
) + len(me_name
) + 2
368 partner_nick
= color_func(c
['MESSAGE']['partner'])('@' + partner_nick
)
369 partner_name
= cycle_color(partner_name
)
370 me_screen_name
= color_func(c
['MESSAGE']['me'])('@' + me_nick
)
371 me_name
= cycle_color(me_name
)
372 left
= ' ' * margin
+ partner_name
+ ' ' + partner_nick
373 right
= me_name
+ ' ' + me_screen_name
+ ' ' * margin
374 h
, w
= os
.popen('stty size', 'r').read().split()
376 line
= '{}{}{}'.format(
377 left
, ' ' * (w
- left_size
- right_size
- 2 * margin
), right
)
383 if m
['sender_screen_name'] == me_nick
:
384 print_right_message(m
)
385 elif m
['recipient_screen_name'] == me_nick
:
386 print_left_message(m
)
389 def print_right_message(m
):
391 Print a message on the right of screen
393 h
, w
= os
.popen('stty size', 'r').read().split()
395 frame_width
= w
// 3 - dg
['frame_margin']
396 frame_width
= max(c
['THREAD_MIN_WIDTH'], frame_width
)
397 step
= frame_width
- 2 * dg
['frame_margin']
398 slicing
= textwrap
.wrap(m
['text'], step
)
399 spaces
= w
- frame_width
- dg
['frame_margin']
400 dotline
= ' ' * spaces
+ '-' * frame_width
401 dotline
= color_func(c
['MESSAGE']['me_frame'])(dotline
)
405 fill
= step
- len(line
)
406 screen_line
= ' ' * spaces
+ '| ' + line
+ ' ' * fill
+ ' '
407 if slicing
[-1] == line
:
408 screen_line
= screen_line
+ ' >'
410 screen_line
= screen_line
+ '|'
411 screen_line
= color_func(c
['MESSAGE']['me_frame'])(screen_line
)
412 printNicely(screen_line
)
415 date
= parser
.parse(m
['created_at'])
416 date
= arrow
.get(date
).to('local').datetime
417 clock_format
= '%Y/%m/%d %H:%M:%S'
419 clock_format
= c
['FORMAT']['MESSAGE']['CLOCK_FORMAT']
422 clock
= date
.strftime(clock_format
)
424 if m
['id'] not in c
['message_dict']:
425 c
['message_dict'].append(m
['id'])
426 rid
= len(c
['message_dict']) - 1
428 rid
= c
['message_dict'].index(m
['id'])
433 virtual_meta
= formater
= c
['THREAD_META_RIGHT']
434 virtual_meta
= clock
.join(virtual_meta
.split('#clock'))
435 virtual_meta
= id.join(virtual_meta
.split('#id'))
437 word
= [wo
for wo
in formater
.split() if '#clock' in wo
][0]
438 delimiter
= color_func(c
['MESSAGE']['clock'])(
439 clock
.join(word
.split('#clock')))
440 formater
= delimiter
.join(formater
.split(word
))
442 word
= [wo
for wo
in formater
.split() if '#id' in wo
][0]
443 delimiter
= color_func(c
['MESSAGE']['id'])(id.join(word
.split('#id')))
444 formater
= delimiter
.join(formater
.split(word
))
446 printNicely(red('Wrong format in config.'))
449 line
= ' ' * (w
- len(virtual_meta
) - dg
['frame_margin']) + meta
453 def print_left_message(m
):
455 Print a message on the left of screen
457 h
, w
= os
.popen('stty size', 'r').read().split()
459 frame_width
= w
// 3 - dg
['frame_margin']
460 frame_width
= max(c
['THREAD_MIN_WIDTH'], frame_width
)
461 step
= frame_width
- 2 * dg
['frame_margin']
462 slicing
= textwrap
.wrap(m
['text'], step
)
463 spaces
= dg
['frame_margin']
464 dotline
= ' ' * spaces
+ '-' * frame_width
465 dotline
= color_func(c
['MESSAGE']['partner_frame'])(dotline
)
469 fill
= step
- len(line
)
470 screen_line
= ' ' + line
+ ' ' * fill
+ ' |'
471 if slicing
[-1] == line
:
472 screen_line
= ' ' * (spaces
- 1) + '< ' + screen_line
474 screen_line
= ' ' * spaces
+ '|' + screen_line
475 screen_line
= color_func(c
['MESSAGE']['partner_frame'])(screen_line
)
476 printNicely(screen_line
)
479 date
= parser
.parse(m
['created_at'])
480 date
= arrow
.get(date
).to('local').datetime
481 clock_format
= '%Y/%m/%d %H:%M:%S'
483 clock_format
= c
['FORMAT']['MESSAGE']['CLOCK_FORMAT']
486 clock
= date
.strftime(clock_format
)
488 if m
['id'] not in c
['message_dict']:
489 c
['message_dict'].append(m
['id'])
490 rid
= len(c
['message_dict']) - 1
492 rid
= c
['message_dict'].index(m
['id'])
497 virtual_meta
= formater
= c
['THREAD_META_LEFT']
498 virtual_meta
= clock
.join(virtual_meta
.split('#clock'))
499 virtual_meta
= id.join(virtual_meta
.split('#id'))
501 word
= [wo
for wo
in formater
.split() if '#clock' in wo
][0]
502 delimiter
= color_func(c
['MESSAGE']['clock'])(
503 clock
.join(word
.split('#clock')))
504 formater
= delimiter
.join(formater
.split(word
))
506 word
= [wo
for wo
in formater
.split() if '#id' in wo
][0]
507 delimiter
= color_func(c
['MESSAGE']['id'])(id.join(word
.split('#id')))
508 formater
= delimiter
.join(formater
.split(word
))
510 printNicely(red('Wrong format in config.'))
513 line
= ' ' * dg
['frame_margin'] + meta
517 def print_message(m
):
522 sender_screen_name
= '@' + m
['sender_screen_name']
523 sender_name
= m
['sender']['name']
524 text
= unescape(m
['text'])
525 recipient_screen_name
= '@' + m
['recipient_screen_name']
526 recipient_name
= m
['recipient']['name']
528 date
= parser
.parse(m
['created_at'])
529 date
= arrow
.get(date
).to('local').datetime
530 clock_format
= '%Y/%m/%d %H:%M:%S'
532 clock_format
= c
['FORMAT']['MESSAGE']['CLOCK_FORMAT']
535 clock
= date
.strftime(clock_format
)
538 if mid
not in c
['message_dict']:
539 c
['message_dict'].append(mid
)
540 rid
= len(c
['message_dict']) - 1
542 rid
= c
['message_dict'].index(mid
)
545 sender_name
= cycle_color(sender_name
)
546 sender_nick
= color_func(c
['MESSAGE']['sender'])(sender_screen_name
)
547 recipient_name
= cycle_color(recipient_name
)
548 recipient_nick
= color_func(
549 c
['MESSAGE']['recipient'])(recipient_screen_name
)
550 to
= color_func(c
['MESSAGE']['to'])('>>>')
554 text
= ''.join(lmap(lambda x
: x
+ ' ' if x
== '\n' else x
, text
))
556 # Load config formater
558 formater
= c
['FORMAT']['MESSAGE']['DISPLAY']
559 formater
= sender_name
.join(formater
.split("#sender_name"))
560 formater
= sender_nick
.join(formater
.split("#sender_nick"))
561 formater
= to
.join(formater
.split("#to"))
562 formater
= recipient_name
.join(formater
.split("#recipient_name"))
563 formater
= recipient_nick
.join(formater
.split("#recipient_nick"))
564 formater
= text
.join(formater
.split("#message"))
566 word
= [wo
for wo
in formater
.split() if '#clock' in wo
][0]
567 delimiter
= color_func(c
['MESSAGE']['clock'])(
568 clock
.join(word
.split('#clock')))
569 formater
= delimiter
.join(formater
.split(word
))
571 word
= [wo
for wo
in formater
.split() if '#id' in wo
][0]
572 delimiter
= color_func(c
['MESSAGE']['id'])(id.join(word
.split('#id')))
573 formater
= delimiter
.join(formater
.split(word
))
575 printNicely(red('Wrong format in config.'))
579 printNicely(formater
)
588 screen_name
= u
['screen_name']
589 description
= u
['description']
590 profile_image_url
= u
['profile_image_url']
591 location
= u
['location']
593 created_at
= u
['created_at']
594 statuses_count
= u
['statuses_count']
595 friends_count
= u
['friends_count']
596 followers_count
= u
['followers_count']
599 statuses_count
= color_func(
600 c
['PROFILE']['statuses_count'])(
601 str(statuses_count
) +
603 friends_count
= color_func(
604 c
['PROFILE']['friends_count'])(
607 followers_count
= color_func(
608 c
['PROFILE']['followers_count'])(
609 str(followers_count
) +
611 count
= statuses_count
+ ' ' + friends_count
+ ' ' + followers_count
613 name
) + color_func(c
['PROFILE']['nick'])(' @' + screen_name
+ ' : ') + count
614 profile_image_raw_url
= 'Profile photo: ' + \
615 color_func(c
['PROFILE']['profile_image_url'])(profile_image_url
)
616 description
= ''.join(
617 lmap(lambda x
: x
+ ' ' * 4 if x
== '\n' else x
, description
))
618 description
= color_func(c
['PROFILE']['description'])(description
)
619 location
= 'Location : ' + color_func(c
['PROFILE']['location'])(location
)
620 url
= 'URL : ' + (color_func(c
['PROFILE']['url'])(url
) if url
else '')
621 date
= parser
.parse(created_at
)
622 lang
, encode
= locale
.getdefaultlocale()
623 clock
= arrow
.get(date
).to('local').humanize(locale
=lang
)
624 clock
= 'Join at ' + color_func(c
['PROFILE']['clock'])(clock
)
627 line1
= u
"{u:>{uw}}".format(
631 line2
= u
"{p:>{pw}}".format(
632 p
=profile_image_raw_url
,
633 pw
=len(profile_image_raw_url
) + 4,
635 line3
= u
"{d:>{dw}}".format(
637 dw
=len(description
) + 4,
639 line4
= u
"{l:>{lw}}".format(
641 lw
=len(location
) + 4,
643 line5
= u
"{u:>{uw}}".format(
647 line6
= u
"{c:>{cw}}".format(
655 if c
['IMAGE_ON_TERM']:
657 response
= requests
.get(profile_image_url
)
658 image_to_display(BytesIO(response
.content
))
663 for line
in [line3
, line4
, line5
, line6
]:
668 def print_trends(trends
):
672 for topic
in trends
[:c
['TREND_MAX']]:
675 line
= cycle_color(name
) + ': ' + color_func(c
['TREND']['url'])(url
)
680 def print_list(group
):
686 name
= grp
['full_name']
687 name
= color_func(c
['GROUP']['name'])(name
+ ' : ')
688 member
= str(grp
['member_count'])
689 member
= color_func(c
['GROUP']['member'])(member
+ ' member')
690 subscriber
= str(grp
['subscriber_count'])
691 subscriber
= color_func(
692 c
['GROUP']['subscriber'])(
695 description
= grp
['description'].strip()
696 description
= color_func(c
['GROUP']['description'])(description
)
698 mode
= color_func(c
['GROUP']['mode'])('Type: ' + mode
)
699 created_at
= grp
['created_at']
700 date
= parser
.parse(created_at
)
701 lang
, encode
= locale
.getdefaultlocale()
702 clock
= arrow
.get(date
).to('local').humanize(locale
=lang
)
703 clock
= 'Created at ' + color_func(c
['GROUP']['clock'])(clock
)
706 line1
= ' ' * 2 + name
+ member
+ ' ' + subscriber
707 line2
= ' ' * 4 + description
708 line3
= ' ' * 4 + mode
709 line4
= ' ' * 4 + clock
721 def show_calendar(month
, date
, rel
):
723 Show the calendar in rainbow mode
725 month
= random_rainbow(month
)
726 date
= ' '.join([cycle_color(i
) for i
in date
.split(' ')])
727 today
= str(int(os
.popen('date +\'%d\'').read().strip()))
732 ary
= line
.split(' ')
734 lambda x
: color_func(c
['CAL']['today'])(x
)
736 else color_func(c
['CAL']['days'])(x
),
738 printNicely(' '.join(ary
))
741 def format_quote(tweet
):
746 screen_name
= '@' + tweet
['user']['screen_name']
748 # Validate quote format
749 if '#owner' not in c
['QUOTE_FORMAT']:
750 printNicely(light_magenta('Quote should contains #owner'))
752 if '#comment' not in c
['QUOTE_FORMAT']:
753 printNicely(light_magenta('Quote format should have #comment'))
758 formater
= c
['QUOTE_FORMAT']
759 formater
= screen_name
.join(formater
.split('#owner'))
760 formater
= text
.join(formater
.split('#tweet'))
761 formater
= u2str(formater
)
764 # Highlight like a tweet
765 notice
= formater
.split()
767 lambda x
: light_green(x
)
772 lambda x
: color_func(c
['TWEET']['rt'])(x
)
776 notice
= lmap(lambda x
: cycle_color(x
) if x
[0] == '@' else x
, notice
)
778 lambda x
: color_func(c
['TWEET']['link'])(x
)
783 lambda x
: color_func(c
['TWEET']['hashtag'])(x
)
787 notice
= ' '.join(notice
)
789 notice
= light_magenta('Quoting: "') + notice
+ light_magenta('"')
794 # Start the color cycle