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 * |
18 | ||
99b52f5f O |
19 | # Draw global variables |
20 | dg = {} | |
1fdd6a5c | 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 | """ | |
99b52f5f O |
38 | dg['cyc'] = init_cycle() |
39 | dg['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): | |
99b52f5f O |
72 | if args not in dg['cache']: |
73 | dg['cache'][args] = func(*args) | |
74 | return dg['cache'][args] | |
e9f5200b VNM |
75 | return wrapper |
76 | ||
77 | ||
78 | @Memoize | |
79 | def cycle_color(s): | |
80 | """ | |
81 | Cycle the colors_shuffle | |
82 | """ | |
99b52f5f | 83 | return next(dg['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( | |
99b52f5f | 92 | [next(dg['cyc'])(i) for i in fi.split('\n')] |
e9f5200b VNM |
93 | )) |
94 | ||
95 | ||
92685d21 | 96 | def check_config(): |
ceec8593 | 97 | """ |
98 | Check if config is changed | |
99 | """ | |
100 | changed = False | |
101 | data = get_all_config() | |
102 | for key in c: | |
103 | if key in data: | |
104 | if data[key] != c[key]: | |
105 | changed = True | |
106 | if changed: | |
107 | reload_config() | |
108 | ||
109 | ||
110 | def validate_theme(theme): | |
111 | """ | |
112 | Validate a theme exists or not | |
113 | """ | |
114 | # Theme changed check | |
115 | files = os.listdir(os.path.dirname(__file__) + '/colorset') | |
116 | themes = [f.split('.')[0] for f in files if f.split('.')[-1] == 'json'] | |
117 | return theme in themes | |
92685d21 | 118 | |
119 | ||
99b52f5f | 120 | def reload_theme(value, prev): |
4cf86720 VNM |
121 | """ |
122 | Check current theme and update if necessary | |
123 | """ | |
99b52f5f | 124 | if value != prev: |
1f2f6159 | 125 | config = os.path.dirname( |
99b52f5f | 126 | __file__) + '/colorset/' + value + '.json' |
4cf86720 VNM |
127 | # Load new config |
128 | data = load_config(config) | |
a5301bc0 VNM |
129 | if data: |
130 | for d in data: | |
131 | c[d] = data[d] | |
99b52f5f | 132 | # Restart color cycle and update config |
ceec8593 | 133 | start_cycle() |
99b52f5f O |
134 | set_config('THEME', value) |
135 | return value | |
136 | return prev | |
7500d90b | 137 | |
fe08f905 VNM |
138 | |
139 | def color_func(func_name): | |
140 | """ | |
141 | Call color function base on name | |
142 | """ | |
fa6e062d O |
143 | if str(func_name).isdigit(): |
144 | return term_color(int(func_name)) | |
c3bab4ef | 145 | return globals()[func_name] |
fe08f905 VNM |
146 | |
147 | ||
4dc385b5 | 148 | def draw(t, keyword=None, humanize=True, fil=[], ig=[]): |
7500d90b VNM |
149 | """ |
150 | Draw the rainbow | |
151 | """ | |
99b52f5f | 152 | # Check config |
92685d21 | 153 | check_config() |
c37c04a9 | 154 | |
7500d90b VNM |
155 | # Retrieve tweet |
156 | tid = t['id'] | |
606def7e | 157 | text = t['text'] |
7500d90b VNM |
158 | screen_name = t['user']['screen_name'] |
159 | name = t['user']['name'] | |
160 | created_at = t['created_at'] | |
161 | favorited = t['favorited'] | |
318cdd67 O |
162 | retweet_count = t['retweet_count'] |
163 | favorite_count = t['favorite_count'] | |
7500d90b | 164 | date = parser.parse(created_at) |
8b3456f9 | 165 | date = arrow.get(date).to('local') |
166 | if humanize: | |
167 | lang, encode = locale.getdefaultlocale() | |
168 | clock = arrow.get(date).to('local').humanize(locale=lang) | |
169 | else: | |
170 | try: | |
171 | clock_format = c['FORMAT']['TWEET']['CLOCK_FORMAT'] | |
172 | except: | |
173 | clock_format = '%Y/%m/%d %H:%M:%S' | |
174 | clock = date.datetime.strftime(clock_format) | |
7500d90b | 175 | |
606def7e BR |
176 | # Pull extended retweet text |
177 | try: | |
18df6e7f O |
178 | text = 'RT @' + t['retweeted_status']['user']['screen_name'] + ': ' +\ |
179 | t['retweeted_status']['text'] | |
606def7e BR |
180 | except: |
181 | pass | |
182 | ||
18df6e7f | 183 | # Unescape HTML character |
606def7e BR |
184 | text = unescape(text) |
185 | ||
7500d90b VNM |
186 | # Get expanded url |
187 | try: | |
188 | expanded_url = [] | |
189 | url = [] | |
190 | urls = t['entities']['urls'] | |
191 | for u in urls: | |
192 | expanded_url.append(u['expanded_url']) | |
193 | url.append(u['url']) | |
194 | except: | |
195 | expanded_url = None | |
196 | url = None | |
197 | ||
198 | # Get media | |
199 | try: | |
200 | media_url = [] | |
201 | media = t['entities']['media'] | |
202 | for m in media: | |
203 | media_url.append(m['media_url']) | |
204 | except: | |
205 | media_url = None | |
206 | ||
207 | # Filter and ignore | |
37cf396a | 208 | mytweet = screen_name == c['original_name'] |
7500d90b | 209 | screen_name = '@' + screen_name |
e3927852 O |
210 | fil = list(set((fil or []) + c['ONLY_LIST'])) |
211 | ig = list(set((ig or []) + c['IGNORE_LIST'])) | |
7500d90b VNM |
212 | if fil and screen_name not in fil: |
213 | return | |
214 | if ig and screen_name in ig: | |
215 | return | |
216 | ||
217 | # Get rainbow id | |
99b52f5f O |
218 | if tid not in c['tweet_dict']: |
219 | c['tweet_dict'].append(tid) | |
220 | rid = len(c['tweet_dict']) - 1 | |
221 | else: | |
222 | rid = c['tweet_dict'].index(tid) | |
7500d90b VNM |
223 | |
224 | # Format info | |
8c7ae594 | 225 | name = cycle_color(name) |
571ea706 O |
226 | if mytweet: |
227 | nick = color_func(c['TWEET']['mynick'])(screen_name) | |
228 | else: | |
229 | nick = color_func(c['TWEET']['nick'])(screen_name) | |
0d9977c7 O |
230 | clock = clock |
231 | id = str(rid) | |
8c7ae594 | 232 | fav = '' |
7500d90b | 233 | if favorited: |
8c7ae594 O |
234 | fav = color_func(c['TWEET']['favorited'])(u'\u2605') |
235 | ||
7500d90b VNM |
236 | tweet = text.split() |
237 | # Replace url | |
238 | if expanded_url: | |
13e6b275 | 239 | for index in xrange(len(expanded_url)): |
c3bab4ef | 240 | tweet = lmap( |
8141aca6 | 241 | lambda x: expanded_url[index] |
242 | if x == url[index] | |
243 | else x, | |
7500d90b VNM |
244 | tweet) |
245 | # Highlight RT | |
c3bab4ef | 246 | tweet = lmap( |
8141aca6 | 247 | lambda x: color_func(c['TWEET']['rt'])(x) |
248 | if x == 'RT' | |
249 | else x, | |
c075e6dc | 250 | tweet) |
7500d90b | 251 | # Highlight screen_name |
c3bab4ef | 252 | tweet = lmap(lambda x: cycle_color(x) if x[0] == '@' else x, tweet) |
7500d90b | 253 | # Highlight link |
c3bab4ef | 254 | tweet = lmap( |
8141aca6 | 255 | lambda x: color_func(c['TWEET']['link'])(x) |
37cf396a | 256 | if x.startswith('http') |
8141aca6 | 257 | else x, |
c075e6dc | 258 | tweet) |
4c025026 | 259 | # Highlight hashtag |
260 | tweet = lmap( | |
8141aca6 | 261 | lambda x: color_func(c['TWEET']['hashtag'])(x) |
262 | if x.startswith('#') | |
263 | else x, | |
4c025026 | 264 | tweet) |
37cf396a O |
265 | # Highlight my tweet |
266 | if mytweet: | |
267 | tweet = [color_func(c['TWEET']['mytweet'])(x) | |
268 | for x in tweet | |
269 | if not any([ | |
270 | x == 'RT', | |
271 | x.startswith('http'), | |
272 | x.startswith('#')]) | |
273 | ] | |
59262e95 | 274 | # Highlight keyword |
7500d90b | 275 | tweet = ' '.join(tweet) |
59262e95 | 276 | if keyword: |
a8c5fce4 | 277 | roj = re.search(keyword, tweet, re.IGNORECASE) |
59262e95 O |
278 | if roj: |
279 | occur = roj.group() | |
280 | ary = tweet.split(occur) | |
b13c6a0d | 281 | delimiter = color_func(c['TWEET']['keyword'])(occur) |
282 | tweet = delimiter.join(ary) | |
7500d90b | 283 | |
8c7ae594 | 284 | # Load config formater |
318cdd67 | 285 | formater = '' |
8c7ae594 O |
286 | try: |
287 | formater = c['FORMAT']['TWEET']['DISPLAY'] | |
7c437a0f O |
288 | formater = name.join(formater.split('#name')) |
289 | formater = nick.join(formater.split('#nick')) | |
290 | formater = fav.join(formater.split('#fav')) | |
291 | formater = tweet.join(formater.split('#tweet')) | |
0d9977c7 | 292 | # Change clock word |
03c0d30b | 293 | word = [wo for wo in formater.split() if '#clock' in wo][0] |
8141aca6 | 294 | delimiter = color_func(c['TWEET']['clock'])( |
295 | clock.join(word.split('#clock'))) | |
0d9977c7 O |
296 | formater = delimiter.join(formater.split(word)) |
297 | # Change id word | |
03c0d30b | 298 | word = [wo for wo in formater.split() if '#id' in wo][0] |
0d9977c7 O |
299 | delimiter = color_func(c['TWEET']['id'])(id.join(word.split('#id'))) |
300 | formater = delimiter.join(formater.split(word)) | |
318cdd67 | 301 | # Change retweet count word |
03c0d30b | 302 | word = [wo for wo in formater.split() if '#rt_count' in wo][0] |
318cdd67 O |
303 | delimiter = color_func(c['TWEET']['retweet_count'])( |
304 | str(retweet_count).join(word.split('#rt_count'))) | |
305 | formater = delimiter.join(formater.split(word)) | |
306 | # Change favorites count word | |
03c0d30b | 307 | word = [wo for wo in formater.split() if '#fa_count' in wo][0] |
318cdd67 O |
308 | delimiter = color_func(c['TWEET']['favorite_count'])( |
309 | str(favorite_count).join(word.split('#fa_count'))) | |
310 | formater = delimiter.join(formater.split(word)) | |
8c7ae594 | 311 | except: |
318cdd67 | 312 | pass |
7500d90b | 313 | |
8c7ae594 O |
314 | # Draw |
315 | printNicely(formater) | |
7500d90b VNM |
316 | |
317 | # Display Image | |
fe9bb33b | 318 | if c['IMAGE_ON_TERM'] and media_url: |
7500d90b | 319 | for mu in media_url: |
17bc529d | 320 | try: |
321 | response = requests.get(mu) | |
77f1d210 | 322 | image_to_display(BytesIO(response.content)) |
323 | except Exception: | |
17bc529d | 324 | printNicely(red('Sorry, image link is broken')) |
7500d90b VNM |
325 | |
326 | ||
67c663f8 O |
327 | def print_threads(d): |
328 | """ | |
329 | Print threads of messages | |
330 | """ | |
331 | id = 1 | |
332 | rel = {} | |
333 | for partner in d: | |
334 | messages = d[partner] | |
335 | count = len(messages) | |
336 | screen_name = '@' + partner[0] | |
337 | name = partner[1] | |
338 | screen_name = color_func(c['MESSAGE']['partner'])(screen_name) | |
339 | name = cycle_color(name) | |
03c0d30b | 340 | thread_id = color_func(c['MESSAGE']['id'])('thread_id:' + str(id)) |
341 | line = ' ' * 2 + name + ' ' + screen_name + \ | |
67c663f8 O |
342 | ' (' + str(count) + ' message) ' + thread_id |
343 | printNicely(line) | |
344 | rel[id] = partner | |
345 | id += 1 | |
346 | dg['thread'] = d | |
347 | return rel | |
348 | ||
349 | ||
350 | def print_thread(partner, me_nick, me_name): | |
351 | """ | |
352 | Print a thread of messages | |
353 | """ | |
354 | # Sort messages by time | |
355 | messages = dg['thread'][partner] | |
03c0d30b | 356 | messages.sort(key=lambda x: parser.parse(x['created_at'])) |
357 | # Use legacy display on non-ascii text message | |
bcb5518e | 358 | ms = [m['text'] for m in messages] |
359 | ums = [m for m in ms if not all(ord(c) < 128 for c in m)] | |
360 | if ums: | |
03c0d30b | 361 | for m in messages: |
362 | print_message(m) | |
363 | printNicely('') | |
364 | return | |
365 | # Print the first line | |
366 | dg['frame_margin'] = margin = 2 | |
367 | partner_nick = partner[0] | |
368 | partner_name = partner[1] | |
369 | left_size = len(partner_nick) + len(partner_name) + 2 | |
370 | right_size = len(me_nick) + len(me_name) + 2 | |
371 | partner_nick = color_func(c['MESSAGE']['partner'])('@' + partner_nick) | |
372 | partner_name = cycle_color(partner_name) | |
67c663f8 O |
373 | me_screen_name = color_func(c['MESSAGE']['me'])('@' + me_nick) |
374 | me_name = cycle_color(me_name) | |
03c0d30b | 375 | left = ' ' * margin + partner_name + ' ' + partner_nick |
67c663f8 O |
376 | right = me_name + ' ' + me_screen_name + ' ' * margin |
377 | h, w = os.popen('stty size', 'r').read().split() | |
378 | w = int(w) | |
03c0d30b | 379 | line = '{}{}{}'.format( |
380 | left, ' ' * (w - left_size - right_size - 2 * margin), right) | |
67c663f8 O |
381 | printNicely('') |
382 | printNicely(line) | |
383 | printNicely('') | |
67c663f8 O |
384 | # Print messages |
385 | for m in messages: | |
386 | if m['sender_screen_name'] == me_nick: | |
387 | print_right_message(m) | |
388 | elif m['recipient_screen_name'] == me_nick: | |
389 | print_left_message(m) | |
390 | ||
391 | ||
392 | def print_right_message(m): | |
393 | """ | |
394 | Print a message on the right of screen | |
395 | """ | |
396 | h, w = os.popen('stty size', 'r').read().split() | |
397 | w = int(w) | |
03c0d30b | 398 | frame_width = w // 3 - dg['frame_margin'] |
37cf396a | 399 | frame_width = max(c['THREAD_MIN_WIDTH'], frame_width) |
03c0d30b | 400 | step = frame_width - 2 * dg['frame_margin'] |
bcb5518e | 401 | slicing = textwrap.wrap(m['text'], step) |
03c0d30b | 402 | spaces = w - frame_width - dg['frame_margin'] |
67c663f8 | 403 | dotline = ' ' * spaces + '-' * frame_width |
03c0d30b | 404 | dotline = color_func(c['MESSAGE']['me_frame'])(dotline) |
223d2e05 | 405 | # Draw the frame |
67c663f8 O |
406 | printNicely(dotline) |
407 | for line in slicing: | |
408 | fill = step - len(line) | |
409 | screen_line = ' ' * spaces + '| ' + line + ' ' * fill + ' ' | |
410 | if slicing[-1] == line: | |
411 | screen_line = screen_line + ' >' | |
412 | else: | |
413 | screen_line = screen_line + '|' | |
03c0d30b | 414 | screen_line = color_func(c['MESSAGE']['me_frame'])(screen_line) |
67c663f8 O |
415 | printNicely(screen_line) |
416 | printNicely(dotline) | |
03c0d30b | 417 | # Format clock |
223d2e05 O |
418 | date = parser.parse(m['created_at']) |
419 | date = arrow.get(date).to('local').datetime | |
420 | clock_format = '%Y/%m/%d %H:%M:%S' | |
421 | try: | |
422 | clock_format = c['FORMAT']['MESSAGE']['CLOCK_FORMAT'] | |
423 | except: | |
424 | pass | |
425 | clock = date.strftime(clock_format) | |
03c0d30b | 426 | # Format id |
223d2e05 O |
427 | if m['id'] not in c['message_dict']: |
428 | c['message_dict'].append(m['id']) | |
429 | rid = len(c['message_dict']) - 1 | |
430 | else: | |
431 | rid = c['message_dict'].index(m['id']) | |
03c0d30b | 432 | id = str(rid) |
433 | # Print meta | |
434 | formater = '' | |
435 | try: | |
436 | virtual_meta = formater = c['THREAD_META_RIGHT'] | |
437 | virtual_meta = clock.join(virtual_meta.split('#clock')) | |
438 | virtual_meta = id.join(virtual_meta.split('#id')) | |
439 | # Change clock word | |
440 | word = [wo for wo in formater.split() if '#clock' in wo][0] | |
441 | delimiter = color_func(c['MESSAGE']['clock'])( | |
442 | clock.join(word.split('#clock'))) | |
443 | formater = delimiter.join(formater.split(word)) | |
444 | # Change id word | |
445 | word = [wo for wo in formater.split() if '#id' in wo][0] | |
446 | delimiter = color_func(c['MESSAGE']['id'])(id.join(word.split('#id'))) | |
447 | formater = delimiter.join(formater.split(word)) | |
448 | except Exception: | |
449 | printNicely(red('Wrong format in config.')) | |
450 | return | |
451 | meta = formater | |
452 | line = ' ' * (w - len(virtual_meta) - dg['frame_margin']) + meta | |
223d2e05 | 453 | printNicely(line) |
67c663f8 O |
454 | |
455 | ||
456 | def print_left_message(m): | |
457 | """ | |
458 | Print a message on the left of screen | |
459 | """ | |
460 | h, w = os.popen('stty size', 'r').read().split() | |
461 | w = int(w) | |
03c0d30b | 462 | frame_width = w // 3 - dg['frame_margin'] |
37cf396a | 463 | frame_width = max(c['THREAD_MIN_WIDTH'], frame_width) |
03c0d30b | 464 | step = frame_width - 2 * dg['frame_margin'] |
bcb5518e | 465 | slicing = textwrap.wrap(m['text'], step) |
03c0d30b | 466 | spaces = dg['frame_margin'] |
67c663f8 | 467 | dotline = ' ' * spaces + '-' * frame_width |
03c0d30b | 468 | dotline = color_func(c['MESSAGE']['partner_frame'])(dotline) |
223d2e05 | 469 | # Draw the frame |
67c663f8 O |
470 | printNicely(dotline) |
471 | for line in slicing: | |
472 | fill = step - len(line) | |
473 | screen_line = ' ' + line + ' ' * fill + ' |' | |
474 | if slicing[-1] == line: | |
03c0d30b | 475 | screen_line = ' ' * (spaces - 1) + '< ' + screen_line |
67c663f8 O |
476 | else: |
477 | screen_line = ' ' * spaces + '|' + screen_line | |
03c0d30b | 478 | screen_line = color_func(c['MESSAGE']['partner_frame'])(screen_line) |
67c663f8 O |
479 | printNicely(screen_line) |
480 | printNicely(dotline) | |
03c0d30b | 481 | # Format clock |
223d2e05 O |
482 | date = parser.parse(m['created_at']) |
483 | date = arrow.get(date).to('local').datetime | |
484 | clock_format = '%Y/%m/%d %H:%M:%S' | |
485 | try: | |
486 | clock_format = c['FORMAT']['MESSAGE']['CLOCK_FORMAT'] | |
487 | except: | |
488 | pass | |
489 | clock = date.strftime(clock_format) | |
03c0d30b | 490 | # Format id |
223d2e05 O |
491 | if m['id'] not in c['message_dict']: |
492 | c['message_dict'].append(m['id']) | |
493 | rid = len(c['message_dict']) - 1 | |
494 | else: | |
495 | rid = c['message_dict'].index(m['id']) | |
03c0d30b | 496 | id = str(rid) |
497 | # Print meta | |
498 | formater = '' | |
499 | try: | |
500 | virtual_meta = formater = c['THREAD_META_LEFT'] | |
501 | virtual_meta = clock.join(virtual_meta.split('#clock')) | |
502 | virtual_meta = id.join(virtual_meta.split('#id')) | |
503 | # Change clock word | |
504 | word = [wo for wo in formater.split() if '#clock' in wo][0] | |
505 | delimiter = color_func(c['MESSAGE']['clock'])( | |
506 | clock.join(word.split('#clock'))) | |
507 | formater = delimiter.join(formater.split(word)) | |
508 | # Change id word | |
509 | word = [wo for wo in formater.split() if '#id' in wo][0] | |
510 | delimiter = color_func(c['MESSAGE']['id'])(id.join(word.split('#id'))) | |
511 | formater = delimiter.join(formater.split(word)) | |
512 | except Exception: | |
513 | printNicely(red('Wrong format in config.')) | |
514 | return | |
515 | meta = formater | |
516 | line = ' ' * dg['frame_margin'] + meta | |
223d2e05 | 517 | printNicely(line) |
67c663f8 O |
518 | |
519 | ||
4dc385b5 | 520 | def print_message(m): |
7500d90b VNM |
521 | """ |
522 | Print direct message | |
523 | """ | |
c37c04a9 | 524 | # Retrieve message |
7500d90b VNM |
525 | sender_screen_name = '@' + m['sender_screen_name'] |
526 | sender_name = m['sender']['name'] | |
b2cde062 | 527 | text = unescape(m['text']) |
7500d90b VNM |
528 | recipient_screen_name = '@' + m['recipient_screen_name'] |
529 | recipient_name = m['recipient']['name'] | |
530 | mid = m['id'] | |
531 | date = parser.parse(m['created_at']) | |
8b3456f9 | 532 | date = arrow.get(date).to('local').datetime |
8c7ae594 O |
533 | clock_format = '%Y/%m/%d %H:%M:%S' |
534 | try: | |
535 | clock_format = c['FORMAT']['MESSAGE']['CLOCK_FORMAT'] | |
536 | except: | |
537 | pass | |
538 | clock = date.strftime(clock_format) | |
7500d90b VNM |
539 | |
540 | # Get rainbow id | |
99b52f5f O |
541 | if mid not in c['message_dict']: |
542 | c['message_dict'].append(mid) | |
543 | rid = len(c['message_dict']) - 1 | |
544 | else: | |
545 | rid = c['message_dict'].index(mid) | |
7500d90b | 546 | |
6fa09c14 | 547 | # Draw |
8c7ae594 O |
548 | sender_name = cycle_color(sender_name) |
549 | sender_nick = color_func(c['MESSAGE']['sender'])(sender_screen_name) | |
550 | recipient_name = cycle_color(recipient_name) | |
0d9977c7 O |
551 | recipient_nick = color_func( |
552 | c['MESSAGE']['recipient'])(recipient_screen_name) | |
8c7ae594 | 553 | to = color_func(c['MESSAGE']['to'])('>>>') |
0d9977c7 O |
554 | clock = clock |
555 | id = str(rid) | |
8c7ae594 | 556 | |
c3bab4ef | 557 | text = ''.join(lmap(lambda x: x + ' ' if x == '\n' else x, text)) |
7500d90b | 558 | |
8c7ae594 O |
559 | # Load config formater |
560 | try: | |
561 | formater = c['FORMAT']['MESSAGE']['DISPLAY'] | |
0d9977c7 O |
562 | formater = sender_name.join(formater.split("#sender_name")) |
563 | formater = sender_nick.join(formater.split("#sender_nick")) | |
564 | formater = to.join(formater.split("#to")) | |
565 | formater = recipient_name.join(formater.split("#recipient_name")) | |
566 | formater = recipient_nick.join(formater.split("#recipient_nick")) | |
567 | formater = text.join(formater.split("#message")) | |
568 | # Change clock word | |
03c0d30b | 569 | word = [wo for wo in formater.split() if '#clock' in wo][0] |
8141aca6 | 570 | delimiter = color_func(c['MESSAGE']['clock'])( |
571 | clock.join(word.split('#clock'))) | |
0d9977c7 O |
572 | formater = delimiter.join(formater.split(word)) |
573 | # Change id word | |
03c0d30b | 574 | word = [wo for wo in formater.split() if '#id' in wo][0] |
0d9977c7 O |
575 | delimiter = color_func(c['MESSAGE']['id'])(id.join(word.split('#id'))) |
576 | formater = delimiter.join(formater.split(word)) | |
8c7ae594 O |
577 | except: |
578 | printNicely(red('Wrong format in config.')) | |
579 | return | |
580 | ||
581 | # Draw | |
582 | printNicely(formater) | |
7500d90b VNM |
583 | |
584 | ||
fe9bb33b | 585 | def show_profile(u): |
7500d90b VNM |
586 | """ |
587 | Show a profile | |
588 | """ | |
589 | # Retrieve info | |
590 | name = u['name'] | |
591 | screen_name = u['screen_name'] | |
592 | description = u['description'] | |
593 | profile_image_url = u['profile_image_url'] | |
594 | location = u['location'] | |
595 | url = u['url'] | |
596 | created_at = u['created_at'] | |
597 | statuses_count = u['statuses_count'] | |
598 | friends_count = u['friends_count'] | |
599 | followers_count = u['followers_count'] | |
6fa09c14 | 600 | |
7500d90b | 601 | # Create content |
c075e6dc O |
602 | statuses_count = color_func( |
603 | c['PROFILE']['statuses_count'])( | |
604 | str(statuses_count) + | |
605 | ' tweets') | |
606 | friends_count = color_func( | |
607 | c['PROFILE']['friends_count'])( | |
608 | str(friends_count) + | |
609 | ' following') | |
610 | followers_count = color_func( | |
611 | c['PROFILE']['followers_count'])( | |
612 | str(followers_count) + | |
613 | ' followers') | |
7500d90b | 614 | count = statuses_count + ' ' + friends_count + ' ' + followers_count |
c075e6dc O |
615 | user = cycle_color( |
616 | name) + color_func(c['PROFILE']['nick'])(' @' + screen_name + ' : ') + count | |
617 | profile_image_raw_url = 'Profile photo: ' + \ | |
618 | color_func(c['PROFILE']['profile_image_url'])(profile_image_url) | |
7500d90b | 619 | description = ''.join( |
c3bab4ef | 620 | lmap(lambda x: x + ' ' * 4 if x == '\n' else x, description)) |
632c6fa5 O |
621 | description = color_func(c['PROFILE']['description'])(description) |
622 | location = 'Location : ' + color_func(c['PROFILE']['location'])(location) | |
623 | url = 'URL : ' + (color_func(c['PROFILE']['url'])(url) if url else '') | |
7500d90b | 624 | date = parser.parse(created_at) |
8b3456f9 | 625 | lang, encode = locale.getdefaultlocale() |
626 | clock = arrow.get(date).to('local').humanize(locale=lang) | |
632c6fa5 | 627 | clock = 'Join at ' + color_func(c['PROFILE']['clock'])(clock) |
6fa09c14 | 628 | |
7500d90b VNM |
629 | # Format |
630 | line1 = u"{u:>{uw}}".format( | |
631 | u=user, | |
632 | uw=len(user) + 2, | |
633 | ) | |
634 | line2 = u"{p:>{pw}}".format( | |
635 | p=profile_image_raw_url, | |
636 | pw=len(profile_image_raw_url) + 4, | |
637 | ) | |
638 | line3 = u"{d:>{dw}}".format( | |
639 | d=description, | |
640 | dw=len(description) + 4, | |
641 | ) | |
642 | line4 = u"{l:>{lw}}".format( | |
643 | l=location, | |
644 | lw=len(location) + 4, | |
645 | ) | |
646 | line5 = u"{u:>{uw}}".format( | |
647 | u=url, | |
648 | uw=len(url) + 4, | |
649 | ) | |
650 | line6 = u"{c:>{cw}}".format( | |
651 | c=clock, | |
652 | cw=len(clock) + 4, | |
653 | ) | |
6fa09c14 | 654 | |
7500d90b VNM |
655 | # Display |
656 | printNicely('') | |
657 | printNicely(line1) | |
fe9bb33b | 658 | if c['IMAGE_ON_TERM']: |
17bc529d | 659 | try: |
660 | response = requests.get(profile_image_url) | |
c37c04a9 | 661 | image_to_display(BytesIO(response.content)) |
17bc529d | 662 | except: |
663 | pass | |
7500d90b VNM |
664 | else: |
665 | printNicely(line2) | |
666 | for line in [line3, line4, line5, line6]: | |
667 | printNicely(line) | |
668 | printNicely('') | |
669 | ||
670 | ||
671 | def print_trends(trends): | |
672 | """ | |
673 | Display topics | |
674 | """ | |
632c6fa5 | 675 | for topic in trends[:c['TREND_MAX']]: |
7500d90b VNM |
676 | name = topic['name'] |
677 | url = topic['url'] | |
8394e34b | 678 | line = cycle_color(name) + ': ' + color_func(c['TREND']['url'])(url) |
7500d90b VNM |
679 | printNicely(line) |
680 | printNicely('') | |
2d341029 O |
681 | |
682 | ||
683 | def print_list(group): | |
684 | """ | |
685 | Display a list | |
686 | """ | |
99b52f5f | 687 | for grp in group: |
2d341029 | 688 | # Format |
99b52f5f | 689 | name = grp['full_name'] |
2d341029 | 690 | name = color_func(c['GROUP']['name'])(name + ' : ') |
99b52f5f | 691 | member = str(grp['member_count']) |
422dd385 | 692 | member = color_func(c['GROUP']['member'])(member + ' member') |
99b52f5f | 693 | subscriber = str(grp['subscriber_count']) |
422dd385 O |
694 | subscriber = color_func( |
695 | c['GROUP']['subscriber'])( | |
696 | subscriber + | |
697 | ' subscriber') | |
99b52f5f | 698 | description = grp['description'].strip() |
2d341029 | 699 | description = color_func(c['GROUP']['description'])(description) |
99b52f5f | 700 | mode = grp['mode'] |
422dd385 | 701 | mode = color_func(c['GROUP']['mode'])('Type: ' + mode) |
99b52f5f | 702 | created_at = grp['created_at'] |
2d341029 | 703 | date = parser.parse(created_at) |
8b3456f9 | 704 | lang, encode = locale.getdefaultlocale() |
705 | clock = arrow.get(date).to('local').humanize(locale=lang) | |
2d341029 O |
706 | clock = 'Created at ' + color_func(c['GROUP']['clock'])(clock) |
707 | ||
2d341029 | 708 | # Create lines |
422dd385 O |
709 | line1 = ' ' * 2 + name + member + ' ' + subscriber |
710 | line2 = ' ' * 4 + description | |
711 | line3 = ' ' * 4 + mode | |
712 | line4 = ' ' * 4 + clock | |
2d341029 O |
713 | |
714 | # Display | |
715 | printNicely('') | |
716 | printNicely(line1) | |
717 | printNicely(line2) | |
718 | printNicely(line3) | |
719 | printNicely(line4) | |
720 | ||
721 | printNicely('') | |
59262e95 O |
722 | |
723 | ||
8141aca6 | 724 | def show_calendar(month, date, rel): |
725 | """ | |
726 | Show the calendar in rainbow mode | |
727 | """ | |
728 | month = random_rainbow(month) | |
729 | date = ' '.join([cycle_color(i) for i in date.split(' ')]) | |
730 | today = str(int(os.popen('date +\'%d\'').read().strip())) | |
731 | # Display | |
732 | printNicely(month) | |
733 | printNicely(date) | |
734 | for line in rel: | |
735 | ary = line.split(' ') | |
736 | ary = lmap( | |
737 | lambda x: color_func(c['CAL']['today'])(x) | |
738 | if x == today | |
739 | else color_func(c['CAL']['days'])(x), | |
740 | ary) | |
741 | printNicely(' '.join(ary)) | |
742 | ||
743 | ||
b7c9c570 | 744 | def format_quote(tweet): |
745 | """ | |
746 | Quoting format | |
747 | """ | |
748 | # Retrieve info | |
749 | screen_name = '@' + tweet['user']['screen_name'] | |
750 | text = tweet['text'] | |
751 | # Validate quote format | |
752 | if '#owner' not in c['QUOTE_FORMAT']: | |
753 | printNicely(light_magenta('Quote should contains #owner')) | |
754 | return False | |
755 | if '#comment' not in c['QUOTE_FORMAT']: | |
756 | printNicely(light_magenta('Quote format should have #comment')) | |
757 | return False | |
758 | # Build formater | |
759 | formater = '' | |
760 | try: | |
761 | formater = c['QUOTE_FORMAT'] | |
762 | formater = screen_name.join(formater.split('#owner')) | |
763 | formater = text.join(formater.split('#tweet')) | |
764 | formater = u2str(formater) | |
765 | except: | |
766 | pass | |
767 | # Highlight like a tweet | |
4dc385b5 O |
768 | notice = formater.split() |
769 | notice = lmap( | |
b7c9c570 | 770 | lambda x: light_green(x) |
771 | if x == '#comment' | |
772 | else x, | |
4dc385b5 O |
773 | notice) |
774 | notice = lmap( | |
b7c9c570 | 775 | lambda x: color_func(c['TWEET']['rt'])(x) |
776 | if x == 'RT' | |
777 | else x, | |
4dc385b5 O |
778 | notice) |
779 | notice = lmap(lambda x: cycle_color(x) if x[0] == '@' else x, notice) | |
780 | notice = lmap( | |
b7c9c570 | 781 | lambda x: color_func(c['TWEET']['link'])(x) |
782 | if x[0:4] == 'http' | |
783 | else x, | |
4dc385b5 O |
784 | notice) |
785 | notice = lmap( | |
b7c9c570 | 786 | lambda x: color_func(c['TWEET']['hashtag'])(x) |
787 | if x.startswith('#') | |
788 | else x, | |
4dc385b5 O |
789 | notice) |
790 | notice = ' '.join(notice) | |
b7c9c570 | 791 | # Notice |
4dc385b5 | 792 | notice = light_magenta('Quoting: "') + notice + light_magenta('"') |
b7c9c570 | 793 | printNicely(notice) |
794 | return formater | |
795 | ||
796 | ||
59262e95 | 797 | # Start the color cycle |
b2cde062 | 798 | start_cycle() |