Commit | Line | Data |
---|---|---|
e9f5200b VNM |
1 | import random |
2 | import itertools | |
7500d90b VNM |
3 | import requests |
4 | import datetime | |
5 | import time | |
6 | ||
2da50cc4 | 7 | from twitter.util import printNicely |
e9f5200b VNM |
8 | from functools import wraps |
9 | from pyfiglet import figlet_format | |
10 | from functools import reduce | |
7500d90b VNM |
11 | from StringIO import StringIO |
12 | from dateutil import parser | |
2da50cc4 | 13 | from .c_image import * |
7500d90b VNM |
14 | from .colors import * |
15 | from .config import * | |
16 | from .db import * | |
17 | ||
18 | db = RainbowDB() | |
1fdd6a5c VNM |
19 | g = {} |
20 | ||
4cf86720 | 21 | |
e9f5200b VNM |
22 | def init_cycle(): |
23 | """ | |
24 | Init the cycle | |
25 | """ | |
26 | colors_shuffle = [globals()[i.encode('utf8')] | |
37ae740e | 27 | if not i.startswith('term_') |
8124f3a8 | 28 | else term_color(int(i[5:])) |
e9f5200b VNM |
29 | for i in c['CYCLE_COLOR']] |
30 | return itertools.cycle(colors_shuffle) | |
1fdd6a5c VNM |
31 | g['cyc'] = init_cycle() |
32 | ||
e9f5200b VNM |
33 | |
34 | def order_rainbow(s): | |
35 | """ | |
36 | Print a string with ordered color with each character | |
37 | """ | |
38 | c = [colors_shuffle[i % 7](s[i]) for i in xrange(len(s))] | |
39 | return reduce(lambda x, y: x + y, c) | |
40 | ||
41 | ||
42 | def random_rainbow(s): | |
43 | """ | |
44 | Print a string with random color with each character | |
45 | """ | |
46 | c = [random.choice(colors_shuffle)(i) for i in s] | |
47 | return reduce(lambda x, y: x + y, c) | |
48 | ||
49 | ||
50 | def Memoize(func): | |
51 | """ | |
52 | Memoize decorator | |
53 | """ | |
54 | cache = {} | |
55 | ||
56 | @wraps(func) | |
57 | def wrapper(*args): | |
58 | if args not in cache: | |
59 | cache[args] = func(*args) | |
60 | return cache[args] | |
61 | return wrapper | |
62 | ||
63 | ||
64 | @Memoize | |
65 | def cycle_color(s): | |
66 | """ | |
67 | Cycle the colors_shuffle | |
68 | """ | |
1fdd6a5c | 69 | return next(g['cyc'])(s) |
e9f5200b VNM |
70 | |
71 | ||
72 | def ascii_art(text): | |
73 | """ | |
74 | Draw the Ascii Art | |
75 | """ | |
76 | fi = figlet_format(text, font='doom') | |
77 | print('\n'.join( | |
1fdd6a5c | 78 | [next(g['cyc'])(i) for i in fi.split('\n')] |
e9f5200b VNM |
79 | )) |
80 | ||
81 | ||
4cf86720 VNM |
82 | def check_theme(): |
83 | """ | |
84 | Check current theme and update if necessary | |
85 | """ | |
86 | exists = db.theme_query() | |
87 | themes = [t.theme_name for t in exists] | |
ddb1e615 VNM |
88 | if c['theme'] != themes[0]: |
89 | c['theme'] = themes[0] | |
4cf86720 | 90 | # Determine path |
ddb1e615 | 91 | if c['theme'] == 'custom': |
4cf86720 VNM |
92 | config = os.environ.get( |
93 | 'HOME', | |
9c7342ca | 94 | os.environ.get('USERPROFILE', |
4cf86720 VNM |
95 | '')) + os.sep + '.rainbow_config.json' |
96 | else: | |
ddb1e615 | 97 | config = 'rainbowstream/colorset/'+c['theme']+'.json' |
4cf86720 VNM |
98 | # Load new config |
99 | data = load_config(config) | |
a5301bc0 VNM |
100 | if data: |
101 | for d in data: | |
102 | c[d] = data[d] | |
9c7342ca | 103 | # Re-init color cycle |
1fdd6a5c | 104 | g['cyc'] = init_cycle() |
7500d90b | 105 | |
fe08f905 VNM |
106 | |
107 | def color_func(func_name): | |
108 | """ | |
109 | Call color function base on name | |
110 | """ | |
111 | pure = func_name.encode('utf8') | |
8124f3a8 VNM |
112 | if pure.startswith('term_') and pure[5:].isdigit(): |
113 | return term_color(int(pure[5:])) | |
1face019 | 114 | return globals()[pure] |
fe08f905 VNM |
115 | |
116 | ||
7500d90b VNM |
117 | def draw(t, iot=False, keyword=None, fil=[], ig=[]): |
118 | """ | |
119 | Draw the rainbow | |
120 | """ | |
121 | ||
4cf86720 | 122 | check_theme() |
7500d90b VNM |
123 | # Retrieve tweet |
124 | tid = t['id'] | |
125 | text = t['text'] | |
126 | screen_name = t['user']['screen_name'] | |
127 | name = t['user']['name'] | |
128 | created_at = t['created_at'] | |
129 | favorited = t['favorited'] | |
130 | date = parser.parse(created_at) | |
131 | date = date - datetime.timedelta(seconds=time.timezone) | |
132 | clock = date.strftime('%Y/%m/%d %H:%M:%S') | |
133 | ||
134 | # Get expanded url | |
135 | try: | |
136 | expanded_url = [] | |
137 | url = [] | |
138 | urls = t['entities']['urls'] | |
139 | for u in urls: | |
140 | expanded_url.append(u['expanded_url']) | |
141 | url.append(u['url']) | |
142 | except: | |
143 | expanded_url = None | |
144 | url = None | |
145 | ||
146 | # Get media | |
147 | try: | |
148 | media_url = [] | |
149 | media = t['entities']['media'] | |
150 | for m in media: | |
151 | media_url.append(m['media_url']) | |
152 | except: | |
153 | media_url = None | |
154 | ||
155 | # Filter and ignore | |
156 | screen_name = '@' + screen_name | |
157 | if fil and screen_name not in fil: | |
158 | return | |
159 | if ig and screen_name in ig: | |
160 | return | |
161 | ||
162 | # Get rainbow id | |
163 | res = db.tweet_to_rainbow_query(tid) | |
164 | if not res: | |
165 | db.tweet_store(tid) | |
166 | res = db.tweet_to_rainbow_query(tid) | |
167 | rid = res[0].rainbow_id | |
168 | ||
169 | # Format info | |
c075e6dc O |
170 | user = cycle_color( |
171 | name) + color_func(c['TWEET']['nick'])(' ' + screen_name + ' ') | |
172 | meta = color_func(c['TWEET']['clock'])( | |
173 | '[' + clock + '] ') + color_func(c['TWEET']['id'])('[id=' + str(rid) + '] ') | |
7500d90b | 174 | if favorited: |
632c6fa5 | 175 | meta = meta + color_func(c['TWEET']['favorite'])(u'\u2605') |
7500d90b VNM |
176 | tweet = text.split() |
177 | # Replace url | |
178 | if expanded_url: | |
179 | for index in range(len(expanded_url)): | |
180 | tweet = map( | |
181 | lambda x: expanded_url[index] if x == url[index] else x, | |
182 | tweet) | |
183 | # Highlight RT | |
c075e6dc O |
184 | tweet = map( |
185 | lambda x: color_func( | |
186 | c['TWEET']['rt'])(x) if x == 'RT' else x, | |
187 | tweet) | |
7500d90b VNM |
188 | # Highlight screen_name |
189 | tweet = map(lambda x: cycle_color(x) if x[0] == '@' else x, tweet) | |
190 | # Highlight link | |
c075e6dc O |
191 | tweet = map( |
192 | lambda x: color_func( | |
193 | c['TWEET']['link'])(x) if x[ | |
194 | 0:4] == 'http' else x, | |
195 | tweet) | |
7500d90b VNM |
196 | # Highlight search keyword |
197 | if keyword: | |
198 | tweet = map( | |
632c6fa5 | 199 | lambda x: color_func(c['TWEET']['keyword'])(x) if |
7500d90b VNM |
200 | ''.join(c for c in x if c.isalnum()).lower() == keyword.lower() |
201 | else x, | |
202 | tweet | |
203 | ) | |
204 | # Recreate tweet | |
205 | tweet = ' '.join(tweet) | |
206 | ||
207 | # Draw rainbow | |
208 | line1 = u"{u:>{uw}}:".format( | |
209 | u=user, | |
210 | uw=len(user) + 2, | |
211 | ) | |
212 | line2 = u"{c:>{cw}}".format( | |
213 | c=meta, | |
214 | cw=len(meta) + 2, | |
215 | ) | |
216 | line3 = ' ' + tweet | |
217 | ||
218 | printNicely('') | |
219 | printNicely(line1) | |
220 | printNicely(line2) | |
221 | printNicely(line3) | |
222 | ||
223 | # Display Image | |
224 | if iot and media_url: | |
225 | for mu in media_url: | |
226 | response = requests.get(mu) | |
227 | image_to_display(StringIO(response.content)) | |
228 | ||
229 | ||
230 | def print_message(m): | |
231 | """ | |
232 | Print direct message | |
233 | """ | |
234 | sender_screen_name = '@' + m['sender_screen_name'] | |
235 | sender_name = m['sender']['name'] | |
236 | text = m['text'] | |
237 | recipient_screen_name = '@' + m['recipient_screen_name'] | |
238 | recipient_name = m['recipient']['name'] | |
239 | mid = m['id'] | |
240 | date = parser.parse(m['created_at']) | |
241 | date = date - datetime.timedelta(seconds=time.timezone) | |
242 | clock = date.strftime('%Y/%m/%d %H:%M:%S') | |
243 | ||
244 | # Get rainbow id | |
245 | res = db.message_to_rainbow_query(mid) | |
246 | if not res: | |
247 | db.message_store(mid) | |
248 | res = db.message_to_rainbow_query(mid) | |
249 | rid = res[0].rainbow_id | |
250 | ||
6fa09c14 | 251 | # Draw |
c075e6dc O |
252 | sender = cycle_color( |
253 | sender_name) + color_func(c['MESSAGE']['sender'])(' ' + sender_screen_name + ' ') | |
254 | recipient = cycle_color(recipient_name) + color_func( | |
255 | c['MESSAGE']['recipient'])( | |
256 | ' ' + recipient_screen_name + ' ') | |
632c6fa5 | 257 | user = sender + color_func(c['MESSAGE']['to'])(' >>> ') + recipient |
c075e6dc O |
258 | meta = color_func( |
259 | c['MESSAGE']['clock'])( | |
260 | '[' + clock + ']') + color_func( | |
261 | c['MESSAGE']['id'])( | |
262 | ' [message_id=' + str(rid) + '] ') | |
7500d90b VNM |
263 | text = ''.join(map(lambda x: x + ' ' if x == '\n' else x, text)) |
264 | ||
265 | line1 = u"{u:>{uw}}:".format( | |
266 | u=user, | |
267 | uw=len(user) + 2, | |
268 | ) | |
269 | line2 = u"{c:>{cw}}".format( | |
270 | c=meta, | |
271 | cw=len(meta) + 2, | |
272 | ) | |
273 | ||
274 | line3 = ' ' + text | |
275 | ||
276 | printNicely('') | |
277 | printNicely(line1) | |
278 | printNicely(line2) | |
279 | printNicely(line3) | |
280 | ||
281 | ||
282 | def show_profile(u, iot=False): | |
283 | """ | |
284 | Show a profile | |
285 | """ | |
286 | # Retrieve info | |
287 | name = u['name'] | |
288 | screen_name = u['screen_name'] | |
289 | description = u['description'] | |
290 | profile_image_url = u['profile_image_url'] | |
291 | location = u['location'] | |
292 | url = u['url'] | |
293 | created_at = u['created_at'] | |
294 | statuses_count = u['statuses_count'] | |
295 | friends_count = u['friends_count'] | |
296 | followers_count = u['followers_count'] | |
6fa09c14 | 297 | |
7500d90b | 298 | # Create content |
c075e6dc O |
299 | statuses_count = color_func( |
300 | c['PROFILE']['statuses_count'])( | |
301 | str(statuses_count) + | |
302 | ' tweets') | |
303 | friends_count = color_func( | |
304 | c['PROFILE']['friends_count'])( | |
305 | str(friends_count) + | |
306 | ' following') | |
307 | followers_count = color_func( | |
308 | c['PROFILE']['followers_count'])( | |
309 | str(followers_count) + | |
310 | ' followers') | |
7500d90b | 311 | count = statuses_count + ' ' + friends_count + ' ' + followers_count |
c075e6dc O |
312 | user = cycle_color( |
313 | name) + color_func(c['PROFILE']['nick'])(' @' + screen_name + ' : ') + count | |
314 | profile_image_raw_url = 'Profile photo: ' + \ | |
315 | color_func(c['PROFILE']['profile_image_url'])(profile_image_url) | |
7500d90b VNM |
316 | description = ''.join( |
317 | map(lambda x: x + ' ' * 4 if x == '\n' else x, description)) | |
632c6fa5 O |
318 | description = color_func(c['PROFILE']['description'])(description) |
319 | location = 'Location : ' + color_func(c['PROFILE']['location'])(location) | |
320 | url = 'URL : ' + (color_func(c['PROFILE']['url'])(url) if url else '') | |
7500d90b VNM |
321 | date = parser.parse(created_at) |
322 | date = date - datetime.timedelta(seconds=time.timezone) | |
323 | clock = date.strftime('%Y/%m/%d %H:%M:%S') | |
632c6fa5 | 324 | clock = 'Join at ' + color_func(c['PROFILE']['clock'])(clock) |
6fa09c14 | 325 | |
7500d90b VNM |
326 | # Format |
327 | line1 = u"{u:>{uw}}".format( | |
328 | u=user, | |
329 | uw=len(user) + 2, | |
330 | ) | |
331 | line2 = u"{p:>{pw}}".format( | |
332 | p=profile_image_raw_url, | |
333 | pw=len(profile_image_raw_url) + 4, | |
334 | ) | |
335 | line3 = u"{d:>{dw}}".format( | |
336 | d=description, | |
337 | dw=len(description) + 4, | |
338 | ) | |
339 | line4 = u"{l:>{lw}}".format( | |
340 | l=location, | |
341 | lw=len(location) + 4, | |
342 | ) | |
343 | line5 = u"{u:>{uw}}".format( | |
344 | u=url, | |
345 | uw=len(url) + 4, | |
346 | ) | |
347 | line6 = u"{c:>{cw}}".format( | |
348 | c=clock, | |
349 | cw=len(clock) + 4, | |
350 | ) | |
6fa09c14 | 351 | |
7500d90b VNM |
352 | # Display |
353 | printNicely('') | |
354 | printNicely(line1) | |
355 | if iot: | |
356 | response = requests.get(profile_image_url) | |
357 | image_to_display(StringIO(response.content), 2, 20) | |
358 | else: | |
359 | printNicely(line2) | |
360 | for line in [line3, line4, line5, line6]: | |
361 | printNicely(line) | |
362 | printNicely('') | |
363 | ||
364 | ||
365 | def print_trends(trends): | |
366 | """ | |
367 | Display topics | |
368 | """ | |
632c6fa5 | 369 | for topic in trends[:c['TREND_MAX']]: |
7500d90b VNM |
370 | name = topic['name'] |
371 | url = topic['url'] | |
8394e34b | 372 | line = cycle_color(name) + ': ' + color_func(c['TREND']['url'])(url) |
7500d90b VNM |
373 | printNicely(line) |
374 | printNicely('') |