Commit | Line | Data |
---|---|---|
e9f5200b VNM |
1 | import random |
2 | import itertools | |
7500d90b | 3 | import requests |
8b3456f9 | 4 | import locale |
5 | import arrow | |
59262e95 | 6 | import re |
67c663f8 | 7 | import os |
7500d90b | 8 | |
2da50cc4 | 9 | from twitter.util import printNicely |
e9f5200b VNM |
10 | from functools import wraps |
11 | from pyfiglet import figlet_format | |
7500d90b | 12 | from dateutil import parser |
2da50cc4 | 13 | from .c_image import * |
7500d90b VNM |
14 | from .colors import * |
15 | from .config import * | |
c3bab4ef | 16 | from .py3patch import * |
17 | ||
99b52f5f O |
18 | # Draw global variables |
19 | dg = {} | |
1fdd6a5c | 20 | |
422dd385 | 21 | |
e9f5200b VNM |
22 | def init_cycle(): |
23 | """ | |
24 | Init the cycle | |
25 | """ | |
26 | colors_shuffle = [globals()[i.encode('utf8')] | |
fa6e062d O |
27 | if not str(i).isdigit() |
28 | else term_color(int(i)) | |
422dd385 | 29 | for i in c['CYCLE_COLOR']] |
5f22104f | 30 | return itertools.cycle(colors_shuffle) |
1fdd6a5c | 31 | |
e9f5200b | 32 | |
59262e95 | 33 | def start_cycle(): |
2359c276 VNM |
34 | """ |
35 | Notify from rainbow | |
36 | """ | |
99b52f5f O |
37 | dg['cyc'] = init_cycle() |
38 | dg['cache'] = {} | |
2359c276 VNM |
39 | |
40 | ||
e9f5200b VNM |
41 | def order_rainbow(s): |
42 | """ | |
43 | Print a string with ordered color with each character | |
44 | """ | |
5f22104f | 45 | colors_shuffle = [globals()[i.encode('utf8')] |
fa6e062d O |
46 | if not str(i).isdigit() |
47 | else term_color(int(i)) | |
422dd385 | 48 | for i in c['CYCLE_COLOR']] |
5f22104f | 49 | colored = [colors_shuffle[i % 7](s[i]) for i in xrange(len(s))] |
c3bab4ef | 50 | return ''.join(colored) |
e9f5200b VNM |
51 | |
52 | ||
53 | def random_rainbow(s): | |
54 | """ | |
55 | Print a string with random color with each character | |
56 | """ | |
5f22104f | 57 | colors_shuffle = [globals()[i.encode('utf8')] |
fa6e062d O |
58 | if not str(i).isdigit() |
59 | else term_color(int(i)) | |
422dd385 | 60 | for i in c['CYCLE_COLOR']] |
5f22104f | 61 | colored = [random.choice(colors_shuffle)(i) for i in s] |
c3bab4ef | 62 | return ''.join(colored) |
e9f5200b VNM |
63 | |
64 | ||
65 | def Memoize(func): | |
66 | """ | |
67 | Memoize decorator | |
68 | """ | |
e9f5200b VNM |
69 | @wraps(func) |
70 | def wrapper(*args): | |
99b52f5f O |
71 | if args not in dg['cache']: |
72 | dg['cache'][args] = func(*args) | |
73 | return dg['cache'][args] | |
e9f5200b VNM |
74 | return wrapper |
75 | ||
76 | ||
77 | @Memoize | |
78 | def cycle_color(s): | |
79 | """ | |
80 | Cycle the colors_shuffle | |
81 | """ | |
99b52f5f | 82 | return next(dg['cyc'])(s) |
e9f5200b VNM |
83 | |
84 | ||
85 | def ascii_art(text): | |
86 | """ | |
87 | Draw the Ascii Art | |
88 | """ | |
89 | fi = figlet_format(text, font='doom') | |
90 | print('\n'.join( | |
99b52f5f | 91 | [next(dg['cyc'])(i) for i in fi.split('\n')] |
e9f5200b VNM |
92 | )) |
93 | ||
94 | ||
92685d21 | 95 | def check_config(): |
ceec8593 | 96 | """ |
97 | Check if config is changed | |
98 | """ | |
99 | changed = False | |
100 | data = get_all_config() | |
101 | for key in c: | |
102 | if key in data: | |
103 | if data[key] != c[key]: | |
104 | changed = True | |
105 | if changed: | |
106 | reload_config() | |
107 | ||
108 | ||
109 | def validate_theme(theme): | |
110 | """ | |
111 | Validate a theme exists or not | |
112 | """ | |
113 | # Theme changed check | |
114 | files = os.listdir(os.path.dirname(__file__) + '/colorset') | |
115 | themes = [f.split('.')[0] for f in files if f.split('.')[-1] == 'json'] | |
116 | return theme in themes | |
92685d21 | 117 | |
118 | ||
99b52f5f | 119 | def reload_theme(value, prev): |
4cf86720 VNM |
120 | """ |
121 | Check current theme and update if necessary | |
122 | """ | |
99b52f5f | 123 | if value != prev: |
1f2f6159 | 124 | config = os.path.dirname( |
99b52f5f | 125 | __file__) + '/colorset/' + value + '.json' |
4cf86720 VNM |
126 | # Load new config |
127 | data = load_config(config) | |
a5301bc0 VNM |
128 | if data: |
129 | for d in data: | |
130 | c[d] = data[d] | |
99b52f5f | 131 | # Restart color cycle and update config |
ceec8593 | 132 | start_cycle() |
99b52f5f O |
133 | set_config('THEME', value) |
134 | return value | |
135 | return prev | |
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 | ||
4dc385b5 | 147 | def draw(t, keyword=None, humanize=True, fil=[], ig=[]): |
7500d90b VNM |
148 | """ |
149 | Draw the rainbow | |
150 | """ | |
99b52f5f | 151 | # Check config |
92685d21 | 152 | check_config() |
c37c04a9 | 153 | |
7500d90b VNM |
154 | # Retrieve tweet |
155 | tid = t['id'] | |
606def7e | 156 | text = t['text'] |
7500d90b VNM |
157 | screen_name = t['user']['screen_name'] |
158 | name = t['user']['name'] | |
159 | created_at = t['created_at'] | |
160 | favorited = t['favorited'] | |
318cdd67 O |
161 | retweet_count = t['retweet_count'] |
162 | favorite_count = t['favorite_count'] | |
7500d90b | 163 | date = parser.parse(created_at) |
8b3456f9 | 164 | date = arrow.get(date).to('local') |
165 | if humanize: | |
166 | lang, encode = locale.getdefaultlocale() | |
167 | clock = arrow.get(date).to('local').humanize(locale=lang) | |
168 | else: | |
169 | try: | |
170 | clock_format = c['FORMAT']['TWEET']['CLOCK_FORMAT'] | |
171 | except: | |
172 | clock_format = '%Y/%m/%d %H:%M:%S' | |
173 | clock = date.datetime.strftime(clock_format) | |
7500d90b | 174 | |
606def7e BR |
175 | # Pull extended retweet text |
176 | try: | |
18df6e7f O |
177 | text = 'RT @' + t['retweeted_status']['user']['screen_name'] + ': ' +\ |
178 | t['retweeted_status']['text'] | |
606def7e BR |
179 | except: |
180 | pass | |
181 | ||
18df6e7f | 182 | # Unescape HTML character |
606def7e BR |
183 | text = unescape(text) |
184 | ||
7500d90b VNM |
185 | # Get expanded url |
186 | try: | |
187 | expanded_url = [] | |
188 | url = [] | |
189 | urls = t['entities']['urls'] | |
190 | for u in urls: | |
191 | expanded_url.append(u['expanded_url']) | |
192 | url.append(u['url']) | |
193 | except: | |
194 | expanded_url = None | |
195 | url = None | |
196 | ||
197 | # Get media | |
198 | try: | |
199 | media_url = [] | |
200 | media = t['entities']['media'] | |
201 | for m in media: | |
202 | media_url.append(m['media_url']) | |
203 | except: | |
204 | media_url = None | |
205 | ||
206 | # Filter and ignore | |
207 | screen_name = '@' + screen_name | |
e3927852 O |
208 | fil = list(set((fil or []) + c['ONLY_LIST'])) |
209 | ig = list(set((ig or []) + c['IGNORE_LIST'])) | |
7500d90b VNM |
210 | if fil and screen_name not in fil: |
211 | return | |
212 | if ig and screen_name in ig: | |
213 | return | |
214 | ||
215 | # Get rainbow id | |
99b52f5f O |
216 | if tid not in c['tweet_dict']: |
217 | c['tweet_dict'].append(tid) | |
218 | rid = len(c['tweet_dict']) - 1 | |
219 | else: | |
220 | rid = c['tweet_dict'].index(tid) | |
7500d90b VNM |
221 | |
222 | # Format info | |
8c7ae594 | 223 | name = cycle_color(name) |
d6cc4c67 | 224 | nick = color_func(c['TWEET']['nick'])(screen_name) |
0d9977c7 O |
225 | clock = clock |
226 | id = str(rid) | |
8c7ae594 | 227 | fav = '' |
7500d90b | 228 | if favorited: |
8c7ae594 O |
229 | fav = color_func(c['TWEET']['favorited'])(u'\u2605') |
230 | ||
7500d90b VNM |
231 | tweet = text.split() |
232 | # Replace url | |
233 | if expanded_url: | |
13e6b275 | 234 | for index in xrange(len(expanded_url)): |
c3bab4ef | 235 | tweet = lmap( |
8141aca6 | 236 | lambda x: expanded_url[index] |
237 | if x == url[index] | |
238 | else x, | |
7500d90b VNM |
239 | tweet) |
240 | # Highlight RT | |
c3bab4ef | 241 | tweet = lmap( |
8141aca6 | 242 | lambda x: color_func(c['TWEET']['rt'])(x) |
243 | if x == 'RT' | |
244 | else x, | |
c075e6dc | 245 | tweet) |
7500d90b | 246 | # Highlight screen_name |
c3bab4ef | 247 | tweet = lmap(lambda x: cycle_color(x) if x[0] == '@' else x, tweet) |
7500d90b | 248 | # Highlight link |
c3bab4ef | 249 | tweet = lmap( |
8141aca6 | 250 | lambda x: color_func(c['TWEET']['link'])(x) |
251 | if x[0:4] == 'http' | |
252 | else x, | |
c075e6dc | 253 | tweet) |
4c025026 | 254 | # Highlight hashtag |
255 | tweet = lmap( | |
8141aca6 | 256 | lambda x: color_func(c['TWEET']['hashtag'])(x) |
257 | if x.startswith('#') | |
258 | else x, | |
4c025026 | 259 | tweet) |
59262e95 | 260 | # Highlight keyword |
7500d90b | 261 | tweet = ' '.join(tweet) |
59262e95 | 262 | if keyword: |
a8c5fce4 | 263 | roj = re.search(keyword, tweet, re.IGNORECASE) |
59262e95 O |
264 | if roj: |
265 | occur = roj.group() | |
266 | ary = tweet.split(occur) | |
b13c6a0d | 267 | delimiter = color_func(c['TWEET']['keyword'])(occur) |
268 | tweet = delimiter.join(ary) | |
7500d90b | 269 | |
8c7ae594 | 270 | # Load config formater |
318cdd67 | 271 | formater = '' |
8c7ae594 O |
272 | try: |
273 | formater = c['FORMAT']['TWEET']['DISPLAY'] | |
7c437a0f O |
274 | formater = name.join(formater.split('#name')) |
275 | formater = nick.join(formater.split('#nick')) | |
276 | formater = fav.join(formater.split('#fav')) | |
277 | formater = tweet.join(formater.split('#tweet')) | |
0d9977c7 O |
278 | # Change clock word |
279 | word = [w for w in formater.split() if '#clock' in w][0] | |
8141aca6 | 280 | delimiter = color_func(c['TWEET']['clock'])( |
281 | clock.join(word.split('#clock'))) | |
0d9977c7 O |
282 | formater = delimiter.join(formater.split(word)) |
283 | # Change id word | |
284 | word = [w for w in formater.split() if '#id' in w][0] | |
285 | delimiter = color_func(c['TWEET']['id'])(id.join(word.split('#id'))) | |
286 | formater = delimiter.join(formater.split(word)) | |
318cdd67 O |
287 | # Change retweet count word |
288 | word = [w for w in formater.split() if '#rt_count' in w][0] | |
289 | delimiter = color_func(c['TWEET']['retweet_count'])( | |
290 | str(retweet_count).join(word.split('#rt_count'))) | |
291 | formater = delimiter.join(formater.split(word)) | |
292 | # Change favorites count word | |
293 | word = [w for w in formater.split() if '#fa_count' in w][0] | |
294 | delimiter = color_func(c['TWEET']['favorite_count'])( | |
295 | str(favorite_count).join(word.split('#fa_count'))) | |
296 | formater = delimiter.join(formater.split(word)) | |
8c7ae594 | 297 | except: |
318cdd67 | 298 | pass |
7500d90b | 299 | |
8c7ae594 O |
300 | # Draw |
301 | printNicely(formater) | |
7500d90b VNM |
302 | |
303 | # Display Image | |
fe9bb33b | 304 | if c['IMAGE_ON_TERM'] and media_url: |
7500d90b | 305 | for mu in media_url: |
17bc529d | 306 | try: |
307 | response = requests.get(mu) | |
77f1d210 | 308 | image_to_display(BytesIO(response.content)) |
309 | except Exception: | |
17bc529d | 310 | printNicely(red('Sorry, image link is broken')) |
7500d90b VNM |
311 | |
312 | ||
67c663f8 O |
313 | def print_threads(d): |
314 | """ | |
315 | Print threads of messages | |
316 | """ | |
317 | id = 1 | |
318 | rel = {} | |
319 | for partner in d: | |
320 | messages = d[partner] | |
321 | count = len(messages) | |
322 | screen_name = '@' + partner[0] | |
323 | name = partner[1] | |
324 | screen_name = color_func(c['MESSAGE']['partner'])(screen_name) | |
325 | name = cycle_color(name) | |
326 | thread_id = color_func(c['MESSAGE']['id'])('thread id:'+str(id)) | |
327 | line = ' '*2 + name + ' ' + screen_name + \ | |
328 | ' (' + str(count) + ' message) ' + thread_id | |
329 | printNicely(line) | |
330 | rel[id] = partner | |
331 | id += 1 | |
332 | dg['thread'] = d | |
333 | return rel | |
334 | ||
335 | ||
336 | def print_thread(partner, me_nick, me_name): | |
337 | """ | |
338 | Print a thread of messages | |
339 | """ | |
340 | # Sort messages by time | |
341 | messages = dg['thread'][partner] | |
342 | messages.sort(key = lambda x:parser.parse(x['created_at'])) | |
343 | ||
344 | # Print the 1st line | |
345 | dg['message_thread_margin'] = margin = 2 | |
346 | left_size = len(partner[0])+len(partner[1]) + margin | |
347 | right_size = len(me_nick) + len(me_name) + margin | |
348 | partner_screen_name = color_func(c['MESSAGE']['partner'])('@' + partner[0]) | |
349 | partner_name = cycle_color(partner[1]) | |
350 | me_screen_name = color_func(c['MESSAGE']['me'])('@' + me_nick) | |
351 | me_name = cycle_color(me_name) | |
352 | left = ' ' * margin + partner_name + ' ' + partner_screen_name | |
353 | right = me_name + ' ' + me_screen_name + ' ' * margin | |
354 | h, w = os.popen('stty size', 'r').read().split() | |
355 | w = int(w) | |
356 | line = '{}{}{}'.format(left, ' '*(w - left_size - right_size - 2 * margin), right) | |
357 | printNicely('') | |
358 | printNicely(line) | |
359 | printNicely('') | |
360 | ||
361 | # Print messages | |
362 | for m in messages: | |
363 | if m['sender_screen_name'] == me_nick: | |
364 | print_right_message(m) | |
365 | elif m['recipient_screen_name'] == me_nick: | |
366 | print_left_message(m) | |
367 | ||
368 | ||
369 | def print_right_message(m): | |
370 | """ | |
371 | Print a message on the right of screen | |
372 | """ | |
373 | h, w = os.popen('stty size', 'r').read().split() | |
374 | w = int(w) | |
375 | frame_width = w //3 - dg['message_thread_margin'] | |
376 | step = frame_width - 2 * dg['message_thread_margin'] | |
377 | slicing = [m['text'][i:i+step] for i in range(0, len(m['text']), step)] | |
378 | spaces = w - frame_width - dg['message_thread_margin'] | |
379 | dotline = ' ' * spaces + '-' * frame_width | |
380 | ||
381 | printNicely(dotline) | |
382 | for line in slicing: | |
383 | fill = step - len(line) | |
384 | screen_line = ' ' * spaces + '| ' + line + ' ' * fill + ' ' | |
385 | if slicing[-1] == line: | |
386 | screen_line = screen_line + ' >' | |
387 | else: | |
388 | screen_line = screen_line + '|' | |
389 | printNicely(screen_line) | |
390 | printNicely(dotline) | |
391 | ||
392 | ||
393 | def print_left_message(m): | |
394 | """ | |
395 | Print a message on the left of screen | |
396 | """ | |
397 | h, w = os.popen('stty size', 'r').read().split() | |
398 | w = int(w) | |
399 | frame_width = w //3 - dg['message_thread_margin'] | |
400 | step = frame_width - 2 * dg['message_thread_margin'] | |
401 | slicing = [m['text'][i:i+step] for i in range(0, len(m['text']), step)] | |
402 | spaces = dg['message_thread_margin'] | |
403 | dotline = ' ' * spaces + '-' * frame_width | |
404 | ||
405 | printNicely(dotline) | |
406 | for line in slicing: | |
407 | fill = step - len(line) | |
408 | screen_line = ' ' + line + ' ' * fill + ' |' | |
409 | if slicing[-1] == line: | |
410 | screen_line = ' ' * (spaces-1) + '< ' + screen_line | |
411 | else: | |
412 | screen_line = ' ' * spaces + '|' + screen_line | |
413 | printNicely(screen_line) | |
414 | printNicely(dotline) | |
415 | ||
416 | ||
4dc385b5 | 417 | def print_message(m): |
7500d90b VNM |
418 | """ |
419 | Print direct message | |
420 | """ | |
c37c04a9 | 421 | # Retrieve message |
7500d90b VNM |
422 | sender_screen_name = '@' + m['sender_screen_name'] |
423 | sender_name = m['sender']['name'] | |
b2cde062 | 424 | text = unescape(m['text']) |
7500d90b VNM |
425 | recipient_screen_name = '@' + m['recipient_screen_name'] |
426 | recipient_name = m['recipient']['name'] | |
427 | mid = m['id'] | |
428 | date = parser.parse(m['created_at']) | |
8b3456f9 | 429 | date = arrow.get(date).to('local').datetime |
8c7ae594 O |
430 | clock_format = '%Y/%m/%d %H:%M:%S' |
431 | try: | |
432 | clock_format = c['FORMAT']['MESSAGE']['CLOCK_FORMAT'] | |
433 | except: | |
434 | pass | |
435 | clock = date.strftime(clock_format) | |
7500d90b VNM |
436 | |
437 | # Get rainbow id | |
99b52f5f O |
438 | if mid not in c['message_dict']: |
439 | c['message_dict'].append(mid) | |
440 | rid = len(c['message_dict']) - 1 | |
441 | else: | |
442 | rid = c['message_dict'].index(mid) | |
7500d90b | 443 | |
6fa09c14 | 444 | # Draw |
8c7ae594 O |
445 | sender_name = cycle_color(sender_name) |
446 | sender_nick = color_func(c['MESSAGE']['sender'])(sender_screen_name) | |
447 | recipient_name = cycle_color(recipient_name) | |
0d9977c7 O |
448 | recipient_nick = color_func( |
449 | c['MESSAGE']['recipient'])(recipient_screen_name) | |
8c7ae594 | 450 | to = color_func(c['MESSAGE']['to'])('>>>') |
0d9977c7 O |
451 | clock = clock |
452 | id = str(rid) | |
8c7ae594 | 453 | |
c3bab4ef | 454 | text = ''.join(lmap(lambda x: x + ' ' if x == '\n' else x, text)) |
7500d90b | 455 | |
8c7ae594 O |
456 | # Load config formater |
457 | try: | |
458 | formater = c['FORMAT']['MESSAGE']['DISPLAY'] | |
0d9977c7 O |
459 | formater = sender_name.join(formater.split("#sender_name")) |
460 | formater = sender_nick.join(formater.split("#sender_nick")) | |
461 | formater = to.join(formater.split("#to")) | |
462 | formater = recipient_name.join(formater.split("#recipient_name")) | |
463 | formater = recipient_nick.join(formater.split("#recipient_nick")) | |
464 | formater = text.join(formater.split("#message")) | |
465 | # Change clock word | |
466 | word = [w for w in formater.split() if '#clock' in w][0] | |
8141aca6 | 467 | delimiter = color_func(c['MESSAGE']['clock'])( |
468 | clock.join(word.split('#clock'))) | |
0d9977c7 O |
469 | formater = delimiter.join(formater.split(word)) |
470 | # Change id word | |
471 | word = [w for w in formater.split() if '#id' in w][0] | |
472 | delimiter = color_func(c['MESSAGE']['id'])(id.join(word.split('#id'))) | |
473 | formater = delimiter.join(formater.split(word)) | |
8c7ae594 O |
474 | except: |
475 | printNicely(red('Wrong format in config.')) | |
476 | return | |
477 | ||
478 | # Draw | |
479 | printNicely(formater) | |
7500d90b VNM |
480 | |
481 | ||
fe9bb33b | 482 | def show_profile(u): |
7500d90b VNM |
483 | """ |
484 | Show a profile | |
485 | """ | |
486 | # Retrieve info | |
487 | name = u['name'] | |
488 | screen_name = u['screen_name'] | |
489 | description = u['description'] | |
490 | profile_image_url = u['profile_image_url'] | |
491 | location = u['location'] | |
492 | url = u['url'] | |
493 | created_at = u['created_at'] | |
494 | statuses_count = u['statuses_count'] | |
495 | friends_count = u['friends_count'] | |
496 | followers_count = u['followers_count'] | |
6fa09c14 | 497 | |
7500d90b | 498 | # Create content |
c075e6dc O |
499 | statuses_count = color_func( |
500 | c['PROFILE']['statuses_count'])( | |
501 | str(statuses_count) + | |
502 | ' tweets') | |
503 | friends_count = color_func( | |
504 | c['PROFILE']['friends_count'])( | |
505 | str(friends_count) + | |
506 | ' following') | |
507 | followers_count = color_func( | |
508 | c['PROFILE']['followers_count'])( | |
509 | str(followers_count) + | |
510 | ' followers') | |
7500d90b | 511 | count = statuses_count + ' ' + friends_count + ' ' + followers_count |
c075e6dc O |
512 | user = cycle_color( |
513 | name) + color_func(c['PROFILE']['nick'])(' @' + screen_name + ' : ') + count | |
514 | profile_image_raw_url = 'Profile photo: ' + \ | |
515 | color_func(c['PROFILE']['profile_image_url'])(profile_image_url) | |
7500d90b | 516 | description = ''.join( |
c3bab4ef | 517 | lmap(lambda x: x + ' ' * 4 if x == '\n' else x, description)) |
632c6fa5 O |
518 | description = color_func(c['PROFILE']['description'])(description) |
519 | location = 'Location : ' + color_func(c['PROFILE']['location'])(location) | |
520 | url = 'URL : ' + (color_func(c['PROFILE']['url'])(url) if url else '') | |
7500d90b | 521 | date = parser.parse(created_at) |
8b3456f9 | 522 | lang, encode = locale.getdefaultlocale() |
523 | clock = arrow.get(date).to('local').humanize(locale=lang) | |
632c6fa5 | 524 | clock = 'Join at ' + color_func(c['PROFILE']['clock'])(clock) |
6fa09c14 | 525 | |
7500d90b VNM |
526 | # Format |
527 | line1 = u"{u:>{uw}}".format( | |
528 | u=user, | |
529 | uw=len(user) + 2, | |
530 | ) | |
531 | line2 = u"{p:>{pw}}".format( | |
532 | p=profile_image_raw_url, | |
533 | pw=len(profile_image_raw_url) + 4, | |
534 | ) | |
535 | line3 = u"{d:>{dw}}".format( | |
536 | d=description, | |
537 | dw=len(description) + 4, | |
538 | ) | |
539 | line4 = u"{l:>{lw}}".format( | |
540 | l=location, | |
541 | lw=len(location) + 4, | |
542 | ) | |
543 | line5 = u"{u:>{uw}}".format( | |
544 | u=url, | |
545 | uw=len(url) + 4, | |
546 | ) | |
547 | line6 = u"{c:>{cw}}".format( | |
548 | c=clock, | |
549 | cw=len(clock) + 4, | |
550 | ) | |
6fa09c14 | 551 | |
7500d90b VNM |
552 | # Display |
553 | printNicely('') | |
554 | printNicely(line1) | |
fe9bb33b | 555 | if c['IMAGE_ON_TERM']: |
17bc529d | 556 | try: |
557 | response = requests.get(profile_image_url) | |
c37c04a9 | 558 | image_to_display(BytesIO(response.content)) |
17bc529d | 559 | except: |
560 | pass | |
7500d90b VNM |
561 | else: |
562 | printNicely(line2) | |
563 | for line in [line3, line4, line5, line6]: | |
564 | printNicely(line) | |
565 | printNicely('') | |
566 | ||
567 | ||
568 | def print_trends(trends): | |
569 | """ | |
570 | Display topics | |
571 | """ | |
632c6fa5 | 572 | for topic in trends[:c['TREND_MAX']]: |
7500d90b VNM |
573 | name = topic['name'] |
574 | url = topic['url'] | |
8394e34b | 575 | line = cycle_color(name) + ': ' + color_func(c['TREND']['url'])(url) |
7500d90b VNM |
576 | printNicely(line) |
577 | printNicely('') | |
2d341029 O |
578 | |
579 | ||
580 | def print_list(group): | |
581 | """ | |
582 | Display a list | |
583 | """ | |
99b52f5f | 584 | for grp in group: |
2d341029 | 585 | # Format |
99b52f5f | 586 | name = grp['full_name'] |
2d341029 | 587 | name = color_func(c['GROUP']['name'])(name + ' : ') |
99b52f5f | 588 | member = str(grp['member_count']) |
422dd385 | 589 | member = color_func(c['GROUP']['member'])(member + ' member') |
99b52f5f | 590 | subscriber = str(grp['subscriber_count']) |
422dd385 O |
591 | subscriber = color_func( |
592 | c['GROUP']['subscriber'])( | |
593 | subscriber + | |
594 | ' subscriber') | |
99b52f5f | 595 | description = grp['description'].strip() |
2d341029 | 596 | description = color_func(c['GROUP']['description'])(description) |
99b52f5f | 597 | mode = grp['mode'] |
422dd385 | 598 | mode = color_func(c['GROUP']['mode'])('Type: ' + mode) |
99b52f5f | 599 | created_at = grp['created_at'] |
2d341029 | 600 | date = parser.parse(created_at) |
8b3456f9 | 601 | lang, encode = locale.getdefaultlocale() |
602 | clock = arrow.get(date).to('local').humanize(locale=lang) | |
2d341029 O |
603 | clock = 'Created at ' + color_func(c['GROUP']['clock'])(clock) |
604 | ||
2d341029 | 605 | # Create lines |
422dd385 O |
606 | line1 = ' ' * 2 + name + member + ' ' + subscriber |
607 | line2 = ' ' * 4 + description | |
608 | line3 = ' ' * 4 + mode | |
609 | line4 = ' ' * 4 + clock | |
2d341029 O |
610 | |
611 | # Display | |
612 | printNicely('') | |
613 | printNicely(line1) | |
614 | printNicely(line2) | |
615 | printNicely(line3) | |
616 | printNicely(line4) | |
617 | ||
618 | printNicely('') | |
59262e95 O |
619 | |
620 | ||
8141aca6 | 621 | def show_calendar(month, date, rel): |
622 | """ | |
623 | Show the calendar in rainbow mode | |
624 | """ | |
625 | month = random_rainbow(month) | |
626 | date = ' '.join([cycle_color(i) for i in date.split(' ')]) | |
627 | today = str(int(os.popen('date +\'%d\'').read().strip())) | |
628 | # Display | |
629 | printNicely(month) | |
630 | printNicely(date) | |
631 | for line in rel: | |
632 | ary = line.split(' ') | |
633 | ary = lmap( | |
634 | lambda x: color_func(c['CAL']['today'])(x) | |
635 | if x == today | |
636 | else color_func(c['CAL']['days'])(x), | |
637 | ary) | |
638 | printNicely(' '.join(ary)) | |
639 | ||
640 | ||
b7c9c570 | 641 | def format_quote(tweet): |
642 | """ | |
643 | Quoting format | |
644 | """ | |
645 | # Retrieve info | |
646 | screen_name = '@' + tweet['user']['screen_name'] | |
647 | text = tweet['text'] | |
648 | # Validate quote format | |
649 | if '#owner' not in c['QUOTE_FORMAT']: | |
650 | printNicely(light_magenta('Quote should contains #owner')) | |
651 | return False | |
652 | if '#comment' not in c['QUOTE_FORMAT']: | |
653 | printNicely(light_magenta('Quote format should have #comment')) | |
654 | return False | |
655 | # Build formater | |
656 | formater = '' | |
657 | try: | |
658 | formater = c['QUOTE_FORMAT'] | |
659 | formater = screen_name.join(formater.split('#owner')) | |
660 | formater = text.join(formater.split('#tweet')) | |
661 | formater = u2str(formater) | |
662 | except: | |
663 | pass | |
664 | # Highlight like a tweet | |
4dc385b5 O |
665 | notice = formater.split() |
666 | notice = lmap( | |
b7c9c570 | 667 | lambda x: light_green(x) |
668 | if x == '#comment' | |
669 | else x, | |
4dc385b5 O |
670 | notice) |
671 | notice = lmap( | |
b7c9c570 | 672 | lambda x: color_func(c['TWEET']['rt'])(x) |
673 | if x == 'RT' | |
674 | else x, | |
4dc385b5 O |
675 | notice) |
676 | notice = lmap(lambda x: cycle_color(x) if x[0] == '@' else x, notice) | |
677 | notice = lmap( | |
b7c9c570 | 678 | lambda x: color_func(c['TWEET']['link'])(x) |
679 | if x[0:4] == 'http' | |
680 | else x, | |
4dc385b5 O |
681 | notice) |
682 | notice = lmap( | |
b7c9c570 | 683 | lambda x: color_func(c['TWEET']['hashtag'])(x) |
684 | if x.startswith('#') | |
685 | else x, | |
4dc385b5 O |
686 | notice) |
687 | notice = ' '.join(notice) | |
b7c9c570 | 688 | # Notice |
4dc385b5 | 689 | notice = light_magenta('Quoting: "') + notice + light_magenta('"') |
b7c9c570 | 690 | printNicely(notice) |
691 | return formater | |
692 | ||
693 | ||
59262e95 | 694 | # Start the color cycle |
b2cde062 | 695 | start_cycle() |