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, fil
=[], ig
=[]):
157 screen_name
= t
['user']['screen_name']
158 name
= t
['user']['name']
159 created_at
= t
['created_at']
160 favorited
= t
['favorited']
161 retweet_count
= t
['retweet_count']
162 favorite_count
= t
['favorite_count']
163 date
= parser
.parse(created_at
)
164 date
= arrow
.get(date
).to('local')
166 lang
, encode
= locale
.getdefaultlocale()
167 clock
= arrow
.get(date
).to('local').humanize(locale
=lang
)
170 clock_format
= c
['FORMAT']['TWEET']['CLOCK_FORMAT']
172 clock_format
= '%Y/%m/%d %H:%M:%S'
173 clock
= date
.datetime
.strftime(clock_format
)
175 # Pull extended retweet text
177 text
= 'RT @' + t
['retweeted_status']['user']['screen_name'] + ': ' +\
178 t
['retweeted_status']['text']
182 # Unescape HTML character
183 text
= unescape(text
)
189 urls
= t
['entities']['urls']
191 expanded_url
.append(u
['expanded_url'])
200 media
= t
['entities']['media']
202 media_url
.append(m
['media_url'])
207 screen_name
= '@' + screen_name
208 fil
= list(set((fil
or []) + c
['ONLY_LIST']))
209 ig
= list(set((ig
or []) + c
['IGNORE_LIST']))
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 xrange(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
273 formater
= c
['FORMAT']['TWEET']['DISPLAY']
274 formater
= name
.join(formater
.split('#name'))
275 formater
= nick
.join(formater
.split('#nick'))
276 formater
= fav
.join(formater
.split('#fav'))
277 formater
= tweet
.join(formater
.split('#tweet'))
279 word
= [w
for w
in formater
.split() if '#clock' in w
][0]
280 delimiter
= color_func(c
['TWEET']['clock'])(
281 clock
.join(word
.split('#clock')))
282 formater
= delimiter
.join(formater
.split(word
))
284 word
= [w
for w
in formater
.split() if '#id' in w
][0]
285 delimiter
= color_func(c
['TWEET']['id'])(id.join(word
.split('#id')))
286 formater
= delimiter
.join(formater
.split(word
))
287 # Change retweet count word
288 word
= [w
for w
in formater
.split() if '#rt_count' in w
][0]
289 delimiter
= color_func(c
['TWEET']['retweet_count'])(
290 str(retweet_count
).join(word
.split('#rt_count')))
291 formater
= delimiter
.join(formater
.split(word
))
292 # Change favorites count word
293 word
= [w
for w
in formater
.split() if '#fa_count' in w
][0]
294 delimiter
= color_func(c
['TWEET']['favorite_count'])(
295 str(favorite_count
).join(word
.split('#fa_count')))
296 formater
= delimiter
.join(formater
.split(word
))
301 printNicely(formater
)
304 if c
['IMAGE_ON_TERM'] and media_url
:
307 response
= requests
.get(mu
)
308 image_to_display(BytesIO(response
.content
))
310 printNicely(red('Sorry, image link is broken'))
313 def print_message(m
):
318 sender_screen_name
= '@' + m
['sender_screen_name']
319 sender_name
= m
['sender']['name']
320 text
= unescape(m
['text'])
321 recipient_screen_name
= '@' + m
['recipient_screen_name']
322 recipient_name
= m
['recipient']['name']
324 date
= parser
.parse(m
['created_at'])
325 date
= arrow
.get(date
).to('local').datetime
326 clock_format
= '%Y/%m/%d %H:%M:%S'
328 clock_format
= c
['FORMAT']['MESSAGE']['CLOCK_FORMAT']
331 clock
= date
.strftime(clock_format
)
334 if mid
not in c
['message_dict']:
335 c
['message_dict'].append(mid
)
336 rid
= len(c
['message_dict']) - 1
338 rid
= c
['message_dict'].index(mid
)
341 sender_name
= cycle_color(sender_name
)
342 sender_nick
= color_func(c
['MESSAGE']['sender'])(sender_screen_name
)
343 recipient_name
= cycle_color(recipient_name
)
344 recipient_nick
= color_func(
345 c
['MESSAGE']['recipient'])(recipient_screen_name
)
346 to
= color_func(c
['MESSAGE']['to'])('>>>')
350 text
= ''.join(lmap(lambda x
: x
+ ' ' if x
== '\n' else x
, text
))
352 # Load config formater
354 formater
= c
['FORMAT']['MESSAGE']['DISPLAY']
355 formater
= sender_name
.join(formater
.split("#sender_name"))
356 formater
= sender_nick
.join(formater
.split("#sender_nick"))
357 formater
= to
.join(formater
.split("#to"))
358 formater
= recipient_name
.join(formater
.split("#recipient_name"))
359 formater
= recipient_nick
.join(formater
.split("#recipient_nick"))
360 formater
= text
.join(formater
.split("#message"))
362 word
= [w
for w
in formater
.split() if '#clock' in w
][0]
363 delimiter
= color_func(c
['MESSAGE']['clock'])(
364 clock
.join(word
.split('#clock')))
365 formater
= delimiter
.join(formater
.split(word
))
367 word
= [w
for w
in formater
.split() if '#id' in w
][0]
368 delimiter
= color_func(c
['MESSAGE']['id'])(id.join(word
.split('#id')))
369 formater
= delimiter
.join(formater
.split(word
))
371 printNicely(red('Wrong format in config.'))
375 printNicely(formater
)
384 screen_name
= u
['screen_name']
385 description
= u
['description']
386 profile_image_url
= u
['profile_image_url']
387 location
= u
['location']
389 created_at
= u
['created_at']
390 statuses_count
= u
['statuses_count']
391 friends_count
= u
['friends_count']
392 followers_count
= u
['followers_count']
395 statuses_count
= color_func(
396 c
['PROFILE']['statuses_count'])(
397 str(statuses_count
) +
399 friends_count
= color_func(
400 c
['PROFILE']['friends_count'])(
403 followers_count
= color_func(
404 c
['PROFILE']['followers_count'])(
405 str(followers_count
) +
407 count
= statuses_count
+ ' ' + friends_count
+ ' ' + followers_count
409 name
) + color_func(c
['PROFILE']['nick'])(' @' + screen_name
+ ' : ') + count
410 profile_image_raw_url
= 'Profile photo: ' + \
411 color_func(c
['PROFILE']['profile_image_url'])(profile_image_url
)
412 description
= ''.join(
413 lmap(lambda x
: x
+ ' ' * 4 if x
== '\n' else x
, description
))
414 description
= color_func(c
['PROFILE']['description'])(description
)
415 location
= 'Location : ' + color_func(c
['PROFILE']['location'])(location
)
416 url
= 'URL : ' + (color_func(c
['PROFILE']['url'])(url
) if url
else '')
417 date
= parser
.parse(created_at
)
418 lang
, encode
= locale
.getdefaultlocale()
419 clock
= arrow
.get(date
).to('local').humanize(locale
=lang
)
420 clock
= 'Join at ' + color_func(c
['PROFILE']['clock'])(clock
)
423 line1
= u
"{u:>{uw}}".format(
427 line2
= u
"{p:>{pw}}".format(
428 p
=profile_image_raw_url
,
429 pw
=len(profile_image_raw_url
) + 4,
431 line3
= u
"{d:>{dw}}".format(
433 dw
=len(description
) + 4,
435 line4
= u
"{l:>{lw}}".format(
437 lw
=len(location
) + 4,
439 line5
= u
"{u:>{uw}}".format(
443 line6
= u
"{c:>{cw}}".format(
451 if c
['IMAGE_ON_TERM']:
453 response
= requests
.get(profile_image_url
)
454 image_to_display(BytesIO(response
.content
))
459 for line
in [line3
, line4
, line5
, line6
]:
464 def print_trends(trends
):
468 for topic
in trends
[:c
['TREND_MAX']]:
471 line
= cycle_color(name
) + ': ' + color_func(c
['TREND']['url'])(url
)
476 def print_list(group
):
482 name
= grp
['full_name']
483 name
= color_func(c
['GROUP']['name'])(name
+ ' : ')
484 member
= str(grp
['member_count'])
485 member
= color_func(c
['GROUP']['member'])(member
+ ' member')
486 subscriber
= str(grp
['subscriber_count'])
487 subscriber
= color_func(
488 c
['GROUP']['subscriber'])(
491 description
= grp
['description'].strip()
492 description
= color_func(c
['GROUP']['description'])(description
)
494 mode
= color_func(c
['GROUP']['mode'])('Type: ' + mode
)
495 created_at
= grp
['created_at']
496 date
= parser
.parse(created_at
)
497 lang
, encode
= locale
.getdefaultlocale()
498 clock
= arrow
.get(date
).to('local').humanize(locale
=lang
)
499 clock
= 'Created at ' + color_func(c
['GROUP']['clock'])(clock
)
502 line1
= ' ' * 2 + name
+ member
+ ' ' + subscriber
503 line2
= ' ' * 4 + description
504 line3
= ' ' * 4 + mode
505 line4
= ' ' * 4 + clock
517 def show_calendar(month
, date
, rel
):
519 Show the calendar in rainbow mode
521 month
= random_rainbow(month
)
522 date
= ' '.join([cycle_color(i
) for i
in date
.split(' ')])
523 today
= str(int(os
.popen('date +\'%d\'').read().strip()))
528 ary
= line
.split(' ')
530 lambda x
: color_func(c
['CAL']['today'])(x
)
532 else color_func(c
['CAL']['days'])(x
),
534 printNicely(' '.join(ary
))
537 def format_quote(tweet
):
542 screen_name
= '@' + tweet
['user']['screen_name']
544 # Validate quote format
545 if '#owner' not in c
['QUOTE_FORMAT']:
546 printNicely(light_magenta('Quote should contains #owner'))
548 if '#comment' not in c
['QUOTE_FORMAT']:
549 printNicely(light_magenta('Quote format should have #comment'))
554 formater
= c
['QUOTE_FORMAT']
555 formater
= screen_name
.join(formater
.split('#owner'))
556 formater
= text
.join(formater
.split('#tweet'))
557 formater
= u2str(formater
)
560 # Highlight like a tweet
561 notice
= formater
.split()
563 lambda x
: light_green(x
)
568 lambda x
: color_func(c
['TWEET']['rt'])(x
)
572 notice
= lmap(lambda x
: cycle_color(x
) if x
[0] == '@' else x
, notice
)
574 lambda x
: color_func(c
['TWEET']['link'])(x
)
579 lambda x
: color_func(c
['TWEET']['hashtag'])(x
)
583 notice
= ' '.join(notice
)
585 notice
= light_magenta('Quoting: "') + notice
+ light_magenta('"')
590 # Start the color cycle