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
)
227 nick
= color_func(c
['TWEET']['mynick'])(screen_name
)
229 nick
= color_func(c
['TWEET']['nick'])(screen_name
)
234 fav
= color_func(c
['TWEET']['favorited'])(u
'\u2605')
239 for index
in xrange(len(expanded_url
)):
241 lambda x
: expanded_url
[index
]
247 lambda x
: color_func(c
['TWEET']['rt'])(x
)
251 # Highlight screen_name
252 tweet
= lmap(lambda x
: cycle_color(x
) if x
[0] == '@' else x
, tweet
)
255 lambda x
: color_func(c
['TWEET']['link'])(x
)
256 if x
.startswith('http')
261 lambda x
: color_func(c
['TWEET']['hashtag'])(x
)
267 tweet
= [color_func(c
['TWEET']['mytweet'])(x
)
271 x
.startswith('http'),
275 tweet
= ' '.join(tweet
)
277 roj
= re
.search(keyword
, tweet
, re
.IGNORECASE
)
280 ary
= tweet
.split(occur
)
281 delimiter
= color_func(c
['TWEET']['keyword'])(occur
)
282 tweet
= delimiter
.join(ary
)
284 # Load config formater
287 formater
= c
['FORMAT']['TWEET']['DISPLAY']
288 formater
= name
.join(formater
.split('#name'))
289 formater
= nick
.join(formater
.split('#nick'))
290 formater
= fav
.join(formater
.split('#fav'))
291 formater
= tweet
.join(formater
.split('#tweet'))
293 word
= [wo
for wo
in formater
.split() if '#clock' in wo
][0]
294 delimiter
= color_func(c
['TWEET']['clock'])(
295 clock
.join(word
.split('#clock')))
296 formater
= delimiter
.join(formater
.split(word
))
298 word
= [wo
for wo
in formater
.split() if '#id' in wo
][0]
299 delimiter
= color_func(c
['TWEET']['id'])(id.join(word
.split('#id')))
300 formater
= delimiter
.join(formater
.split(word
))
301 # Change retweet count word
302 word
= [wo
for wo
in formater
.split() if '#rt_count' in wo
][0]
303 delimiter
= color_func(c
['TWEET']['retweet_count'])(
304 str(retweet_count
).join(word
.split('#rt_count')))
305 formater
= delimiter
.join(formater
.split(word
))
306 # Change favorites count word
307 word
= [wo
for wo
in formater
.split() if '#fa_count' in wo
][0]
308 delimiter
= color_func(c
['TWEET']['favorite_count'])(
309 str(favorite_count
).join(word
.split('#fa_count')))
310 formater
= delimiter
.join(formater
.split(word
))
315 printNicely(formater
)
318 if c
['IMAGE_ON_TERM'] and media_url
:
321 response
= requests
.get(mu
)
322 image_to_display(BytesIO(response
.content
))
324 printNicely(red('Sorry, image link is broken'))
327 def print_threads(d
):
329 Print threads of messages
334 messages
= d
[partner
]
335 count
= len(messages
)
336 screen_name
= '@' + partner
[0]
338 screen_name
= color_func(c
['MESSAGE']['partner'])(screen_name
)
339 name
= cycle_color(name
)
340 thread_id
= color_func(c
['MESSAGE']['id'])('thread_id:' + str(id))
341 line
= ' ' * 2 + name
+ ' ' + screen_name
+ \
342 ' (' + str(count
) + ' message) ' + thread_id
350 def print_thread(partner
, me_nick
, me_name
):
352 Print a thread of messages
354 # Sort messages by time
355 messages
= dg
['thread'][partner
]
356 messages
.sort(key
=lambda x
: parser
.parse(x
['created_at']))
357 # Use legacy display on non-ascii text message
358 ms
= [m
['text'] for m
in messages
]
359 ums
= [m
for m
in ms
if not all(ord(c
) < 128 for c
in m
)]
365 # Print the first line
366 dg
['frame_margin'] = margin
= 2
367 partner_nick
= partner
[0]
368 partner_name
= partner
[1]
369 left_size
= len(partner_nick
) + len(partner_name
) + 2
370 right_size
= len(me_nick
) + len(me_name
) + 2
371 partner_nick
= color_func(c
['MESSAGE']['partner'])('@' + partner_nick
)
372 partner_name
= cycle_color(partner_name
)
373 me_screen_name
= color_func(c
['MESSAGE']['me'])('@' + me_nick
)
374 me_name
= cycle_color(me_name
)
375 left
= ' ' * margin
+ partner_name
+ ' ' + partner_nick
376 right
= me_name
+ ' ' + me_screen_name
+ ' ' * margin
377 h
, w
= os
.popen('stty size', 'r').read().split()
379 line
= '{}{}{}'.format(
380 left
, ' ' * (w
- left_size
- right_size
- 2 * margin
), right
)
386 if m
['sender_screen_name'] == me_nick
:
387 print_right_message(m
)
388 elif m
['recipient_screen_name'] == me_nick
:
389 print_left_message(m
)
392 def print_right_message(m
):
394 Print a message on the right of screen
396 h
, w
= os
.popen('stty size', 'r').read().split()
398 frame_width
= w
// 3 - dg
['frame_margin']
399 frame_width
= max(c
['THREAD_MIN_WIDTH'], frame_width
)
400 step
= frame_width
- 2 * dg
['frame_margin']
401 slicing
= textwrap
.wrap(m
['text'], step
)
402 spaces
= w
- frame_width
- dg
['frame_margin']
403 dotline
= ' ' * spaces
+ '-' * frame_width
404 dotline
= color_func(c
['MESSAGE']['me_frame'])(dotline
)
408 fill
= step
- len(line
)
409 screen_line
= ' ' * spaces
+ '| ' + line
+ ' ' * fill
+ ' '
410 if slicing
[-1] == line
:
411 screen_line
= screen_line
+ ' >'
413 screen_line
= screen_line
+ '|'
414 screen_line
= color_func(c
['MESSAGE']['me_frame'])(screen_line
)
415 printNicely(screen_line
)
418 date
= parser
.parse(m
['created_at'])
419 date
= arrow
.get(date
).to('local').datetime
420 clock_format
= '%Y/%m/%d %H:%M:%S'
422 clock_format
= c
['FORMAT']['MESSAGE']['CLOCK_FORMAT']
425 clock
= date
.strftime(clock_format
)
427 if m
['id'] not in c
['message_dict']:
428 c
['message_dict'].append(m
['id'])
429 rid
= len(c
['message_dict']) - 1
431 rid
= c
['message_dict'].index(m
['id'])
436 virtual_meta
= formater
= c
['THREAD_META_RIGHT']
437 virtual_meta
= clock
.join(virtual_meta
.split('#clock'))
438 virtual_meta
= id.join(virtual_meta
.split('#id'))
440 word
= [wo
for wo
in formater
.split() if '#clock' in wo
][0]
441 delimiter
= color_func(c
['MESSAGE']['clock'])(
442 clock
.join(word
.split('#clock')))
443 formater
= delimiter
.join(formater
.split(word
))
445 word
= [wo
for wo
in formater
.split() if '#id' in wo
][0]
446 delimiter
= color_func(c
['MESSAGE']['id'])(id.join(word
.split('#id')))
447 formater
= delimiter
.join(formater
.split(word
))
449 printNicely(red('Wrong format in config.'))
452 line
= ' ' * (w
- len(virtual_meta
) - dg
['frame_margin']) + meta
456 def print_left_message(m
):
458 Print a message on the left of screen
460 h
, w
= os
.popen('stty size', 'r').read().split()
462 frame_width
= w
// 3 - dg
['frame_margin']
463 frame_width
= max(c
['THREAD_MIN_WIDTH'], frame_width
)
464 step
= frame_width
- 2 * dg
['frame_margin']
465 slicing
= textwrap
.wrap(m
['text'], step
)
466 spaces
= dg
['frame_margin']
467 dotline
= ' ' * spaces
+ '-' * frame_width
468 dotline
= color_func(c
['MESSAGE']['partner_frame'])(dotline
)
472 fill
= step
- len(line
)
473 screen_line
= ' ' + line
+ ' ' * fill
+ ' |'
474 if slicing
[-1] == line
:
475 screen_line
= ' ' * (spaces
- 1) + '< ' + screen_line
477 screen_line
= ' ' * spaces
+ '|' + screen_line
478 screen_line
= color_func(c
['MESSAGE']['partner_frame'])(screen_line
)
479 printNicely(screen_line
)
482 date
= parser
.parse(m
['created_at'])
483 date
= arrow
.get(date
).to('local').datetime
484 clock_format
= '%Y/%m/%d %H:%M:%S'
486 clock_format
= c
['FORMAT']['MESSAGE']['CLOCK_FORMAT']
489 clock
= date
.strftime(clock_format
)
491 if m
['id'] not in c
['message_dict']:
492 c
['message_dict'].append(m
['id'])
493 rid
= len(c
['message_dict']) - 1
495 rid
= c
['message_dict'].index(m
['id'])
500 virtual_meta
= formater
= c
['THREAD_META_LEFT']
501 virtual_meta
= clock
.join(virtual_meta
.split('#clock'))
502 virtual_meta
= id.join(virtual_meta
.split('#id'))
504 word
= [wo
for wo
in formater
.split() if '#clock' in wo
][0]
505 delimiter
= color_func(c
['MESSAGE']['clock'])(
506 clock
.join(word
.split('#clock')))
507 formater
= delimiter
.join(formater
.split(word
))
509 word
= [wo
for wo
in formater
.split() if '#id' in wo
][0]
510 delimiter
= color_func(c
['MESSAGE']['id'])(id.join(word
.split('#id')))
511 formater
= delimiter
.join(formater
.split(word
))
513 printNicely(red('Wrong format in config.'))
516 line
= ' ' * dg
['frame_margin'] + meta
520 def print_message(m
):
525 sender_screen_name
= '@' + m
['sender_screen_name']
526 sender_name
= m
['sender']['name']
527 text
= unescape(m
['text'])
528 recipient_screen_name
= '@' + m
['recipient_screen_name']
529 recipient_name
= m
['recipient']['name']
531 date
= parser
.parse(m
['created_at'])
532 date
= arrow
.get(date
).to('local').datetime
533 clock_format
= '%Y/%m/%d %H:%M:%S'
535 clock_format
= c
['FORMAT']['MESSAGE']['CLOCK_FORMAT']
538 clock
= date
.strftime(clock_format
)
541 if mid
not in c
['message_dict']:
542 c
['message_dict'].append(mid
)
543 rid
= len(c
['message_dict']) - 1
545 rid
= c
['message_dict'].index(mid
)
548 sender_name
= cycle_color(sender_name
)
549 sender_nick
= color_func(c
['MESSAGE']['sender'])(sender_screen_name
)
550 recipient_name
= cycle_color(recipient_name
)
551 recipient_nick
= color_func(
552 c
['MESSAGE']['recipient'])(recipient_screen_name
)
553 to
= color_func(c
['MESSAGE']['to'])('>>>')
557 text
= ''.join(lmap(lambda x
: x
+ ' ' if x
== '\n' else x
, text
))
559 # Load config formater
561 formater
= c
['FORMAT']['MESSAGE']['DISPLAY']
562 formater
= sender_name
.join(formater
.split("#sender_name"))
563 formater
= sender_nick
.join(formater
.split("#sender_nick"))
564 formater
= to
.join(formater
.split("#to"))
565 formater
= recipient_name
.join(formater
.split("#recipient_name"))
566 formater
= recipient_nick
.join(formater
.split("#recipient_nick"))
567 formater
= text
.join(formater
.split("#message"))
569 word
= [wo
for wo
in formater
.split() if '#clock' in wo
][0]
570 delimiter
= color_func(c
['MESSAGE']['clock'])(
571 clock
.join(word
.split('#clock')))
572 formater
= delimiter
.join(formater
.split(word
))
574 word
= [wo
for wo
in formater
.split() if '#id' in wo
][0]
575 delimiter
= color_func(c
['MESSAGE']['id'])(id.join(word
.split('#id')))
576 formater
= delimiter
.join(formater
.split(word
))
578 printNicely(red('Wrong format in config.'))
582 printNicely(formater
)
591 screen_name
= u
['screen_name']
592 description
= u
['description']
593 profile_image_url
= u
['profile_image_url']
594 location
= u
['location']
596 created_at
= u
['created_at']
597 statuses_count
= u
['statuses_count']
598 friends_count
= u
['friends_count']
599 followers_count
= u
['followers_count']
602 statuses_count
= color_func(
603 c
['PROFILE']['statuses_count'])(
604 str(statuses_count
) +
606 friends_count
= color_func(
607 c
['PROFILE']['friends_count'])(
610 followers_count
= color_func(
611 c
['PROFILE']['followers_count'])(
612 str(followers_count
) +
614 count
= statuses_count
+ ' ' + friends_count
+ ' ' + followers_count
616 name
) + color_func(c
['PROFILE']['nick'])(' @' + screen_name
+ ' : ') + count
617 profile_image_raw_url
= 'Profile photo: ' + \
618 color_func(c
['PROFILE']['profile_image_url'])(profile_image_url
)
619 description
= ''.join(
620 lmap(lambda x
: x
+ ' ' * 4 if x
== '\n' else x
, description
))
621 description
= color_func(c
['PROFILE']['description'])(description
)
622 location
= 'Location : ' + color_func(c
['PROFILE']['location'])(location
)
623 url
= 'URL : ' + (color_func(c
['PROFILE']['url'])(url
) if url
else '')
624 date
= parser
.parse(created_at
)
625 lang
, encode
= locale
.getdefaultlocale()
626 clock
= arrow
.get(date
).to('local').humanize(locale
=lang
)
627 clock
= 'Join at ' + color_func(c
['PROFILE']['clock'])(clock
)
630 line1
= u
"{u:>{uw}}".format(
634 line2
= u
"{p:>{pw}}".format(
635 p
=profile_image_raw_url
,
636 pw
=len(profile_image_raw_url
) + 4,
638 line3
= u
"{d:>{dw}}".format(
640 dw
=len(description
) + 4,
642 line4
= u
"{l:>{lw}}".format(
644 lw
=len(location
) + 4,
646 line5
= u
"{u:>{uw}}".format(
650 line6
= u
"{c:>{cw}}".format(
658 if c
['IMAGE_ON_TERM']:
660 response
= requests
.get(profile_image_url
)
661 image_to_display(BytesIO(response
.content
))
666 for line
in [line3
, line4
, line5
, line6
]:
671 def print_trends(trends
):
675 for topic
in trends
[:c
['TREND_MAX']]:
678 line
= cycle_color(name
) + ': ' + color_func(c
['TREND']['url'])(url
)
683 def print_list(group
):
689 name
= grp
['full_name']
690 name
= color_func(c
['GROUP']['name'])(name
+ ' : ')
691 member
= str(grp
['member_count'])
692 member
= color_func(c
['GROUP']['member'])(member
+ ' member')
693 subscriber
= str(grp
['subscriber_count'])
694 subscriber
= color_func(
695 c
['GROUP']['subscriber'])(
698 description
= grp
['description'].strip()
699 description
= color_func(c
['GROUP']['description'])(description
)
701 mode
= color_func(c
['GROUP']['mode'])('Type: ' + mode
)
702 created_at
= grp
['created_at']
703 date
= parser
.parse(created_at
)
704 lang
, encode
= locale
.getdefaultlocale()
705 clock
= arrow
.get(date
).to('local').humanize(locale
=lang
)
706 clock
= 'Created at ' + color_func(c
['GROUP']['clock'])(clock
)
709 line1
= ' ' * 2 + name
+ member
+ ' ' + subscriber
710 line2
= ' ' * 4 + description
711 line3
= ' ' * 4 + mode
712 line4
= ' ' * 4 + clock
724 def show_calendar(month
, date
, rel
):
726 Show the calendar in rainbow mode
728 month
= random_rainbow(month
)
729 date
= ' '.join([cycle_color(i
) for i
in date
.split(' ')])
730 today
= str(int(os
.popen('date +\'%d\'').read().strip()))
735 ary
= line
.split(' ')
737 lambda x
: color_func(c
['CAL']['today'])(x
)
739 else color_func(c
['CAL']['days'])(x
),
741 printNicely(' '.join(ary
))
744 def format_quote(tweet
):
749 screen_name
= '@' + tweet
['user']['screen_name']
751 # Validate quote format
752 if '#owner' not in c
['QUOTE_FORMAT']:
753 printNicely(light_magenta('Quote should contains #owner'))
755 if '#comment' not in c
['QUOTE_FORMAT']:
756 printNicely(light_magenta('Quote format should have #comment'))
761 formater
= c
['QUOTE_FORMAT']
762 formater
= screen_name
.join(formater
.split('#owner'))
763 formater
= text
.join(formater
.split('#tweet'))
764 formater
= u2str(formater
)
767 # Highlight like a tweet
768 notice
= formater
.split()
770 lambda x
: light_green(x
)
775 lambda x
: color_func(c
['TWEET']['rt'])(x
)
779 notice
= lmap(lambda x
: cycle_color(x
) if x
[0] == '@' else x
, notice
)
781 lambda x
: color_func(c
['TWEET']['link'])(x
)
786 lambda x
: color_func(c
['TWEET']['hashtag'])(x
)
790 notice
= ' '.join(notice
)
792 notice
= light_magenta('Quoting: "') + notice
+ light_magenta('"')
797 # Start the color cycle