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
)
43 def color_func(func_name
):
45 Call color function base on name
47 pure
= func_name
.encode('utf8')
48 if pure
.startswith('RGB_') and pure
[4:].isdigit():
49 return RGB(int(pure
[4:]))
50 return globals()[pure
]
53 def draw(t
, iot
=False, keyword
=None, fil
=[], ig
=[]):
62 screen_name
= t
['user']['screen_name']
63 name
= t
['user']['name']
64 created_at
= t
['created_at']
65 favorited
= t
['favorited']
66 date
= parser
.parse(created_at
)
67 date
= date
- datetime
.timedelta(seconds
=time
.timezone
)
68 clock
= date
.strftime('%Y/%m/%d %H:%M:%S')
74 urls
= t
['entities']['urls']
76 expanded_url
.append(u
['expanded_url'])
85 media
= t
['entities']['media']
87 media_url
.append(m
['media_url'])
92 screen_name
= '@' + screen_name
93 if fil
and screen_name
not in fil
:
95 if ig
and screen_name
in ig
:
99 res
= db
.tweet_to_rainbow_query(tid
)
102 res
= db
.tweet_to_rainbow_query(tid
)
103 rid
= res
[0].rainbow_id
107 name
) + color_func(c
['TWEET']['nick'])(' ' + screen_name
+ ' ')
108 meta
= color_func(c
['TWEET']['clock'])(
109 '[' + clock
+ '] ') + color_func(c
['TWEET']['id'])('[id=' + str(rid
) + '] ')
111 meta
= meta
+ color_func(c
['TWEET']['favorite'])(u
'\u2605')
115 for index
in range(len(expanded_url
)):
117 lambda x
: expanded_url
[index
] if x
== url
[index
] else x
,
121 lambda x
: color_func(
122 c
['TWEET']['rt'])(x
) if x
== 'RT' else x
,
124 # Highlight screen_name
125 tweet
= map(lambda x
: cycle_color(x
) if x
[0] == '@' else x
, tweet
)
128 lambda x
: color_func(
129 c
['TWEET']['link'])(x
) if x
[
130 0:4] == 'http' else x
,
132 # Highlight search keyword
135 lambda x
: color_func(c
['TWEET']['keyword'])(x
) if
136 ''.join(c
for c
in x
if c
.isalnum()).lower() == keyword
.lower()
141 tweet
= ' '.join(tweet
)
144 line1
= u
"{u:>{uw}}:".format(
148 line2
= u
"{c:>{cw}}".format(
160 if iot
and media_url
:
162 response
= requests
.get(mu
)
163 image_to_display(StringIO(response
.content
))
166 def print_message(m
):
170 sender_screen_name
= '@' + m
['sender_screen_name']
171 sender_name
= m
['sender']['name']
173 recipient_screen_name
= '@' + m
['recipient_screen_name']
174 recipient_name
= m
['recipient']['name']
176 date
= parser
.parse(m
['created_at'])
177 date
= date
- datetime
.timedelta(seconds
=time
.timezone
)
178 clock
= date
.strftime('%Y/%m/%d %H:%M:%S')
181 res
= db
.message_to_rainbow_query(mid
)
183 db
.message_store(mid
)
184 res
= db
.message_to_rainbow_query(mid
)
185 rid
= res
[0].rainbow_id
188 sender
= cycle_color(
189 sender_name
) + color_func(c
['MESSAGE']['sender'])(' ' + sender_screen_name
+ ' ')
190 recipient
= cycle_color(recipient_name
) + color_func(
191 c
['MESSAGE']['recipient'])(
192 ' ' + recipient_screen_name
+ ' ')
193 user
= sender
+ color_func(c
['MESSAGE']['to'])(' >>> ') + recipient
195 c
['MESSAGE']['clock'])(
196 '[' + clock
+ ']') + color_func(
198 ' [message_id=' + str(rid
) + '] ')
199 text
= ''.join(map(lambda x
: x
+ ' ' if x
== '\n' else x
, text
))
201 line1
= u
"{u:>{uw}}:".format(
205 line2
= u
"{c:>{cw}}".format(
218 def show_profile(u
, iot
=False):
224 screen_name
= u
['screen_name']
225 description
= u
['description']
226 profile_image_url
= u
['profile_image_url']
227 location
= u
['location']
229 created_at
= u
['created_at']
230 statuses_count
= u
['statuses_count']
231 friends_count
= u
['friends_count']
232 followers_count
= u
['followers_count']
235 statuses_count
= color_func(
236 c
['PROFILE']['statuses_count'])(
237 str(statuses_count
) +
239 friends_count
= color_func(
240 c
['PROFILE']['friends_count'])(
243 followers_count
= color_func(
244 c
['PROFILE']['followers_count'])(
245 str(followers_count
) +
247 count
= statuses_count
+ ' ' + friends_count
+ ' ' + followers_count
249 name
) + color_func(c
['PROFILE']['nick'])(' @' + screen_name
+ ' : ') + count
250 profile_image_raw_url
= 'Profile photo: ' + \
251 color_func(c
['PROFILE']['profile_image_url'])(profile_image_url
)
252 description
= ''.join(
253 map(lambda x
: x
+ ' ' * 4 if x
== '\n' else x
, description
))
254 description
= color_func(c
['PROFILE']['description'])(description
)
255 location
= 'Location : ' + color_func(c
['PROFILE']['location'])(location
)
256 url
= 'URL : ' + (color_func(c
['PROFILE']['url'])(url
) if url
else '')
257 date
= parser
.parse(created_at
)
258 date
= date
- datetime
.timedelta(seconds
=time
.timezone
)
259 clock
= date
.strftime('%Y/%m/%d %H:%M:%S')
260 clock
= 'Join at ' + color_func(c
['PROFILE']['clock'])(clock
)
263 line1
= u
"{u:>{uw}}".format(
267 line2
= u
"{p:>{pw}}".format(
268 p
=profile_image_raw_url
,
269 pw
=len(profile_image_raw_url
) + 4,
271 line3
= u
"{d:>{dw}}".format(
273 dw
=len(description
) + 4,
275 line4
= u
"{l:>{lw}}".format(
277 lw
=len(location
) + 4,
279 line5
= u
"{u:>{uw}}".format(
283 line6
= u
"{c:>{cw}}".format(
292 response
= requests
.get(profile_image_url
)
293 image_to_display(StringIO(response
.content
), 2, 20)
296 for line
in [line3
, line4
, line5
, line6
]:
301 def print_trends(trends
):
305 for topic
in trends
[:c
['TREND_MAX']]:
308 line
= cycle_color(name
) + ': ' + color_func(TREND
['url'])(url
)