11fa0000fb2fbf06e929a72d160fc1ea5ebd8758
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, check_semaphore
=False, fil
=[], ig
=[]):
153 # Check the semaphore pause and lock (stream process only)
166 screen_name
= t
['user']['screen_name']
167 name
= t
['user']['name']
168 created_at
= t
['created_at']
169 favorited
= t
['favorited']
170 retweet_count
= t
['retweet_count']
171 favorite_count
= t
['favorite_count']
172 date
= parser
.parse(created_at
)
173 date
= arrow
.get(date
).to('local')
175 lang
, encode
= locale
.getdefaultlocale()
176 clock
= arrow
.get(date
).to('local').humanize(locale
=lang
)
179 clock_format
= c
['FORMAT']['TWEET']['CLOCK_FORMAT']
181 clock_format
= '%Y/%m/%d %H:%M:%S'
182 clock
= date
.datetime
.strftime(clock_format
)
184 # Pull extended retweet text
186 text
= 'RT @' + t
['retweeted_status']['user']['screen_name'] + ': ' +\
187 t
['retweeted_status']['text']
191 # Unescape HTML character
192 text
= unescape(text
)
198 urls
= t
['entities']['urls']
200 expanded_url
.append(u
['expanded_url'])
209 media
= t
['entities']['media']
211 media_url
.append(m
['media_url'])
216 screen_name
= '@' + screen_name
217 fil
= list(set((fil
or []) + c
['ONLY_LIST']))
218 ig
= list(set((ig
or []) + c
['IGNORE_LIST']))
219 if fil
and screen_name
not in fil
:
221 if ig
and screen_name
in ig
:
225 if tid
not in c
['tweet_dict']:
226 c
['tweet_dict'].append(tid
)
227 rid
= len(c
['tweet_dict']) - 1
229 rid
= c
['tweet_dict'].index(tid
)
232 name
= cycle_color(name
)
233 nick
= color_func(c
['TWEET']['nick'])(screen_name
)
238 fav
= color_func(c
['TWEET']['favorited'])(u
'\u2605')
243 for index
in xrange(len(expanded_url
)):
245 lambda x
: expanded_url
[index
]
251 lambda x
: color_func(c
['TWEET']['rt'])(x
)
255 # Highlight screen_name
256 tweet
= lmap(lambda x
: cycle_color(x
) if x
[0] == '@' else x
, tweet
)
259 lambda x
: color_func(c
['TWEET']['link'])(x
)
265 lambda x
: color_func(c
['TWEET']['hashtag'])(x
)
270 tweet
= ' '.join(tweet
)
272 roj
= re
.search(keyword
, tweet
, re
.IGNORECASE
)
275 ary
= tweet
.split(occur
)
276 delimiter
= color_func(c
['TWEET']['keyword'])(occur
)
277 tweet
= delimiter
.join(ary
)
279 # Load config formater
282 formater
= c
['FORMAT']['TWEET']['DISPLAY']
283 formater
= name
.join(formater
.split('#name'))
284 formater
= nick
.join(formater
.split('#nick'))
285 formater
= fav
.join(formater
.split('#fav'))
286 formater
= tweet
.join(formater
.split('#tweet'))
288 word
= [w
for w
in formater
.split() if '#clock' in w
][0]
289 delimiter
= color_func(c
['TWEET']['clock'])(
290 clock
.join(word
.split('#clock')))
291 formater
= delimiter
.join(formater
.split(word
))
293 word
= [w
for w
in formater
.split() if '#id' in w
][0]
294 delimiter
= color_func(c
['TWEET']['id'])(id.join(word
.split('#id')))
295 formater
= delimiter
.join(formater
.split(word
))
296 # Change retweet count word
297 word
= [w
for w
in formater
.split() if '#rt_count' in w
][0]
298 delimiter
= color_func(c
['TWEET']['retweet_count'])(
299 str(retweet_count
).join(word
.split('#rt_count')))
300 formater
= delimiter
.join(formater
.split(word
))
301 # Change favorites count word
302 word
= [w
for w
in formater
.split() if '#fa_count' in w
][0]
303 delimiter
= color_func(c
['TWEET']['favorite_count'])(
304 str(favorite_count
).join(word
.split('#fa_count')))
305 formater
= delimiter
.join(formater
.split(word
))
310 printNicely(formater
)
313 if c
['IMAGE_ON_TERM'] and media_url
:
316 response
= requests
.get(mu
)
317 image_to_display(BytesIO(response
.content
))
319 printNicely(red('Sorry, image link is broken'))
322 def print_message(m
, check_semaphore
=False):
327 # Check the semaphore pause and lock (stream process only)
335 sender_screen_name
= '@' + m
['sender_screen_name']
336 sender_name
= m
['sender']['name']
337 text
= unescape(m
['text'])
338 recipient_screen_name
= '@' + m
['recipient_screen_name']
339 recipient_name
= m
['recipient']['name']
341 date
= parser
.parse(m
['created_at'])
342 date
= arrow
.get(date
).to('local').datetime
343 clock_format
= '%Y/%m/%d %H:%M:%S'
345 clock_format
= c
['FORMAT']['MESSAGE']['CLOCK_FORMAT']
348 clock
= date
.strftime(clock_format
)
351 if mid
not in c
['message_dict']:
352 c
['message_dict'].append(mid
)
353 rid
= len(c
['message_dict']) - 1
355 rid
= c
['message_dict'].index(mid
)
358 sender_name
= cycle_color(sender_name
)
359 sender_nick
= color_func(c
['MESSAGE']['sender'])(sender_screen_name
)
360 recipient_name
= cycle_color(recipient_name
)
361 recipient_nick
= color_func(
362 c
['MESSAGE']['recipient'])(recipient_screen_name
)
363 to
= color_func(c
['MESSAGE']['to'])('>>>')
367 text
= ''.join(lmap(lambda x
: x
+ ' ' if x
== '\n' else x
, text
))
369 # Load config formater
371 formater
= c
['FORMAT']['MESSAGE']['DISPLAY']
372 formater
= sender_name
.join(formater
.split("#sender_name"))
373 formater
= sender_nick
.join(formater
.split("#sender_nick"))
374 formater
= to
.join(formater
.split("#to"))
375 formater
= recipient_name
.join(formater
.split("#recipient_name"))
376 formater
= recipient_nick
.join(formater
.split("#recipient_nick"))
377 formater
= text
.join(formater
.split("#message"))
379 word
= [w
for w
in formater
.split() if '#clock' in w
][0]
380 delimiter
= color_func(c
['MESSAGE']['clock'])(
381 clock
.join(word
.split('#clock')))
382 formater
= delimiter
.join(formater
.split(word
))
384 word
= [w
for w
in formater
.split() if '#id' in w
][0]
385 delimiter
= color_func(c
['MESSAGE']['id'])(id.join(word
.split('#id')))
386 formater
= delimiter
.join(formater
.split(word
))
388 printNicely(red('Wrong format in config.'))
392 printNicely(formater
)
401 screen_name
= u
['screen_name']
402 description
= u
['description']
403 profile_image_url
= u
['profile_image_url']
404 location
= u
['location']
406 created_at
= u
['created_at']
407 statuses_count
= u
['statuses_count']
408 friends_count
= u
['friends_count']
409 followers_count
= u
['followers_count']
412 statuses_count
= color_func(
413 c
['PROFILE']['statuses_count'])(
414 str(statuses_count
) +
416 friends_count
= color_func(
417 c
['PROFILE']['friends_count'])(
420 followers_count
= color_func(
421 c
['PROFILE']['followers_count'])(
422 str(followers_count
) +
424 count
= statuses_count
+ ' ' + friends_count
+ ' ' + followers_count
426 name
) + color_func(c
['PROFILE']['nick'])(' @' + screen_name
+ ' : ') + count
427 profile_image_raw_url
= 'Profile photo: ' + \
428 color_func(c
['PROFILE']['profile_image_url'])(profile_image_url
)
429 description
= ''.join(
430 lmap(lambda x
: x
+ ' ' * 4 if x
== '\n' else x
, description
))
431 description
= color_func(c
['PROFILE']['description'])(description
)
432 location
= 'Location : ' + color_func(c
['PROFILE']['location'])(location
)
433 url
= 'URL : ' + (color_func(c
['PROFILE']['url'])(url
) if url
else '')
434 date
= parser
.parse(created_at
)
435 lang
, encode
= locale
.getdefaultlocale()
436 clock
= arrow
.get(date
).to('local').humanize(locale
=lang
)
437 clock
= 'Join at ' + color_func(c
['PROFILE']['clock'])(clock
)
440 line1
= u
"{u:>{uw}}".format(
444 line2
= u
"{p:>{pw}}".format(
445 p
=profile_image_raw_url
,
446 pw
=len(profile_image_raw_url
) + 4,
448 line3
= u
"{d:>{dw}}".format(
450 dw
=len(description
) + 4,
452 line4
= u
"{l:>{lw}}".format(
454 lw
=len(location
) + 4,
456 line5
= u
"{u:>{uw}}".format(
460 line6
= u
"{c:>{cw}}".format(
468 if c
['IMAGE_ON_TERM']:
470 response
= requests
.get(profile_image_url
)
471 image_to_display(BytesIO(response
.content
))
476 for line
in [line3
, line4
, line5
, line6
]:
481 def print_trends(trends
):
485 for topic
in trends
[:c
['TREND_MAX']]:
488 line
= cycle_color(name
) + ': ' + color_func(c
['TREND']['url'])(url
)
493 def print_list(group
):
499 name
= grp
['full_name']
500 name
= color_func(c
['GROUP']['name'])(name
+ ' : ')
501 member
= str(grp
['member_count'])
502 member
= color_func(c
['GROUP']['member'])(member
+ ' member')
503 subscriber
= str(grp
['subscriber_count'])
504 subscriber
= color_func(
505 c
['GROUP']['subscriber'])(
508 description
= grp
['description'].strip()
509 description
= color_func(c
['GROUP']['description'])(description
)
511 mode
= color_func(c
['GROUP']['mode'])('Type: ' + mode
)
512 created_at
= grp
['created_at']
513 date
= parser
.parse(created_at
)
514 lang
, encode
= locale
.getdefaultlocale()
515 clock
= arrow
.get(date
).to('local').humanize(locale
=lang
)
516 clock
= 'Created at ' + color_func(c
['GROUP']['clock'])(clock
)
519 line1
= ' ' * 2 + name
+ member
+ ' ' + subscriber
520 line2
= ' ' * 4 + description
521 line3
= ' ' * 4 + mode
522 line4
= ' ' * 4 + clock
534 def show_calendar(month
, date
, rel
):
536 Show the calendar in rainbow mode
538 month
= random_rainbow(month
)
539 date
= ' '.join([cycle_color(i
) for i
in date
.split(' ')])
540 today
= str(int(os
.popen('date +\'%d\'').read().strip()))
545 ary
= line
.split(' ')
547 lambda x
: color_func(c
['CAL']['today'])(x
)
549 else color_func(c
['CAL']['days'])(x
),
551 printNicely(' '.join(ary
))
554 def format_quote(tweet
):
559 screen_name
= '@' + tweet
['user']['screen_name']
561 # Validate quote format
562 if '#owner' not in c
['QUOTE_FORMAT']:
563 printNicely(light_magenta('Quote should contains #owner'))
565 if '#comment' not in c
['QUOTE_FORMAT']:
566 printNicely(light_magenta('Quote format should have #comment'))
571 formater
= c
['QUOTE_FORMAT']
572 formater
= screen_name
.join(formater
.split('#owner'))
573 formater
= text
.join(formater
.split('#tweet'))
574 formater
= u2str(formater
)
577 # Highlight like a tweet
578 formater
= formater
.split()
580 lambda x
: light_green(x
)
585 lambda x
: color_func(c
['TWEET']['rt'])(x
)
589 formater
= lmap(lambda x
: cycle_color(x
) if x
[0] == '@' else x
, formater
)
591 lambda x
: color_func(c
['TWEET']['link'])(x
)
596 lambda x
: color_func(c
['TWEET']['hashtag'])(x
)
600 formater
= ' '.join(formater
)
602 notice
= light_magenta('Quoting: "') + formater
+ light_magenta('"')
607 # Start the color cycle