7 from twitter
.util
import printNicely
8 from functools
import wraps
9 from pyfiglet
import figlet_format
10 from functools
import reduce
11 from StringIO
import StringIO
12 from dateutil
import parser
13 from .c_image
import *
25 colors_shuffle
= [globals()[i
.encode('utf8')]
26 if not i
.startswith('term_')
27 else term_color(int(i
[5:]))
28 for i
in c
['CYCLE_COLOR']]
29 return itertools
.cycle(colors_shuffle
)
30 g
['cyc'] = init_cycle()
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 i
.startswith('term_')
48 else term_color(int(i
[5:]))
49 for i
in c
['CYCLE_COLOR']]
50 colored
= [colors_shuffle
[i
% 7](s
[i
]) for i
in xrange(len(s
))]
51 return reduce(lambda x
, y
: x
+ y
, 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 i
.startswith('term_')
60 else term_color(int(i
[5:]))
61 for i
in c
['CYCLE_COLOR']]
62 colored
= [random
.choice(colors_shuffle
)(i
) for i
in s
]
63 return reduce(lambda x
, y
: x
+ y
, 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
= map(lambda x
: color_func(c
['CAL']['today'])(x
)
110 else color_func(c
['CAL']['days'])(x
)
112 printNicely(' '.join(ary
))
117 Check current theme and update if necessary
119 exists
= db
.theme_query()
120 themes
= [t
.theme_name
for t
in exists
]
121 if c
['theme'] != themes
[0]:
122 c
['theme'] = themes
[0]
124 if c
['theme'] == 'custom':
125 config
= os
.environ
.get(
127 os
.environ
.get('USERPROFILE',
128 '')) + os
.sep
+ '.rainbow_config.json'
130 config
= os
.path
.dirname(__file__
) + '/colorset/'+c
['theme']+'.json'
132 data
= load_config(config
)
136 # Re-init color cycle
137 g
['cyc'] = init_cycle()
140 def color_func(func_name
):
142 Call color function base on name
144 pure
= func_name
.encode('utf8')
145 if pure
.startswith('term_') and pure
[5:].isdigit():
146 return term_color(int(pure
[5:]))
147 return globals()[pure
]
150 def draw(t
, iot
=False, keyword
=None, fil
=[], ig
=[]):
159 screen_name
= t
['user']['screen_name']
160 name
= t
['user']['name']
161 created_at
= t
['created_at']
162 favorited
= t
['favorited']
163 date
= parser
.parse(created_at
)
164 date
= date
- datetime
.timedelta(seconds
=time
.timezone
)
165 clock
= date
.strftime('%Y/%m/%d %H:%M:%S')
171 urls
= t
['entities']['urls']
173 expanded_url
.append(u
['expanded_url'])
182 media
= t
['entities']['media']
184 media_url
.append(m
['media_url'])
189 screen_name
= '@' + screen_name
190 if fil
and screen_name
not in fil
:
192 if ig
and screen_name
in ig
:
196 res
= db
.tweet_to_rainbow_query(tid
)
199 res
= db
.tweet_to_rainbow_query(tid
)
200 rid
= res
[0].rainbow_id
204 name
) + color_func(c
['TWEET']['nick'])(' ' + screen_name
+ ' ')
205 meta
= color_func(c
['TWEET']['clock'])(
206 '[' + clock
+ '] ') + color_func(c
['TWEET']['id'])('[id=' + str(rid
) + '] ')
208 meta
= meta
+ color_func(c
['TWEET']['favorited'])(u
'\u2605')
212 for index
in range(len(expanded_url
)):
214 lambda x
: expanded_url
[index
] if x
== url
[index
] else x
,
218 lambda x
: color_func(
219 c
['TWEET']['rt'])(x
) if x
== 'RT' else x
,
221 # Highlight screen_name
222 tweet
= map(lambda x
: cycle_color(x
) if x
[0] == '@' else x
, tweet
)
225 lambda x
: color_func(
226 c
['TWEET']['link'])(x
) if x
[
227 0:4] == 'http' else x
,
229 # Highlight search keyword
232 lambda x
: color_func(c
['TWEET']['keyword'])(x
) if
233 ''.join(c
for c
in x
if c
.isalnum()).lower() == keyword
.lower()
238 tweet
= ' '.join(tweet
)
241 line1
= u
"{u:>{uw}}:".format(
245 line2
= u
"{c:>{cw}}".format(
257 if iot
and media_url
:
260 response
= requests
.get(mu
)
261 image_to_display(StringIO(response
.content
))
263 printNicely(red('Sorry, image link is broken'))
266 def print_message(m
):
270 sender_screen_name
= '@' + m
['sender_screen_name']
271 sender_name
= m
['sender']['name']
273 recipient_screen_name
= '@' + m
['recipient_screen_name']
274 recipient_name
= m
['recipient']['name']
276 date
= parser
.parse(m
['created_at'])
277 date
= date
- datetime
.timedelta(seconds
=time
.timezone
)
278 clock
= date
.strftime('%Y/%m/%d %H:%M:%S')
281 res
= db
.message_to_rainbow_query(mid
)
283 db
.message_store(mid
)
284 res
= db
.message_to_rainbow_query(mid
)
285 rid
= res
[0].rainbow_id
288 sender
= cycle_color(
289 sender_name
) + color_func(c
['MESSAGE']['sender'])(' ' + sender_screen_name
+ ' ')
290 recipient
= cycle_color(recipient_name
) + color_func(
291 c
['MESSAGE']['recipient'])(
292 ' ' + recipient_screen_name
+ ' ')
293 user
= sender
+ color_func(c
['MESSAGE']['to'])(' >>> ') + recipient
295 c
['MESSAGE']['clock'])(
296 '[' + clock
+ ']') + color_func(
298 ' [message_id=' + str(rid
) + '] ')
299 text
= ''.join(map(lambda x
: x
+ ' ' if x
== '\n' else x
, text
))
301 line1
= u
"{u:>{uw}}:".format(
305 line2
= u
"{c:>{cw}}".format(
318 def show_profile(u
, iot
=False):
324 screen_name
= u
['screen_name']
325 description
= u
['description']
326 profile_image_url
= u
['profile_image_url']
327 location
= u
['location']
329 created_at
= u
['created_at']
330 statuses_count
= u
['statuses_count']
331 friends_count
= u
['friends_count']
332 followers_count
= u
['followers_count']
335 statuses_count
= color_func(
336 c
['PROFILE']['statuses_count'])(
337 str(statuses_count
) +
339 friends_count
= color_func(
340 c
['PROFILE']['friends_count'])(
343 followers_count
= color_func(
344 c
['PROFILE']['followers_count'])(
345 str(followers_count
) +
347 count
= statuses_count
+ ' ' + friends_count
+ ' ' + followers_count
349 name
) + color_func(c
['PROFILE']['nick'])(' @' + screen_name
+ ' : ') + count
350 profile_image_raw_url
= 'Profile photo: ' + \
351 color_func(c
['PROFILE']['profile_image_url'])(profile_image_url
)
352 description
= ''.join(
353 map(lambda x
: x
+ ' ' * 4 if x
== '\n' else x
, description
))
354 description
= color_func(c
['PROFILE']['description'])(description
)
355 location
= 'Location : ' + color_func(c
['PROFILE']['location'])(location
)
356 url
= 'URL : ' + (color_func(c
['PROFILE']['url'])(url
) if url
else '')
357 date
= parser
.parse(created_at
)
358 date
= date
- datetime
.timedelta(seconds
=time
.timezone
)
359 clock
= date
.strftime('%Y/%m/%d %H:%M:%S')
360 clock
= 'Join at ' + color_func(c
['PROFILE']['clock'])(clock
)
363 line1
= u
"{u:>{uw}}".format(
367 line2
= u
"{p:>{pw}}".format(
368 p
=profile_image_raw_url
,
369 pw
=len(profile_image_raw_url
) + 4,
371 line3
= u
"{d:>{dw}}".format(
373 dw
=len(description
) + 4,
375 line4
= u
"{l:>{lw}}".format(
377 lw
=len(location
) + 4,
379 line5
= u
"{u:>{uw}}".format(
383 line6
= u
"{c:>{cw}}".format(
393 response
= requests
.get(profile_image_url
)
394 image_to_display(StringIO(response
.content
), 2, 20)
399 for line
in [line3
, line4
, line5
, line6
]:
404 def print_trends(trends
):
408 for topic
in trends
[:c
['TREND_MAX']]:
411 line
= cycle_color(name
) + ': ' + color_func(c
['TREND']['url'])(url
)
416 def print_list(group
):
423 name
= color_func(c
['GROUP']['name'])(name
+ ' : ')
424 member
= str(g
['member_count'])
425 member
= color_func(c
['GROUP']['member'])(member
+' member')
426 subscriber
= str(g
['subscriber_count'])
427 subscriber
= color_func(c
['GROUP']['subscriber'])(subscriber
+' subscriber')
428 description
= g
['description'].strip()
429 description
= color_func(c
['GROUP']['description'])(description
)
431 mode
= color_func(c
['GROUP']['mode'])('Type: '+mode
)
432 created_at
= g
['created_at']
433 date
= parser
.parse(created_at
)
434 date
= date
- datetime
.timedelta(seconds
=time
.timezone
)
435 clock
= date
.strftime('%Y/%m/%d %H:%M:%S')
436 clock
= 'Created at ' + color_func(c
['GROUP']['clock'])(clock
)
439 db
.list_store(g
['id'],g
['name'])
442 line1
= ' '*2 + name
+ member
+ ' ' + subscriber
443 line2
= ' '*4 + description
445 line4
= ' '*4 + clock