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')]
98 Check if config is changed
101 data
= get_all_config()
104 if data
[key
] != c
[key
]:
110 def validate_theme(theme
):
112 Validate a theme exists or not
114 # Theme changed check
115 files
= os
.listdir(os
.path
.dirname(__file__
) + '/colorset')
116 themes
= [f
.split('.')[0] for f
in files
if f
.split('.')[-1] == 'json']
117 return theme
in themes
120 def reload_theme(current_config
):
122 Check current theme and update if necessary
124 exists
= db
.theme_query()
125 themes
= [t
.theme_name
for t
in exists
]
126 if current_config
!= themes
[0]:
127 config
= os
.path
.dirname(
128 __file__
) + '/colorset/' + current_config
+ '.json'
130 data
= load_config(config
)
134 # Restart color cycle and update db/config
136 db
.theme_update(current_config
)
137 set_config('THEME', current_config
)
140 def color_func(func_name
):
142 Call color function base on name
144 if str(func_name
).isdigit():
145 return term_color(int(func_name
))
146 return globals()[func_name
]
149 def draw(t
, keyword
=None, check_semaphore
=False, fil
=[], ig
=[]):
154 # Check the semaphore lock (stream process only)
156 if db
.semaphore_query_pause():
158 while db
.semaphore_query_lock():
161 # Check config and theme
163 reload_theme(c
['THEME'])
168 screen_name
= t
['user']['screen_name']
169 name
= t
['user']['name']
170 created_at
= t
['created_at']
171 favorited
= t
['favorited']
172 date
= parser
.parse(created_at
)
173 date
= date
- datetime
.timedelta(seconds
=time
.timezone
)
174 clock_format
= '%Y/%m/%d %H:%M:%S'
176 clock_format
= c
['FORMAT']['TWEET']['CLOCK_FORMAT']
179 clock
= date
.strftime(clock_format
)
181 # Pull extended retweet text
183 text
= 'RT @' + t
['retweeted_status']['user']['screen_name'] + ': ' +\
184 t
['retweeted_status']['text']
188 # Unescape HTML character
189 text
= unescape(text
)
195 urls
= t
['entities']['urls']
197 expanded_url
.append(u
['expanded_url'])
206 media
= t
['entities']['media']
208 media_url
.append(m
['media_url'])
213 screen_name
= '@' + screen_name
214 if fil
and screen_name
not in fil
:
216 if ig
and screen_name
in ig
:
220 res
= db
.tweet_to_rainbow_query(tid
)
223 res
= db
.tweet_to_rainbow_query(tid
)
224 rid
= res
[0].rainbow_id
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 range(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
276 formater
= c
['FORMAT']['TWEET']['DISPLAY']
277 formater
= name
.join(formater
.split("#name"))
278 formater
= nick
.join(formater
.split("#nick"))
279 formater
= fav
.join(formater
.split("#fav"))
280 formater
= tweet
.join(formater
.split("#tweet"))
282 word
= [w
for w
in formater
.split() if '#clock' in w
][0]
283 delimiter
= color_func(c
['TWEET']['clock'])(
284 clock
.join(word
.split('#clock')))
285 formater
= delimiter
.join(formater
.split(word
))
287 word
= [w
for w
in formater
.split() if '#id' in w
][0]
288 delimiter
= color_func(c
['TWEET']['id'])(id.join(word
.split('#id')))
289 formater
= delimiter
.join(formater
.split(word
))
291 printNicely(red('Wrong format in config.'))
295 printNicely(formater
)
298 if c
['IMAGE_ON_TERM'] and media_url
:
301 response
= requests
.get(mu
)
302 image_to_display(BytesIO(response
.content
))
304 printNicely(red('Sorry, image link is broken'))
307 def print_message(m
, check_semaphore
=False):
312 # Check the semaphore lock (stream process only)
314 if db
.semaphore_query_pause():
316 while db
.semaphore_query_lock():
320 sender_screen_name
= '@' + m
['sender_screen_name']
321 sender_name
= m
['sender']['name']
322 text
= unescape(m
['text'])
323 recipient_screen_name
= '@' + m
['recipient_screen_name']
324 recipient_name
= m
['recipient']['name']
326 date
= parser
.parse(m
['created_at'])
327 date
= date
- datetime
.timedelta(seconds
=time
.timezone
)
328 clock_format
= '%Y/%m/%d %H:%M:%S'
330 clock_format
= c
['FORMAT']['MESSAGE']['CLOCK_FORMAT']
333 clock
= date
.strftime(clock_format
)
336 res
= db
.message_to_rainbow_query(mid
)
338 db
.message_store(mid
)
339 res
= db
.message_to_rainbow_query(mid
)
340 rid
= res
[0].rainbow_id
343 sender_name
= cycle_color(sender_name
)
344 sender_nick
= color_func(c
['MESSAGE']['sender'])(sender_screen_name
)
345 recipient_name
= cycle_color(recipient_name
)
346 recipient_nick
= color_func(
347 c
['MESSAGE']['recipient'])(recipient_screen_name
)
348 to
= color_func(c
['MESSAGE']['to'])('>>>')
352 text
= ''.join(lmap(lambda x
: x
+ ' ' if x
== '\n' else x
, text
))
354 # Load config formater
356 formater
= c
['FORMAT']['MESSAGE']['DISPLAY']
357 formater
= sender_name
.join(formater
.split("#sender_name"))
358 formater
= sender_nick
.join(formater
.split("#sender_nick"))
359 formater
= to
.join(formater
.split("#to"))
360 formater
= recipient_name
.join(formater
.split("#recipient_name"))
361 formater
= recipient_nick
.join(formater
.split("#recipient_nick"))
362 formater
= text
.join(formater
.split("#message"))
364 word
= [w
for w
in formater
.split() if '#clock' in w
][0]
365 delimiter
= color_func(c
['MESSAGE']['clock'])(
366 clock
.join(word
.split('#clock')))
367 formater
= delimiter
.join(formater
.split(word
))
369 word
= [w
for w
in formater
.split() if '#id' in w
][0]
370 delimiter
= color_func(c
['MESSAGE']['id'])(id.join(word
.split('#id')))
371 formater
= delimiter
.join(formater
.split(word
))
373 printNicely(red('Wrong format in config.'))
377 printNicely(formater
)
386 screen_name
= u
['screen_name']
387 description
= u
['description']
388 profile_image_url
= u
['profile_image_url']
389 location
= u
['location']
391 created_at
= u
['created_at']
392 statuses_count
= u
['statuses_count']
393 friends_count
= u
['friends_count']
394 followers_count
= u
['followers_count']
397 statuses_count
= color_func(
398 c
['PROFILE']['statuses_count'])(
399 str(statuses_count
) +
401 friends_count
= color_func(
402 c
['PROFILE']['friends_count'])(
405 followers_count
= color_func(
406 c
['PROFILE']['followers_count'])(
407 str(followers_count
) +
409 count
= statuses_count
+ ' ' + friends_count
+ ' ' + followers_count
411 name
) + color_func(c
['PROFILE']['nick'])(' @' + screen_name
+ ' : ') + count
412 profile_image_raw_url
= 'Profile photo: ' + \
413 color_func(c
['PROFILE']['profile_image_url'])(profile_image_url
)
414 description
= ''.join(
415 lmap(lambda x
: x
+ ' ' * 4 if x
== '\n' else x
, description
))
416 description
= color_func(c
['PROFILE']['description'])(description
)
417 location
= 'Location : ' + color_func(c
['PROFILE']['location'])(location
)
418 url
= 'URL : ' + (color_func(c
['PROFILE']['url'])(url
) if url
else '')
419 date
= parser
.parse(created_at
)
420 date
= date
- datetime
.timedelta(seconds
=time
.timezone
)
421 clock
= date
.strftime('%Y/%m/%d %H:%M:%S')
422 clock
= 'Join at ' + color_func(c
['PROFILE']['clock'])(clock
)
425 line1
= u
"{u:>{uw}}".format(
429 line2
= u
"{p:>{pw}}".format(
430 p
=profile_image_raw_url
,
431 pw
=len(profile_image_raw_url
) + 4,
433 line3
= u
"{d:>{dw}}".format(
435 dw
=len(description
) + 4,
437 line4
= u
"{l:>{lw}}".format(
439 lw
=len(location
) + 4,
441 line5
= u
"{u:>{uw}}".format(
445 line6
= u
"{c:>{cw}}".format(
453 if c
['IMAGE_ON_TERM']:
455 response
= requests
.get(profile_image_url
)
456 image_to_display(BytesIO(response
.content
))
461 for line
in [line3
, line4
, line5
, line6
]:
466 def print_trends(trends
):
470 for topic
in trends
[:c
['TREND_MAX']]:
473 line
= cycle_color(name
) + ': ' + color_func(c
['TREND']['url'])(url
)
478 def print_list(group
):
484 name
= g
['full_name']
485 name
= color_func(c
['GROUP']['name'])(name
+ ' : ')
486 member
= str(g
['member_count'])
487 member
= color_func(c
['GROUP']['member'])(member
+ ' member')
488 subscriber
= str(g
['subscriber_count'])
489 subscriber
= color_func(
490 c
['GROUP']['subscriber'])(
493 description
= g
['description'].strip()
494 description
= color_func(c
['GROUP']['description'])(description
)
496 mode
= color_func(c
['GROUP']['mode'])('Type: ' + mode
)
497 created_at
= g
['created_at']
498 date
= parser
.parse(created_at
)
499 date
= date
- datetime
.timedelta(seconds
=time
.timezone
)
500 clock
= date
.strftime('%Y/%m/%d %H:%M:%S')
501 clock
= 'Created at ' + color_func(c
['GROUP']['clock'])(clock
)
504 line1
= ' ' * 2 + name
+ member
+ ' ' + subscriber
505 line2
= ' ' * 4 + description
506 line3
= ' ' * 4 + mode
507 line4
= ' ' * 4 + clock
519 def show_calendar(month
, date
, rel
):
521 Show the calendar in rainbow mode
523 month
= random_rainbow(month
)
524 date
= ' '.join([cycle_color(i
) for i
in date
.split(' ')])
525 today
= str(int(os
.popen('date +\'%d\'').read().strip()))
530 ary
= line
.split(' ')
532 lambda x
: color_func(c
['CAL']['today'])(x
)
534 else color_func(c
['CAL']['days'])(x
),
536 printNicely(' '.join(ary
))
539 # Start the color cycle