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