d9afe06c3ec2ed0366e209761dbb109f35aa53de
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 date
= parser
.parse(created_at
)
169 date
= date
- datetime
.timedelta(seconds
=time
.timezone
)
170 clock_format
= '%Y/%m/%d %H:%M:%S'
172 clock_format
= c
['FORMAT']['TWEET']['CLOCK_FORMAT']
175 clock
= date
.strftime(clock_format
)
177 # Pull extended retweet text
179 text
= 'RT @' + t
['retweeted_status']['user']['screen_name'] + ': ' +\
180 t
['retweeted_status']['text']
184 # Unescape HTML character
185 text
= unescape(text
)
191 urls
= t
['entities']['urls']
193 expanded_url
.append(u
['expanded_url'])
202 media
= t
['entities']['media']
204 media_url
.append(m
['media_url'])
209 screen_name
= '@' + screen_name
210 if fil
and screen_name
not in fil
:
212 if ig
and screen_name
in ig
:
216 if tid
not in c
['tweet_dict']:
217 c
['tweet_dict'].append(tid
)
218 rid
= len(c
['tweet_dict']) - 1
220 rid
= c
['tweet_dict'].index(tid
)
223 name
= cycle_color(name
)
224 nick
= color_func(c
['TWEET']['nick'])(screen_name
)
229 fav
= color_func(c
['TWEET']['favorited'])(u
'\u2605')
234 for index
in range(len(expanded_url
)):
236 lambda x
: expanded_url
[index
]
242 lambda x
: color_func(c
['TWEET']['rt'])(x
)
246 # Highlight screen_name
247 tweet
= lmap(lambda x
: cycle_color(x
) if x
[0] == '@' else x
, tweet
)
250 lambda x
: color_func(c
['TWEET']['link'])(x
)
256 lambda x
: color_func(c
['TWEET']['hashtag'])(x
)
261 tweet
= ' '.join(tweet
)
263 roj
= re
.search(keyword
, tweet
, re
.IGNORECASE
)
266 ary
= tweet
.split(occur
)
267 delimiter
= color_func(c
['TWEET']['keyword'])(occur
)
268 tweet
= delimiter
.join(ary
)
270 # Load config formater
272 formater
= c
['FORMAT']['TWEET']['DISPLAY']
273 formater
= name
.join(formater
.split("#name"))
274 formater
= nick
.join(formater
.split("#nick"))
275 formater
= fav
.join(formater
.split("#fav"))
276 formater
= tweet
.join(formater
.split("#tweet"))
278 word
= [w
for w
in formater
.split() if '#clock' in w
][0]
279 delimiter
= color_func(c
['TWEET']['clock'])(
280 clock
.join(word
.split('#clock')))
281 formater
= delimiter
.join(formater
.split(word
))
283 word
= [w
for w
in formater
.split() if '#id' in w
][0]
284 delimiter
= color_func(c
['TWEET']['id'])(id.join(word
.split('#id')))
285 formater
= delimiter
.join(formater
.split(word
))
287 printNicely(red('Wrong format in config.'))
291 printNicely(formater
)
294 if c
['IMAGE_ON_TERM'] and media_url
:
297 response
= requests
.get(mu
)
298 image_to_display(BytesIO(response
.content
))
300 printNicely(red('Sorry, image link is broken'))
303 def print_message(m
, check_semaphore
=False):
308 # Check the semaphore pause and lock (stream process only)
316 sender_screen_name
= '@' + m
['sender_screen_name']
317 sender_name
= m
['sender']['name']
318 text
= unescape(m
['text'])
319 recipient_screen_name
= '@' + m
['recipient_screen_name']
320 recipient_name
= m
['recipient']['name']
322 date
= parser
.parse(m
['created_at'])
323 date
= date
- datetime
.timedelta(seconds
=time
.timezone
)
324 clock_format
= '%Y/%m/%d %H:%M:%S'
326 clock_format
= c
['FORMAT']['MESSAGE']['CLOCK_FORMAT']
329 clock
= date
.strftime(clock_format
)
332 if mid
not in c
['message_dict']:
333 c
['message_dict'].append(mid
)
334 rid
= len(c
['message_dict']) - 1
336 rid
= c
['message_dict'].index(mid
)
339 sender_name
= cycle_color(sender_name
)
340 sender_nick
= color_func(c
['MESSAGE']['sender'])(sender_screen_name
)
341 recipient_name
= cycle_color(recipient_name
)
342 recipient_nick
= color_func(
343 c
['MESSAGE']['recipient'])(recipient_screen_name
)
344 to
= color_func(c
['MESSAGE']['to'])('>>>')
348 text
= ''.join(lmap(lambda x
: x
+ ' ' if x
== '\n' else x
, text
))
350 # Load config formater
352 formater
= c
['FORMAT']['MESSAGE']['DISPLAY']
353 formater
= sender_name
.join(formater
.split("#sender_name"))
354 formater
= sender_nick
.join(formater
.split("#sender_nick"))
355 formater
= to
.join(formater
.split("#to"))
356 formater
= recipient_name
.join(formater
.split("#recipient_name"))
357 formater
= recipient_nick
.join(formater
.split("#recipient_nick"))
358 formater
= text
.join(formater
.split("#message"))
360 word
= [w
for w
in formater
.split() if '#clock' in w
][0]
361 delimiter
= color_func(c
['MESSAGE']['clock'])(
362 clock
.join(word
.split('#clock')))
363 formater
= delimiter
.join(formater
.split(word
))
365 word
= [w
for w
in formater
.split() if '#id' in w
][0]
366 delimiter
= color_func(c
['MESSAGE']['id'])(id.join(word
.split('#id')))
367 formater
= delimiter
.join(formater
.split(word
))
369 printNicely(red('Wrong format in config.'))
373 printNicely(formater
)
382 screen_name
= u
['screen_name']
383 description
= u
['description']
384 profile_image_url
= u
['profile_image_url']
385 location
= u
['location']
387 created_at
= u
['created_at']
388 statuses_count
= u
['statuses_count']
389 friends_count
= u
['friends_count']
390 followers_count
= u
['followers_count']
393 statuses_count
= color_func(
394 c
['PROFILE']['statuses_count'])(
395 str(statuses_count
) +
397 friends_count
= color_func(
398 c
['PROFILE']['friends_count'])(
401 followers_count
= color_func(
402 c
['PROFILE']['followers_count'])(
403 str(followers_count
) +
405 count
= statuses_count
+ ' ' + friends_count
+ ' ' + followers_count
407 name
) + color_func(c
['PROFILE']['nick'])(' @' + screen_name
+ ' : ') + count
408 profile_image_raw_url
= 'Profile photo: ' + \
409 color_func(c
['PROFILE']['profile_image_url'])(profile_image_url
)
410 description
= ''.join(
411 lmap(lambda x
: x
+ ' ' * 4 if x
== '\n' else x
, description
))
412 description
= color_func(c
['PROFILE']['description'])(description
)
413 location
= 'Location : ' + color_func(c
['PROFILE']['location'])(location
)
414 url
= 'URL : ' + (color_func(c
['PROFILE']['url'])(url
) if url
else '')
415 date
= parser
.parse(created_at
)
416 date
= date
- datetime
.timedelta(seconds
=time
.timezone
)
417 clock
= date
.strftime('%Y/%m/%d %H:%M:%S')
418 clock
= 'Join at ' + color_func(c
['PROFILE']['clock'])(clock
)
421 line1
= u
"{u:>{uw}}".format(
425 line2
= u
"{p:>{pw}}".format(
426 p
=profile_image_raw_url
,
427 pw
=len(profile_image_raw_url
) + 4,
429 line3
= u
"{d:>{dw}}".format(
431 dw
=len(description
) + 4,
433 line4
= u
"{l:>{lw}}".format(
435 lw
=len(location
) + 4,
437 line5
= u
"{u:>{uw}}".format(
441 line6
= u
"{c:>{cw}}".format(
449 if c
['IMAGE_ON_TERM']:
451 response
= requests
.get(profile_image_url
)
452 image_to_display(BytesIO(response
.content
))
457 for line
in [line3
, line4
, line5
, line6
]:
462 def print_trends(trends
):
466 for topic
in trends
[:c
['TREND_MAX']]:
469 line
= cycle_color(name
) + ': ' + color_func(c
['TREND']['url'])(url
)
474 def print_list(group
):
480 name
= grp
['full_name']
481 name
= color_func(c
['GROUP']['name'])(name
+ ' : ')
482 member
= str(grp
['member_count'])
483 member
= color_func(c
['GROUP']['member'])(member
+ ' member')
484 subscriber
= str(grp
['subscriber_count'])
485 subscriber
= color_func(
486 c
['GROUP']['subscriber'])(
489 description
= grp
['description'].strip()
490 description
= color_func(c
['GROUP']['description'])(description
)
492 mode
= color_func(c
['GROUP']['mode'])('Type: ' + mode
)
493 created_at
= grp
['created_at']
494 date
= parser
.parse(created_at
)
495 date
= date
- datetime
.timedelta(seconds
=time
.timezone
)
496 clock
= date
.strftime('%Y/%m/%d %H:%M:%S')
497 clock
= 'Created at ' + color_func(c
['GROUP']['clock'])(clock
)
500 line1
= ' ' * 2 + name
+ member
+ ' ' + subscriber
501 line2
= ' ' * 4 + description
502 line3
= ' ' * 4 + mode
503 line4
= ' ' * 4 + clock
515 def show_calendar(month
, date
, rel
):
517 Show the calendar in rainbow mode
519 month
= random_rainbow(month
)
520 date
= ' '.join([cycle_color(i
) for i
in date
.split(' ')])
521 today
= str(int(os
.popen('date +\'%d\'').read().strip()))
526 ary
= line
.split(' ')
528 lambda x
: color_func(c
['CAL']['today'])(x
)
530 else color_func(c
['CAL']['days'])(x
),
532 printNicely(' '.join(ary
))
535 # Start the color cycle