Commit | Line | Data |
---|---|---|
e9f5200b VNM |
1 | import random |
2 | import itertools | |
7500d90b VNM |
3 | import requests |
4 | import datetime | |
5 | import time | |
59262e95 | 6 | import re |
7500d90b | 7 | |
2da50cc4 | 8 | from twitter.util import printNicely |
e9f5200b VNM |
9 | from functools import wraps |
10 | from pyfiglet import figlet_format | |
7500d90b | 11 | from dateutil import parser |
2da50cc4 | 12 | from .c_image import * |
7500d90b VNM |
13 | from .colors import * |
14 | from .config import * | |
15 | from .db import * | |
c3bab4ef | 16 | from .py3patch import * |
17 | ||
7500d90b VNM |
18 | |
19 | db = RainbowDB() | |
1fdd6a5c VNM |
20 | g = {} |
21 | ||
422dd385 | 22 | |
b2cde062 BR |
23 | def unescape(s): |
24 | p = HTMLParser() | |
25 | return p.unescape(s) | |
26 | ||
27 | ||
e9f5200b VNM |
28 | def init_cycle(): |
29 | """ | |
30 | Init the cycle | |
31 | """ | |
32 | colors_shuffle = [globals()[i.encode('utf8')] | |
fa6e062d O |
33 | if not str(i).isdigit() |
34 | else term_color(int(i)) | |
422dd385 | 35 | for i in c['CYCLE_COLOR']] |
5f22104f | 36 | return itertools.cycle(colors_shuffle) |
1fdd6a5c | 37 | |
e9f5200b | 38 | |
59262e95 | 39 | def start_cycle(): |
2359c276 VNM |
40 | """ |
41 | Notify from rainbow | |
42 | """ | |
5f22104f | 43 | g['cyc'] = init_cycle() |
e43ebfa6 | 44 | g['cache'] = {} |
2359c276 VNM |
45 | |
46 | ||
e9f5200b VNM |
47 | def order_rainbow(s): |
48 | """ | |
49 | Print a string with ordered color with each character | |
50 | """ | |
5f22104f | 51 | colors_shuffle = [globals()[i.encode('utf8')] |
fa6e062d O |
52 | if not str(i).isdigit() |
53 | else term_color(int(i)) | |
422dd385 | 54 | for i in c['CYCLE_COLOR']] |
5f22104f | 55 | colored = [colors_shuffle[i % 7](s[i]) for i in xrange(len(s))] |
c3bab4ef | 56 | return ''.join(colored) |
e9f5200b VNM |
57 | |
58 | ||
59 | def random_rainbow(s): | |
60 | """ | |
61 | Print a string with random color with each character | |
62 | """ | |
5f22104f | 63 | colors_shuffle = [globals()[i.encode('utf8')] |
fa6e062d O |
64 | if not str(i).isdigit() |
65 | else term_color(int(i)) | |
422dd385 | 66 | for i in c['CYCLE_COLOR']] |
5f22104f | 67 | colored = [random.choice(colors_shuffle)(i) for i in s] |
c3bab4ef | 68 | return ''.join(colored) |
e9f5200b VNM |
69 | |
70 | ||
71 | def Memoize(func): | |
72 | """ | |
73 | Memoize decorator | |
74 | """ | |
e9f5200b VNM |
75 | @wraps(func) |
76 | def wrapper(*args): | |
e43ebfa6 | 77 | if args not in g['cache']: |
78 | g['cache'][args] = func(*args) | |
79 | return g['cache'][args] | |
e9f5200b VNM |
80 | return wrapper |
81 | ||
82 | ||
83 | @Memoize | |
84 | def cycle_color(s): | |
85 | """ | |
86 | Cycle the colors_shuffle | |
87 | """ | |
1fdd6a5c | 88 | return next(g['cyc'])(s) |
e9f5200b VNM |
89 | |
90 | ||
91 | def ascii_art(text): | |
92 | """ | |
93 | Draw the Ascii Art | |
94 | """ | |
95 | fi = figlet_format(text, font='doom') | |
96 | print('\n'.join( | |
1fdd6a5c | 97 | [next(g['cyc'])(i) for i in fi.split('\n')] |
e9f5200b VNM |
98 | )) |
99 | ||
100 | ||
2a0cabee O |
101 | def show_calendar(month, date, rel): |
102 | """ | |
103 | Show the calendar in rainbow mode | |
104 | """ | |
105 | month = random_rainbow(month) | |
106 | date = ' '.join([cycle_color(i) for i in date.split(' ')]) | |
107 | today = str(int(os.popen('date +\'%d\'').read().strip())) | |
108 | # Display | |
109 | printNicely(month) | |
110 | printNicely(date) | |
111 | for line in rel: | |
112 | ary = line.split(' ') | |
c3bab4ef | 113 | ary = lmap(lambda x: color_func(c['CAL']['today'])(x) |
422dd385 O |
114 | if x == today |
115 | else color_func(c['CAL']['days'])(x), ary) | |
2a0cabee O |
116 | printNicely(' '.join(ary)) |
117 | ||
118 | ||
4cf86720 VNM |
119 | def check_theme(): |
120 | """ | |
121 | Check current theme and update if necessary | |
122 | """ | |
123 | exists = db.theme_query() | |
124 | themes = [t.theme_name for t in exists] | |
1f2f6159 O |
125 | if c['THEME'] != themes[0]: |
126 | c['THEME'] = themes[0] | |
127 | config = os.path.dirname( | |
128 | __file__) + '/colorset/' + c['THEME'] + '.json' | |
4cf86720 VNM |
129 | # Load new config |
130 | data = load_config(config) | |
a5301bc0 VNM |
131 | if data: |
132 | for d in data: | |
133 | c[d] = data[d] | |
9c7342ca | 134 | # Re-init color cycle |
1fdd6a5c | 135 | g['cyc'] = init_cycle() |
7500d90b | 136 | |
fe08f905 VNM |
137 | |
138 | def color_func(func_name): | |
139 | """ | |
140 | Call color function base on name | |
141 | """ | |
fa6e062d O |
142 | if str(func_name).isdigit(): |
143 | return term_color(int(func_name)) | |
c3bab4ef | 144 | return globals()[func_name] |
fe08f905 VNM |
145 | |
146 | ||
9683e61d | 147 | def draw(t, iot=False, keyword=None, check_semaphore=False, fil=[], ig=[]): |
7500d90b VNM |
148 | """ |
149 | Draw the rainbow | |
150 | """ | |
151 | ||
4cf86720 | 152 | check_theme() |
7500d90b VNM |
153 | # Retrieve tweet |
154 | tid = t['id'] | |
b2cde062 | 155 | text = unescape(t['text']) |
7500d90b VNM |
156 | screen_name = t['user']['screen_name'] |
157 | name = t['user']['name'] | |
158 | created_at = t['created_at'] | |
159 | favorited = t['favorited'] | |
160 | date = parser.parse(created_at) | |
161 | date = date - datetime.timedelta(seconds=time.timezone) | |
162 | clock = date.strftime('%Y/%m/%d %H:%M:%S') | |
163 | ||
164 | # Get expanded url | |
165 | try: | |
166 | expanded_url = [] | |
167 | url = [] | |
168 | urls = t['entities']['urls'] | |
169 | for u in urls: | |
170 | expanded_url.append(u['expanded_url']) | |
171 | url.append(u['url']) | |
172 | except: | |
173 | expanded_url = None | |
174 | url = None | |
175 | ||
176 | # Get media | |
177 | try: | |
178 | media_url = [] | |
179 | media = t['entities']['media'] | |
180 | for m in media: | |
181 | media_url.append(m['media_url']) | |
182 | except: | |
183 | media_url = None | |
184 | ||
185 | # Filter and ignore | |
186 | screen_name = '@' + screen_name | |
187 | if fil and screen_name not in fil: | |
188 | return | |
189 | if ig and screen_name in ig: | |
190 | return | |
191 | ||
192 | # Get rainbow id | |
193 | res = db.tweet_to_rainbow_query(tid) | |
194 | if not res: | |
195 | db.tweet_store(tid) | |
196 | res = db.tweet_to_rainbow_query(tid) | |
197 | rid = res[0].rainbow_id | |
198 | ||
199 | # Format info | |
c075e6dc O |
200 | user = cycle_color( |
201 | name) + color_func(c['TWEET']['nick'])(' ' + screen_name + ' ') | |
202 | meta = color_func(c['TWEET']['clock'])( | |
203 | '[' + clock + '] ') + color_func(c['TWEET']['id'])('[id=' + str(rid) + '] ') | |
7500d90b | 204 | if favorited: |
1a0ac1b8 | 205 | meta = meta + color_func(c['TWEET']['favorited'])(u'\u2605') |
7500d90b VNM |
206 | tweet = text.split() |
207 | # Replace url | |
208 | if expanded_url: | |
209 | for index in range(len(expanded_url)): | |
c3bab4ef | 210 | tweet = lmap( |
7500d90b VNM |
211 | lambda x: expanded_url[index] if x == url[index] else x, |
212 | tweet) | |
213 | # Highlight RT | |
c3bab4ef | 214 | tweet = lmap( |
c075e6dc O |
215 | lambda x: color_func( |
216 | c['TWEET']['rt'])(x) if x == 'RT' else x, | |
217 | tweet) | |
7500d90b | 218 | # Highlight screen_name |
c3bab4ef | 219 | tweet = lmap(lambda x: cycle_color(x) if x[0] == '@' else x, tweet) |
7500d90b | 220 | # Highlight link |
c3bab4ef | 221 | tweet = lmap( |
c075e6dc O |
222 | lambda x: color_func( |
223 | c['TWEET']['link'])(x) if x[ | |
224 | 0:4] == 'http' else x, | |
225 | tweet) | |
59262e95 O |
226 | |
227 | # Highlight keyword | |
7500d90b | 228 | tweet = ' '.join(tweet) |
59262e95 O |
229 | if keyword: |
230 | roj = re.search(keyword,tweet,re.IGNORECASE) | |
231 | if roj: | |
232 | occur = roj.group() | |
233 | ary = tweet.split(occur) | |
234 | delimeter = color_func(c['TWEET']['keyword'])(occur) | |
235 | tweet = delimeter.join(ary) | |
7500d90b VNM |
236 | |
237 | # Draw rainbow | |
238 | line1 = u"{u:>{uw}}:".format( | |
239 | u=user, | |
240 | uw=len(user) + 2, | |
241 | ) | |
242 | line2 = u"{c:>{cw}}".format( | |
243 | c=meta, | |
244 | cw=len(meta) + 2, | |
245 | ) | |
246 | line3 = ' ' + tweet | |
247 | ||
9683e61d O |
248 | # Check the semaphore lock |
249 | if check_semaphore: | |
250 | while db.semaphore_query(): | |
251 | time.sleep(0.5) | |
252 | ||
253 | # Output | |
7500d90b VNM |
254 | printNicely('') |
255 | printNicely(line1) | |
256 | printNicely(line2) | |
257 | printNicely(line3) | |
258 | ||
259 | # Display Image | |
260 | if iot and media_url: | |
261 | for mu in media_url: | |
17bc529d | 262 | try: |
263 | response = requests.get(mu) | |
77f1d210 | 264 | image_to_display(BytesIO(response.content)) |
265 | except Exception: | |
17bc529d | 266 | printNicely(red('Sorry, image link is broken')) |
7500d90b VNM |
267 | |
268 | ||
269 | def print_message(m): | |
270 | """ | |
271 | Print direct message | |
272 | """ | |
273 | sender_screen_name = '@' + m['sender_screen_name'] | |
274 | sender_name = m['sender']['name'] | |
b2cde062 | 275 | text = unescape(m['text']) |
7500d90b VNM |
276 | recipient_screen_name = '@' + m['recipient_screen_name'] |
277 | recipient_name = m['recipient']['name'] | |
278 | mid = m['id'] | |
279 | date = parser.parse(m['created_at']) | |
280 | date = date - datetime.timedelta(seconds=time.timezone) | |
281 | clock = date.strftime('%Y/%m/%d %H:%M:%S') | |
282 | ||
283 | # Get rainbow id | |
284 | res = db.message_to_rainbow_query(mid) | |
285 | if not res: | |
286 | db.message_store(mid) | |
287 | res = db.message_to_rainbow_query(mid) | |
288 | rid = res[0].rainbow_id | |
289 | ||
6fa09c14 | 290 | # Draw |
c075e6dc O |
291 | sender = cycle_color( |
292 | sender_name) + color_func(c['MESSAGE']['sender'])(' ' + sender_screen_name + ' ') | |
293 | recipient = cycle_color(recipient_name) + color_func( | |
294 | c['MESSAGE']['recipient'])( | |
295 | ' ' + recipient_screen_name + ' ') | |
632c6fa5 | 296 | user = sender + color_func(c['MESSAGE']['to'])(' >>> ') + recipient |
c075e6dc O |
297 | meta = color_func( |
298 | c['MESSAGE']['clock'])( | |
299 | '[' + clock + ']') + color_func( | |
300 | c['MESSAGE']['id'])( | |
301 | ' [message_id=' + str(rid) + '] ') | |
c3bab4ef | 302 | text = ''.join(lmap(lambda x: x + ' ' if x == '\n' else x, text)) |
7500d90b VNM |
303 | |
304 | line1 = u"{u:>{uw}}:".format( | |
305 | u=user, | |
306 | uw=len(user) + 2, | |
307 | ) | |
308 | line2 = u"{c:>{cw}}".format( | |
309 | c=meta, | |
310 | cw=len(meta) + 2, | |
311 | ) | |
312 | ||
313 | line3 = ' ' + text | |
314 | ||
315 | printNicely('') | |
316 | printNicely(line1) | |
317 | printNicely(line2) | |
318 | printNicely(line3) | |
319 | ||
320 | ||
321 | def show_profile(u, iot=False): | |
322 | """ | |
323 | Show a profile | |
324 | """ | |
325 | # Retrieve info | |
326 | name = u['name'] | |
327 | screen_name = u['screen_name'] | |
328 | description = u['description'] | |
329 | profile_image_url = u['profile_image_url'] | |
330 | location = u['location'] | |
331 | url = u['url'] | |
332 | created_at = u['created_at'] | |
333 | statuses_count = u['statuses_count'] | |
334 | friends_count = u['friends_count'] | |
335 | followers_count = u['followers_count'] | |
6fa09c14 | 336 | |
7500d90b | 337 | # Create content |
c075e6dc O |
338 | statuses_count = color_func( |
339 | c['PROFILE']['statuses_count'])( | |
340 | str(statuses_count) + | |
341 | ' tweets') | |
342 | friends_count = color_func( | |
343 | c['PROFILE']['friends_count'])( | |
344 | str(friends_count) + | |
345 | ' following') | |
346 | followers_count = color_func( | |
347 | c['PROFILE']['followers_count'])( | |
348 | str(followers_count) + | |
349 | ' followers') | |
7500d90b | 350 | count = statuses_count + ' ' + friends_count + ' ' + followers_count |
c075e6dc O |
351 | user = cycle_color( |
352 | name) + color_func(c['PROFILE']['nick'])(' @' + screen_name + ' : ') + count | |
353 | profile_image_raw_url = 'Profile photo: ' + \ | |
354 | color_func(c['PROFILE']['profile_image_url'])(profile_image_url) | |
7500d90b | 355 | description = ''.join( |
c3bab4ef | 356 | lmap(lambda x: x + ' ' * 4 if x == '\n' else x, description)) |
632c6fa5 O |
357 | description = color_func(c['PROFILE']['description'])(description) |
358 | location = 'Location : ' + color_func(c['PROFILE']['location'])(location) | |
359 | url = 'URL : ' + (color_func(c['PROFILE']['url'])(url) if url else '') | |
7500d90b VNM |
360 | date = parser.parse(created_at) |
361 | date = date - datetime.timedelta(seconds=time.timezone) | |
362 | clock = date.strftime('%Y/%m/%d %H:%M:%S') | |
632c6fa5 | 363 | clock = 'Join at ' + color_func(c['PROFILE']['clock'])(clock) |
6fa09c14 | 364 | |
7500d90b VNM |
365 | # Format |
366 | line1 = u"{u:>{uw}}".format( | |
367 | u=user, | |
368 | uw=len(user) + 2, | |
369 | ) | |
370 | line2 = u"{p:>{pw}}".format( | |
371 | p=profile_image_raw_url, | |
372 | pw=len(profile_image_raw_url) + 4, | |
373 | ) | |
374 | line3 = u"{d:>{dw}}".format( | |
375 | d=description, | |
376 | dw=len(description) + 4, | |
377 | ) | |
378 | line4 = u"{l:>{lw}}".format( | |
379 | l=location, | |
380 | lw=len(location) + 4, | |
381 | ) | |
382 | line5 = u"{u:>{uw}}".format( | |
383 | u=url, | |
384 | uw=len(url) + 4, | |
385 | ) | |
386 | line6 = u"{c:>{cw}}".format( | |
387 | c=clock, | |
388 | cw=len(clock) + 4, | |
389 | ) | |
6fa09c14 | 390 | |
7500d90b VNM |
391 | # Display |
392 | printNicely('') | |
393 | printNicely(line1) | |
394 | if iot: | |
17bc529d | 395 | try: |
396 | response = requests.get(profile_image_url) | |
77f1d210 | 397 | image_to_display(BytesIO(response.content), 2, 20) |
17bc529d | 398 | except: |
399 | pass | |
7500d90b VNM |
400 | else: |
401 | printNicely(line2) | |
402 | for line in [line3, line4, line5, line6]: | |
403 | printNicely(line) | |
404 | printNicely('') | |
405 | ||
406 | ||
407 | def print_trends(trends): | |
408 | """ | |
409 | Display topics | |
410 | """ | |
632c6fa5 | 411 | for topic in trends[:c['TREND_MAX']]: |
7500d90b VNM |
412 | name = topic['name'] |
413 | url = topic['url'] | |
8394e34b | 414 | line = cycle_color(name) + ': ' + color_func(c['TREND']['url'])(url) |
7500d90b VNM |
415 | printNicely(line) |
416 | printNicely('') | |
2d341029 O |
417 | |
418 | ||
419 | def print_list(group): | |
420 | """ | |
421 | Display a list | |
422 | """ | |
423 | for g in group: | |
424 | # Format | |
422dd385 | 425 | name = g['full_name'] |
2d341029 O |
426 | name = color_func(c['GROUP']['name'])(name + ' : ') |
427 | member = str(g['member_count']) | |
422dd385 | 428 | member = color_func(c['GROUP']['member'])(member + ' member') |
2d341029 | 429 | subscriber = str(g['subscriber_count']) |
422dd385 O |
430 | subscriber = color_func( |
431 | c['GROUP']['subscriber'])( | |
432 | subscriber + | |
433 | ' subscriber') | |
2d341029 O |
434 | description = g['description'].strip() |
435 | description = color_func(c['GROUP']['description'])(description) | |
436 | mode = g['mode'] | |
422dd385 | 437 | mode = color_func(c['GROUP']['mode'])('Type: ' + mode) |
2d341029 O |
438 | created_at = g['created_at'] |
439 | date = parser.parse(created_at) | |
440 | date = date - datetime.timedelta(seconds=time.timezone) | |
441 | clock = date.strftime('%Y/%m/%d %H:%M:%S') | |
442 | clock = 'Created at ' + color_func(c['GROUP']['clock'])(clock) | |
443 | ||
2d341029 | 444 | # Create lines |
422dd385 O |
445 | line1 = ' ' * 2 + name + member + ' ' + subscriber |
446 | line2 = ' ' * 4 + description | |
447 | line3 = ' ' * 4 + mode | |
448 | line4 = ' ' * 4 + clock | |
2d341029 O |
449 | |
450 | # Display | |
451 | printNicely('') | |
452 | printNicely(line1) | |
453 | printNicely(line2) | |
454 | printNicely(line3) | |
455 | printNicely(line4) | |
456 | ||
457 | printNicely('') | |
59262e95 O |
458 | |
459 | ||
460 | # Start the color cycle | |
b2cde062 | 461 | start_cycle() |