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 *
26 colors_shuffle
= [globals()[i
.encode('utf8')]
27 if not i
.startswith('term_')
28 else term_color(int(i
[5:]))
29 for i
in c
['CYCLE_COLOR']]
30 return itertools
.cycle(colors_shuffle
)
31 g
['cyc'] = init_cycle()
38 g
['cyc'] = init_cycle()
43 Print a string with ordered color with each character
45 c
= [colors_shuffle
[i
% 7](s
[i
]) for i
in xrange(len(s
))]
46 return reduce(lambda x
, y
: x
+ y
, c
)
49 def random_rainbow(s
):
51 Print a string with random color with each character
53 c
= [random
.choice(colors_shuffle
)(i
) for i
in s
]
54 return reduce(lambda x
, y
: x
+ y
, c
)
66 cache
[args
] = func(*args
)
74 Cycle the colors_shuffle
76 return next(g
['cyc'])(s
)
83 fi
= figlet_format(text
, font
='doom')
85 [next(g
['cyc'])(i
) for i
in fi
.split('\n')]
91 Check current theme and update if necessary
93 exists
= db
.theme_query()
94 themes
= [t
.theme_name
for t
in exists
]
95 if c
['theme'] != themes
[0]:
96 c
['theme'] = themes
[0]
98 if c
['theme'] == 'custom':
99 config
= os
.environ
.get(
101 os
.environ
.get('USERPROFILE',
102 '')) + os
.sep
+ '.rainbow_config.json'
104 config
= 'rainbowstream/colorset/'+c
['theme']+'.json'
106 data
= load_config(config
)
110 # Re-init color cycle
111 g
['cyc'] = init_cycle()
114 def color_func(func_name
):
116 Call color function base on name
118 pure
= func_name
.encode('utf8')
119 if pure
.startswith('term_') and pure
[5:].isdigit():
120 return term_color(int(pure
[5:]))
121 return globals()[pure
]
124 def draw(t
, iot
=False, keyword
=None, fil
=[], ig
=[]):
133 screen_name
= t
['user']['screen_name']
134 name
= t
['user']['name']
135 created_at
= t
['created_at']
136 favorited
= t
['favorited']
137 date
= parser
.parse(created_at
)
138 date
= date
- datetime
.timedelta(seconds
=time
.timezone
)
139 clock
= date
.strftime('%Y/%m/%d %H:%M:%S')
145 urls
= t
['entities']['urls']
147 expanded_url
.append(u
['expanded_url'])
156 media
= t
['entities']['media']
158 media_url
.append(m
['media_url'])
163 screen_name
= '@' + screen_name
164 if fil
and screen_name
not in fil
:
166 if ig
and screen_name
in ig
:
170 res
= db
.tweet_to_rainbow_query(tid
)
173 res
= db
.tweet_to_rainbow_query(tid
)
174 rid
= res
[0].rainbow_id
178 name
) + color_func(c
['TWEET']['nick'])(' ' + screen_name
+ ' ')
179 meta
= color_func(c
['TWEET']['clock'])(
180 '[' + clock
+ '] ') + color_func(c
['TWEET']['id'])('[id=' + str(rid
) + '] ')
182 meta
= meta
+ color_func(c
['TWEET']['favorite'])(u
'\u2605')
186 for index
in range(len(expanded_url
)):
188 lambda x
: expanded_url
[index
] if x
== url
[index
] else x
,
192 lambda x
: color_func(
193 c
['TWEET']['rt'])(x
) if x
== 'RT' else x
,
195 # Highlight screen_name
196 tweet
= map(lambda x
: cycle_color(x
) if x
[0] == '@' else x
, tweet
)
199 lambda x
: color_func(
200 c
['TWEET']['link'])(x
) if x
[
201 0:4] == 'http' else x
,
203 # Highlight search keyword
206 lambda x
: color_func(c
['TWEET']['keyword'])(x
) if
207 ''.join(c
for c
in x
if c
.isalnum()).lower() == keyword
.lower()
212 tweet
= ' '.join(tweet
)
215 line1
= u
"{u:>{uw}}:".format(
219 line2
= u
"{c:>{cw}}".format(
231 if iot
and media_url
:
233 response
= requests
.get(mu
)
234 image_to_display(StringIO(response
.content
))
237 def print_message(m
):
241 sender_screen_name
= '@' + m
['sender_screen_name']
242 sender_name
= m
['sender']['name']
244 recipient_screen_name
= '@' + m
['recipient_screen_name']
245 recipient_name
= m
['recipient']['name']
247 date
= parser
.parse(m
['created_at'])
248 date
= date
- datetime
.timedelta(seconds
=time
.timezone
)
249 clock
= date
.strftime('%Y/%m/%d %H:%M:%S')
252 res
= db
.message_to_rainbow_query(mid
)
254 db
.message_store(mid
)
255 res
= db
.message_to_rainbow_query(mid
)
256 rid
= res
[0].rainbow_id
259 sender
= cycle_color(
260 sender_name
) + color_func(c
['MESSAGE']['sender'])(' ' + sender_screen_name
+ ' ')
261 recipient
= cycle_color(recipient_name
) + color_func(
262 c
['MESSAGE']['recipient'])(
263 ' ' + recipient_screen_name
+ ' ')
264 user
= sender
+ color_func(c
['MESSAGE']['to'])(' >>> ') + recipient
266 c
['MESSAGE']['clock'])(
267 '[' + clock
+ ']') + color_func(
269 ' [message_id=' + str(rid
) + '] ')
270 text
= ''.join(map(lambda x
: x
+ ' ' if x
== '\n' else x
, text
))
272 line1
= u
"{u:>{uw}}:".format(
276 line2
= u
"{c:>{cw}}".format(
289 def show_profile(u
, iot
=False):
295 screen_name
= u
['screen_name']
296 description
= u
['description']
297 profile_image_url
= u
['profile_image_url']
298 location
= u
['location']
300 created_at
= u
['created_at']
301 statuses_count
= u
['statuses_count']
302 friends_count
= u
['friends_count']
303 followers_count
= u
['followers_count']
306 statuses_count
= color_func(
307 c
['PROFILE']['statuses_count'])(
308 str(statuses_count
) +
310 friends_count
= color_func(
311 c
['PROFILE']['friends_count'])(
314 followers_count
= color_func(
315 c
['PROFILE']['followers_count'])(
316 str(followers_count
) +
318 count
= statuses_count
+ ' ' + friends_count
+ ' ' + followers_count
320 name
) + color_func(c
['PROFILE']['nick'])(' @' + screen_name
+ ' : ') + count
321 profile_image_raw_url
= 'Profile photo: ' + \
322 color_func(c
['PROFILE']['profile_image_url'])(profile_image_url
)
323 description
= ''.join(
324 map(lambda x
: x
+ ' ' * 4 if x
== '\n' else x
, description
))
325 description
= color_func(c
['PROFILE']['description'])(description
)
326 location
= 'Location : ' + color_func(c
['PROFILE']['location'])(location
)
327 url
= 'URL : ' + (color_func(c
['PROFILE']['url'])(url
) if url
else '')
328 date
= parser
.parse(created_at
)
329 date
= date
- datetime
.timedelta(seconds
=time
.timezone
)
330 clock
= date
.strftime('%Y/%m/%d %H:%M:%S')
331 clock
= 'Join at ' + color_func(c
['PROFILE']['clock'])(clock
)
334 line1
= u
"{u:>{uw}}".format(
338 line2
= u
"{p:>{pw}}".format(
339 p
=profile_image_raw_url
,
340 pw
=len(profile_image_raw_url
) + 4,
342 line3
= u
"{d:>{dw}}".format(
344 dw
=len(description
) + 4,
346 line4
= u
"{l:>{lw}}".format(
348 lw
=len(location
) + 4,
350 line5
= u
"{u:>{uw}}".format(
354 line6
= u
"{c:>{cw}}".format(
363 response
= requests
.get(profile_image_url
)
364 image_to_display(StringIO(response
.content
), 2, 20)
367 for line
in [line3
, line4
, line5
, line6
]:
372 def print_trends(trends
):
376 for topic
in trends
[:c
['TREND_MAX']]:
379 line
= cycle_color(name
) + ': ' + color_func(c
['TREND']['url'])(url
)