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 fil
= list(set((fil
or []) + c
['ONLY_LIST']))
211 ig
= list(set((ig
or []) + c
['IGNORE_LIST']))
212 if fil
and screen_name
not in fil
:
214 if ig
and screen_name
in ig
:
218 if tid
not in c
['tweet_dict']:
219 c
['tweet_dict'].append(tid
)
220 rid
= len(c
['tweet_dict']) - 1
222 rid
= c
['tweet_dict'].index(tid
)
225 name
= cycle_color(name
)
226 nick
= color_func(c
['TWEET']['nick'])(screen_name
)
231 fav
= color_func(c
['TWEET']['favorited'])(u
'\u2605')
236 for index
in range(len(expanded_url
)):
238 lambda x
: expanded_url
[index
]
244 lambda x
: color_func(c
['TWEET']['rt'])(x
)
248 # Highlight screen_name
249 tweet
= lmap(lambda x
: cycle_color(x
) if x
[0] == '@' else x
, tweet
)
252 lambda x
: color_func(c
['TWEET']['link'])(x
)
258 lambda x
: color_func(c
['TWEET']['hashtag'])(x
)
263 tweet
= ' '.join(tweet
)
265 roj
= re
.search(keyword
, tweet
, re
.IGNORECASE
)
268 ary
= tweet
.split(occur
)
269 delimiter
= color_func(c
['TWEET']['keyword'])(occur
)
270 tweet
= delimiter
.join(ary
)
272 # Load config formater
274 formater
= c
['FORMAT']['TWEET']['DISPLAY']
275 formater
= name
.join(formater
.split("#name"))
276 formater
= nick
.join(formater
.split("#nick"))
277 formater
= fav
.join(formater
.split("#fav"))
278 formater
= tweet
.join(formater
.split("#tweet"))
280 word
= [w
for w
in formater
.split() if '#clock' in w
][0]
281 delimiter
= color_func(c
['TWEET']['clock'])(
282 clock
.join(word
.split('#clock')))
283 formater
= delimiter
.join(formater
.split(word
))
285 word
= [w
for w
in formater
.split() if '#id' in w
][0]
286 delimiter
= color_func(c
['TWEET']['id'])(id.join(word
.split('#id')))
287 formater
= delimiter
.join(formater
.split(word
))
289 printNicely(red('Wrong format in config.'))
293 printNicely(formater
)
296 if c
['IMAGE_ON_TERM'] and media_url
:
299 response
= requests
.get(mu
)
300 image_to_display(BytesIO(response
.content
))
302 printNicely(red('Sorry, image link is broken'))
305 def print_message(m
, check_semaphore
=False):
310 # Check the semaphore pause and lock (stream process only)
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
= date
- datetime
.timedelta(seconds
=time
.timezone
)
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 date
= date
- datetime
.timedelta(seconds
=time
.timezone
)
419 clock
= date
.strftime('%Y/%m/%d %H:%M:%S')
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 date
= date
- datetime
.timedelta(seconds
=time
.timezone
)
498 clock
= date
.strftime('%Y/%m/%d %H:%M:%S')
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 # Start the color cycle