7 from twitter
.util
import printNicely
8 from functools
import wraps
9 from pyfiglet
import figlet_format
10 from dateutil
import parser
11 from .c_image
import *
15 from .py3patch
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 ''.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 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 ''.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
)
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 if func_name
.startswith('term_') and func_name
[5:].isdigit():
145 return term_color(int(func_name
[5:]))
146 return globals()[func_name
]
149 def draw(t
, iot
=False, keyword
=None, fil
=[], ig
=[]):
158 screen_name
= t
['user']['screen_name']
159 name
= t
['user']['name']
160 created_at
= t
['created_at']
161 favorited
= t
['favorited']
162 date
= parser
.parse(created_at
)
163 date
= date
- datetime
.timedelta(seconds
=time
.timezone
)
164 clock
= date
.strftime('%Y/%m/%d %H:%M:%S')
170 urls
= t
['entities']['urls']
172 expanded_url
.append(u
['expanded_url'])
181 media
= t
['entities']['media']
183 media_url
.append(m
['media_url'])
188 screen_name
= '@' + screen_name
189 if fil
and screen_name
not in fil
:
191 if ig
and screen_name
in ig
:
195 res
= db
.tweet_to_rainbow_query(tid
)
198 res
= db
.tweet_to_rainbow_query(tid
)
199 rid
= res
[0].rainbow_id
203 name
) + color_func(c
['TWEET']['nick'])(' ' + screen_name
+ ' ')
204 meta
= color_func(c
['TWEET']['clock'])(
205 '[' + clock
+ '] ') + color_func(c
['TWEET']['id'])('[id=' + str(rid
) + '] ')
207 meta
= meta
+ color_func(c
['TWEET']['favorited'])(u
'\u2605')
211 for index
in range(len(expanded_url
)):
213 lambda x
: expanded_url
[index
] if x
== url
[index
] else x
,
217 lambda x
: color_func(
218 c
['TWEET']['rt'])(x
) if x
== 'RT' else x
,
220 # Highlight screen_name
221 tweet
= lmap(lambda x
: cycle_color(x
) if x
[0] == '@' else x
, tweet
)
224 lambda x
: color_func(
225 c
['TWEET']['link'])(x
) if x
[
226 0:4] == 'http' else x
,
228 # Highlight search keyword
231 lambda x
: color_func(c
['TWEET']['keyword'])(x
) if
232 ''.join(c
for c
in x
if c
.isalnum()).lower() == keyword
.lower()
237 tweet
= ' '.join(tweet
)
240 line1
= u
"{u:>{uw}}:".format(
244 line2
= u
"{c:>{cw}}".format(
256 if iot
and media_url
:
259 response
= requests
.get(mu
)
260 image_to_display(BytesIO(response
.content
))
262 printNicely(red('Sorry, image link is broken'))
265 def print_message(m
):
269 sender_screen_name
= '@' + m
['sender_screen_name']
270 sender_name
= m
['sender']['name']
272 recipient_screen_name
= '@' + m
['recipient_screen_name']
273 recipient_name
= m
['recipient']['name']
275 date
= parser
.parse(m
['created_at'])
276 date
= date
- datetime
.timedelta(seconds
=time
.timezone
)
277 clock
= date
.strftime('%Y/%m/%d %H:%M:%S')
280 res
= db
.message_to_rainbow_query(mid
)
282 db
.message_store(mid
)
283 res
= db
.message_to_rainbow_query(mid
)
284 rid
= res
[0].rainbow_id
287 sender
= cycle_color(
288 sender_name
) + color_func(c
['MESSAGE']['sender'])(' ' + sender_screen_name
+ ' ')
289 recipient
= cycle_color(recipient_name
) + color_func(
290 c
['MESSAGE']['recipient'])(
291 ' ' + recipient_screen_name
+ ' ')
292 user
= sender
+ color_func(c
['MESSAGE']['to'])(' >>> ') + recipient
294 c
['MESSAGE']['clock'])(
295 '[' + clock
+ ']') + color_func(
297 ' [message_id=' + str(rid
) + '] ')
298 text
= ''.join(lmap(lambda x
: x
+ ' ' if x
== '\n' else x
, text
))
300 line1
= u
"{u:>{uw}}:".format(
304 line2
= u
"{c:>{cw}}".format(
317 def show_profile(u
, iot
=False):
323 screen_name
= u
['screen_name']
324 description
= u
['description']
325 profile_image_url
= u
['profile_image_url']
326 location
= u
['location']
328 created_at
= u
['created_at']
329 statuses_count
= u
['statuses_count']
330 friends_count
= u
['friends_count']
331 followers_count
= u
['followers_count']
334 statuses_count
= color_func(
335 c
['PROFILE']['statuses_count'])(
336 str(statuses_count
) +
338 friends_count
= color_func(
339 c
['PROFILE']['friends_count'])(
342 followers_count
= color_func(
343 c
['PROFILE']['followers_count'])(
344 str(followers_count
) +
346 count
= statuses_count
+ ' ' + friends_count
+ ' ' + followers_count
348 name
) + color_func(c
['PROFILE']['nick'])(' @' + screen_name
+ ' : ') + count
349 profile_image_raw_url
= 'Profile photo: ' + \
350 color_func(c
['PROFILE']['profile_image_url'])(profile_image_url
)
351 description
= ''.join(
352 lmap(lambda x
: x
+ ' ' * 4 if x
== '\n' else x
, description
))
353 description
= color_func(c
['PROFILE']['description'])(description
)
354 location
= 'Location : ' + color_func(c
['PROFILE']['location'])(location
)
355 url
= 'URL : ' + (color_func(c
['PROFILE']['url'])(url
) if url
else '')
356 date
= parser
.parse(created_at
)
357 date
= date
- datetime
.timedelta(seconds
=time
.timezone
)
358 clock
= date
.strftime('%Y/%m/%d %H:%M:%S')
359 clock
= 'Join at ' + color_func(c
['PROFILE']['clock'])(clock
)
362 line1
= u
"{u:>{uw}}".format(
366 line2
= u
"{p:>{pw}}".format(
367 p
=profile_image_raw_url
,
368 pw
=len(profile_image_raw_url
) + 4,
370 line3
= u
"{d:>{dw}}".format(
372 dw
=len(description
) + 4,
374 line4
= u
"{l:>{lw}}".format(
376 lw
=len(location
) + 4,
378 line5
= u
"{u:>{uw}}".format(
382 line6
= u
"{c:>{cw}}".format(
392 response
= requests
.get(profile_image_url
)
393 image_to_display(BytesIO(response
.content
), 2, 20)
398 for line
in [line3
, line4
, line5
, line6
]:
403 def print_trends(trends
):
407 for topic
in trends
[:c
['TREND_MAX']]:
410 line
= cycle_color(name
) + ': ' + color_func(c
['TREND']['url'])(url
)
415 def print_list(group
):
422 name
= color_func(c
['GROUP']['name'])(name
+ ' : ')
423 member
= str(g
['member_count'])
424 member
= color_func(c
['GROUP']['member'])(member
+' member')
425 subscriber
= str(g
['subscriber_count'])
426 subscriber
= color_func(c
['GROUP']['subscriber'])(subscriber
+' subscriber')
427 description
= g
['description'].strip()
428 description
= color_func(c
['GROUP']['description'])(description
)
430 mode
= color_func(c
['GROUP']['mode'])('Type: '+mode
)
431 created_at
= g
['created_at']
432 date
= parser
.parse(created_at
)
433 date
= date
- datetime
.timedelta(seconds
=time
.timezone
)
434 clock
= date
.strftime('%Y/%m/%d %H:%M:%S')
435 clock
= 'Created at ' + color_func(c
['GROUP']['clock'])(clock
)
438 db
.list_store(g
['id'],g
['name'])
441 line1
= ' '*2 + name
+ member
+ ' ' + subscriber
442 line2
= ' '*4 + description
444 line4
= ' '*4 + clock