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()
121 if data
[key
] != c
[key
]:
129 Check current theme and update if necessary
131 exists
= db
.theme_query()
132 themes
= [t
.theme_name
for t
in exists
]
133 if c
['THEME'] != themes
[0]:
134 c
['THEME'] = themes
[0]
135 config
= os
.path
.dirname(
136 __file__
) + '/colorset/' + c
['THEME'] + '.json'
138 data
= load_config(config
)
142 # Re-init color cycle
143 g
['cyc'] = init_cycle()
146 def color_func(func_name
):
148 Call color function base on name
150 if str(func_name
).isdigit():
151 return term_color(int(func_name
))
152 return globals()[func_name
]
155 def draw(t
, iot
=False, keyword
=None, check_semaphore
=False, fil
=[], ig
=[]):
165 screen_name
= t
['user']['screen_name']
166 name
= t
['user']['name']
167 created_at
= t
['created_at']
168 favorited
= t
['favorited']
169 date
= parser
.parse(created_at
)
170 date
= date
- datetime
.timedelta(seconds
=time
.timezone
)
171 clock
= date
.strftime('%Y/%m/%d %H:%M:%S')
173 # Pull extended retweet text
175 text
= 'RT @' + t
['retweeted_status']['user']['screen_name'] + ': ' +\
176 t
['retweeted_status']['text']
180 # Unescape HTML character
181 text
= unescape(text
)
187 urls
= t
['entities']['urls']
189 expanded_url
.append(u
['expanded_url'])
198 media
= t
['entities']['media']
200 media_url
.append(m
['media_url'])
205 screen_name
= '@' + screen_name
206 if fil
and screen_name
not in fil
:
208 if ig
and screen_name
in ig
:
212 res
= db
.tweet_to_rainbow_query(tid
)
215 res
= db
.tweet_to_rainbow_query(tid
)
216 rid
= res
[0].rainbow_id
220 name
) + color_func(c
['TWEET']['nick'])(' ' + screen_name
+ ' ')
221 meta
= color_func(c
['TWEET']['clock'])(
222 '[' + clock
+ '] ') + color_func(c
['TWEET']['id'])('[id=' + str(rid
) + '] ')
224 meta
= meta
+ color_func(c
['TWEET']['favorited'])(u
'\u2605')
228 for index
in range(len(expanded_url
)):
230 lambda x
: expanded_url
[index
] if x
== url
[index
] else x
,
234 lambda x
: color_func(
235 c
['TWEET']['rt'])(x
) if x
== 'RT' else x
,
237 # Highlight screen_name
238 tweet
= lmap(lambda x
: cycle_color(x
) if x
[0] == '@' else x
, tweet
)
241 lambda x
: color_func(
242 c
['TWEET']['link'])(x
) if x
[
243 0:4] == 'http' else x
,
247 tweet
= ' '.join(tweet
)
249 roj
= re
.search(keyword
, tweet
, re
.IGNORECASE
)
252 ary
= tweet
.split(occur
)
253 delimeter
= color_func(c
['TWEET']['keyword'])(occur
)
254 tweet
= delimeter
.join(ary
)
257 line1
= u
"{u:>{uw}}:".format(
261 line2
= u
"{c:>{cw}}".format(
267 # Check the semaphore lock
269 while db
.semaphore_query():
279 if iot
and media_url
:
282 response
= requests
.get(mu
)
283 image_to_display(BytesIO(response
.content
))
285 printNicely(red('Sorry, image link is broken'))
288 def print_message(m
):
292 sender_screen_name
= '@' + m
['sender_screen_name']
293 sender_name
= m
['sender']['name']
294 text
= unescape(m
['text'])
295 recipient_screen_name
= '@' + m
['recipient_screen_name']
296 recipient_name
= m
['recipient']['name']
298 date
= parser
.parse(m
['created_at'])
299 date
= date
- datetime
.timedelta(seconds
=time
.timezone
)
300 clock
= date
.strftime('%Y/%m/%d %H:%M:%S')
303 res
= db
.message_to_rainbow_query(mid
)
305 db
.message_store(mid
)
306 res
= db
.message_to_rainbow_query(mid
)
307 rid
= res
[0].rainbow_id
310 sender
= cycle_color(
311 sender_name
) + color_func(c
['MESSAGE']['sender'])(' ' + sender_screen_name
+ ' ')
312 recipient
= cycle_color(recipient_name
) + color_func(
313 c
['MESSAGE']['recipient'])(
314 ' ' + recipient_screen_name
+ ' ')
315 user
= sender
+ color_func(c
['MESSAGE']['to'])(' >>> ') + recipient
317 c
['MESSAGE']['clock'])(
318 '[' + clock
+ ']') + color_func(
320 ' [message_id=' + str(rid
) + '] ')
321 text
= ''.join(lmap(lambda x
: x
+ ' ' if x
== '\n' else x
, text
))
323 line1
= u
"{u:>{uw}}:".format(
327 line2
= u
"{c:>{cw}}".format(
340 def show_profile(u
, iot
=False):
346 screen_name
= u
['screen_name']
347 description
= u
['description']
348 profile_image_url
= u
['profile_image_url']
349 location
= u
['location']
351 created_at
= u
['created_at']
352 statuses_count
= u
['statuses_count']
353 friends_count
= u
['friends_count']
354 followers_count
= u
['followers_count']
357 statuses_count
= color_func(
358 c
['PROFILE']['statuses_count'])(
359 str(statuses_count
) +
361 friends_count
= color_func(
362 c
['PROFILE']['friends_count'])(
365 followers_count
= color_func(
366 c
['PROFILE']['followers_count'])(
367 str(followers_count
) +
369 count
= statuses_count
+ ' ' + friends_count
+ ' ' + followers_count
371 name
) + color_func(c
['PROFILE']['nick'])(' @' + screen_name
+ ' : ') + count
372 profile_image_raw_url
= 'Profile photo: ' + \
373 color_func(c
['PROFILE']['profile_image_url'])(profile_image_url
)
374 description
= ''.join(
375 lmap(lambda x
: x
+ ' ' * 4 if x
== '\n' else x
, description
))
376 description
= color_func(c
['PROFILE']['description'])(description
)
377 location
= 'Location : ' + color_func(c
['PROFILE']['location'])(location
)
378 url
= 'URL : ' + (color_func(c
['PROFILE']['url'])(url
) if url
else '')
379 date
= parser
.parse(created_at
)
380 date
= date
- datetime
.timedelta(seconds
=time
.timezone
)
381 clock
= date
.strftime('%Y/%m/%d %H:%M:%S')
382 clock
= 'Join at ' + color_func(c
['PROFILE']['clock'])(clock
)
385 line1
= u
"{u:>{uw}}".format(
389 line2
= u
"{p:>{pw}}".format(
390 p
=profile_image_raw_url
,
391 pw
=len(profile_image_raw_url
) + 4,
393 line3
= u
"{d:>{dw}}".format(
395 dw
=len(description
) + 4,
397 line4
= u
"{l:>{lw}}".format(
399 lw
=len(location
) + 4,
401 line5
= u
"{u:>{uw}}".format(
405 line6
= u
"{c:>{cw}}".format(
415 response
= requests
.get(profile_image_url
)
416 image_to_display(BytesIO(response
.content
), 2, 20)
421 for line
in [line3
, line4
, line5
, line6
]:
426 def print_trends(trends
):
430 for topic
in trends
[:c
['TREND_MAX']]:
433 line
= cycle_color(name
) + ': ' + color_func(c
['TREND']['url'])(url
)
438 def print_list(group
):
444 name
= g
['full_name']
445 name
= color_func(c
['GROUP']['name'])(name
+ ' : ')
446 member
= str(g
['member_count'])
447 member
= color_func(c
['GROUP']['member'])(member
+ ' member')
448 subscriber
= str(g
['subscriber_count'])
449 subscriber
= color_func(
450 c
['GROUP']['subscriber'])(
453 description
= g
['description'].strip()
454 description
= color_func(c
['GROUP']['description'])(description
)
456 mode
= color_func(c
['GROUP']['mode'])('Type: ' + mode
)
457 created_at
= g
['created_at']
458 date
= parser
.parse(created_at
)
459 date
= date
- datetime
.timedelta(seconds
=time
.timezone
)
460 clock
= date
.strftime('%Y/%m/%d %H:%M:%S')
461 clock
= 'Created at ' + color_func(c
['GROUP']['clock'])(clock
)
464 line1
= ' ' * 2 + name
+ member
+ ' ' + subscriber
465 line2
= ' ' * 4 + description
466 line3
= ' ' * 4 + mode
467 line4
= ' ' * 4 + clock
479 # Start the color cycle