00d3e252ec0766bdae4b03383ea07f8c554ab93c
8 from twitter
.util
import printNicely
9 from functools
import wraps
10 from pyfiglet
import figlet_format
11 from dateutil
import parser
12 from .c_image
import *
15 from .py3patch
import *
17 # Draw global variables
25 colors_shuffle
= [globals()[i
.encode('utf8')]
26 if not str(i
).isdigit()
27 else term_color(int(i
))
28 for i
in c
['CYCLE_COLOR']]
29 return itertools
.cycle(colors_shuffle
)
36 dg
['cyc'] = init_cycle()
42 Print a string with ordered color with each character
44 colors_shuffle
= [globals()[i
.encode('utf8')]
45 if not str(i
).isdigit()
46 else term_color(int(i
))
47 for i
in c
['CYCLE_COLOR']]
48 colored
= [colors_shuffle
[i
% 7](s
[i
]) for i
in xrange(len(s
))]
49 return ''.join(colored
)
52 def random_rainbow(s
):
54 Print a string with random color with each character
56 colors_shuffle
= [globals()[i
.encode('utf8')]
57 if not str(i
).isdigit()
58 else term_color(int(i
))
59 for i
in c
['CYCLE_COLOR']]
60 colored
= [random
.choice(colors_shuffle
)(i
) for i
in s
]
61 return ''.join(colored
)
70 if args
not in dg
['cache']:
71 dg
['cache'][args
] = func(*args
)
72 return dg
['cache'][args
]
79 Cycle the colors_shuffle
81 return next(dg
['cyc'])(s
)
88 fi
= figlet_format(text
, font
='doom')
90 [next(dg
['cyc'])(i
) for i
in fi
.split('\n')]
96 Check if config is changed
99 data
= get_all_config()
102 if data
[key
] != c
[key
]:
108 def validate_theme(theme
):
110 Validate a theme exists or not
112 # Theme changed check
113 files
= os
.listdir(os
.path
.dirname(__file__
) + '/colorset')
114 themes
= [f
.split('.')[0] for f
in files
if f
.split('.')[-1] == 'json']
115 return theme
in themes
118 def reload_theme(value
, prev
):
120 Check current theme and update if necessary
123 config
= os
.path
.dirname(
124 __file__
) + '/colorset/' + value
+ '.json'
126 data
= load_config(config
)
130 # Restart color cycle and update config
132 set_config('THEME', value
)
137 def color_func(func_name
):
139 Call color function base on name
141 if str(func_name
).isdigit():
142 return term_color(int(func_name
))
143 return globals()[func_name
]
146 def draw(t
, keyword
=None, check_semaphore
=False, fil
=[], ig
=[]):
151 # Check the semaphore pause and lock (stream process only)
164 screen_name
= t
['user']['screen_name']
165 name
= t
['user']['name']
166 created_at
= t
['created_at']
167 favorited
= t
['favorited']
168 retweet_count
= t
['retweet_count']
169 favorite_count
= t
['favorite_count']
170 date
= parser
.parse(created_at
)
171 date
= date
- datetime
.timedelta(seconds
=time
.timezone
)
172 clock_format
= '%Y/%m/%d %H:%M:%S'
174 clock_format
= c
['FORMAT']['TWEET']['CLOCK_FORMAT']
177 clock
= date
.strftime(clock_format
)
179 # Pull extended retweet text
181 text
= 'RT @' + t
['retweeted_status']['user']['screen_name'] + ': ' +\
182 t
['retweeted_status']['text']
186 # Unescape HTML character
187 text
= unescape(text
)
193 urls
= t
['entities']['urls']
195 expanded_url
.append(u
['expanded_url'])
204 media
= t
['entities']['media']
206 media_url
.append(m
['media_url'])
211 screen_name
= '@' + screen_name
212 fil
= list(set((fil
or []) + c
['ONLY_LIST']))
213 ig
= list(set((ig
or []) + c
['IGNORE_LIST']))
214 if fil
and screen_name
not in fil
:
216 if ig
and screen_name
in ig
:
220 if tid
not in c
['tweet_dict']:
221 c
['tweet_dict'].append(tid
)
222 rid
= len(c
['tweet_dict']) - 1
224 rid
= c
['tweet_dict'].index(tid
)
227 name
= cycle_color(name
)
228 nick
= color_func(c
['TWEET']['nick'])(screen_name
)
233 fav
= color_func(c
['TWEET']['favorited'])(u
'\u2605')
238 for index
in xrange(len(expanded_url
)):
240 lambda x
: expanded_url
[index
]
246 lambda x
: color_func(c
['TWEET']['rt'])(x
)
250 # Highlight screen_name
251 tweet
= lmap(lambda x
: cycle_color(x
) if x
[0] == '@' else x
, tweet
)
254 lambda x
: color_func(c
['TWEET']['link'])(x
)
260 lambda x
: color_func(c
['TWEET']['hashtag'])(x
)
265 tweet
= ' '.join(tweet
)
267 roj
= re
.search(keyword
, tweet
, re
.IGNORECASE
)
270 ary
= tweet
.split(occur
)
271 delimiter
= color_func(c
['TWEET']['keyword'])(occur
)
272 tweet
= delimiter
.join(ary
)
274 # Load config formater
277 formater
= c
['FORMAT']['TWEET']['DISPLAY']
278 formater
= name
.join(formater
.split('#name'))
279 formater
= nick
.join(formater
.split('#nick'))
280 formater
= fav
.join(formater
.split('#fav'))
281 formater
= tweet
.join(formater
.split('#tweet'))
283 word
= [w
for w
in formater
.split() if '#clock' in w
][0]
284 delimiter
= color_func(c
['TWEET']['clock'])(
285 clock
.join(word
.split('#clock')))
286 formater
= delimiter
.join(formater
.split(word
))
288 word
= [w
for w
in formater
.split() if '#id' in w
][0]
289 delimiter
= color_func(c
['TWEET']['id'])(id.join(word
.split('#id')))
290 formater
= delimiter
.join(formater
.split(word
))
291 # Change retweet count word
292 word
= [w
for w
in formater
.split() if '#rt_count' in w
][0]
293 delimiter
= color_func(c
['TWEET']['retweet_count'])(
294 str(retweet_count
).join(word
.split('#rt_count')))
295 formater
= delimiter
.join(formater
.split(word
))
296 # Change favorites count word
297 word
= [w
for w
in formater
.split() if '#fa_count' in w
][0]
298 delimiter
= color_func(c
['TWEET']['favorite_count'])(
299 str(favorite_count
).join(word
.split('#fa_count')))
300 formater
= delimiter
.join(formater
.split(word
))
305 printNicely(formater
)
308 if c
['IMAGE_ON_TERM'] and media_url
:
311 response
= requests
.get(mu
)
312 image_to_display(BytesIO(response
.content
))
314 printNicely(red('Sorry, image link is broken'))
317 def print_message(m
, check_semaphore
=False):
322 # Check the semaphore pause and lock (stream process only)
330 sender_screen_name
= '@' + m
['sender_screen_name']
331 sender_name
= m
['sender']['name']
332 text
= unescape(m
['text'])
333 recipient_screen_name
= '@' + m
['recipient_screen_name']
334 recipient_name
= m
['recipient']['name']
336 date
= parser
.parse(m
['created_at'])
337 date
= date
- datetime
.timedelta(seconds
=time
.timezone
)
338 clock_format
= '%Y/%m/%d %H:%M:%S'
340 clock_format
= c
['FORMAT']['MESSAGE']['CLOCK_FORMAT']
343 clock
= date
.strftime(clock_format
)
346 if mid
not in c
['message_dict']:
347 c
['message_dict'].append(mid
)
348 rid
= len(c
['message_dict']) - 1
350 rid
= c
['message_dict'].index(mid
)
353 sender_name
= cycle_color(sender_name
)
354 sender_nick
= color_func(c
['MESSAGE']['sender'])(sender_screen_name
)
355 recipient_name
= cycle_color(recipient_name
)
356 recipient_nick
= color_func(
357 c
['MESSAGE']['recipient'])(recipient_screen_name
)
358 to
= color_func(c
['MESSAGE']['to'])('>>>')
362 text
= ''.join(lmap(lambda x
: x
+ ' ' if x
== '\n' else x
, text
))
364 # Load config formater
366 formater
= c
['FORMAT']['MESSAGE']['DISPLAY']
367 formater
= sender_name
.join(formater
.split("#sender_name"))
368 formater
= sender_nick
.join(formater
.split("#sender_nick"))
369 formater
= to
.join(formater
.split("#to"))
370 formater
= recipient_name
.join(formater
.split("#recipient_name"))
371 formater
= recipient_nick
.join(formater
.split("#recipient_nick"))
372 formater
= text
.join(formater
.split("#message"))
374 word
= [w
for w
in formater
.split() if '#clock' in w
][0]
375 delimiter
= color_func(c
['MESSAGE']['clock'])(
376 clock
.join(word
.split('#clock')))
377 formater
= delimiter
.join(formater
.split(word
))
379 word
= [w
for w
in formater
.split() if '#id' in w
][0]
380 delimiter
= color_func(c
['MESSAGE']['id'])(id.join(word
.split('#id')))
381 formater
= delimiter
.join(formater
.split(word
))
383 printNicely(red('Wrong format in config.'))
387 printNicely(formater
)
396 screen_name
= u
['screen_name']
397 description
= u
['description']
398 profile_image_url
= u
['profile_image_url']
399 location
= u
['location']
401 created_at
= u
['created_at']
402 statuses_count
= u
['statuses_count']
403 friends_count
= u
['friends_count']
404 followers_count
= u
['followers_count']
407 statuses_count
= color_func(
408 c
['PROFILE']['statuses_count'])(
409 str(statuses_count
) +
411 friends_count
= color_func(
412 c
['PROFILE']['friends_count'])(
415 followers_count
= color_func(
416 c
['PROFILE']['followers_count'])(
417 str(followers_count
) +
419 count
= statuses_count
+ ' ' + friends_count
+ ' ' + followers_count
421 name
) + color_func(c
['PROFILE']['nick'])(' @' + screen_name
+ ' : ') + count
422 profile_image_raw_url
= 'Profile photo: ' + \
423 color_func(c
['PROFILE']['profile_image_url'])(profile_image_url
)
424 description
= ''.join(
425 lmap(lambda x
: x
+ ' ' * 4 if x
== '\n' else x
, description
))
426 description
= color_func(c
['PROFILE']['description'])(description
)
427 location
= 'Location : ' + color_func(c
['PROFILE']['location'])(location
)
428 url
= 'URL : ' + (color_func(c
['PROFILE']['url'])(url
) if url
else '')
429 date
= parser
.parse(created_at
)
430 date
= date
- datetime
.timedelta(seconds
=time
.timezone
)
431 clock
= date
.strftime('%Y/%m/%d %H:%M:%S')
432 clock
= 'Join at ' + color_func(c
['PROFILE']['clock'])(clock
)
435 line1
= u
"{u:>{uw}}".format(
439 line2
= u
"{p:>{pw}}".format(
440 p
=profile_image_raw_url
,
441 pw
=len(profile_image_raw_url
) + 4,
443 line3
= u
"{d:>{dw}}".format(
445 dw
=len(description
) + 4,
447 line4
= u
"{l:>{lw}}".format(
449 lw
=len(location
) + 4,
451 line5
= u
"{u:>{uw}}".format(
455 line6
= u
"{c:>{cw}}".format(
463 if c
['IMAGE_ON_TERM']:
465 response
= requests
.get(profile_image_url
)
466 image_to_display(BytesIO(response
.content
))
471 for line
in [line3
, line4
, line5
, line6
]:
476 def print_trends(trends
):
480 for topic
in trends
[:c
['TREND_MAX']]:
483 line
= cycle_color(name
) + ': ' + color_func(c
['TREND']['url'])(url
)
488 def print_list(group
):
494 name
= grp
['full_name']
495 name
= color_func(c
['GROUP']['name'])(name
+ ' : ')
496 member
= str(grp
['member_count'])
497 member
= color_func(c
['GROUP']['member'])(member
+ ' member')
498 subscriber
= str(grp
['subscriber_count'])
499 subscriber
= color_func(
500 c
['GROUP']['subscriber'])(
503 description
= grp
['description'].strip()
504 description
= color_func(c
['GROUP']['description'])(description
)
506 mode
= color_func(c
['GROUP']['mode'])('Type: ' + mode
)
507 created_at
= grp
['created_at']
508 date
= parser
.parse(created_at
)
509 date
= date
- datetime
.timedelta(seconds
=time
.timezone
)
510 clock
= date
.strftime('%Y/%m/%d %H:%M:%S')
511 clock
= 'Created at ' + color_func(c
['GROUP']['clock'])(clock
)
514 line1
= ' ' * 2 + name
+ member
+ ' ' + subscriber
515 line2
= ' ' * 4 + description
516 line3
= ' ' * 4 + mode
517 line4
= ' ' * 4 + clock
529 def show_calendar(month
, date
, rel
):
531 Show the calendar in rainbow mode
533 month
= random_rainbow(month
)
534 date
= ' '.join([cycle_color(i
) for i
in date
.split(' ')])
535 today
= str(int(os
.popen('date +\'%d\'').read().strip()))
540 ary
= line
.split(' ')
542 lambda x
: color_func(c
['CAL']['today'])(x
)
544 else color_func(c
['CAL']['days'])(x
),
546 printNicely(' '.join(ary
))
549 def format_quote(tweet
):
554 screen_name
= '@' + tweet
['user']['screen_name']
556 # Validate quote format
557 if '#owner' not in c
['QUOTE_FORMAT']:
558 printNicely(light_magenta('Quote should contains #owner'))
560 if '#comment' not in c
['QUOTE_FORMAT']:
561 printNicely(light_magenta('Quote format should have #comment'))
566 formater
= c
['QUOTE_FORMAT']
567 formater
= screen_name
.join(formater
.split('#owner'))
568 formater
= text
.join(formater
.split('#tweet'))
569 formater
= u2str(formater
)
572 # Highlight like a tweet
573 formater
= formater
.split()
575 lambda x
: light_green(x
)
580 lambda x
: color_func(c
['TWEET']['rt'])(x
)
584 formater
= lmap(lambda x
: cycle_color(x
) if x
[0] == '@' else x
, formater
)
586 lambda x
: color_func(c
['TWEET']['link'])(x
)
591 lambda x
: color_func(c
['TWEET']['hashtag'])(x
)
595 formater
= ' '.join(formater
)
597 notice
= light_magenta('Quoting: "') + formater
+ light_magenta('"')
602 # Start the color cycle