4fbe82723f46861c422dafb28d7ab3cb4abb7bf8
9 from twitter
.util
import printNicely
10 from functools
import wraps
11 from pyfiglet
import figlet_format
12 from dateutil
import parser
13 from .c_image
import *
16 from .py3patch
import *
18 # Draw global variables
26 colors_shuffle
= [globals()[i
.encode('utf8')]
27 if not str(i
).isdigit()
28 else term_color(int(i
))
29 for i
in c
['CYCLE_COLOR']]
30 return itertools
.cycle(colors_shuffle
)
37 dg
['cyc'] = init_cycle()
43 Print a string with ordered color with each character
45 colors_shuffle
= [globals()[i
.encode('utf8')]
46 if not str(i
).isdigit()
47 else term_color(int(i
))
48 for i
in c
['CYCLE_COLOR']]
49 colored
= [colors_shuffle
[i
% 7](s
[i
]) for i
in xrange(len(s
))]
50 return ''.join(colored
)
53 def random_rainbow(s
):
55 Print a string with random color with each character
57 colors_shuffle
= [globals()[i
.encode('utf8')]
58 if not str(i
).isdigit()
59 else term_color(int(i
))
60 for i
in c
['CYCLE_COLOR']]
61 colored
= [random
.choice(colors_shuffle
)(i
) for i
in s
]
62 return ''.join(colored
)
71 if args
not in dg
['cache']:
72 dg
['cache'][args
] = func(*args
)
73 return dg
['cache'][args
]
80 Cycle the colors_shuffle
82 return next(dg
['cyc'])(s
)
89 fi
= figlet_format(text
, font
='doom')
91 [next(dg
['cyc'])(i
) for i
in fi
.split('\n')]
97 Check if config is changed
100 data
= get_all_config()
103 if data
[key
] != c
[key
]:
109 def validate_theme(theme
):
111 Validate a theme exists or not
113 # Theme changed check
114 files
= os
.listdir(os
.path
.dirname(__file__
) + '/colorset')
115 themes
= [f
.split('.')[0] for f
in files
if f
.split('.')[-1] == 'json']
116 return theme
in themes
119 def reload_theme(value
, prev
):
121 Check current theme and update if necessary
124 config
= os
.path
.dirname(
125 __file__
) + '/colorset/' + value
+ '.json'
127 data
= load_config(config
)
131 # Restart color cycle and update config
133 set_config('THEME', value
)
138 def color_func(func_name
):
140 Call color function base on name
142 if str(func_name
).isdigit():
143 return term_color(int(func_name
))
144 return globals()[func_name
]
147 def draw(t
, keyword
=None, humanize
=True, check_semaphore
=False, fil
=[], ig
=[]):
152 # Check the semaphore pause and lock (stream process only)
165 screen_name
= t
['user']['screen_name']
166 name
= t
['user']['name']
167 created_at
= t
['created_at']
168 favorited
= t
['favorited']
169 retweet_count
= t
['retweet_count']
170 favorite_count
= t
['favorite_count']
171 date
= parser
.parse(created_at
)
172 date
= arrow
.get(date
).to('local')
174 lang
, encode
= locale
.getdefaultlocale()
175 clock
= arrow
.get(date
).to('local').humanize(locale
=lang
)
178 clock_format
= c
['FORMAT']['TWEET']['CLOCK_FORMAT']
180 clock_format
= '%Y/%m/%d %H:%M:%S'
181 clock
= date
.datetime
.strftime(clock_format
)
183 # Pull extended retweet text
185 text
= 'RT @' + t
['retweeted_status']['user']['screen_name'] + ': ' +\
186 t
['retweeted_status']['text']
190 # Unescape HTML character
191 text
= unescape(text
)
197 urls
= t
['entities']['urls']
199 expanded_url
.append(u
['expanded_url'])
208 media
= t
['entities']['media']
210 media_url
.append(m
['media_url'])
215 screen_name
= '@' + screen_name
216 fil
= list(set((fil
or []) + c
['ONLY_LIST']))
217 ig
= list(set((ig
or []) + c
['IGNORE_LIST']))
218 if fil
and screen_name
not in fil
:
220 if ig
and screen_name
in ig
:
224 if tid
not in c
['tweet_dict']:
225 c
['tweet_dict'].append(tid
)
226 rid
= len(c
['tweet_dict']) - 1
228 rid
= c
['tweet_dict'].index(tid
)
231 name
= cycle_color(name
)
232 nick
= color_func(c
['TWEET']['nick'])(screen_name
)
237 fav
= color_func(c
['TWEET']['favorited'])(u
'\u2605')
242 for index
in xrange(len(expanded_url
)):
244 lambda x
: expanded_url
[index
]
250 lambda x
: color_func(c
['TWEET']['rt'])(x
)
254 # Highlight screen_name
255 tweet
= lmap(lambda x
: cycle_color(x
) if x
[0] == '@' else x
, tweet
)
258 lambda x
: color_func(c
['TWEET']['link'])(x
)
264 lambda x
: color_func(c
['TWEET']['hashtag'])(x
)
269 tweet
= ' '.join(tweet
)
271 roj
= re
.search(keyword
, tweet
, re
.IGNORECASE
)
274 ary
= tweet
.split(occur
)
275 delimiter
= color_func(c
['TWEET']['keyword'])(occur
)
276 tweet
= delimiter
.join(ary
)
278 # Load config formater
281 formater
= c
['FORMAT']['TWEET']['DISPLAY']
282 formater
= name
.join(formater
.split('#name'))
283 formater
= nick
.join(formater
.split('#nick'))
284 formater
= fav
.join(formater
.split('#fav'))
285 formater
= tweet
.join(formater
.split('#tweet'))
287 word
= [w
for w
in formater
.split() if '#clock' in w
][0]
288 delimiter
= color_func(c
['TWEET']['clock'])(
289 clock
.join(word
.split('#clock')))
290 formater
= delimiter
.join(formater
.split(word
))
292 word
= [w
for w
in formater
.split() if '#id' in w
][0]
293 delimiter
= color_func(c
['TWEET']['id'])(id.join(word
.split('#id')))
294 formater
= delimiter
.join(formater
.split(word
))
295 # Change retweet count word
296 word
= [w
for w
in formater
.split() if '#rt_count' in w
][0]
297 delimiter
= color_func(c
['TWEET']['retweet_count'])(
298 str(retweet_count
).join(word
.split('#rt_count')))
299 formater
= delimiter
.join(formater
.split(word
))
300 # Change favorites count word
301 word
= [w
for w
in formater
.split() if '#fa_count' in w
][0]
302 delimiter
= color_func(c
['TWEET']['favorite_count'])(
303 str(favorite_count
).join(word
.split('#fa_count')))
304 formater
= delimiter
.join(formater
.split(word
))
309 printNicely(formater
)
312 if c
['IMAGE_ON_TERM'] and media_url
:
315 response
= requests
.get(mu
)
316 image_to_display(BytesIO(response
.content
))
318 printNicely(red('Sorry, image link is broken'))
321 def print_message(m
, check_semaphore
=False):
326 # Check the semaphore pause and lock (stream process only)
334 sender_screen_name
= '@' + m
['sender_screen_name']
335 sender_name
= m
['sender']['name']
336 text
= unescape(m
['text'])
337 recipient_screen_name
= '@' + m
['recipient_screen_name']
338 recipient_name
= m
['recipient']['name']
340 date
= parser
.parse(m
['created_at'])
341 date
= arrow
.get(date
).to('local').datetime
342 clock_format
= '%Y/%m/%d %H:%M:%S'
344 clock_format
= c
['FORMAT']['MESSAGE']['CLOCK_FORMAT']
347 clock
= date
.strftime(clock_format
)
350 if mid
not in c
['message_dict']:
351 c
['message_dict'].append(mid
)
352 rid
= len(c
['message_dict']) - 1
354 rid
= c
['message_dict'].index(mid
)
357 sender_name
= cycle_color(sender_name
)
358 sender_nick
= color_func(c
['MESSAGE']['sender'])(sender_screen_name
)
359 recipient_name
= cycle_color(recipient_name
)
360 recipient_nick
= color_func(
361 c
['MESSAGE']['recipient'])(recipient_screen_name
)
362 to
= color_func(c
['MESSAGE']['to'])('>>>')
366 text
= ''.join(lmap(lambda x
: x
+ ' ' if x
== '\n' else x
, text
))
368 # Load config formater
370 formater
= c
['FORMAT']['MESSAGE']['DISPLAY']
371 formater
= sender_name
.join(formater
.split("#sender_name"))
372 formater
= sender_nick
.join(formater
.split("#sender_nick"))
373 formater
= to
.join(formater
.split("#to"))
374 formater
= recipient_name
.join(formater
.split("#recipient_name"))
375 formater
= recipient_nick
.join(formater
.split("#recipient_nick"))
376 formater
= text
.join(formater
.split("#message"))
378 word
= [w
for w
in formater
.split() if '#clock' in w
][0]
379 delimiter
= color_func(c
['MESSAGE']['clock'])(
380 clock
.join(word
.split('#clock')))
381 formater
= delimiter
.join(formater
.split(word
))
383 word
= [w
for w
in formater
.split() if '#id' in w
][0]
384 delimiter
= color_func(c
['MESSAGE']['id'])(id.join(word
.split('#id')))
385 formater
= delimiter
.join(formater
.split(word
))
387 printNicely(red('Wrong format in config.'))
391 printNicely(formater
)
400 screen_name
= u
['screen_name']
401 description
= u
['description']
402 profile_image_url
= u
['profile_image_url']
403 location
= u
['location']
405 created_at
= u
['created_at']
406 statuses_count
= u
['statuses_count']
407 friends_count
= u
['friends_count']
408 followers_count
= u
['followers_count']
411 statuses_count
= color_func(
412 c
['PROFILE']['statuses_count'])(
413 str(statuses_count
) +
415 friends_count
= color_func(
416 c
['PROFILE']['friends_count'])(
419 followers_count
= color_func(
420 c
['PROFILE']['followers_count'])(
421 str(followers_count
) +
423 count
= statuses_count
+ ' ' + friends_count
+ ' ' + followers_count
425 name
) + color_func(c
['PROFILE']['nick'])(' @' + screen_name
+ ' : ') + count
426 profile_image_raw_url
= 'Profile photo: ' + \
427 color_func(c
['PROFILE']['profile_image_url'])(profile_image_url
)
428 description
= ''.join(
429 lmap(lambda x
: x
+ ' ' * 4 if x
== '\n' else x
, description
))
430 description
= color_func(c
['PROFILE']['description'])(description
)
431 location
= 'Location : ' + color_func(c
['PROFILE']['location'])(location
)
432 url
= 'URL : ' + (color_func(c
['PROFILE']['url'])(url
) if url
else '')
433 date
= parser
.parse(created_at
)
434 lang
, encode
= locale
.getdefaultlocale()
435 clock
= arrow
.get(date
).to('local').humanize(locale
=lang
)
436 clock
= 'Join at ' + color_func(c
['PROFILE']['clock'])(clock
)
439 line1
= u
"{u:>{uw}}".format(
443 line2
= u
"{p:>{pw}}".format(
444 p
=profile_image_raw_url
,
445 pw
=len(profile_image_raw_url
) + 4,
447 line3
= u
"{d:>{dw}}".format(
449 dw
=len(description
) + 4,
451 line4
= u
"{l:>{lw}}".format(
453 lw
=len(location
) + 4,
455 line5
= u
"{u:>{uw}}".format(
459 line6
= u
"{c:>{cw}}".format(
467 if c
['IMAGE_ON_TERM']:
469 response
= requests
.get(profile_image_url
)
470 image_to_display(BytesIO(response
.content
))
475 for line
in [line3
, line4
, line5
, line6
]:
480 def print_trends(trends
):
484 for topic
in trends
[:c
['TREND_MAX']]:
487 line
= cycle_color(name
) + ': ' + color_func(c
['TREND']['url'])(url
)
492 def print_list(group
):
498 name
= grp
['full_name']
499 name
= color_func(c
['GROUP']['name'])(name
+ ' : ')
500 member
= str(grp
['member_count'])
501 member
= color_func(c
['GROUP']['member'])(member
+ ' member')
502 subscriber
= str(grp
['subscriber_count'])
503 subscriber
= color_func(
504 c
['GROUP']['subscriber'])(
507 description
= grp
['description'].strip()
508 description
= color_func(c
['GROUP']['description'])(description
)
510 mode
= color_func(c
['GROUP']['mode'])('Type: ' + mode
)
511 created_at
= grp
['created_at']
512 date
= parser
.parse(created_at
)
513 lang
, encode
= locale
.getdefaultlocale()
514 clock
= arrow
.get(date
).to('local').humanize(locale
=lang
)
515 clock
= 'Created at ' + color_func(c
['GROUP']['clock'])(clock
)
518 line1
= ' ' * 2 + name
+ member
+ ' ' + subscriber
519 line2
= ' ' * 4 + description
520 line3
= ' ' * 4 + mode
521 line4
= ' ' * 4 + clock
533 def show_calendar(month
, date
, rel
):
535 Show the calendar in rainbow mode
537 month
= random_rainbow(month
)
538 date
= ' '.join([cycle_color(i
) for i
in date
.split(' ')])
539 today
= str(int(os
.popen('date +\'%d\'').read().strip()))
544 ary
= line
.split(' ')
546 lambda x
: color_func(c
['CAL']['today'])(x
)
548 else color_func(c
['CAL']['days'])(x
),
550 printNicely(' '.join(ary
))
553 def format_quote(tweet
):
558 screen_name
= '@' + tweet
['user']['screen_name']
560 # Validate quote format
561 if '#owner' not in c
['QUOTE_FORMAT']:
562 printNicely(light_magenta('Quote should contains #owner'))
564 if '#comment' not in c
['QUOTE_FORMAT']:
565 printNicely(light_magenta('Quote format should have #comment'))
570 formater
= c
['QUOTE_FORMAT']
571 formater
= screen_name
.join(formater
.split('#owner'))
572 formater
= text
.join(formater
.split('#tweet'))
573 formater
= u2str(formater
)
576 # Highlight like a tweet
577 formater
= formater
.split()
579 lambda x
: light_green(x
)
584 lambda x
: color_func(c
['TWEET']['rt'])(x
)
588 formater
= lmap(lambda x
: cycle_color(x
) if x
[0] == '@' else x
, formater
)
590 lambda x
: color_func(c
['TWEET']['link'])(x
)
595 lambda x
: color_func(c
['TWEET']['hashtag'])(x
)
599 formater
= ' '.join(formater
)
601 notice
= light_magenta('Quoting: "') + formater
+ light_magenta('"')
606 # Start the color cycle