8 from twitter
.util
import printNicely
9 from StringIO
import StringIO
10 from dateutil
import parser
11 from .c_image
import *
19 def color_func(func_name
):
21 Call color function base on name
23 pure
= func_name
.encode('utf8')
24 if pure
.startswith('RGB_') and pure
[4:].isdigit():
25 return RGB(int(pure
[4:]))
26 return globals()[pure
]
29 def draw(t
, iot
=False, keyword
=None, fil
=[], ig
=[]):
37 screen_name
= t
['user']['screen_name']
38 name
= t
['user']['name']
39 created_at
= t
['created_at']
40 favorited
= t
['favorited']
41 date
= parser
.parse(created_at
)
42 date
= date
- datetime
.timedelta(seconds
=time
.timezone
)
43 clock
= date
.strftime('%Y/%m/%d %H:%M:%S')
49 urls
= t
['entities']['urls']
51 expanded_url
.append(u
['expanded_url'])
60 media
= t
['entities']['media']
62 media_url
.append(m
['media_url'])
67 screen_name
= '@' + screen_name
68 if fil
and screen_name
not in fil
:
70 if ig
and screen_name
in ig
:
74 res
= db
.tweet_to_rainbow_query(tid
)
77 res
= db
.tweet_to_rainbow_query(tid
)
78 rid
= res
[0].rainbow_id
82 name
) + color_func(c
['TWEET']['nick'])(' ' + screen_name
+ ' ')
83 meta
= color_func(c
['TWEET']['clock'])(
84 '[' + clock
+ '] ') + color_func(c
['TWEET']['id'])('[id=' + str(rid
) + '] ')
86 meta
= meta
+ color_func(c
['TWEET']['favorite'])(u
'\u2605')
90 for index
in range(len(expanded_url
)):
92 lambda x
: expanded_url
[index
] if x
== url
[index
] else x
,
97 c
['TWEET']['rt'])(x
) if x
== 'RT' else x
,
99 # Highlight screen_name
100 tweet
= map(lambda x
: cycle_color(x
) if x
[0] == '@' else x
, tweet
)
103 lambda x
: color_func(
104 c
['TWEET']['link'])(x
) if x
[
105 0:4] == 'http' else x
,
107 # Highlight search keyword
110 lambda x
: color_func(c
['TWEET']['keyword'])(x
) if
111 ''.join(c
for c
in x
if c
.isalnum()).lower() == keyword
.lower()
116 tweet
= ' '.join(tweet
)
119 line1
= u
"{u:>{uw}}:".format(
123 line2
= u
"{c:>{cw}}".format(
135 if iot
and media_url
:
137 response
= requests
.get(mu
)
138 image_to_display(StringIO(response
.content
))
141 def print_message(m
):
145 sender_screen_name
= '@' + m
['sender_screen_name']
146 sender_name
= m
['sender']['name']
148 recipient_screen_name
= '@' + m
['recipient_screen_name']
149 recipient_name
= m
['recipient']['name']
151 date
= parser
.parse(m
['created_at'])
152 date
= date
- datetime
.timedelta(seconds
=time
.timezone
)
153 clock
= date
.strftime('%Y/%m/%d %H:%M:%S')
156 res
= db
.message_to_rainbow_query(mid
)
158 db
.message_store(mid
)
159 res
= db
.message_to_rainbow_query(mid
)
160 rid
= res
[0].rainbow_id
163 sender
= cycle_color(
164 sender_name
) + color_func(c
['MESSAGE']['sender'])(' ' + sender_screen_name
+ ' ')
165 recipient
= cycle_color(recipient_name
) + color_func(
166 c
['MESSAGE']['recipient'])(
167 ' ' + recipient_screen_name
+ ' ')
168 user
= sender
+ color_func(c
['MESSAGE']['to'])(' >>> ') + recipient
170 c
['MESSAGE']['clock'])(
171 '[' + clock
+ ']') + color_func(
173 ' [message_id=' + str(rid
) + '] ')
174 text
= ''.join(map(lambda x
: x
+ ' ' if x
== '\n' else x
, text
))
176 line1
= u
"{u:>{uw}}:".format(
180 line2
= u
"{c:>{cw}}".format(
193 def show_profile(u
, iot
=False):
199 screen_name
= u
['screen_name']
200 description
= u
['description']
201 profile_image_url
= u
['profile_image_url']
202 location
= u
['location']
204 created_at
= u
['created_at']
205 statuses_count
= u
['statuses_count']
206 friends_count
= u
['friends_count']
207 followers_count
= u
['followers_count']
210 statuses_count
= color_func(
211 c
['PROFILE']['statuses_count'])(
212 str(statuses_count
) +
214 friends_count
= color_func(
215 c
['PROFILE']['friends_count'])(
218 followers_count
= color_func(
219 c
['PROFILE']['followers_count'])(
220 str(followers_count
) +
222 count
= statuses_count
+ ' ' + friends_count
+ ' ' + followers_count
224 name
) + color_func(c
['PROFILE']['nick'])(' @' + screen_name
+ ' : ') + count
225 profile_image_raw_url
= 'Profile photo: ' + \
226 color_func(c
['PROFILE']['profile_image_url'])(profile_image_url
)
227 description
= ''.join(
228 map(lambda x
: x
+ ' ' * 4 if x
== '\n' else x
, description
))
229 description
= color_func(c
['PROFILE']['description'])(description
)
230 location
= 'Location : ' + color_func(c
['PROFILE']['location'])(location
)
231 url
= 'URL : ' + (color_func(c
['PROFILE']['url'])(url
) if url
else '')
232 date
= parser
.parse(created_at
)
233 date
= date
- datetime
.timedelta(seconds
=time
.timezone
)
234 clock
= date
.strftime('%Y/%m/%d %H:%M:%S')
235 clock
= 'Join at ' + color_func(c
['PROFILE']['clock'])(clock
)
238 line1
= u
"{u:>{uw}}".format(
242 line2
= u
"{p:>{pw}}".format(
243 p
=profile_image_raw_url
,
244 pw
=len(profile_image_raw_url
) + 4,
246 line3
= u
"{d:>{dw}}".format(
248 dw
=len(description
) + 4,
250 line4
= u
"{l:>{lw}}".format(
252 lw
=len(location
) + 4,
254 line5
= u
"{u:>{uw}}".format(
258 line6
= u
"{c:>{cw}}".format(
267 response
= requests
.get(profile_image_url
)
268 image_to_display(StringIO(response
.content
), 2, 20)
271 for line
in [line3
, line4
, line5
, line6
]:
276 def print_trends(trends
):
280 for topic
in trends
[:c
['TREND_MAX']]:
283 line
= cycle_color(name
) + ': ' + color_func(TREND
['url'])(url
)