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
81 user
= cycle_color(name
) + color_func(TWEET
['nick'])(' ' + screen_name
+ ' ')
82 meta
= color_func(TWEET
['clock'])('[' + clock
+ '] ') + color_func(TWEET
['id'])('[id=' + str(rid
) + '] ')
84 meta
= meta
+ color_func(TWEET
['favorite'])(u
'\u2605')
88 for index
in range(len(expanded_url
)):
90 lambda x
: expanded_url
[index
] if x
== url
[index
] else x
,
93 tweet
= map(lambda x
: color_func(TWEET
['rt'])(x
) if x
== 'RT' else x
, tweet
)
94 # Highlight screen_name
95 tweet
= map(lambda x
: cycle_color(x
) if x
[0] == '@' else x
, tweet
)
97 tweet
= map(lambda x
: color_func(TWEET
['link'])(x
) if x
[0:4] == 'http' else x
, tweet
)
98 # Highlight search keyword
101 lambda x
: color_func(TWEET
['keyword'])(x
) if
102 ''.join(c
for c
in x
if c
.isalnum()).lower() == keyword
.lower()
107 tweet
= ' '.join(tweet
)
110 line1
= u
"{u:>{uw}}:".format(
114 line2
= u
"{c:>{cw}}".format(
126 if iot
and media_url
:
128 response
= requests
.get(mu
)
129 image_to_display(StringIO(response
.content
))
132 def print_message(m
):
136 sender_screen_name
= '@' + m
['sender_screen_name']
137 sender_name
= m
['sender']['name']
139 recipient_screen_name
= '@' + m
['recipient_screen_name']
140 recipient_name
= m
['recipient']['name']
142 date
= parser
.parse(m
['created_at'])
143 date
= date
- datetime
.timedelta(seconds
=time
.timezone
)
144 clock
= date
.strftime('%Y/%m/%d %H:%M:%S')
147 res
= db
.message_to_rainbow_query(mid
)
149 db
.message_store(mid
)
150 res
= db
.message_to_rainbow_query(mid
)
151 rid
= res
[0].rainbow_id
154 sender
= cycle_color(sender_name
) + color_func(MESSAGE
['sender'])(' ' + sender_screen_name
+ ' ')
155 recipient
= cycle_color(recipient_name
) + color_func(MESSAGE
['recipient'])(' ' + recipient_screen_name
+ ' ')
156 user
= sender
+ color_func(MESSAGE
['to'])(' >>> ') + recipient
157 meta
= color_func(MESSAGE
['clock'])('[' + clock
+ ']') + color_func(MESSAGE
['id'])(' [message_id=' + str(rid
) + '] ')
158 text
= ''.join(map(lambda x
: x
+ ' ' if x
== '\n' else x
, text
))
160 line1
= u
"{u:>{uw}}:".format(
164 line2
= u
"{c:>{cw}}".format(
177 def show_profile(u
, iot
=False):
183 screen_name
= u
['screen_name']
184 description
= u
['description']
185 profile_image_url
= u
['profile_image_url']
186 location
= u
['location']
188 created_at
= u
['created_at']
189 statuses_count
= u
['statuses_count']
190 friends_count
= u
['friends_count']
191 followers_count
= u
['followers_count']
194 statuses_count
= color_func(PROFILE
['statuses_count'])(str(statuses_count
) + ' tweets')
195 friends_count
= color_func(PROFILE
['friends_count'])(str(friends_count
) + ' following')
196 followers_count
= color_func(PROFILE
['followers_count'])(str(followers_count
) + ' followers')
197 count
= statuses_count
+ ' ' + friends_count
+ ' ' + followers_count
198 user
= cycle_color(name
) + color_func(PROFILE
['nick'])(' @' + screen_name
+ ' : ') + count
199 profile_image_raw_url
= 'Profile photo: ' + color_func(PROFILE
['profile_image_url'])(profile_image_url
)
200 description
= ''.join(
201 map(lambda x
: x
+ ' ' * 4 if x
== '\n' else x
, description
))
202 description
= color_func(PROFILE
['description'])(description
)
203 location
= 'Location : ' + color_func(PROFILE
['location'])(location
)
204 url
= 'URL : ' + (color_func(PROFILE
['url'])(url
) if url
else '')
205 date
= parser
.parse(created_at
)
206 date
= date
- datetime
.timedelta(seconds
=time
.timezone
)
207 clock
= date
.strftime('%Y/%m/%d %H:%M:%S')
208 clock
= 'Join at ' + color_func(PROFILE
['clock'])(clock
)
211 line1
= u
"{u:>{uw}}".format(
215 line2
= u
"{p:>{pw}}".format(
216 p
=profile_image_raw_url
,
217 pw
=len(profile_image_raw_url
) + 4,
219 line3
= u
"{d:>{dw}}".format(
221 dw
=len(description
) + 4,
223 line4
= u
"{l:>{lw}}".format(
225 lw
=len(location
) + 4,
227 line5
= u
"{u:>{uw}}".format(
231 line6
= u
"{c:>{cw}}".format(
240 response
= requests
.get(profile_image_url
)
241 image_to_display(StringIO(response
.content
), 2, 20)
244 for line
in [line3
, line4
, line5
, line6
]:
249 def print_trends(trends
):
253 for topic
in trends
[:TREND_MAX
]:
256 line
= cycle_color(name
) + ': ' + TREND
['url'](url
)