8 from twitter
.util
import printNicely
9 from StringIO
import StringIO
10 from dateutil
import parser
11 from .c_image
import *
21 Check current theme and update if necessary
23 exists
= db
.theme_query()
24 themes
= [t
.theme_name
for t
in exists
]
25 if cur_theme
!= themes
[0]:
28 if cur_theme
== 'user':
29 config
= os
.environ
.get(
33 '')) + os
.sep
+ '.rainbow_config.json'
35 config
= 'rainbowstream/colorset/'+cur_theme
+'.json'
37 data
= load_config(config
)
42 def color_func(func_name
):
44 Call color function base on name
46 pure
= func_name
.encode('utf8')
47 if pure
.startswith('RGB_') and pure
[4:].isdigit():
48 return RGB(int(pure
[4:]))
49 return globals()[pure
]
52 def draw(t
, iot
=False, keyword
=None, fil
=[], ig
=[]):
61 screen_name
= t
['user']['screen_name']
62 name
= t
['user']['name']
63 created_at
= t
['created_at']
64 favorited
= t
['favorited']
65 date
= parser
.parse(created_at
)
66 date
= date
- datetime
.timedelta(seconds
=time
.timezone
)
67 clock
= date
.strftime('%Y/%m/%d %H:%M:%S')
73 urls
= t
['entities']['urls']
75 expanded_url
.append(u
['expanded_url'])
84 media
= t
['entities']['media']
86 media_url
.append(m
['media_url'])
91 screen_name
= '@' + screen_name
92 if fil
and screen_name
not in fil
:
94 if ig
and screen_name
in ig
:
98 res
= db
.tweet_to_rainbow_query(tid
)
101 res
= db
.tweet_to_rainbow_query(tid
)
102 rid
= res
[0].rainbow_id
106 name
) + color_func(c
['TWEET']['nick'])(' ' + screen_name
+ ' ')
107 meta
= color_func(c
['TWEET']['clock'])(
108 '[' + clock
+ '] ') + color_func(c
['TWEET']['id'])('[id=' + str(rid
) + '] ')
110 meta
= meta
+ color_func(c
['TWEET']['favorite'])(u
'\u2605')
114 for index
in range(len(expanded_url
)):
116 lambda x
: expanded_url
[index
] if x
== url
[index
] else x
,
120 lambda x
: color_func(
121 c
['TWEET']['rt'])(x
) if x
== 'RT' else x
,
123 # Highlight screen_name
124 tweet
= map(lambda x
: cycle_color(x
) if x
[0] == '@' else x
, tweet
)
127 lambda x
: color_func(
128 c
['TWEET']['link'])(x
) if x
[
129 0:4] == 'http' else x
,
131 # Highlight search keyword
134 lambda x
: color_func(c
['TWEET']['keyword'])(x
) if
135 ''.join(c
for c
in x
if c
.isalnum()).lower() == keyword
.lower()
140 tweet
= ' '.join(tweet
)
143 line1
= u
"{u:>{uw}}:".format(
147 line2
= u
"{c:>{cw}}".format(
159 if iot
and media_url
:
161 response
= requests
.get(mu
)
162 image_to_display(StringIO(response
.content
))
165 def print_message(m
):
169 sender_screen_name
= '@' + m
['sender_screen_name']
170 sender_name
= m
['sender']['name']
172 recipient_screen_name
= '@' + m
['recipient_screen_name']
173 recipient_name
= m
['recipient']['name']
175 date
= parser
.parse(m
['created_at'])
176 date
= date
- datetime
.timedelta(seconds
=time
.timezone
)
177 clock
= date
.strftime('%Y/%m/%d %H:%M:%S')
180 res
= db
.message_to_rainbow_query(mid
)
182 db
.message_store(mid
)
183 res
= db
.message_to_rainbow_query(mid
)
184 rid
= res
[0].rainbow_id
187 sender
= cycle_color(
188 sender_name
) + color_func(c
['MESSAGE']['sender'])(' ' + sender_screen_name
+ ' ')
189 recipient
= cycle_color(recipient_name
) + color_func(
190 c
['MESSAGE']['recipient'])(
191 ' ' + recipient_screen_name
+ ' ')
192 user
= sender
+ color_func(c
['MESSAGE']['to'])(' >>> ') + recipient
194 c
['MESSAGE']['clock'])(
195 '[' + clock
+ ']') + color_func(
197 ' [message_id=' + str(rid
) + '] ')
198 text
= ''.join(map(lambda x
: x
+ ' ' if x
== '\n' else x
, text
))
200 line1
= u
"{u:>{uw}}:".format(
204 line2
= u
"{c:>{cw}}".format(
217 def show_profile(u
, iot
=False):
223 screen_name
= u
['screen_name']
224 description
= u
['description']
225 profile_image_url
= u
['profile_image_url']
226 location
= u
['location']
228 created_at
= u
['created_at']
229 statuses_count
= u
['statuses_count']
230 friends_count
= u
['friends_count']
231 followers_count
= u
['followers_count']
234 statuses_count
= color_func(
235 c
['PROFILE']['statuses_count'])(
236 str(statuses_count
) +
238 friends_count
= color_func(
239 c
['PROFILE']['friends_count'])(
242 followers_count
= color_func(
243 c
['PROFILE']['followers_count'])(
244 str(followers_count
) +
246 count
= statuses_count
+ ' ' + friends_count
+ ' ' + followers_count
248 name
) + color_func(c
['PROFILE']['nick'])(' @' + screen_name
+ ' : ') + count
249 profile_image_raw_url
= 'Profile photo: ' + \
250 color_func(c
['PROFILE']['profile_image_url'])(profile_image_url
)
251 description
= ''.join(
252 map(lambda x
: x
+ ' ' * 4 if x
== '\n' else x
, description
))
253 description
= color_func(c
['PROFILE']['description'])(description
)
254 location
= 'Location : ' + color_func(c
['PROFILE']['location'])(location
)
255 url
= 'URL : ' + (color_func(c
['PROFILE']['url'])(url
) if url
else '')
256 date
= parser
.parse(created_at
)
257 date
= date
- datetime
.timedelta(seconds
=time
.timezone
)
258 clock
= date
.strftime('%Y/%m/%d %H:%M:%S')
259 clock
= 'Join at ' + color_func(c
['PROFILE']['clock'])(clock
)
262 line1
= u
"{u:>{uw}}".format(
266 line2
= u
"{p:>{pw}}".format(
267 p
=profile_image_raw_url
,
268 pw
=len(profile_image_raw_url
) + 4,
270 line3
= u
"{d:>{dw}}".format(
272 dw
=len(description
) + 4,
274 line4
= u
"{l:>{lw}}".format(
276 lw
=len(location
) + 4,
278 line5
= u
"{u:>{uw}}".format(
282 line6
= u
"{c:>{cw}}".format(
291 response
= requests
.get(profile_image_url
)
292 image_to_display(StringIO(response
.content
), 2, 20)
295 for line
in [line3
, line4
, line5
, line6
]:
300 def print_trends(trends
):
304 for topic
in trends
[:c
['TREND_MAX']]:
307 line
= cycle_color(name
) + ': ' + color_func(TREND
['url'])(url
)