Commit | Line | Data |
---|---|---|
e9f5200b | 1 | import random |
bcb5518e | 2 | import textwrap |
e9f5200b | 3 | import itertools |
7500d90b | 4 | import requests |
8b3456f9 | 5 | import locale |
6 | import arrow | |
59262e95 | 7 | import re |
67c663f8 | 8 | import os |
7500d90b | 9 | |
2da50cc4 | 10 | from twitter.util import printNicely |
e9f5200b VNM |
11 | from functools import wraps |
12 | from pyfiglet import figlet_format | |
7500d90b | 13 | from dateutil import parser |
2da50cc4 | 14 | from .c_image import * |
7500d90b VNM |
15 | from .colors import * |
16 | from .config import * | |
c3bab4ef | 17 | from .py3patch import * |
841260fe | 18 | from .emoji import * |
c3bab4ef | 19 | |
99b52f5f O |
20 | # Draw global variables |
21 | dg = {} | |
1fdd6a5c | 22 | |
422dd385 | 23 | |
e9f5200b VNM |
24 | def init_cycle(): |
25 | """ | |
26 | Init the cycle | |
27 | """ | |
28 | colors_shuffle = [globals()[i.encode('utf8')] | |
fa6e062d O |
29 | if not str(i).isdigit() |
30 | else term_color(int(i)) | |
422dd385 | 31 | for i in c['CYCLE_COLOR']] |
5f22104f | 32 | return itertools.cycle(colors_shuffle) |
1fdd6a5c | 33 | |
e9f5200b | 34 | |
59262e95 | 35 | def start_cycle(): |
2359c276 VNM |
36 | """ |
37 | Notify from rainbow | |
38 | """ | |
99b52f5f O |
39 | dg['cyc'] = init_cycle() |
40 | dg['cache'] = {} | |
e5455a30 | 41 | dg['humanize_unsupported'] = False |
2359c276 VNM |
42 | |
43 | ||
e9f5200b VNM |
44 | def order_rainbow(s): |
45 | """ | |
46 | Print a string with ordered color with each character | |
47 | """ | |
5f22104f | 48 | colors_shuffle = [globals()[i.encode('utf8')] |
fa6e062d O |
49 | if not str(i).isdigit() |
50 | else term_color(int(i)) | |
422dd385 | 51 | for i in c['CYCLE_COLOR']] |
5f22104f | 52 | colored = [colors_shuffle[i % 7](s[i]) for i in xrange(len(s))] |
c3bab4ef | 53 | return ''.join(colored) |
e9f5200b VNM |
54 | |
55 | ||
56 | def random_rainbow(s): | |
57 | """ | |
58 | Print a string with random color with each character | |
59 | """ | |
5f22104f | 60 | colors_shuffle = [globals()[i.encode('utf8')] |
fa6e062d O |
61 | if not str(i).isdigit() |
62 | else term_color(int(i)) | |
422dd385 | 63 | for i in c['CYCLE_COLOR']] |
5f22104f | 64 | colored = [random.choice(colors_shuffle)(i) for i in s] |
c3bab4ef | 65 | return ''.join(colored) |
e9f5200b VNM |
66 | |
67 | ||
68 | def Memoize(func): | |
69 | """ | |
70 | Memoize decorator | |
71 | """ | |
e9f5200b VNM |
72 | @wraps(func) |
73 | def wrapper(*args): | |
99b52f5f O |
74 | if args not in dg['cache']: |
75 | dg['cache'][args] = func(*args) | |
76 | return dg['cache'][args] | |
e9f5200b VNM |
77 | return wrapper |
78 | ||
79 | ||
80 | @Memoize | |
81 | def cycle_color(s): | |
82 | """ | |
83 | Cycle the colors_shuffle | |
84 | """ | |
99b52f5f | 85 | return next(dg['cyc'])(s) |
e9f5200b VNM |
86 | |
87 | ||
88 | def ascii_art(text): | |
89 | """ | |
90 | Draw the Ascii Art | |
91 | """ | |
92 | fi = figlet_format(text, font='doom') | |
93 | print('\n'.join( | |
99b52f5f | 94 | [next(dg['cyc'])(i) for i in fi.split('\n')] |
e9f5200b VNM |
95 | )) |
96 | ||
97 | ||
92685d21 | 98 | def check_config(): |
ceec8593 | 99 | """ |
100 | Check if config is changed | |
101 | """ | |
102 | changed = False | |
103 | data = get_all_config() | |
104 | for key in c: | |
105 | if key in data: | |
106 | if data[key] != c[key]: | |
107 | changed = True | |
108 | if changed: | |
109 | reload_config() | |
110 | ||
111 | ||
112 | def validate_theme(theme): | |
113 | """ | |
114 | Validate a theme exists or not | |
115 | """ | |
116 | # Theme changed check | |
117 | files = os.listdir(os.path.dirname(__file__) + '/colorset') | |
118 | themes = [f.split('.')[0] for f in files if f.split('.')[-1] == 'json'] | |
119 | return theme in themes | |
92685d21 | 120 | |
121 | ||
99b52f5f | 122 | def reload_theme(value, prev): |
4cf86720 VNM |
123 | """ |
124 | Check current theme and update if necessary | |
125 | """ | |
99b52f5f | 126 | if value != prev: |
1f2f6159 | 127 | config = os.path.dirname( |
99b52f5f | 128 | __file__) + '/colorset/' + value + '.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] | |
99b52f5f | 134 | # Restart color cycle and update config |
ceec8593 | 135 | start_cycle() |
99b52f5f O |
136 | set_config('THEME', value) |
137 | return value | |
138 | return prev | |
7500d90b | 139 | |
fe08f905 VNM |
140 | |
141 | def color_func(func_name): | |
142 | """ | |
143 | Call color function base on name | |
144 | """ | |
fa6e062d O |
145 | if str(func_name).isdigit(): |
146 | return term_color(int(func_name)) | |
c3bab4ef | 147 | return globals()[func_name] |
fe08f905 VNM |
148 | |
149 | ||
e5455a30 O |
150 | def fallback_humanize(date, fallback_format=None, use_fallback=False): |
151 | """ | |
152 | Format date with arrow and a fallback format | |
153 | """ | |
154 | # Convert to local timezone | |
155 | date = arrow.get(date).to('local') | |
156 | # Set default fallback format | |
157 | if not fallback_format: | |
158 | fallback_format = '%Y/%m/%d %H:%M:%S' | |
159 | # Determine using fallback format or not by a variable | |
160 | if use_fallback: | |
161 | return date.datetime.strftime(fallback_format) | |
162 | try: | |
163 | # Use Arrow's humanize function | |
164 | lang, encode = locale.getdefaultlocale() | |
165 | clock = date.humanize(locale=lang) | |
166 | except: | |
167 | # Notice at the 1st time only | |
168 | if not dg['humanize_unsupported']: | |
169 | dg['humanize_unsupported'] = True | |
170 | printNicely( | |
171 | light_magenta('Humanized date display method does not support your $LC_ALL.')) | |
172 | # Fallback when LC_ALL is not supported | |
173 | clock = date.datetime.strftime(fallback_format) | |
174 | return clock | |
175 | ||
176 | ||
99cd1fba | 177 | def draw(t, keyword=None, humanize=True, noti=False, fil=[], ig=[]): |
7500d90b VNM |
178 | """ |
179 | Draw the rainbow | |
180 | """ | |
99b52f5f | 181 | # Check config |
92685d21 | 182 | check_config() |
c37c04a9 | 183 | |
7500d90b VNM |
184 | # Retrieve tweet |
185 | tid = t['id'] | |
606def7e | 186 | text = t['text'] |
7500d90b VNM |
187 | screen_name = t['user']['screen_name'] |
188 | name = t['user']['name'] | |
189 | created_at = t['created_at'] | |
190 | favorited = t['favorited'] | |
318cdd67 O |
191 | retweet_count = t['retweet_count'] |
192 | favorite_count = t['favorite_count'] | |
413857b5 | 193 | client = t['source'] |
7500d90b | 194 | date = parser.parse(created_at) |
e5455a30 O |
195 | try: |
196 | clock_format = c['FORMAT']['TWEET']['CLOCK_FORMAT'] | |
197 | except: | |
198 | clock_format = '%Y/%m/%d %H:%M:%S' | |
199 | clock = fallback_humanize(date, clock_format, not humanize) | |
7500d90b | 200 | |
606def7e BR |
201 | # Pull extended retweet text |
202 | try: | |
18df6e7f O |
203 | text = 'RT @' + t['retweeted_status']['user']['screen_name'] + ': ' +\ |
204 | t['retweeted_status']['text'] | |
38a6dc30 O |
205 | # Display as a notification |
206 | target = t['retweeted_status']['user']['screen_name'] | |
207 | if all([target == c['original_name'], not noti]): | |
208 | # Add to evens for 'notification' command | |
209 | t['event'] = 'retweet' | |
210 | c['events'].append(t) | |
211 | notify_retweet(t) | |
212 | return | |
606def7e BR |
213 | except: |
214 | pass | |
215 | ||
18df6e7f | 216 | # Unescape HTML character |
606def7e BR |
217 | text = unescape(text) |
218 | ||
413857b5 O |
219 | # Get client name |
220 | try: | |
221 | client = client.split('>')[-2].split('<')[0] | |
222 | except: | |
223 | client = None | |
224 | ||
7500d90b VNM |
225 | # Get expanded url |
226 | try: | |
227 | expanded_url = [] | |
228 | url = [] | |
229 | urls = t['entities']['urls'] | |
230 | for u in urls: | |
231 | expanded_url.append(u['expanded_url']) | |
232 | url.append(u['url']) | |
233 | except: | |
234 | expanded_url = None | |
235 | url = None | |
236 | ||
237 | # Get media | |
238 | try: | |
239 | media_url = [] | |
240 | media = t['entities']['media'] | |
241 | for m in media: | |
242 | media_url.append(m['media_url']) | |
243 | except: | |
244 | media_url = None | |
245 | ||
246 | # Filter and ignore | |
37cf396a | 247 | mytweet = screen_name == c['original_name'] |
7500d90b | 248 | screen_name = '@' + screen_name |
e3927852 O |
249 | fil = list(set((fil or []) + c['ONLY_LIST'])) |
250 | ig = list(set((ig or []) + c['IGNORE_LIST'])) | |
7500d90b VNM |
251 | if fil and screen_name not in fil: |
252 | return | |
253 | if ig and screen_name in ig: | |
254 | return | |
255 | ||
256 | # Get rainbow id | |
99b52f5f O |
257 | if tid not in c['tweet_dict']: |
258 | c['tweet_dict'].append(tid) | |
259 | rid = len(c['tweet_dict']) - 1 | |
260 | else: | |
261 | rid = c['tweet_dict'].index(tid) | |
7500d90b VNM |
262 | |
263 | # Format info | |
8c7ae594 | 264 | name = cycle_color(name) |
571ea706 O |
265 | if mytweet: |
266 | nick = color_func(c['TWEET']['mynick'])(screen_name) | |
267 | else: | |
268 | nick = color_func(c['TWEET']['nick'])(screen_name) | |
0d9977c7 O |
269 | clock = clock |
270 | id = str(rid) | |
8c7ae594 | 271 | fav = '' |
7500d90b | 272 | if favorited: |
8c7ae594 O |
273 | fav = color_func(c['TWEET']['favorited'])(u'\u2605') |
274 | ||
7500d90b VNM |
275 | tweet = text.split() |
276 | # Replace url | |
277 | if expanded_url: | |
13e6b275 | 278 | for index in xrange(len(expanded_url)): |
c3bab4ef | 279 | tweet = lmap( |
8141aca6 | 280 | lambda x: expanded_url[index] |
281 | if x == url[index] | |
282 | else x, | |
7500d90b VNM |
283 | tweet) |
284 | # Highlight RT | |
c3bab4ef | 285 | tweet = lmap( |
8141aca6 | 286 | lambda x: color_func(c['TWEET']['rt'])(x) |
287 | if x == 'RT' | |
288 | else x, | |
c075e6dc | 289 | tweet) |
7500d90b | 290 | # Highlight screen_name |
c3bab4ef | 291 | tweet = lmap(lambda x: cycle_color(x) if x[0] == '@' else x, tweet) |
7500d90b | 292 | # Highlight link |
c3bab4ef | 293 | tweet = lmap( |
8141aca6 | 294 | lambda x: color_func(c['TWEET']['link'])(x) |
37cf396a | 295 | if x.startswith('http') |
8141aca6 | 296 | else x, |
c075e6dc | 297 | tweet) |
4c025026 | 298 | # Highlight hashtag |
299 | tweet = lmap( | |
8141aca6 | 300 | lambda x: color_func(c['TWEET']['hashtag'])(x) |
301 | if x.startswith('#') | |
302 | else x, | |
4c025026 | 303 | tweet) |
37cf396a O |
304 | # Highlight my tweet |
305 | if mytweet: | |
306 | tweet = [color_func(c['TWEET']['mytweet'])(x) | |
307 | for x in tweet | |
308 | if not any([ | |
309 | x == 'RT', | |
310 | x.startswith('http'), | |
311 | x.startswith('#')]) | |
312 | ] | |
59262e95 | 313 | # Highlight keyword |
7500d90b | 314 | tweet = ' '.join(tweet) |
59262e95 | 315 | if keyword: |
a8c5fce4 | 316 | roj = re.search(keyword, tweet, re.IGNORECASE) |
59262e95 O |
317 | if roj: |
318 | occur = roj.group() | |
319 | ary = tweet.split(occur) | |
b13c6a0d | 320 | delimiter = color_func(c['TWEET']['keyword'])(occur) |
321 | tweet = delimiter.join(ary) | |
7500d90b | 322 | |
8c7ae594 | 323 | # Load config formater |
318cdd67 | 324 | formater = '' |
8c7ae594 O |
325 | try: |
326 | formater = c['FORMAT']['TWEET']['DISPLAY'] | |
7c437a0f O |
327 | formater = name.join(formater.split('#name')) |
328 | formater = nick.join(formater.split('#nick')) | |
329 | formater = fav.join(formater.split('#fav')) | |
330 | formater = tweet.join(formater.split('#tweet')) | |
0d9977c7 | 331 | # Change clock word |
03c0d30b | 332 | word = [wo for wo in formater.split() if '#clock' in wo][0] |
8141aca6 | 333 | delimiter = color_func(c['TWEET']['clock'])( |
334 | clock.join(word.split('#clock'))) | |
0d9977c7 O |
335 | formater = delimiter.join(formater.split(word)) |
336 | # Change id word | |
03c0d30b | 337 | word = [wo for wo in formater.split() if '#id' in wo][0] |
0d9977c7 O |
338 | delimiter = color_func(c['TWEET']['id'])(id.join(word.split('#id'))) |
339 | formater = delimiter.join(formater.split(word)) | |
413857b5 O |
340 | # Change client word |
341 | word = [wo for wo in formater.split() if '#client' in wo][0] | |
342 | delimiter = color_func(c['TWEET']['client'])( | |
343 | client.join(word.split('#client'))) | |
344 | formater = delimiter.join(formater.split(word)) | |
318cdd67 | 345 | # Change retweet count word |
03c0d30b | 346 | word = [wo for wo in formater.split() if '#rt_count' in wo][0] |
318cdd67 O |
347 | delimiter = color_func(c['TWEET']['retweet_count'])( |
348 | str(retweet_count).join(word.split('#rt_count'))) | |
349 | formater = delimiter.join(formater.split(word)) | |
350 | # Change favorites count word | |
03c0d30b | 351 | word = [wo for wo in formater.split() if '#fa_count' in wo][0] |
318cdd67 O |
352 | delimiter = color_func(c['TWEET']['favorite_count'])( |
353 | str(favorite_count).join(word.split('#fa_count'))) | |
354 | formater = delimiter.join(formater.split(word)) | |
841260fe | 355 | formater = emojize(formater) |
8c7ae594 | 356 | except: |
318cdd67 | 357 | pass |
7500d90b | 358 | |
99cd1fba O |
359 | # Add spaces in begining of line if this is inside a notification |
360 | if noti: | |
361 | formater = '\n '.join(formater.split('\n')) | |
38a6dc30 O |
362 | # Reformat |
363 | if formater.startswith('\n'): | |
364 | formater = formater[1:] | |
99cd1fba | 365 | |
8c7ae594 O |
366 | # Draw |
367 | printNicely(formater) | |
7500d90b VNM |
368 | |
369 | # Display Image | |
fe9bb33b | 370 | if c['IMAGE_ON_TERM'] and media_url: |
7500d90b | 371 | for mu in media_url: |
17bc529d | 372 | try: |
373 | response = requests.get(mu) | |
77f1d210 | 374 | image_to_display(BytesIO(response.content)) |
375 | except Exception: | |
17bc529d | 376 | printNicely(red('Sorry, image link is broken')) |
7500d90b VNM |
377 | |
378 | ||
67c663f8 O |
379 | def print_threads(d): |
380 | """ | |
381 | Print threads of messages | |
382 | """ | |
383 | id = 1 | |
384 | rel = {} | |
385 | for partner in d: | |
386 | messages = d[partner] | |
387 | count = len(messages) | |
388 | screen_name = '@' + partner[0] | |
389 | name = partner[1] | |
390 | screen_name = color_func(c['MESSAGE']['partner'])(screen_name) | |
391 | name = cycle_color(name) | |
03c0d30b | 392 | thread_id = color_func(c['MESSAGE']['id'])('thread_id:' + str(id)) |
393 | line = ' ' * 2 + name + ' ' + screen_name + \ | |
67c663f8 O |
394 | ' (' + str(count) + ' message) ' + thread_id |
395 | printNicely(line) | |
396 | rel[id] = partner | |
397 | id += 1 | |
398 | dg['thread'] = d | |
399 | return rel | |
400 | ||
401 | ||
402 | def print_thread(partner, me_nick, me_name): | |
403 | """ | |
404 | Print a thread of messages | |
405 | """ | |
406 | # Sort messages by time | |
407 | messages = dg['thread'][partner] | |
03c0d30b | 408 | messages.sort(key=lambda x: parser.parse(x['created_at'])) |
409 | # Use legacy display on non-ascii text message | |
bcb5518e | 410 | ms = [m['text'] for m in messages] |
411 | ums = [m for m in ms if not all(ord(c) < 128 for c in m)] | |
412 | if ums: | |
03c0d30b | 413 | for m in messages: |
414 | print_message(m) | |
415 | printNicely('') | |
416 | return | |
417 | # Print the first line | |
418 | dg['frame_margin'] = margin = 2 | |
419 | partner_nick = partner[0] | |
420 | partner_name = partner[1] | |
421 | left_size = len(partner_nick) + len(partner_name) + 2 | |
422 | right_size = len(me_nick) + len(me_name) + 2 | |
423 | partner_nick = color_func(c['MESSAGE']['partner'])('@' + partner_nick) | |
424 | partner_name = cycle_color(partner_name) | |
67c663f8 O |
425 | me_screen_name = color_func(c['MESSAGE']['me'])('@' + me_nick) |
426 | me_name = cycle_color(me_name) | |
03c0d30b | 427 | left = ' ' * margin + partner_name + ' ' + partner_nick |
67c663f8 O |
428 | right = me_name + ' ' + me_screen_name + ' ' * margin |
429 | h, w = os.popen('stty size', 'r').read().split() | |
430 | w = int(w) | |
03c0d30b | 431 | line = '{}{}{}'.format( |
432 | left, ' ' * (w - left_size - right_size - 2 * margin), right) | |
67c663f8 O |
433 | printNicely('') |
434 | printNicely(line) | |
435 | printNicely('') | |
67c663f8 O |
436 | # Print messages |
437 | for m in messages: | |
438 | if m['sender_screen_name'] == me_nick: | |
439 | print_right_message(m) | |
440 | elif m['recipient_screen_name'] == me_nick: | |
441 | print_left_message(m) | |
442 | ||
443 | ||
444 | def print_right_message(m): | |
445 | """ | |
446 | Print a message on the right of screen | |
447 | """ | |
448 | h, w = os.popen('stty size', 'r').read().split() | |
449 | w = int(w) | |
03c0d30b | 450 | frame_width = w // 3 - dg['frame_margin'] |
37cf396a | 451 | frame_width = max(c['THREAD_MIN_WIDTH'], frame_width) |
03c0d30b | 452 | step = frame_width - 2 * dg['frame_margin'] |
bcb5518e | 453 | slicing = textwrap.wrap(m['text'], step) |
03c0d30b | 454 | spaces = w - frame_width - dg['frame_margin'] |
67c663f8 | 455 | dotline = ' ' * spaces + '-' * frame_width |
03c0d30b | 456 | dotline = color_func(c['MESSAGE']['me_frame'])(dotline) |
223d2e05 | 457 | # Draw the frame |
67c663f8 O |
458 | printNicely(dotline) |
459 | for line in slicing: | |
460 | fill = step - len(line) | |
461 | screen_line = ' ' * spaces + '| ' + line + ' ' * fill + ' ' | |
462 | if slicing[-1] == line: | |
463 | screen_line = screen_line + ' >' | |
464 | else: | |
465 | screen_line = screen_line + '|' | |
03c0d30b | 466 | screen_line = color_func(c['MESSAGE']['me_frame'])(screen_line) |
67c663f8 O |
467 | printNicely(screen_line) |
468 | printNicely(dotline) | |
03c0d30b | 469 | # Format clock |
223d2e05 O |
470 | date = parser.parse(m['created_at']) |
471 | date = arrow.get(date).to('local').datetime | |
472 | clock_format = '%Y/%m/%d %H:%M:%S' | |
473 | try: | |
474 | clock_format = c['FORMAT']['MESSAGE']['CLOCK_FORMAT'] | |
475 | except: | |
476 | pass | |
477 | clock = date.strftime(clock_format) | |
03c0d30b | 478 | # Format id |
223d2e05 O |
479 | if m['id'] not in c['message_dict']: |
480 | c['message_dict'].append(m['id']) | |
481 | rid = len(c['message_dict']) - 1 | |
482 | else: | |
483 | rid = c['message_dict'].index(m['id']) | |
03c0d30b | 484 | id = str(rid) |
485 | # Print meta | |
486 | formater = '' | |
487 | try: | |
488 | virtual_meta = formater = c['THREAD_META_RIGHT'] | |
489 | virtual_meta = clock.join(virtual_meta.split('#clock')) | |
490 | virtual_meta = id.join(virtual_meta.split('#id')) | |
491 | # Change clock word | |
492 | word = [wo for wo in formater.split() if '#clock' in wo][0] | |
493 | delimiter = color_func(c['MESSAGE']['clock'])( | |
494 | clock.join(word.split('#clock'))) | |
495 | formater = delimiter.join(formater.split(word)) | |
496 | # Change id word | |
497 | word = [wo for wo in formater.split() if '#id' in wo][0] | |
498 | delimiter = color_func(c['MESSAGE']['id'])(id.join(word.split('#id'))) | |
499 | formater = delimiter.join(formater.split(word)) | |
841260fe | 500 | formater = emojize(formater) |
03c0d30b | 501 | except Exception: |
502 | printNicely(red('Wrong format in config.')) | |
503 | return | |
504 | meta = formater | |
505 | line = ' ' * (w - len(virtual_meta) - dg['frame_margin']) + meta | |
223d2e05 | 506 | printNicely(line) |
67c663f8 O |
507 | |
508 | ||
509 | def print_left_message(m): | |
510 | """ | |
511 | Print a message on the left of screen | |
512 | """ | |
513 | h, w = os.popen('stty size', 'r').read().split() | |
514 | w = int(w) | |
03c0d30b | 515 | frame_width = w // 3 - dg['frame_margin'] |
37cf396a | 516 | frame_width = max(c['THREAD_MIN_WIDTH'], frame_width) |
03c0d30b | 517 | step = frame_width - 2 * dg['frame_margin'] |
bcb5518e | 518 | slicing = textwrap.wrap(m['text'], step) |
03c0d30b | 519 | spaces = dg['frame_margin'] |
67c663f8 | 520 | dotline = ' ' * spaces + '-' * frame_width |
03c0d30b | 521 | dotline = color_func(c['MESSAGE']['partner_frame'])(dotline) |
223d2e05 | 522 | # Draw the frame |
67c663f8 O |
523 | printNicely(dotline) |
524 | for line in slicing: | |
525 | fill = step - len(line) | |
526 | screen_line = ' ' + line + ' ' * fill + ' |' | |
527 | if slicing[-1] == line: | |
03c0d30b | 528 | screen_line = ' ' * (spaces - 1) + '< ' + screen_line |
67c663f8 O |
529 | else: |
530 | screen_line = ' ' * spaces + '|' + screen_line | |
03c0d30b | 531 | screen_line = color_func(c['MESSAGE']['partner_frame'])(screen_line) |
67c663f8 O |
532 | printNicely(screen_line) |
533 | printNicely(dotline) | |
03c0d30b | 534 | # Format clock |
223d2e05 O |
535 | date = parser.parse(m['created_at']) |
536 | date = arrow.get(date).to('local').datetime | |
537 | clock_format = '%Y/%m/%d %H:%M:%S' | |
538 | try: | |
539 | clock_format = c['FORMAT']['MESSAGE']['CLOCK_FORMAT'] | |
540 | except: | |
541 | pass | |
542 | clock = date.strftime(clock_format) | |
03c0d30b | 543 | # Format id |
223d2e05 O |
544 | if m['id'] not in c['message_dict']: |
545 | c['message_dict'].append(m['id']) | |
546 | rid = len(c['message_dict']) - 1 | |
547 | else: | |
548 | rid = c['message_dict'].index(m['id']) | |
03c0d30b | 549 | id = str(rid) |
550 | # Print meta | |
551 | formater = '' | |
552 | try: | |
553 | virtual_meta = formater = c['THREAD_META_LEFT'] | |
554 | virtual_meta = clock.join(virtual_meta.split('#clock')) | |
555 | virtual_meta = id.join(virtual_meta.split('#id')) | |
556 | # Change clock word | |
557 | word = [wo for wo in formater.split() if '#clock' in wo][0] | |
558 | delimiter = color_func(c['MESSAGE']['clock'])( | |
559 | clock.join(word.split('#clock'))) | |
560 | formater = delimiter.join(formater.split(word)) | |
561 | # Change id word | |
562 | word = [wo for wo in formater.split() if '#id' in wo][0] | |
563 | delimiter = color_func(c['MESSAGE']['id'])(id.join(word.split('#id'))) | |
564 | formater = delimiter.join(formater.split(word)) | |
841260fe | 565 | formater = emojize(formater) |
03c0d30b | 566 | except Exception: |
567 | printNicely(red('Wrong format in config.')) | |
568 | return | |
569 | meta = formater | |
570 | line = ' ' * dg['frame_margin'] + meta | |
223d2e05 | 571 | printNicely(line) |
67c663f8 O |
572 | |
573 | ||
4dc385b5 | 574 | def print_message(m): |
7500d90b VNM |
575 | """ |
576 | Print direct message | |
577 | """ | |
c37c04a9 | 578 | # Retrieve message |
7500d90b VNM |
579 | sender_screen_name = '@' + m['sender_screen_name'] |
580 | sender_name = m['sender']['name'] | |
b2cde062 | 581 | text = unescape(m['text']) |
7500d90b VNM |
582 | recipient_screen_name = '@' + m['recipient_screen_name'] |
583 | recipient_name = m['recipient']['name'] | |
584 | mid = m['id'] | |
585 | date = parser.parse(m['created_at']) | |
8b3456f9 | 586 | date = arrow.get(date).to('local').datetime |
8c7ae594 O |
587 | clock_format = '%Y/%m/%d %H:%M:%S' |
588 | try: | |
589 | clock_format = c['FORMAT']['MESSAGE']['CLOCK_FORMAT'] | |
590 | except: | |
591 | pass | |
592 | clock = date.strftime(clock_format) | |
7500d90b VNM |
593 | |
594 | # Get rainbow id | |
99b52f5f O |
595 | if mid not in c['message_dict']: |
596 | c['message_dict'].append(mid) | |
597 | rid = len(c['message_dict']) - 1 | |
598 | else: | |
599 | rid = c['message_dict'].index(mid) | |
7500d90b | 600 | |
6fa09c14 | 601 | # Draw |
8c7ae594 O |
602 | sender_name = cycle_color(sender_name) |
603 | sender_nick = color_func(c['MESSAGE']['sender'])(sender_screen_name) | |
604 | recipient_name = cycle_color(recipient_name) | |
0d9977c7 O |
605 | recipient_nick = color_func( |
606 | c['MESSAGE']['recipient'])(recipient_screen_name) | |
8c7ae594 | 607 | to = color_func(c['MESSAGE']['to'])('>>>') |
0d9977c7 O |
608 | clock = clock |
609 | id = str(rid) | |
8c7ae594 | 610 | |
c3bab4ef | 611 | text = ''.join(lmap(lambda x: x + ' ' if x == '\n' else x, text)) |
7500d90b | 612 | |
8c7ae594 O |
613 | # Load config formater |
614 | try: | |
615 | formater = c['FORMAT']['MESSAGE']['DISPLAY'] | |
0d9977c7 O |
616 | formater = sender_name.join(formater.split("#sender_name")) |
617 | formater = sender_nick.join(formater.split("#sender_nick")) | |
618 | formater = to.join(formater.split("#to")) | |
619 | formater = recipient_name.join(formater.split("#recipient_name")) | |
620 | formater = recipient_nick.join(formater.split("#recipient_nick")) | |
621 | formater = text.join(formater.split("#message")) | |
622 | # Change clock word | |
03c0d30b | 623 | word = [wo for wo in formater.split() if '#clock' in wo][0] |
8141aca6 | 624 | delimiter = color_func(c['MESSAGE']['clock'])( |
625 | clock.join(word.split('#clock'))) | |
0d9977c7 O |
626 | formater = delimiter.join(formater.split(word)) |
627 | # Change id word | |
03c0d30b | 628 | word = [wo for wo in formater.split() if '#id' in wo][0] |
0d9977c7 O |
629 | delimiter = color_func(c['MESSAGE']['id'])(id.join(word.split('#id'))) |
630 | formater = delimiter.join(formater.split(word)) | |
841260fe | 631 | formater = emojize(formater) |
8c7ae594 O |
632 | except: |
633 | printNicely(red('Wrong format in config.')) | |
634 | return | |
635 | ||
636 | # Draw | |
637 | printNicely(formater) | |
7500d90b VNM |
638 | |
639 | ||
38a6dc30 O |
640 | def notify_retweet(t): |
641 | """ | |
642 | Notify a retweet | |
643 | """ | |
644 | source = t['user'] | |
645 | created_at = t['created_at'] | |
646 | # Format | |
647 | source_user = cycle_color(source['name']) + \ | |
648 | color_func(c['NOTIFICATION']['source_nick'])( | |
649 | ' @' + source['screen_name']) | |
650 | notify = color_func(c['NOTIFICATION']['notify'])( | |
651 | 'retweeted your tweet') | |
652 | date = parser.parse(created_at) | |
e5455a30 | 653 | clock = fallback_humanize(date) |
38a6dc30 O |
654 | clock = color_func(c['NOTIFICATION']['clock'])(clock) |
655 | meta = c['NOTIFY_FORMAT'] | |
656 | meta = source_user.join(meta.split('#source_user')) | |
657 | meta = notify.join(meta.split('#notify')) | |
658 | meta = clock.join(meta.split('#clock')) | |
104619ce | 659 | meta = emojize(meta) |
38a6dc30 O |
660 | # Output |
661 | printNicely('') | |
662 | printNicely(meta) | |
663 | draw(t=t['retweeted_status'], noti=True) | |
664 | ||
665 | ||
99cd1fba O |
666 | def notify_favorite(e): |
667 | """ | |
668 | Notify a favorite event | |
669 | """ | |
670 | # Retrieve info | |
671 | target = e['target'] | |
672 | if target['screen_name'] != c['original_name']: | |
673 | return | |
674 | source = e['source'] | |
675 | target_object = e['target_object'] | |
676 | created_at = e['created_at'] | |
677 | # Format | |
678 | source_user = cycle_color(source['name']) + \ | |
679 | color_func(c['NOTIFICATION']['source_nick'])( | |
680 | ' @' + source['screen_name']) | |
38a6dc30 O |
681 | notify = color_func(c['NOTIFICATION']['notify'])( |
682 | 'favorited your tweet') | |
99cd1fba | 683 | date = parser.parse(created_at) |
e5455a30 | 684 | clock = fallback_humanize(date) |
99cd1fba | 685 | clock = color_func(c['NOTIFICATION']['clock'])(clock) |
38a6dc30 O |
686 | meta = c['NOTIFY_FORMAT'] |
687 | meta = source_user.join(meta.split('#source_user')) | |
688 | meta = notify.join(meta.split('#notify')) | |
689 | meta = clock.join(meta.split('#clock')) | |
104619ce | 690 | meta = emojize(meta) |
99cd1fba O |
691 | # Output |
692 | printNicely('') | |
693 | printNicely(meta) | |
694 | draw(t=target_object, noti=True) | |
695 | ||
696 | ||
697 | def notify_unfavorite(e): | |
698 | """ | |
699 | Notify a unfavorite event | |
700 | """ | |
701 | # Retrieve info | |
702 | target = e['target'] | |
703 | if target['screen_name'] != c['original_name']: | |
704 | return | |
705 | source = e['source'] | |
706 | target_object = e['target_object'] | |
707 | created_at = e['created_at'] | |
708 | # Format | |
709 | source_user = cycle_color(source['name']) + \ | |
710 | color_func(c['NOTIFICATION']['source_nick'])( | |
711 | ' @' + source['screen_name']) | |
712 | notify = color_func(c['NOTIFICATION']['notify'])( | |
38a6dc30 | 713 | 'unfavorited your tweet') |
99cd1fba | 714 | date = parser.parse(created_at) |
e5455a30 | 715 | clock = fallback_humanize(date) |
99cd1fba | 716 | clock = color_func(c['NOTIFICATION']['clock'])(clock) |
38a6dc30 O |
717 | meta = c['NOTIFY_FORMAT'] |
718 | meta = source_user.join(meta.split('#source_user')) | |
719 | meta = notify.join(meta.split('#notify')) | |
720 | meta = clock.join(meta.split('#clock')) | |
104619ce | 721 | meta = emojize(meta) |
99cd1fba O |
722 | # Output |
723 | printNicely('') | |
724 | printNicely(meta) | |
725 | draw(t=target_object, noti=True) | |
726 | ||
727 | ||
728 | def notify_follow(e): | |
729 | """ | |
730 | Notify a follow event | |
731 | """ | |
732 | # Retrieve info | |
733 | target = e['target'] | |
734 | if target['screen_name'] != c['original_name']: | |
735 | return | |
736 | source = e['source'] | |
737 | created_at = e['created_at'] | |
738 | # Format | |
739 | source_user = cycle_color(source['name']) + \ | |
740 | color_func(c['NOTIFICATION']['source_nick'])( | |
741 | ' @' + source['screen_name']) | |
38a6dc30 O |
742 | notify = color_func(c['NOTIFICATION']['notify'])( |
743 | 'followed you') | |
99cd1fba | 744 | date = parser.parse(created_at) |
e5455a30 | 745 | clock = fallback_humanize(date) |
99cd1fba | 746 | clock = color_func(c['NOTIFICATION']['clock'])(clock) |
38a6dc30 O |
747 | meta = c['NOTIFY_FORMAT'] |
748 | meta = source_user.join(meta.split('#source_user')) | |
749 | meta = notify.join(meta.split('#notify')) | |
750 | meta = clock.join(meta.split('#clock')) | |
104619ce | 751 | meta = emojize(meta) |
99cd1fba O |
752 | # Output |
753 | printNicely('') | |
754 | printNicely(meta) | |
755 | ||
756 | ||
757 | def notify_list_member_added(e): | |
758 | """ | |
759 | Notify a list_member_added event | |
760 | """ | |
761 | # Retrieve info | |
762 | target = e['target'] | |
763 | if target['screen_name'] != c['original_name']: | |
764 | return | |
765 | source = e['source'] | |
766 | target_object = [e['target_object']] # list of Twitter list | |
767 | created_at = e['created_at'] | |
768 | # Format | |
769 | source_user = cycle_color(source['name']) + \ | |
770 | color_func(c['NOTIFICATION']['source_nick'])( | |
771 | ' @' + source['screen_name']) | |
38a6dc30 O |
772 | notify = color_func(c['NOTIFICATION']['notify'])( |
773 | 'added you to a list') | |
99cd1fba | 774 | date = parser.parse(created_at) |
e5455a30 | 775 | clock = fallback_humanize(date) |
99cd1fba | 776 | clock = color_func(c['NOTIFICATION']['clock'])(clock) |
38a6dc30 O |
777 | meta = c['NOTIFY_FORMAT'] |
778 | meta = source_user.join(meta.split('#source_user')) | |
779 | meta = notify.join(meta.split('#notify')) | |
780 | meta = clock.join(meta.split('#clock')) | |
104619ce | 781 | meta = emojize(meta) |
99cd1fba O |
782 | # Output |
783 | printNicely('') | |
784 | printNicely(meta) | |
785 | print_list(target_object, noti=True) | |
786 | ||
787 | ||
788 | def notify_list_member_removed(e): | |
789 | """ | |
790 | Notify a list_member_removed event | |
791 | """ | |
792 | # Retrieve info | |
793 | target = e['target'] | |
794 | if target['screen_name'] != c['original_name']: | |
795 | return | |
796 | source = e['source'] | |
797 | target_object = [e['target_object']] # list of Twitter list | |
798 | created_at = e['created_at'] | |
799 | # Format | |
800 | source_user = cycle_color(source['name']) + \ | |
801 | color_func(c['NOTIFICATION']['source_nick'])( | |
802 | ' @' + source['screen_name']) | |
803 | notify = color_func(c['NOTIFICATION']['notify'])( | |
38a6dc30 | 804 | 'removed you from a list') |
99cd1fba | 805 | date = parser.parse(created_at) |
e5455a30 | 806 | clock = fallback_humanize(date) |
99cd1fba | 807 | clock = color_func(c['NOTIFICATION']['clock'])(clock) |
38a6dc30 O |
808 | meta = c['NOTIFY_FORMAT'] |
809 | meta = source_user.join(meta.split('#source_user')) | |
810 | meta = notify.join(meta.split('#notify')) | |
811 | meta = clock.join(meta.split('#clock')) | |
104619ce | 812 | meta = emojize(meta) |
99cd1fba O |
813 | # Output |
814 | printNicely('') | |
815 | printNicely(meta) | |
816 | print_list(target_object, noti=True) | |
817 | ||
818 | ||
819 | def notify_list_user_subscribed(e): | |
820 | """ | |
821 | Notify a list_user_subscribed event | |
822 | """ | |
823 | # Retrieve info | |
824 | target = e['target'] | |
825 | if target['screen_name'] != c['original_name']: | |
826 | return | |
827 | source = e['source'] | |
828 | target_object = [e['target_object']] # list of Twitter list | |
829 | created_at = e['created_at'] | |
830 | # Format | |
831 | source_user = cycle_color(source['name']) + \ | |
832 | color_func(c['NOTIFICATION']['source_nick'])( | |
833 | ' @' + source['screen_name']) | |
834 | notify = color_func(c['NOTIFICATION']['notify'])( | |
38a6dc30 | 835 | 'subscribed to your list') |
99cd1fba | 836 | date = parser.parse(created_at) |
e5455a30 | 837 | clock = fallback_humanize(date) |
99cd1fba | 838 | clock = color_func(c['NOTIFICATION']['clock'])(clock) |
38a6dc30 O |
839 | meta = c['NOTIFY_FORMAT'] |
840 | meta = source_user.join(meta.split('#source_user')) | |
841 | meta = notify.join(meta.split('#notify')) | |
842 | meta = clock.join(meta.split('#clock')) | |
104619ce | 843 | meta = emojize(meta) |
99cd1fba O |
844 | # Output |
845 | printNicely('') | |
846 | printNicely(meta) | |
847 | print_list(target_object, noti=True) | |
848 | ||
849 | ||
850 | def notify_list_user_unsubscribed(e): | |
851 | """ | |
852 | Notify a list_user_unsubscribed event | |
853 | """ | |
854 | # Retrieve info | |
855 | target = e['target'] | |
856 | if target['screen_name'] != c['original_name']: | |
857 | return | |
858 | source = e['source'] | |
859 | target_object = [e['target_object']] # list of Twitter list | |
860 | created_at = e['created_at'] | |
861 | # Format | |
862 | source_user = cycle_color(source['name']) + \ | |
863 | color_func(c['NOTIFICATION']['source_nick'])( | |
864 | ' @' + source['screen_name']) | |
865 | notify = color_func(c['NOTIFICATION']['notify'])( | |
38a6dc30 | 866 | 'unsubscribed from your list') |
99cd1fba | 867 | date = parser.parse(created_at) |
e5455a30 | 868 | clock = fallback_humanize(date) |
99cd1fba | 869 | clock = color_func(c['NOTIFICATION']['clock'])(clock) |
38a6dc30 O |
870 | meta = c['NOTIFY_FORMAT'] |
871 | meta = source_user.join(meta.split('#source_user')) | |
872 | meta = notify.join(meta.split('#notify')) | |
873 | meta = clock.join(meta.split('#clock')) | |
104619ce | 874 | meta = emojize(meta) |
99cd1fba O |
875 | # Output |
876 | printNicely('') | |
877 | printNicely(meta) | |
878 | print_list(target_object, noti=True) | |
879 | ||
880 | ||
881 | def print_event(e): | |
882 | """ | |
883 | Notify an event | |
884 | """ | |
885 | event_dict = { | |
38a6dc30 | 886 | 'retweet': notify_retweet, |
99cd1fba O |
887 | 'favorite': notify_favorite, |
888 | 'unfavorite': notify_unfavorite, | |
889 | 'follow': notify_follow, | |
890 | 'list_member_added': notify_list_member_added, | |
891 | 'list_member_removed': notify_list_member_removed, | |
892 | 'list_user_subscribed': notify_list_user_subscribed, | |
893 | 'list_user_unsubscribed': notify_list_user_unsubscribed, | |
894 | } | |
69340648 | 895 | event_dict.get(e['event'], lambda *args: None)(e) |
99cd1fba O |
896 | |
897 | ||
fe9bb33b | 898 | def show_profile(u): |
7500d90b VNM |
899 | """ |
900 | Show a profile | |
901 | """ | |
902 | # Retrieve info | |
903 | name = u['name'] | |
904 | screen_name = u['screen_name'] | |
905 | description = u['description'] | |
906 | profile_image_url = u['profile_image_url'] | |
907 | location = u['location'] | |
908 | url = u['url'] | |
909 | created_at = u['created_at'] | |
910 | statuses_count = u['statuses_count'] | |
911 | friends_count = u['friends_count'] | |
912 | followers_count = u['followers_count'] | |
6fa09c14 | 913 | |
7500d90b | 914 | # Create content |
c075e6dc O |
915 | statuses_count = color_func( |
916 | c['PROFILE']['statuses_count'])( | |
917 | str(statuses_count) + | |
918 | ' tweets') | |
919 | friends_count = color_func( | |
920 | c['PROFILE']['friends_count'])( | |
921 | str(friends_count) + | |
922 | ' following') | |
923 | followers_count = color_func( | |
924 | c['PROFILE']['followers_count'])( | |
925 | str(followers_count) + | |
926 | ' followers') | |
7500d90b | 927 | count = statuses_count + ' ' + friends_count + ' ' + followers_count |
c075e6dc O |
928 | user = cycle_color( |
929 | name) + color_func(c['PROFILE']['nick'])(' @' + screen_name + ' : ') + count | |
930 | profile_image_raw_url = 'Profile photo: ' + \ | |
931 | color_func(c['PROFILE']['profile_image_url'])(profile_image_url) | |
7500d90b | 932 | description = ''.join( |
c3bab4ef | 933 | lmap(lambda x: x + ' ' * 4 if x == '\n' else x, description)) |
632c6fa5 O |
934 | description = color_func(c['PROFILE']['description'])(description) |
935 | location = 'Location : ' + color_func(c['PROFILE']['location'])(location) | |
936 | url = 'URL : ' + (color_func(c['PROFILE']['url'])(url) if url else '') | |
7500d90b | 937 | date = parser.parse(created_at) |
e5455a30 | 938 | clock = fallback_humanize(date) |
632c6fa5 | 939 | clock = 'Join at ' + color_func(c['PROFILE']['clock'])(clock) |
6fa09c14 | 940 | |
7500d90b VNM |
941 | # Format |
942 | line1 = u"{u:>{uw}}".format( | |
943 | u=user, | |
944 | uw=len(user) + 2, | |
945 | ) | |
946 | line2 = u"{p:>{pw}}".format( | |
947 | p=profile_image_raw_url, | |
948 | pw=len(profile_image_raw_url) + 4, | |
949 | ) | |
950 | line3 = u"{d:>{dw}}".format( | |
951 | d=description, | |
952 | dw=len(description) + 4, | |
953 | ) | |
954 | line4 = u"{l:>{lw}}".format( | |
955 | l=location, | |
956 | lw=len(location) + 4, | |
957 | ) | |
958 | line5 = u"{u:>{uw}}".format( | |
959 | u=url, | |
960 | uw=len(url) + 4, | |
961 | ) | |
962 | line6 = u"{c:>{cw}}".format( | |
963 | c=clock, | |
964 | cw=len(clock) + 4, | |
965 | ) | |
6fa09c14 | 966 | |
7500d90b VNM |
967 | # Display |
968 | printNicely('') | |
969 | printNicely(line1) | |
fe9bb33b | 970 | if c['IMAGE_ON_TERM']: |
17bc529d | 971 | try: |
972 | response = requests.get(profile_image_url) | |
c37c04a9 | 973 | image_to_display(BytesIO(response.content)) |
17bc529d | 974 | except: |
975 | pass | |
7500d90b VNM |
976 | else: |
977 | printNicely(line2) | |
978 | for line in [line3, line4, line5, line6]: | |
979 | printNicely(line) | |
980 | printNicely('') | |
981 | ||
982 | ||
983 | def print_trends(trends): | |
984 | """ | |
985 | Display topics | |
986 | """ | |
632c6fa5 | 987 | for topic in trends[:c['TREND_MAX']]: |
7500d90b VNM |
988 | name = topic['name'] |
989 | url = topic['url'] | |
8394e34b | 990 | line = cycle_color(name) + ': ' + color_func(c['TREND']['url'])(url) |
7500d90b VNM |
991 | printNicely(line) |
992 | printNicely('') | |
2d341029 O |
993 | |
994 | ||
99cd1fba | 995 | def print_list(group, noti=False): |
2d341029 O |
996 | """ |
997 | Display a list | |
998 | """ | |
99b52f5f | 999 | for grp in group: |
2d341029 | 1000 | # Format |
99b52f5f | 1001 | name = grp['full_name'] |
2d341029 | 1002 | name = color_func(c['GROUP']['name'])(name + ' : ') |
99b52f5f | 1003 | member = str(grp['member_count']) |
422dd385 | 1004 | member = color_func(c['GROUP']['member'])(member + ' member') |
99b52f5f | 1005 | subscriber = str(grp['subscriber_count']) |
422dd385 O |
1006 | subscriber = color_func( |
1007 | c['GROUP']['subscriber'])( | |
1008 | subscriber + | |
1009 | ' subscriber') | |
99b52f5f | 1010 | description = grp['description'].strip() |
2d341029 | 1011 | description = color_func(c['GROUP']['description'])(description) |
99b52f5f | 1012 | mode = grp['mode'] |
422dd385 | 1013 | mode = color_func(c['GROUP']['mode'])('Type: ' + mode) |
99b52f5f | 1014 | created_at = grp['created_at'] |
2d341029 | 1015 | date = parser.parse(created_at) |
e5455a30 | 1016 | clock = fallback_humanize(date) |
2d341029 O |
1017 | clock = 'Created at ' + color_func(c['GROUP']['clock'])(clock) |
1018 | ||
99cd1fba O |
1019 | prefix = ' ' * 2 |
1020 | # Add spaces in begining of line if this is inside a notification | |
1021 | if noti: | |
1022 | prefix = ' ' * 2 + prefix | |
2d341029 | 1023 | # Create lines |
99cd1fba O |
1024 | line1 = prefix + name + member + ' ' + subscriber |
1025 | line2 = prefix + ' ' * 2 + description | |
1026 | line3 = prefix + ' ' * 2 + mode | |
1027 | line4 = prefix + ' ' * 2 + clock | |
2d341029 O |
1028 | |
1029 | # Display | |
1030 | printNicely('') | |
1031 | printNicely(line1) | |
1032 | printNicely(line2) | |
1033 | printNicely(line3) | |
1034 | printNicely(line4) | |
1035 | ||
38a6dc30 O |
1036 | if not noti: |
1037 | printNicely('') | |
59262e95 O |
1038 | |
1039 | ||
8141aca6 | 1040 | def show_calendar(month, date, rel): |
1041 | """ | |
1042 | Show the calendar in rainbow mode | |
1043 | """ | |
1044 | month = random_rainbow(month) | |
1045 | date = ' '.join([cycle_color(i) for i in date.split(' ')]) | |
1046 | today = str(int(os.popen('date +\'%d\'').read().strip())) | |
1047 | # Display | |
1048 | printNicely(month) | |
1049 | printNicely(date) | |
1050 | for line in rel: | |
1051 | ary = line.split(' ') | |
1052 | ary = lmap( | |
1053 | lambda x: color_func(c['CAL']['today'])(x) | |
1054 | if x == today | |
1055 | else color_func(c['CAL']['days'])(x), | |
1056 | ary) | |
1057 | printNicely(' '.join(ary)) | |
1058 | ||
1059 | ||
b7c9c570 | 1060 | def format_quote(tweet): |
1061 | """ | |
1062 | Quoting format | |
1063 | """ | |
1064 | # Retrieve info | |
1065 | screen_name = '@' + tweet['user']['screen_name'] | |
1066 | text = tweet['text'] | |
1067 | # Validate quote format | |
1068 | if '#owner' not in c['QUOTE_FORMAT']: | |
1069 | printNicely(light_magenta('Quote should contains #owner')) | |
1070 | return False | |
1071 | if '#comment' not in c['QUOTE_FORMAT']: | |
1072 | printNicely(light_magenta('Quote format should have #comment')) | |
1073 | return False | |
1074 | # Build formater | |
1075 | formater = '' | |
1076 | try: | |
1077 | formater = c['QUOTE_FORMAT'] | |
1078 | formater = screen_name.join(formater.split('#owner')) | |
1079 | formater = text.join(formater.split('#tweet')) | |
1080 | formater = u2str(formater) | |
104619ce | 1081 | formater = emojize(formater) |
b7c9c570 | 1082 | except: |
1083 | pass | |
1084 | # Highlight like a tweet | |
4dc385b5 O |
1085 | notice = formater.split() |
1086 | notice = lmap( | |
b7c9c570 | 1087 | lambda x: light_green(x) |
1088 | if x == '#comment' | |
1089 | else x, | |
4dc385b5 O |
1090 | notice) |
1091 | notice = lmap( | |
b7c9c570 | 1092 | lambda x: color_func(c['TWEET']['rt'])(x) |
1093 | if x == 'RT' | |
1094 | else x, | |
4dc385b5 O |
1095 | notice) |
1096 | notice = lmap(lambda x: cycle_color(x) if x[0] == '@' else x, notice) | |
1097 | notice = lmap( | |
b7c9c570 | 1098 | lambda x: color_func(c['TWEET']['link'])(x) |
1099 | if x[0:4] == 'http' | |
1100 | else x, | |
4dc385b5 O |
1101 | notice) |
1102 | notice = lmap( | |
b7c9c570 | 1103 | lambda x: color_func(c['TWEET']['hashtag'])(x) |
1104 | if x.startswith('#') | |
1105 | else x, | |
4dc385b5 O |
1106 | notice) |
1107 | notice = ' '.join(notice) | |
b7c9c570 | 1108 | # Notice |
4dc385b5 | 1109 | notice = light_magenta('Quoting: "') + notice + light_magenta('"') |
b7c9c570 | 1110 | printNicely(notice) |
1111 | return formater | |
1112 | ||
1113 | ||
59262e95 | 1114 | # Start the color cycle |
b2cde062 | 1115 | start_cycle() |