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 *
16 from .py3patch
import *
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 g
['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 g
['cache']:
73 g
['cache'][args
] = func(*args
)
74 return g
['cache'][args
]
81 Cycle the colors_shuffle
83 return next(g
['cyc'])(s
)
90 fi
= figlet_format(text
, font
='doom')
92 [next(g
['cyc'])(i
) for i
in fi
.split('\n')]
96 def show_calendar(month
, date
, rel
):
98 Show the calendar in rainbow mode
100 month
= random_rainbow(month
)
101 date
= ' '.join([cycle_color(i
) for i
in date
.split(' ')])
102 today
= str(int(os
.popen('date +\'%d\'').read().strip()))
107 ary
= line
.split(' ')
108 ary
= lmap(lambda x
: color_func(c
['CAL']['today'])(x
)
110 else color_func(c
['CAL']['days'])(x
), ary
)
111 printNicely(' '.join(ary
))
116 Check if config is changed
119 data
= get_all_config()
122 if data
[key
] != c
[key
]:
128 def validate_theme(theme
):
130 Validate a theme exists or not
132 # Theme changed check
133 files
= os
.listdir(os
.path
.dirname(__file__
) + '/colorset')
134 themes
= [f
.split('.')[0] for f
in files
if f
.split('.')[-1] == 'json']
135 return theme
in themes
138 def reload_theme(current_config
):
140 Check current theme and update if necessary
142 exists
= db
.theme_query()
143 themes
= [t
.theme_name
for t
in exists
]
144 if current_config
!= themes
[0]:
145 config
= os
.path
.dirname(
146 __file__
) + '/colorset/' + current_config
+ '.json'
148 data
= load_config(config
)
152 # Restart color cycle and update db/config
154 db
.theme_update(current_config
)
155 set_config('THEME', current_config
)
158 def color_func(func_name
):
160 Call color function base on name
162 if str(func_name
).isdigit():
163 return term_color(int(func_name
))
164 return globals()[func_name
]
167 def draw(t
, keyword
=None, check_semaphore
=False, fil
=[], ig
=[]):
172 # Check the semaphore lock (stream process only)
174 if db
.semaphore_query_pause():
176 while db
.semaphore_query_lock():
179 # Check config and theme
181 reload_theme(c
['THEME'])
186 screen_name
= t
['user']['screen_name']
187 name
= t
['user']['name']
188 created_at
= t
['created_at']
189 favorited
= t
['favorited']
190 date
= parser
.parse(created_at
)
191 date
= date
- datetime
.timedelta(seconds
=time
.timezone
)
192 clock_format
= '%Y/%m/%d %H:%M:%S'
194 clock_format
= c
['FORMAT']['TWEET']['CLOCK_FORMAT']
197 clock
= date
.strftime(clock_format
)
199 # Pull extended retweet text
201 text
= 'RT @' + t
['retweeted_status']['user']['screen_name'] + ': ' +\
202 t
['retweeted_status']['text']
206 # Unescape HTML character
207 text
= unescape(text
)
213 urls
= t
['entities']['urls']
215 expanded_url
.append(u
['expanded_url'])
224 media
= t
['entities']['media']
226 media_url
.append(m
['media_url'])
231 screen_name
= '@' + screen_name
232 if fil
and screen_name
not in fil
:
234 if ig
and screen_name
in ig
:
238 res
= db
.tweet_to_rainbow_query(tid
)
241 res
= db
.tweet_to_rainbow_query(tid
)
242 rid
= res
[0].rainbow_id
245 name
= cycle_color(name
)
246 nick
= color_func(c
['TWEET']['nick'])(screen_name
)
251 fav
= color_func(c
['TWEET']['favorited'])(u
'\u2605')
256 for index
in range(len(expanded_url
)):
258 lambda x
: expanded_url
[index
] if x
== url
[index
] else x
,
262 lambda x
: color_func(
263 c
['TWEET']['rt'])(x
) if x
== 'RT' else x
,
265 # Highlight screen_name
266 tweet
= lmap(lambda x
: cycle_color(x
) if x
[0] == '@' else x
, tweet
)
269 lambda x
: color_func(
270 c
['TWEET']['link'])(x
) if x
[
271 0:4] == 'http' else x
,
275 lambda x
: color_func(
276 c
['TWEET']['hashtag'])(x
) if x
.startswith('#') else x
,
279 tweet
= ' '.join(tweet
)
281 roj
= re
.search(keyword
, tweet
, re
.IGNORECASE
)
284 ary
= tweet
.split(occur
)
285 delimiter
= color_func(c
['TWEET']['keyword'])(occur
)
286 tweet
= delimiter
.join(ary
)
288 # Load config formater
290 formater
= c
['FORMAT']['TWEET']['DISPLAY']
291 formater
= name
.join(formater
.split("#name"))
292 formater
= nick
.join(formater
.split("#nick"))
293 formater
= fav
.join(formater
.split("#fav"))
294 formater
= tweet
.join(formater
.split("#tweet"))
296 word
= [w
for w
in formater
.split() if '#clock' in w
][0]
297 delimiter
= color_func(
298 c
['TWEET']['clock'])(
300 word
.split('#clock')))
301 formater
= delimiter
.join(formater
.split(word
))
303 word
= [w
for w
in formater
.split() if '#id' in w
][0]
304 delimiter
= color_func(c
['TWEET']['id'])(id.join(word
.split('#id')))
305 formater
= delimiter
.join(formater
.split(word
))
307 printNicely(red('Wrong format in config.'))
311 printNicely(formater
)
314 if c
['IMAGE_ON_TERM'] and media_url
:
317 response
= requests
.get(mu
)
318 image_to_display(BytesIO(response
.content
))
320 printNicely(red('Sorry, image link is broken'))
323 def print_message(m
, check_semaphore
=False):
328 # Check the semaphore lock (stream process only)
330 if db
.semaphore_query_pause():
332 while db
.semaphore_query_lock():
336 sender_screen_name
= '@' + m
['sender_screen_name']
337 sender_name
= m
['sender']['name']
338 text
= unescape(m
['text'])
339 recipient_screen_name
= '@' + m
['recipient_screen_name']
340 recipient_name
= m
['recipient']['name']
342 date
= parser
.parse(m
['created_at'])
343 date
= date
- datetime
.timedelta(seconds
=time
.timezone
)
344 clock_format
= '%Y/%m/%d %H:%M:%S'
346 clock_format
= c
['FORMAT']['MESSAGE']['CLOCK_FORMAT']
349 clock
= date
.strftime(clock_format
)
352 res
= db
.message_to_rainbow_query(mid
)
354 db
.message_store(mid
)
355 res
= db
.message_to_rainbow_query(mid
)
356 rid
= res
[0].rainbow_id
359 sender_name
= cycle_color(sender_name
)
360 sender_nick
= color_func(c
['MESSAGE']['sender'])(sender_screen_name
)
361 recipient_name
= cycle_color(recipient_name
)
362 recipient_nick
= color_func(
363 c
['MESSAGE']['recipient'])(recipient_screen_name
)
364 to
= color_func(c
['MESSAGE']['to'])('>>>')
368 text
= ''.join(lmap(lambda x
: x
+ ' ' if x
== '\n' else x
, text
))
370 # Load config formater
372 formater
= c
['FORMAT']['MESSAGE']['DISPLAY']
373 formater
= sender_name
.join(formater
.split("#sender_name"))
374 formater
= sender_nick
.join(formater
.split("#sender_nick"))
375 formater
= to
.join(formater
.split("#to"))
376 formater
= recipient_name
.join(formater
.split("#recipient_name"))
377 formater
= recipient_nick
.join(formater
.split("#recipient_nick"))
378 formater
= text
.join(formater
.split("#message"))
380 word
= [w
for w
in formater
.split() if '#clock' in w
][0]
381 delimiter
= color_func(
382 c
['MESSAGE']['clock'])(
384 word
.split('#clock')))
385 formater
= delimiter
.join(formater
.split(word
))
387 word
= [w
for w
in formater
.split() if '#id' in w
][0]
388 delimiter
= color_func(c
['MESSAGE']['id'])(id.join(word
.split('#id')))
389 formater
= delimiter
.join(formater
.split(word
))
391 printNicely(red('Wrong format in config.'))
395 printNicely(formater
)
404 screen_name
= u
['screen_name']
405 description
= u
['description']
406 profile_image_url
= u
['profile_image_url']
407 location
= u
['location']
409 created_at
= u
['created_at']
410 statuses_count
= u
['statuses_count']
411 friends_count
= u
['friends_count']
412 followers_count
= u
['followers_count']
415 statuses_count
= color_func(
416 c
['PROFILE']['statuses_count'])(
417 str(statuses_count
) +
419 friends_count
= color_func(
420 c
['PROFILE']['friends_count'])(
423 followers_count
= color_func(
424 c
['PROFILE']['followers_count'])(
425 str(followers_count
) +
427 count
= statuses_count
+ ' ' + friends_count
+ ' ' + followers_count
429 name
) + color_func(c
['PROFILE']['nick'])(' @' + screen_name
+ ' : ') + count
430 profile_image_raw_url
= 'Profile photo: ' + \
431 color_func(c
['PROFILE']['profile_image_url'])(profile_image_url
)
432 description
= ''.join(
433 lmap(lambda x
: x
+ ' ' * 4 if x
== '\n' else x
, description
))
434 description
= color_func(c
['PROFILE']['description'])(description
)
435 location
= 'Location : ' + color_func(c
['PROFILE']['location'])(location
)
436 url
= 'URL : ' + (color_func(c
['PROFILE']['url'])(url
) if url
else '')
437 date
= parser
.parse(created_at
)
438 date
= date
- datetime
.timedelta(seconds
=time
.timezone
)
439 clock
= date
.strftime('%Y/%m/%d %H:%M:%S')
440 clock
= 'Join at ' + color_func(c
['PROFILE']['clock'])(clock
)
443 line1
= u
"{u:>{uw}}".format(
447 line2
= u
"{p:>{pw}}".format(
448 p
=profile_image_raw_url
,
449 pw
=len(profile_image_raw_url
) + 4,
451 line3
= u
"{d:>{dw}}".format(
453 dw
=len(description
) + 4,
455 line4
= u
"{l:>{lw}}".format(
457 lw
=len(location
) + 4,
459 line5
= u
"{u:>{uw}}".format(
463 line6
= u
"{c:>{cw}}".format(
471 if c
['IMAGE_ON_TERM']:
473 response
= requests
.get(profile_image_url
)
474 image_to_display(BytesIO(response
.content
))
479 for line
in [line3
, line4
, line5
, line6
]:
484 def print_trends(trends
):
488 for topic
in trends
[:c
['TREND_MAX']]:
491 line
= cycle_color(name
) + ': ' + color_func(c
['TREND']['url'])(url
)
496 def print_list(group
):
502 name
= g
['full_name']
503 name
= color_func(c
['GROUP']['name'])(name
+ ' : ')
504 member
= str(g
['member_count'])
505 member
= color_func(c
['GROUP']['member'])(member
+ ' member')
506 subscriber
= str(g
['subscriber_count'])
507 subscriber
= color_func(
508 c
['GROUP']['subscriber'])(
511 description
= g
['description'].strip()
512 description
= color_func(c
['GROUP']['description'])(description
)
514 mode
= color_func(c
['GROUP']['mode'])('Type: ' + mode
)
515 created_at
= g
['created_at']
516 date
= parser
.parse(created_at
)
517 date
= date
- datetime
.timedelta(seconds
=time
.timezone
)
518 clock
= date
.strftime('%Y/%m/%d %H:%M:%S')
519 clock
= 'Created at ' + color_func(c
['GROUP']['clock'])(clock
)
522 line1
= ' ' * 2 + name
+ member
+ ' ' + subscriber
523 line2
= ' ' * 4 + description
524 line3
= ' ' * 4 + mode
525 line4
= ' ' * 4 + clock
537 # Start the color cycle