autopep8
[rainbowstream.git] / rainbowstream / draw.py
CommitLineData
7500d90b
VNM
1"""
2Draw
3"""
4import requests
5import datetime
6import time
7
2da50cc4 8from twitter.util import printNicely
7500d90b
VNM
9from StringIO import StringIO
10from dateutil import parser
2da50cc4 11from .c_image import *
7500d90b
VNM
12from .colors import *
13from .config import *
14from .db import *
15
16db = RainbowDB()
17
fe08f905
VNM
18
19def color_func(func_name):
20 """
21 Call color function base on name
22 """
23 pure = func_name.encode('utf8')
92be926e 24 if pure.startswith('RGB_') and pure[4:].isdigit():
93384849 25 return RGB(int(pure[4:]))
1face019 26 return globals()[pure]
fe08f905
VNM
27
28
7500d90b
VNM
29def draw(t, iot=False, keyword=None, fil=[], ig=[]):
30 """
31 Draw the rainbow
32 """
33
34 # Retrieve tweet
35 tid = t['id']
36 text = t['text']
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')
44
45 # Get expanded url
46 try:
47 expanded_url = []
48 url = []
49 urls = t['entities']['urls']
50 for u in urls:
51 expanded_url.append(u['expanded_url'])
52 url.append(u['url'])
53 except:
54 expanded_url = None
55 url = None
56
57 # Get media
58 try:
59 media_url = []
60 media = t['entities']['media']
61 for m in media:
62 media_url.append(m['media_url'])
63 except:
64 media_url = None
65
66 # Filter and ignore
67 screen_name = '@' + screen_name
68 if fil and screen_name not in fil:
69 return
70 if ig and screen_name in ig:
71 return
72
73 # Get rainbow id
74 res = db.tweet_to_rainbow_query(tid)
75 if not res:
76 db.tweet_store(tid)
77 res = db.tweet_to_rainbow_query(tid)
78 rid = res[0].rainbow_id
79
80 # Format info
c075e6dc
O
81 user = cycle_color(
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) + '] ')
7500d90b 85 if favorited:
632c6fa5 86 meta = meta + color_func(c['TWEET']['favorite'])(u'\u2605')
7500d90b
VNM
87 tweet = text.split()
88 # Replace url
89 if expanded_url:
90 for index in range(len(expanded_url)):
91 tweet = map(
92 lambda x: expanded_url[index] if x == url[index] else x,
93 tweet)
94 # Highlight RT
c075e6dc
O
95 tweet = map(
96 lambda x: color_func(
97 c['TWEET']['rt'])(x) if x == 'RT' else x,
98 tweet)
7500d90b
VNM
99 # Highlight screen_name
100 tweet = map(lambda x: cycle_color(x) if x[0] == '@' else x, tweet)
101 # Highlight link
c075e6dc
O
102 tweet = map(
103 lambda x: color_func(
104 c['TWEET']['link'])(x) if x[
105 0:4] == 'http' else x,
106 tweet)
7500d90b
VNM
107 # Highlight search keyword
108 if keyword:
109 tweet = map(
632c6fa5 110 lambda x: color_func(c['TWEET']['keyword'])(x) if
7500d90b
VNM
111 ''.join(c for c in x if c.isalnum()).lower() == keyword.lower()
112 else x,
113 tweet
114 )
115 # Recreate tweet
116 tweet = ' '.join(tweet)
117
118 # Draw rainbow
119 line1 = u"{u:>{uw}}:".format(
120 u=user,
121 uw=len(user) + 2,
122 )
123 line2 = u"{c:>{cw}}".format(
124 c=meta,
125 cw=len(meta) + 2,
126 )
127 line3 = ' ' + tweet
128
129 printNicely('')
130 printNicely(line1)
131 printNicely(line2)
132 printNicely(line3)
133
134 # Display Image
135 if iot and media_url:
136 for mu in media_url:
137 response = requests.get(mu)
138 image_to_display(StringIO(response.content))
139
140
141def print_message(m):
142 """
143 Print direct message
144 """
145 sender_screen_name = '@' + m['sender_screen_name']
146 sender_name = m['sender']['name']
147 text = m['text']
148 recipient_screen_name = '@' + m['recipient_screen_name']
149 recipient_name = m['recipient']['name']
150 mid = m['id']
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')
154
155 # Get rainbow id
156 res = db.message_to_rainbow_query(mid)
157 if not res:
158 db.message_store(mid)
159 res = db.message_to_rainbow_query(mid)
160 rid = res[0].rainbow_id
161
6fa09c14 162 # Draw
c075e6dc
O
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 + ' ')
632c6fa5 168 user = sender + color_func(c['MESSAGE']['to'])(' >>> ') + recipient
c075e6dc
O
169 meta = color_func(
170 c['MESSAGE']['clock'])(
171 '[' + clock + ']') + color_func(
172 c['MESSAGE']['id'])(
173 ' [message_id=' + str(rid) + '] ')
7500d90b
VNM
174 text = ''.join(map(lambda x: x + ' ' if x == '\n' else x, text))
175
176 line1 = u"{u:>{uw}}:".format(
177 u=user,
178 uw=len(user) + 2,
179 )
180 line2 = u"{c:>{cw}}".format(
181 c=meta,
182 cw=len(meta) + 2,
183 )
184
185 line3 = ' ' + text
186
187 printNicely('')
188 printNicely(line1)
189 printNicely(line2)
190 printNicely(line3)
191
192
193def show_profile(u, iot=False):
194 """
195 Show a profile
196 """
197 # Retrieve info
198 name = u['name']
199 screen_name = u['screen_name']
200 description = u['description']
201 profile_image_url = u['profile_image_url']
202 location = u['location']
203 url = u['url']
204 created_at = u['created_at']
205 statuses_count = u['statuses_count']
206 friends_count = u['friends_count']
207 followers_count = u['followers_count']
6fa09c14 208
7500d90b 209 # Create content
c075e6dc
O
210 statuses_count = color_func(
211 c['PROFILE']['statuses_count'])(
212 str(statuses_count) +
213 ' tweets')
214 friends_count = color_func(
215 c['PROFILE']['friends_count'])(
216 str(friends_count) +
217 ' following')
218 followers_count = color_func(
219 c['PROFILE']['followers_count'])(
220 str(followers_count) +
221 ' followers')
7500d90b 222 count = statuses_count + ' ' + friends_count + ' ' + followers_count
c075e6dc
O
223 user = cycle_color(
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)
7500d90b
VNM
227 description = ''.join(
228 map(lambda x: x + ' ' * 4 if x == '\n' else x, description))
632c6fa5
O
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 '')
7500d90b
VNM
232 date = parser.parse(created_at)
233 date = date - datetime.timedelta(seconds=time.timezone)
234 clock = date.strftime('%Y/%m/%d %H:%M:%S')
632c6fa5 235 clock = 'Join at ' + color_func(c['PROFILE']['clock'])(clock)
6fa09c14 236
7500d90b
VNM
237 # Format
238 line1 = u"{u:>{uw}}".format(
239 u=user,
240 uw=len(user) + 2,
241 )
242 line2 = u"{p:>{pw}}".format(
243 p=profile_image_raw_url,
244 pw=len(profile_image_raw_url) + 4,
245 )
246 line3 = u"{d:>{dw}}".format(
247 d=description,
248 dw=len(description) + 4,
249 )
250 line4 = u"{l:>{lw}}".format(
251 l=location,
252 lw=len(location) + 4,
253 )
254 line5 = u"{u:>{uw}}".format(
255 u=url,
256 uw=len(url) + 4,
257 )
258 line6 = u"{c:>{cw}}".format(
259 c=clock,
260 cw=len(clock) + 4,
261 )
6fa09c14 262
7500d90b
VNM
263 # Display
264 printNicely('')
265 printNicely(line1)
266 if iot:
267 response = requests.get(profile_image_url)
268 image_to_display(StringIO(response.content), 2, 20)
269 else:
270 printNicely(line2)
271 for line in [line3, line4, line5, line6]:
272 printNicely(line)
273 printNicely('')
274
275
276def print_trends(trends):
277 """
278 Display topics
279 """
632c6fa5 280 for topic in trends[:c['TREND_MAX']]:
7500d90b
VNM
281 name = topic['name']
282 url = topic['url']
632c6fa5 283 line = cycle_color(name) + ': ' + color_func(TREND['url'])(url)
7500d90b
VNM
284 printNicely(line)
285 printNicely('')