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 current theme and update if necessary
118 exists
= db
.theme_query()
119 themes
= [t
.theme_name
for t
in exists
]
120 if c
['THEME'] != themes
[0]:
121 c
['THEME'] = themes
[0]
122 config
= os
.path
.dirname(
123 __file__
) + '/colorset/' + c
['THEME'] + '.json'
125 data
= load_config(config
)
129 # Re-init color cycle
130 g
['cyc'] = init_cycle()
133 def color_func(func_name
):
135 Call color function base on name
137 if str(func_name
).isdigit():
138 return term_color(int(func_name
))
139 return globals()[func_name
]
142 def draw(t
, iot
=False, keyword
=None, check_semaphore
=False, fil
=[], ig
=[]):
151 screen_name
= t
['user']['screen_name']
152 name
= t
['user']['name']
153 created_at
= t
['created_at']
154 favorited
= t
['favorited']
155 date
= parser
.parse(created_at
)
156 date
= date
- datetime
.timedelta(seconds
=time
.timezone
)
157 clock
= date
.strftime('%Y/%m/%d %H:%M:%S')
159 # Pull extended retweet text
161 text
= 'RT @' + t
['retweeted_status']['user']['screen_name'] + ': ' +\
162 t
['retweeted_status']['text']
166 # Unescape HTML character
167 text
= unescape(text
)
173 urls
= t
['entities']['urls']
175 expanded_url
.append(u
['expanded_url'])
184 media
= t
['entities']['media']
186 media_url
.append(m
['media_url'])
191 screen_name
= '@' + screen_name
192 if fil
and screen_name
not in fil
:
194 if ig
and screen_name
in ig
:
198 res
= db
.tweet_to_rainbow_query(tid
)
201 res
= db
.tweet_to_rainbow_query(tid
)
202 rid
= res
[0].rainbow_id
206 name
) + color_func(c
['TWEET']['nick'])(' ' + screen_name
+ ' ')
207 meta
= color_func(c
['TWEET']['clock'])(
208 '[' + clock
+ '] ') + color_func(c
['TWEET']['id'])('[id=' + str(rid
) + '] ')
210 meta
= meta
+ color_func(c
['TWEET']['favorited'])(u
'\u2605')
214 for index
in range(len(expanded_url
)):
216 lambda x
: expanded_url
[index
] if x
== url
[index
] else x
,
220 lambda x
: color_func(
221 c
['TWEET']['rt'])(x
) if x
== 'RT' else x
,
223 # Highlight screen_name
224 tweet
= lmap(lambda x
: cycle_color(x
) if x
[0] == '@' else x
, tweet
)
227 lambda x
: color_func(
228 c
['TWEET']['link'])(x
) if x
[
229 0:4] == 'http' else x
,
233 tweet
= ' '.join(tweet
)
235 roj
= re
.search(keyword
, tweet
, re
.IGNORECASE
)
238 ary
= tweet
.split(occur
)
239 delimeter
= color_func(c
['TWEET']['keyword'])(occur
)
240 tweet
= delimeter
.join(ary
)
243 line1
= u
"{u:>{uw}}:".format(
247 line2
= u
"{c:>{cw}}".format(
253 # Check the semaphore lock
255 while db
.semaphore_query():
265 if iot
and media_url
:
268 response
= requests
.get(mu
)
269 image_to_display(BytesIO(response
.content
))
271 printNicely(red('Sorry, image link is broken'))
274 def print_message(m
):
278 sender_screen_name
= '@' + m
['sender_screen_name']
279 sender_name
= m
['sender']['name']
280 text
= unescape(m
['text'])
281 recipient_screen_name
= '@' + m
['recipient_screen_name']
282 recipient_name
= m
['recipient']['name']
284 date
= parser
.parse(m
['created_at'])
285 date
= date
- datetime
.timedelta(seconds
=time
.timezone
)
286 clock
= date
.strftime('%Y/%m/%d %H:%M:%S')
289 res
= db
.message_to_rainbow_query(mid
)
291 db
.message_store(mid
)
292 res
= db
.message_to_rainbow_query(mid
)
293 rid
= res
[0].rainbow_id
296 sender
= cycle_color(
297 sender_name
) + color_func(c
['MESSAGE']['sender'])(' ' + sender_screen_name
+ ' ')
298 recipient
= cycle_color(recipient_name
) + color_func(
299 c
['MESSAGE']['recipient'])(
300 ' ' + recipient_screen_name
+ ' ')
301 user
= sender
+ color_func(c
['MESSAGE']['to'])(' >>> ') + recipient
303 c
['MESSAGE']['clock'])(
304 '[' + clock
+ ']') + color_func(
306 ' [message_id=' + str(rid
) + '] ')
307 text
= ''.join(lmap(lambda x
: x
+ ' ' if x
== '\n' else x
, text
))
309 line1
= u
"{u:>{uw}}:".format(
313 line2
= u
"{c:>{cw}}".format(
326 def show_profile(u
, iot
=False):
332 screen_name
= u
['screen_name']
333 description
= u
['description']
334 profile_image_url
= u
['profile_image_url']
335 location
= u
['location']
337 created_at
= u
['created_at']
338 statuses_count
= u
['statuses_count']
339 friends_count
= u
['friends_count']
340 followers_count
= u
['followers_count']
343 statuses_count
= color_func(
344 c
['PROFILE']['statuses_count'])(
345 str(statuses_count
) +
347 friends_count
= color_func(
348 c
['PROFILE']['friends_count'])(
351 followers_count
= color_func(
352 c
['PROFILE']['followers_count'])(
353 str(followers_count
) +
355 count
= statuses_count
+ ' ' + friends_count
+ ' ' + followers_count
357 name
) + color_func(c
['PROFILE']['nick'])(' @' + screen_name
+ ' : ') + count
358 profile_image_raw_url
= 'Profile photo: ' + \
359 color_func(c
['PROFILE']['profile_image_url'])(profile_image_url
)
360 description
= ''.join(
361 lmap(lambda x
: x
+ ' ' * 4 if x
== '\n' else x
, description
))
362 description
= color_func(c
['PROFILE']['description'])(description
)
363 location
= 'Location : ' + color_func(c
['PROFILE']['location'])(location
)
364 url
= 'URL : ' + (color_func(c
['PROFILE']['url'])(url
) if url
else '')
365 date
= parser
.parse(created_at
)
366 date
= date
- datetime
.timedelta(seconds
=time
.timezone
)
367 clock
= date
.strftime('%Y/%m/%d %H:%M:%S')
368 clock
= 'Join at ' + color_func(c
['PROFILE']['clock'])(clock
)
371 line1
= u
"{u:>{uw}}".format(
375 line2
= u
"{p:>{pw}}".format(
376 p
=profile_image_raw_url
,
377 pw
=len(profile_image_raw_url
) + 4,
379 line3
= u
"{d:>{dw}}".format(
381 dw
=len(description
) + 4,
383 line4
= u
"{l:>{lw}}".format(
385 lw
=len(location
) + 4,
387 line5
= u
"{u:>{uw}}".format(
391 line6
= u
"{c:>{cw}}".format(
401 response
= requests
.get(profile_image_url
)
402 image_to_display(BytesIO(response
.content
), 2, 20)
407 for line
in [line3
, line4
, line5
, line6
]:
412 def print_trends(trends
):
416 for topic
in trends
[:c
['TREND_MAX']]:
419 line
= cycle_color(name
) + ': ' + color_func(c
['TREND']['url'])(url
)
424 def print_list(group
):
430 name
= g
['full_name']
431 name
= color_func(c
['GROUP']['name'])(name
+ ' : ')
432 member
= str(g
['member_count'])
433 member
= color_func(c
['GROUP']['member'])(member
+ ' member')
434 subscriber
= str(g
['subscriber_count'])
435 subscriber
= color_func(
436 c
['GROUP']['subscriber'])(
439 description
= g
['description'].strip()
440 description
= color_func(c
['GROUP']['description'])(description
)
442 mode
= color_func(c
['GROUP']['mode'])('Type: ' + mode
)
443 created_at
= g
['created_at']
444 date
= parser
.parse(created_at
)
445 date
= date
- datetime
.timedelta(seconds
=time
.timezone
)
446 clock
= date
.strftime('%Y/%m/%d %H:%M:%S')
447 clock
= 'Created at ' + color_func(c
['GROUP']['clock'])(clock
)
450 line1
= ' ' * 2 + name
+ member
+ ' ' + subscriber
451 line2
= ' ' * 4 + description
452 line3
= ' ' * 4 + mode
453 line4
= ' ' * 4 + clock
465 # Start the color cycle