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()
36 Print a string with ordered color with each character
38 c
= [colors_shuffle
[i
% 7](s
[i
]) for i
in xrange(len(s
))]
39 return reduce(lambda x
, y
: x
+ y
, c
)
42 def random_rainbow(s
):
44 Print a string with random color with each character
46 c
= [random
.choice(colors_shuffle
)(i
) for i
in s
]
47 return reduce(lambda x
, y
: x
+ y
, c
)
59 cache
[args
] = func(*args
)
67 Cycle the colors_shuffle
69 return next(g
['cyc'])(s
)
76 fi
= figlet_format(text
, font
='doom')
78 [next(g
['cyc'])(i
) for i
in fi
.split('\n')]
84 Check current theme and update if necessary
86 exists
= db
.theme_query()
87 themes
= [t
.theme_name
for t
in exists
]
88 if c
['theme'] != themes
[0]:
89 c
['theme'] = themes
[0]
91 if c
['theme'] == 'custom':
92 config
= os
.environ
.get(
94 os
.environ
.get('USERPROFILE',
95 '')) + os
.sep
+ '.rainbow_config.json'
97 config
= 'rainbowstream/colorset/'+c
['theme']+'.json'
99 data
= load_config(config
)
103 # Re-init color cycle
104 g
['cyc'] = init_cycle()
107 def color_func(func_name
):
109 Call color function base on name
111 pure
= func_name
.encode('utf8')
112 if pure
.startswith('term_') and pure
[5:].isdigit():
113 return term_color(int(pure
[5:]))
114 return globals()[pure
]
117 def draw(t
, iot
=False, keyword
=None, fil
=[], ig
=[]):
126 screen_name
= t
['user']['screen_name']
127 name
= t
['user']['name']
128 created_at
= t
['created_at']
129 favorited
= t
['favorited']
130 date
= parser
.parse(created_at
)
131 date
= date
- datetime
.timedelta(seconds
=time
.timezone
)
132 clock
= date
.strftime('%Y/%m/%d %H:%M:%S')
138 urls
= t
['entities']['urls']
140 expanded_url
.append(u
['expanded_url'])
149 media
= t
['entities']['media']
151 media_url
.append(m
['media_url'])
156 screen_name
= '@' + screen_name
157 if fil
and screen_name
not in fil
:
159 if ig
and screen_name
in ig
:
163 res
= db
.tweet_to_rainbow_query(tid
)
166 res
= db
.tweet_to_rainbow_query(tid
)
167 rid
= res
[0].rainbow_id
171 name
) + color_func(c
['TWEET']['nick'])(' ' + screen_name
+ ' ')
172 meta
= color_func(c
['TWEET']['clock'])(
173 '[' + clock
+ '] ') + color_func(c
['TWEET']['id'])('[id=' + str(rid
) + '] ')
175 meta
= meta
+ color_func(c
['TWEET']['favorite'])(u
'\u2605')
179 for index
in range(len(expanded_url
)):
181 lambda x
: expanded_url
[index
] if x
== url
[index
] else x
,
185 lambda x
: color_func(
186 c
['TWEET']['rt'])(x
) if x
== 'RT' else x
,
188 # Highlight screen_name
189 tweet
= map(lambda x
: cycle_color(x
) if x
[0] == '@' else x
, tweet
)
192 lambda x
: color_func(
193 c
['TWEET']['link'])(x
) if x
[
194 0:4] == 'http' else x
,
196 # Highlight search keyword
199 lambda x
: color_func(c
['TWEET']['keyword'])(x
) if
200 ''.join(c
for c
in x
if c
.isalnum()).lower() == keyword
.lower()
205 tweet
= ' '.join(tweet
)
208 line1
= u
"{u:>{uw}}:".format(
212 line2
= u
"{c:>{cw}}".format(
224 if iot
and media_url
:
226 response
= requests
.get(mu
)
227 image_to_display(StringIO(response
.content
))
230 def print_message(m
):
234 sender_screen_name
= '@' + m
['sender_screen_name']
235 sender_name
= m
['sender']['name']
237 recipient_screen_name
= '@' + m
['recipient_screen_name']
238 recipient_name
= m
['recipient']['name']
240 date
= parser
.parse(m
['created_at'])
241 date
= date
- datetime
.timedelta(seconds
=time
.timezone
)
242 clock
= date
.strftime('%Y/%m/%d %H:%M:%S')
245 res
= db
.message_to_rainbow_query(mid
)
247 db
.message_store(mid
)
248 res
= db
.message_to_rainbow_query(mid
)
249 rid
= res
[0].rainbow_id
252 sender
= cycle_color(
253 sender_name
) + color_func(c
['MESSAGE']['sender'])(' ' + sender_screen_name
+ ' ')
254 recipient
= cycle_color(recipient_name
) + color_func(
255 c
['MESSAGE']['recipient'])(
256 ' ' + recipient_screen_name
+ ' ')
257 user
= sender
+ color_func(c
['MESSAGE']['to'])(' >>> ') + recipient
259 c
['MESSAGE']['clock'])(
260 '[' + clock
+ ']') + color_func(
262 ' [message_id=' + str(rid
) + '] ')
263 text
= ''.join(map(lambda x
: x
+ ' ' if x
== '\n' else x
, text
))
265 line1
= u
"{u:>{uw}}:".format(
269 line2
= u
"{c:>{cw}}".format(
282 def show_profile(u
, iot
=False):
288 screen_name
= u
['screen_name']
289 description
= u
['description']
290 profile_image_url
= u
['profile_image_url']
291 location
= u
['location']
293 created_at
= u
['created_at']
294 statuses_count
= u
['statuses_count']
295 friends_count
= u
['friends_count']
296 followers_count
= u
['followers_count']
299 statuses_count
= color_func(
300 c
['PROFILE']['statuses_count'])(
301 str(statuses_count
) +
303 friends_count
= color_func(
304 c
['PROFILE']['friends_count'])(
307 followers_count
= color_func(
308 c
['PROFILE']['followers_count'])(
309 str(followers_count
) +
311 count
= statuses_count
+ ' ' + friends_count
+ ' ' + followers_count
313 name
) + color_func(c
['PROFILE']['nick'])(' @' + screen_name
+ ' : ') + count
314 profile_image_raw_url
= 'Profile photo: ' + \
315 color_func(c
['PROFILE']['profile_image_url'])(profile_image_url
)
316 description
= ''.join(
317 map(lambda x
: x
+ ' ' * 4 if x
== '\n' else x
, description
))
318 description
= color_func(c
['PROFILE']['description'])(description
)
319 location
= 'Location : ' + color_func(c
['PROFILE']['location'])(location
)
320 url
= 'URL : ' + (color_func(c
['PROFILE']['url'])(url
) if url
else '')
321 date
= parser
.parse(created_at
)
322 date
= date
- datetime
.timedelta(seconds
=time
.timezone
)
323 clock
= date
.strftime('%Y/%m/%d %H:%M:%S')
324 clock
= 'Join at ' + color_func(c
['PROFILE']['clock'])(clock
)
327 line1
= u
"{u:>{uw}}".format(
331 line2
= u
"{p:>{pw}}".format(
332 p
=profile_image_raw_url
,
333 pw
=len(profile_image_raw_url
) + 4,
335 line3
= u
"{d:>{dw}}".format(
337 dw
=len(description
) + 4,
339 line4
= u
"{l:>{lw}}".format(
341 lw
=len(location
) + 4,
343 line5
= u
"{u:>{uw}}".format(
347 line6
= u
"{c:>{cw}}".format(
356 response
= requests
.get(profile_image_url
)
357 image_to_display(StringIO(response
.content
), 2, 20)
360 for line
in [line3
, line4
, line5
, line6
]:
365 def print_trends(trends
):
369 for topic
in trends
[:c
['TREND_MAX']]:
372 line
= cycle_color(name
) + ': ' + color_func(c
['TREND']['url'])(url
)