9f8a0eb9adcf5c3dda181d1d804d813a2717166b
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 screen_name
= '@' + screen_name
209 fil
= list(set((fil
or []) + c
['ONLY_LIST']))
210 ig
= list(set((ig
or []) + c
['IGNORE_LIST']))
211 if fil
and screen_name
not in fil
:
213 if ig
and screen_name
in ig
:
217 if tid
not in c
['tweet_dict']:
218 c
['tweet_dict'].append(tid
)
219 rid
= len(c
['tweet_dict']) - 1
221 rid
= c
['tweet_dict'].index(tid
)
224 name
= cycle_color(name
)
225 nick
= color_func(c
['TWEET']['nick'])(screen_name
)
230 fav
= color_func(c
['TWEET']['favorited'])(u
'\u2605')
235 for index
in xrange(len(expanded_url
)):
237 lambda x
: expanded_url
[index
]
243 lambda x
: color_func(c
['TWEET']['rt'])(x
)
247 # Highlight screen_name
248 tweet
= lmap(lambda x
: cycle_color(x
) if x
[0] == '@' else x
, tweet
)
251 lambda x
: color_func(c
['TWEET']['link'])(x
)
257 lambda x
: color_func(c
['TWEET']['hashtag'])(x
)
262 tweet
= ' '.join(tweet
)
264 roj
= re
.search(keyword
, tweet
, re
.IGNORECASE
)
267 ary
= tweet
.split(occur
)
268 delimiter
= color_func(c
['TWEET']['keyword'])(occur
)
269 tweet
= delimiter
.join(ary
)
271 # Load config formater
274 formater
= c
['FORMAT']['TWEET']['DISPLAY']
275 formater
= name
.join(formater
.split('#name'))
276 formater
= nick
.join(formater
.split('#nick'))
277 formater
= fav
.join(formater
.split('#fav'))
278 formater
= tweet
.join(formater
.split('#tweet'))
280 word
= [wo
for wo
in formater
.split() if '#clock' in wo
][0]
281 delimiter
= color_func(c
['TWEET']['clock'])(
282 clock
.join(word
.split('#clock')))
283 formater
= delimiter
.join(formater
.split(word
))
285 word
= [wo
for wo
in formater
.split() if '#id' in wo
][0]
286 delimiter
= color_func(c
['TWEET']['id'])(id.join(word
.split('#id')))
287 formater
= delimiter
.join(formater
.split(word
))
288 # Change retweet count word
289 word
= [wo
for wo
in formater
.split() if '#rt_count' in wo
][0]
290 delimiter
= color_func(c
['TWEET']['retweet_count'])(
291 str(retweet_count
).join(word
.split('#rt_count')))
292 formater
= delimiter
.join(formater
.split(word
))
293 # Change favorites count word
294 word
= [wo
for wo
in formater
.split() if '#fa_count' in wo
][0]
295 delimiter
= color_func(c
['TWEET']['favorite_count'])(
296 str(favorite_count
).join(word
.split('#fa_count')))
297 formater
= delimiter
.join(formater
.split(word
))
302 printNicely(formater
)
305 if c
['IMAGE_ON_TERM'] and media_url
:
308 response
= requests
.get(mu
)
309 image_to_display(BytesIO(response
.content
))
311 printNicely(red('Sorry, image link is broken'))
314 def print_threads(d
):
316 Print threads of messages
321 messages
= d
[partner
]
322 count
= len(messages
)
323 screen_name
= '@' + partner
[0]
325 screen_name
= color_func(c
['MESSAGE']['partner'])(screen_name
)
326 name
= cycle_color(name
)
327 thread_id
= color_func(c
['MESSAGE']['id'])('thread_id:' + str(id))
328 line
= ' ' * 2 + name
+ ' ' + screen_name
+ \
329 ' (' + str(count
) + ' message) ' + thread_id
337 def print_thread(partner
, me_nick
, me_name
):
339 Print a thread of messages
341 # Sort messages by time
342 messages
= dg
['thread'][partner
]
343 messages
.sort(key
=lambda x
: parser
.parse(x
['created_at']))
344 # Use legacy display on non-ascii text message
345 ms
= [m
['text'] for m
in messages
]
346 ums
= [m
for m
in ms
if not all(ord(c
) < 128 for c
in m
)]
352 # Print the first line
353 dg
['frame_margin'] = margin
= 2
354 partner_nick
= partner
[0]
355 partner_name
= partner
[1]
356 left_size
= len(partner_nick
) + len(partner_name
) + 2
357 right_size
= len(me_nick
) + len(me_name
) + 2
358 partner_nick
= color_func(c
['MESSAGE']['partner'])('@' + partner_nick
)
359 partner_name
= cycle_color(partner_name
)
360 me_screen_name
= color_func(c
['MESSAGE']['me'])('@' + me_nick
)
361 me_name
= cycle_color(me_name
)
362 left
= ' ' * margin
+ partner_name
+ ' ' + partner_nick
363 right
= me_name
+ ' ' + me_screen_name
+ ' ' * margin
364 h
, w
= os
.popen('stty size', 'r').read().split()
366 line
= '{}{}{}'.format(
367 left
, ' ' * (w
- left_size
- right_size
- 2 * margin
), right
)
373 if m
['sender_screen_name'] == me_nick
:
374 print_right_message(m
)
375 elif m
['recipient_screen_name'] == me_nick
:
376 print_left_message(m
)
379 def print_right_message(m
):
381 Print a message on the right of screen
383 h
, w
= os
.popen('stty size', 'r').read().split()
385 frame_width
= w
// 3 - dg
['frame_margin']
386 step
= frame_width
- 2 * dg
['frame_margin']
387 slicing
= textwrap
.wrap(m
['text'], step
)
388 spaces
= w
- frame_width
- dg
['frame_margin']
389 dotline
= ' ' * spaces
+ '-' * frame_width
390 dotline
= color_func(c
['MESSAGE']['me_frame'])(dotline
)
394 fill
= step
- len(line
)
395 screen_line
= ' ' * spaces
+ '| ' + line
+ ' ' * fill
+ ' '
396 if slicing
[-1] == line
:
397 screen_line
= screen_line
+ ' >'
399 screen_line
= screen_line
+ '|'
400 screen_line
= color_func(c
['MESSAGE']['me_frame'])(screen_line
)
401 printNicely(screen_line
)
404 date
= parser
.parse(m
['created_at'])
405 date
= arrow
.get(date
).to('local').datetime
406 clock_format
= '%Y/%m/%d %H:%M:%S'
408 clock_format
= c
['FORMAT']['MESSAGE']['CLOCK_FORMAT']
411 clock
= date
.strftime(clock_format
)
413 if m
['id'] not in c
['message_dict']:
414 c
['message_dict'].append(m
['id'])
415 rid
= len(c
['message_dict']) - 1
417 rid
= c
['message_dict'].index(m
['id'])
422 virtual_meta
= formater
= c
['THREAD_META_RIGHT']
423 virtual_meta
= clock
.join(virtual_meta
.split('#clock'))
424 virtual_meta
= id.join(virtual_meta
.split('#id'))
426 word
= [wo
for wo
in formater
.split() if '#clock' in wo
][0]
427 delimiter
= color_func(c
['MESSAGE']['clock'])(
428 clock
.join(word
.split('#clock')))
429 formater
= delimiter
.join(formater
.split(word
))
431 word
= [wo
for wo
in formater
.split() if '#id' in wo
][0]
432 delimiter
= color_func(c
['MESSAGE']['id'])(id.join(word
.split('#id')))
433 formater
= delimiter
.join(formater
.split(word
))
435 printNicely(red('Wrong format in config.'))
438 line
= ' ' * (w
- len(virtual_meta
) - dg
['frame_margin']) + meta
442 def print_left_message(m
):
444 Print a message on the left of screen
446 h
, w
= os
.popen('stty size', 'r').read().split()
448 frame_width
= w
// 3 - dg
['frame_margin']
449 step
= frame_width
- 2 * dg
['frame_margin']
450 slicing
= textwrap
.wrap(m
['text'], step
)
451 spaces
= dg
['frame_margin']
452 dotline
= ' ' * spaces
+ '-' * frame_width
453 dotline
= color_func(c
['MESSAGE']['partner_frame'])(dotline
)
457 fill
= step
- len(line
)
458 screen_line
= ' ' + line
+ ' ' * fill
+ ' |'
459 if slicing
[-1] == line
:
460 screen_line
= ' ' * (spaces
- 1) + '< ' + screen_line
462 screen_line
= ' ' * spaces
+ '|' + screen_line
463 screen_line
= color_func(c
['MESSAGE']['partner_frame'])(screen_line
)
464 printNicely(screen_line
)
467 date
= parser
.parse(m
['created_at'])
468 date
= arrow
.get(date
).to('local').datetime
469 clock_format
= '%Y/%m/%d %H:%M:%S'
471 clock_format
= c
['FORMAT']['MESSAGE']['CLOCK_FORMAT']
474 clock
= date
.strftime(clock_format
)
476 if m
['id'] not in c
['message_dict']:
477 c
['message_dict'].append(m
['id'])
478 rid
= len(c
['message_dict']) - 1
480 rid
= c
['message_dict'].index(m
['id'])
485 virtual_meta
= formater
= c
['THREAD_META_LEFT']
486 virtual_meta
= clock
.join(virtual_meta
.split('#clock'))
487 virtual_meta
= id.join(virtual_meta
.split('#id'))
489 word
= [wo
for wo
in formater
.split() if '#clock' in wo
][0]
490 delimiter
= color_func(c
['MESSAGE']['clock'])(
491 clock
.join(word
.split('#clock')))
492 formater
= delimiter
.join(formater
.split(word
))
494 word
= [wo
for wo
in formater
.split() if '#id' in wo
][0]
495 delimiter
= color_func(c
['MESSAGE']['id'])(id.join(word
.split('#id')))
496 formater
= delimiter
.join(formater
.split(word
))
498 printNicely(red('Wrong format in config.'))
501 line
= ' ' * dg
['frame_margin'] + meta
505 def print_message(m
):
510 sender_screen_name
= '@' + m
['sender_screen_name']
511 sender_name
= m
['sender']['name']
512 text
= unescape(m
['text'])
513 recipient_screen_name
= '@' + m
['recipient_screen_name']
514 recipient_name
= m
['recipient']['name']
516 date
= parser
.parse(m
['created_at'])
517 date
= arrow
.get(date
).to('local').datetime
518 clock_format
= '%Y/%m/%d %H:%M:%S'
520 clock_format
= c
['FORMAT']['MESSAGE']['CLOCK_FORMAT']
523 clock
= date
.strftime(clock_format
)
526 if mid
not in c
['message_dict']:
527 c
['message_dict'].append(mid
)
528 rid
= len(c
['message_dict']) - 1
530 rid
= c
['message_dict'].index(mid
)
533 sender_name
= cycle_color(sender_name
)
534 sender_nick
= color_func(c
['MESSAGE']['sender'])(sender_screen_name
)
535 recipient_name
= cycle_color(recipient_name
)
536 recipient_nick
= color_func(
537 c
['MESSAGE']['recipient'])(recipient_screen_name
)
538 to
= color_func(c
['MESSAGE']['to'])('>>>')
542 text
= ''.join(lmap(lambda x
: x
+ ' ' if x
== '\n' else x
, text
))
544 # Load config formater
546 formater
= c
['FORMAT']['MESSAGE']['DISPLAY']
547 formater
= sender_name
.join(formater
.split("#sender_name"))
548 formater
= sender_nick
.join(formater
.split("#sender_nick"))
549 formater
= to
.join(formater
.split("#to"))
550 formater
= recipient_name
.join(formater
.split("#recipient_name"))
551 formater
= recipient_nick
.join(formater
.split("#recipient_nick"))
552 formater
= text
.join(formater
.split("#message"))
554 word
= [wo
for wo
in formater
.split() if '#clock' in wo
][0]
555 delimiter
= color_func(c
['MESSAGE']['clock'])(
556 clock
.join(word
.split('#clock')))
557 formater
= delimiter
.join(formater
.split(word
))
559 word
= [wo
for wo
in formater
.split() if '#id' in wo
][0]
560 delimiter
= color_func(c
['MESSAGE']['id'])(id.join(word
.split('#id')))
561 formater
= delimiter
.join(formater
.split(word
))
563 printNicely(red('Wrong format in config.'))
567 printNicely(formater
)
576 screen_name
= u
['screen_name']
577 description
= u
['description']
578 profile_image_url
= u
['profile_image_url']
579 location
= u
['location']
581 created_at
= u
['created_at']
582 statuses_count
= u
['statuses_count']
583 friends_count
= u
['friends_count']
584 followers_count
= u
['followers_count']
587 statuses_count
= color_func(
588 c
['PROFILE']['statuses_count'])(
589 str(statuses_count
) +
591 friends_count
= color_func(
592 c
['PROFILE']['friends_count'])(
595 followers_count
= color_func(
596 c
['PROFILE']['followers_count'])(
597 str(followers_count
) +
599 count
= statuses_count
+ ' ' + friends_count
+ ' ' + followers_count
601 name
) + color_func(c
['PROFILE']['nick'])(' @' + screen_name
+ ' : ') + count
602 profile_image_raw_url
= 'Profile photo: ' + \
603 color_func(c
['PROFILE']['profile_image_url'])(profile_image_url
)
604 description
= ''.join(
605 lmap(lambda x
: x
+ ' ' * 4 if x
== '\n' else x
, description
))
606 description
= color_func(c
['PROFILE']['description'])(description
)
607 location
= 'Location : ' + color_func(c
['PROFILE']['location'])(location
)
608 url
= 'URL : ' + (color_func(c
['PROFILE']['url'])(url
) if url
else '')
609 date
= parser
.parse(created_at
)
610 lang
, encode
= locale
.getdefaultlocale()
611 clock
= arrow
.get(date
).to('local').humanize(locale
=lang
)
612 clock
= 'Join at ' + color_func(c
['PROFILE']['clock'])(clock
)
615 line1
= u
"{u:>{uw}}".format(
619 line2
= u
"{p:>{pw}}".format(
620 p
=profile_image_raw_url
,
621 pw
=len(profile_image_raw_url
) + 4,
623 line3
= u
"{d:>{dw}}".format(
625 dw
=len(description
) + 4,
627 line4
= u
"{l:>{lw}}".format(
629 lw
=len(location
) + 4,
631 line5
= u
"{u:>{uw}}".format(
635 line6
= u
"{c:>{cw}}".format(
643 if c
['IMAGE_ON_TERM']:
645 response
= requests
.get(profile_image_url
)
646 image_to_display(BytesIO(response
.content
))
651 for line
in [line3
, line4
, line5
, line6
]:
656 def print_trends(trends
):
660 for topic
in trends
[:c
['TREND_MAX']]:
663 line
= cycle_color(name
) + ': ' + color_func(c
['TREND']['url'])(url
)
668 def print_list(group
):
674 name
= grp
['full_name']
675 name
= color_func(c
['GROUP']['name'])(name
+ ' : ')
676 member
= str(grp
['member_count'])
677 member
= color_func(c
['GROUP']['member'])(member
+ ' member')
678 subscriber
= str(grp
['subscriber_count'])
679 subscriber
= color_func(
680 c
['GROUP']['subscriber'])(
683 description
= grp
['description'].strip()
684 description
= color_func(c
['GROUP']['description'])(description
)
686 mode
= color_func(c
['GROUP']['mode'])('Type: ' + mode
)
687 created_at
= grp
['created_at']
688 date
= parser
.parse(created_at
)
689 lang
, encode
= locale
.getdefaultlocale()
690 clock
= arrow
.get(date
).to('local').humanize(locale
=lang
)
691 clock
= 'Created at ' + color_func(c
['GROUP']['clock'])(clock
)
694 line1
= ' ' * 2 + name
+ member
+ ' ' + subscriber
695 line2
= ' ' * 4 + description
696 line3
= ' ' * 4 + mode
697 line4
= ' ' * 4 + clock
709 def show_calendar(month
, date
, rel
):
711 Show the calendar in rainbow mode
713 month
= random_rainbow(month
)
714 date
= ' '.join([cycle_color(i
) for i
in date
.split(' ')])
715 today
= str(int(os
.popen('date +\'%d\'').read().strip()))
720 ary
= line
.split(' ')
722 lambda x
: color_func(c
['CAL']['today'])(x
)
724 else color_func(c
['CAL']['days'])(x
),
726 printNicely(' '.join(ary
))
729 def format_quote(tweet
):
734 screen_name
= '@' + tweet
['user']['screen_name']
736 # Validate quote format
737 if '#owner' not in c
['QUOTE_FORMAT']:
738 printNicely(light_magenta('Quote should contains #owner'))
740 if '#comment' not in c
['QUOTE_FORMAT']:
741 printNicely(light_magenta('Quote format should have #comment'))
746 formater
= c
['QUOTE_FORMAT']
747 formater
= screen_name
.join(formater
.split('#owner'))
748 formater
= text
.join(formater
.split('#tweet'))
749 formater
= u2str(formater
)
752 # Highlight like a tweet
753 notice
= formater
.split()
755 lambda x
: light_green(x
)
760 lambda x
: color_func(c
['TWEET']['rt'])(x
)
764 notice
= lmap(lambda x
: cycle_color(x
) if x
[0] == '@' else x
, notice
)
766 lambda x
: color_func(c
['TWEET']['link'])(x
)
771 lambda x
: color_func(c
['TWEET']['hashtag'])(x
)
775 notice
= ' '.join(notice
)
777 notice
= light_magenta('Quoting: "') + notice
+ light_magenta('"')
782 # Start the color cycle