remake screenshot
[rainbowstream.git] / rainbowstream / draw.py
CommitLineData
e9f5200b 1import random
bcb5518e 2import textwrap
e9f5200b 3import itertools
7500d90b 4import requests
8b3456f9 5import locale
6import arrow
59262e95 7import re
67c663f8 8import os
7500d90b 9
2da50cc4 10from twitter.util import printNicely
e9f5200b
VNM
11from functools import wraps
12from pyfiglet import figlet_format
7500d90b 13from dateutil import parser
2da50cc4 14from .c_image import *
7500d90b
VNM
15from .colors import *
16from .config import *
c3bab4ef 17from .py3patch import *
18
99b52f5f
O
19# Draw global variables
20dg = {}
1fdd6a5c 21
422dd385 22
e9f5200b
VNM
23def 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 34def 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
42def 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
54def 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
66def 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
79def cycle_color(s):
80 """
81 Cycle the colors_shuffle
82 """
99b52f5f 83 return next(dg['cyc'])(s)
e9f5200b
VNM
84
85
86def 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 96def 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
110def 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 120def 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
139def 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 148def 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
208 screen_name = '@' + screen_name
e3927852
O
209 fil = list(set((fil or []) + c['ONLY_LIST']))
210 ig = list(set((ig or []) + c['IGNORE_LIST']))
7500d90b
VNM
211 if fil and screen_name not in fil:
212 return
213 if ig and screen_name in ig:
214 return
215
216 # Get rainbow id
99b52f5f
O
217 if tid not in c['tweet_dict']:
218 c['tweet_dict'].append(tid)
219 rid = len(c['tweet_dict']) - 1
220 else:
221 rid = c['tweet_dict'].index(tid)
7500d90b
VNM
222
223 # Format info
8c7ae594 224 name = cycle_color(name)
d6cc4c67 225 nick = color_func(c['TWEET']['nick'])(screen_name)
0d9977c7
O
226 clock = clock
227 id = str(rid)
8c7ae594 228 fav = ''
7500d90b 229 if favorited:
8c7ae594
O
230 fav = color_func(c['TWEET']['favorited'])(u'\u2605')
231
7500d90b
VNM
232 tweet = text.split()
233 # Replace url
234 if expanded_url:
13e6b275 235 for index in xrange(len(expanded_url)):
c3bab4ef 236 tweet = lmap(
8141aca6 237 lambda x: expanded_url[index]
238 if x == url[index]
239 else x,
7500d90b
VNM
240 tweet)
241 # Highlight RT
c3bab4ef 242 tweet = lmap(
8141aca6 243 lambda x: color_func(c['TWEET']['rt'])(x)
244 if x == 'RT'
245 else x,
c075e6dc 246 tweet)
7500d90b 247 # Highlight screen_name
c3bab4ef 248 tweet = lmap(lambda x: cycle_color(x) if x[0] == '@' else x, tweet)
7500d90b 249 # Highlight link
c3bab4ef 250 tweet = lmap(
8141aca6 251 lambda x: color_func(c['TWEET']['link'])(x)
252 if x[0:4] == 'http'
253 else x,
c075e6dc 254 tweet)
4c025026 255 # Highlight hashtag
256 tweet = lmap(
8141aca6 257 lambda x: color_func(c['TWEET']['hashtag'])(x)
258 if x.startswith('#')
259 else x,
4c025026 260 tweet)
59262e95 261 # Highlight keyword
7500d90b 262 tweet = ' '.join(tweet)
59262e95 263 if keyword:
a8c5fce4 264 roj = re.search(keyword, tweet, re.IGNORECASE)
59262e95
O
265 if roj:
266 occur = roj.group()
267 ary = tweet.split(occur)
b13c6a0d 268 delimiter = color_func(c['TWEET']['keyword'])(occur)
269 tweet = delimiter.join(ary)
7500d90b 270
8c7ae594 271 # Load config formater
318cdd67 272 formater = ''
8c7ae594
O
273 try:
274 formater = c['FORMAT']['TWEET']['DISPLAY']
7c437a0f
O
275 formater = name.join(formater.split('#name'))
276 formater = nick.join(formater.split('#nick'))
277 formater = fav.join(formater.split('#fav'))
278 formater = tweet.join(formater.split('#tweet'))
0d9977c7 279 # Change clock word
03c0d30b 280 word = [wo for wo in formater.split() if '#clock' in wo][0]
8141aca6 281 delimiter = color_func(c['TWEET']['clock'])(
282 clock.join(word.split('#clock')))
0d9977c7
O
283 formater = delimiter.join(formater.split(word))
284 # Change id word
03c0d30b 285 word = [wo for wo in formater.split() if '#id' in wo][0]
0d9977c7
O
286 delimiter = color_func(c['TWEET']['id'])(id.join(word.split('#id')))
287 formater = delimiter.join(formater.split(word))
318cdd67 288 # Change retweet count word
03c0d30b 289 word = [wo for wo in formater.split() if '#rt_count' in wo][0]
318cdd67
O
290 delimiter = color_func(c['TWEET']['retweet_count'])(
291 str(retweet_count).join(word.split('#rt_count')))
292 formater = delimiter.join(formater.split(word))
293 # Change favorites count word
03c0d30b 294 word = [wo for wo in formater.split() if '#fa_count' in wo][0]
318cdd67
O
295 delimiter = color_func(c['TWEET']['favorite_count'])(
296 str(favorite_count).join(word.split('#fa_count')))
297 formater = delimiter.join(formater.split(word))
8c7ae594 298 except:
318cdd67 299 pass
7500d90b 300
8c7ae594
O
301 # Draw
302 printNicely(formater)
7500d90b
VNM
303
304 # Display Image
fe9bb33b 305 if c['IMAGE_ON_TERM'] and media_url:
7500d90b 306 for mu in media_url:
17bc529d 307 try:
308 response = requests.get(mu)
77f1d210 309 image_to_display(BytesIO(response.content))
310 except Exception:
17bc529d 311 printNicely(red('Sorry, image link is broken'))
7500d90b
VNM
312
313
67c663f8
O
314def print_threads(d):
315 """
316 Print threads of messages
317 """
318 id = 1
319 rel = {}
320 for partner in d:
321 messages = d[partner]
322 count = len(messages)
323 screen_name = '@' + partner[0]
324 name = partner[1]
325 screen_name = color_func(c['MESSAGE']['partner'])(screen_name)
326 name = cycle_color(name)
03c0d30b 327 thread_id = color_func(c['MESSAGE']['id'])('thread_id:' + str(id))
328 line = ' ' * 2 + name + ' ' + screen_name + \
67c663f8
O
329 ' (' + str(count) + ' message) ' + thread_id
330 printNicely(line)
331 rel[id] = partner
332 id += 1
333 dg['thread'] = d
334 return rel
335
336
337def print_thread(partner, me_nick, me_name):
338 """
339 Print a thread of messages
340 """
341 # Sort messages by time
342 messages = dg['thread'][partner]
03c0d30b 343 messages.sort(key=lambda x: parser.parse(x['created_at']))
344 # Use legacy display on non-ascii text message
bcb5518e 345 ms = [m['text'] for m in messages]
346 ums = [m for m in ms if not all(ord(c) < 128 for c in m)]
347 if ums:
03c0d30b 348 for m in messages:
349 print_message(m)
350 printNicely('')
351 return
352 # Print the first line
353 dg['frame_margin'] = margin = 2
354 partner_nick = partner[0]
355 partner_name = partner[1]
356 left_size = len(partner_nick) + len(partner_name) + 2
357 right_size = len(me_nick) + len(me_name) + 2
358 partner_nick = color_func(c['MESSAGE']['partner'])('@' + partner_nick)
359 partner_name = cycle_color(partner_name)
67c663f8
O
360 me_screen_name = color_func(c['MESSAGE']['me'])('@' + me_nick)
361 me_name = cycle_color(me_name)
03c0d30b 362 left = ' ' * margin + partner_name + ' ' + partner_nick
67c663f8
O
363 right = me_name + ' ' + me_screen_name + ' ' * margin
364 h, w = os.popen('stty size', 'r').read().split()
365 w = int(w)
03c0d30b 366 line = '{}{}{}'.format(
367 left, ' ' * (w - left_size - right_size - 2 * margin), right)
67c663f8
O
368 printNicely('')
369 printNicely(line)
370 printNicely('')
67c663f8
O
371 # Print messages
372 for m in messages:
373 if m['sender_screen_name'] == me_nick:
374 print_right_message(m)
375 elif m['recipient_screen_name'] == me_nick:
376 print_left_message(m)
377
378
379def print_right_message(m):
380 """
381 Print a message on the right of screen
382 """
383 h, w = os.popen('stty size', 'r').read().split()
384 w = int(w)
03c0d30b 385 frame_width = w // 3 - dg['frame_margin']
386 step = frame_width - 2 * dg['frame_margin']
bcb5518e 387 slicing = textwrap.wrap(m['text'], step)
03c0d30b 388 spaces = w - frame_width - dg['frame_margin']
67c663f8 389 dotline = ' ' * spaces + '-' * frame_width
03c0d30b 390 dotline = color_func(c['MESSAGE']['me_frame'])(dotline)
223d2e05 391 # Draw the frame
67c663f8
O
392 printNicely(dotline)
393 for line in slicing:
394 fill = step - len(line)
395 screen_line = ' ' * spaces + '| ' + line + ' ' * fill + ' '
396 if slicing[-1] == line:
397 screen_line = screen_line + ' >'
398 else:
399 screen_line = screen_line + '|'
03c0d30b 400 screen_line = color_func(c['MESSAGE']['me_frame'])(screen_line)
67c663f8
O
401 printNicely(screen_line)
402 printNicely(dotline)
03c0d30b 403 # Format clock
223d2e05
O
404 date = parser.parse(m['created_at'])
405 date = arrow.get(date).to('local').datetime
406 clock_format = '%Y/%m/%d %H:%M:%S'
407 try:
408 clock_format = c['FORMAT']['MESSAGE']['CLOCK_FORMAT']
409 except:
410 pass
411 clock = date.strftime(clock_format)
03c0d30b 412 # Format id
223d2e05
O
413 if m['id'] not in c['message_dict']:
414 c['message_dict'].append(m['id'])
415 rid = len(c['message_dict']) - 1
416 else:
417 rid = c['message_dict'].index(m['id'])
03c0d30b 418 id = str(rid)
419 # Print meta
420 formater = ''
421 try:
422 virtual_meta = formater = c['THREAD_META_RIGHT']
423 virtual_meta = clock.join(virtual_meta.split('#clock'))
424 virtual_meta = id.join(virtual_meta.split('#id'))
425 # Change clock word
426 word = [wo for wo in formater.split() if '#clock' in wo][0]
427 delimiter = color_func(c['MESSAGE']['clock'])(
428 clock.join(word.split('#clock')))
429 formater = delimiter.join(formater.split(word))
430 # Change id word
431 word = [wo for wo in formater.split() if '#id' in wo][0]
432 delimiter = color_func(c['MESSAGE']['id'])(id.join(word.split('#id')))
433 formater = delimiter.join(formater.split(word))
434 except Exception:
435 printNicely(red('Wrong format in config.'))
436 return
437 meta = formater
438 line = ' ' * (w - len(virtual_meta) - dg['frame_margin']) + meta
223d2e05 439 printNicely(line)
67c663f8
O
440
441
442def print_left_message(m):
443 """
444 Print a message on the left of screen
445 """
446 h, w = os.popen('stty size', 'r').read().split()
447 w = int(w)
03c0d30b 448 frame_width = w // 3 - dg['frame_margin']
449 step = frame_width - 2 * dg['frame_margin']
bcb5518e 450 slicing = textwrap.wrap(m['text'], step)
03c0d30b 451 spaces = dg['frame_margin']
67c663f8 452 dotline = ' ' * spaces + '-' * frame_width
03c0d30b 453 dotline = color_func(c['MESSAGE']['partner_frame'])(dotline)
223d2e05 454 # Draw the frame
67c663f8
O
455 printNicely(dotline)
456 for line in slicing:
457 fill = step - len(line)
458 screen_line = ' ' + line + ' ' * fill + ' |'
459 if slicing[-1] == line:
03c0d30b 460 screen_line = ' ' * (spaces - 1) + '< ' + screen_line
67c663f8
O
461 else:
462 screen_line = ' ' * spaces + '|' + screen_line
03c0d30b 463 screen_line = color_func(c['MESSAGE']['partner_frame'])(screen_line)
67c663f8
O
464 printNicely(screen_line)
465 printNicely(dotline)
03c0d30b 466 # Format clock
223d2e05
O
467 date = parser.parse(m['created_at'])
468 date = arrow.get(date).to('local').datetime
469 clock_format = '%Y/%m/%d %H:%M:%S'
470 try:
471 clock_format = c['FORMAT']['MESSAGE']['CLOCK_FORMAT']
472 except:
473 pass
474 clock = date.strftime(clock_format)
03c0d30b 475 # Format id
223d2e05
O
476 if m['id'] not in c['message_dict']:
477 c['message_dict'].append(m['id'])
478 rid = len(c['message_dict']) - 1
479 else:
480 rid = c['message_dict'].index(m['id'])
03c0d30b 481 id = str(rid)
482 # Print meta
483 formater = ''
484 try:
485 virtual_meta = formater = c['THREAD_META_LEFT']
486 virtual_meta = clock.join(virtual_meta.split('#clock'))
487 virtual_meta = id.join(virtual_meta.split('#id'))
488 # Change clock word
489 word = [wo for wo in formater.split() if '#clock' in wo][0]
490 delimiter = color_func(c['MESSAGE']['clock'])(
491 clock.join(word.split('#clock')))
492 formater = delimiter.join(formater.split(word))
493 # Change id word
494 word = [wo for wo in formater.split() if '#id' in wo][0]
495 delimiter = color_func(c['MESSAGE']['id'])(id.join(word.split('#id')))
496 formater = delimiter.join(formater.split(word))
497 except Exception:
498 printNicely(red('Wrong format in config.'))
499 return
500 meta = formater
501 line = ' ' * dg['frame_margin'] + meta
223d2e05 502 printNicely(line)
67c663f8
O
503
504
4dc385b5 505def print_message(m):
7500d90b
VNM
506 """
507 Print direct message
508 """
c37c04a9 509 # Retrieve message
7500d90b
VNM
510 sender_screen_name = '@' + m['sender_screen_name']
511 sender_name = m['sender']['name']
b2cde062 512 text = unescape(m['text'])
7500d90b
VNM
513 recipient_screen_name = '@' + m['recipient_screen_name']
514 recipient_name = m['recipient']['name']
515 mid = m['id']
516 date = parser.parse(m['created_at'])
8b3456f9 517 date = arrow.get(date).to('local').datetime
8c7ae594
O
518 clock_format = '%Y/%m/%d %H:%M:%S'
519 try:
520 clock_format = c['FORMAT']['MESSAGE']['CLOCK_FORMAT']
521 except:
522 pass
523 clock = date.strftime(clock_format)
7500d90b
VNM
524
525 # Get rainbow id
99b52f5f
O
526 if mid not in c['message_dict']:
527 c['message_dict'].append(mid)
528 rid = len(c['message_dict']) - 1
529 else:
530 rid = c['message_dict'].index(mid)
7500d90b 531
6fa09c14 532 # Draw
8c7ae594
O
533 sender_name = cycle_color(sender_name)
534 sender_nick = color_func(c['MESSAGE']['sender'])(sender_screen_name)
535 recipient_name = cycle_color(recipient_name)
0d9977c7
O
536 recipient_nick = color_func(
537 c['MESSAGE']['recipient'])(recipient_screen_name)
8c7ae594 538 to = color_func(c['MESSAGE']['to'])('>>>')
0d9977c7
O
539 clock = clock
540 id = str(rid)
8c7ae594 541
c3bab4ef 542 text = ''.join(lmap(lambda x: x + ' ' if x == '\n' else x, text))
7500d90b 543
8c7ae594
O
544 # Load config formater
545 try:
546 formater = c['FORMAT']['MESSAGE']['DISPLAY']
0d9977c7
O
547 formater = sender_name.join(formater.split("#sender_name"))
548 formater = sender_nick.join(formater.split("#sender_nick"))
549 formater = to.join(formater.split("#to"))
550 formater = recipient_name.join(formater.split("#recipient_name"))
551 formater = recipient_nick.join(formater.split("#recipient_nick"))
552 formater = text.join(formater.split("#message"))
553 # Change clock word
03c0d30b 554 word = [wo for wo in formater.split() if '#clock' in wo][0]
8141aca6 555 delimiter = color_func(c['MESSAGE']['clock'])(
556 clock.join(word.split('#clock')))
0d9977c7
O
557 formater = delimiter.join(formater.split(word))
558 # Change id word
03c0d30b 559 word = [wo for wo in formater.split() if '#id' in wo][0]
0d9977c7
O
560 delimiter = color_func(c['MESSAGE']['id'])(id.join(word.split('#id')))
561 formater = delimiter.join(formater.split(word))
8c7ae594
O
562 except:
563 printNicely(red('Wrong format in config.'))
564 return
565
566 # Draw
567 printNicely(formater)
7500d90b
VNM
568
569
fe9bb33b 570def show_profile(u):
7500d90b
VNM
571 """
572 Show a profile
573 """
574 # Retrieve info
575 name = u['name']
576 screen_name = u['screen_name']
577 description = u['description']
578 profile_image_url = u['profile_image_url']
579 location = u['location']
580 url = u['url']
581 created_at = u['created_at']
582 statuses_count = u['statuses_count']
583 friends_count = u['friends_count']
584 followers_count = u['followers_count']
6fa09c14 585
7500d90b 586 # Create content
c075e6dc
O
587 statuses_count = color_func(
588 c['PROFILE']['statuses_count'])(
589 str(statuses_count) +
590 ' tweets')
591 friends_count = color_func(
592 c['PROFILE']['friends_count'])(
593 str(friends_count) +
594 ' following')
595 followers_count = color_func(
596 c['PROFILE']['followers_count'])(
597 str(followers_count) +
598 ' followers')
7500d90b 599 count = statuses_count + ' ' + friends_count + ' ' + followers_count
c075e6dc
O
600 user = cycle_color(
601 name) + color_func(c['PROFILE']['nick'])(' @' + screen_name + ' : ') + count
602 profile_image_raw_url = 'Profile photo: ' + \
603 color_func(c['PROFILE']['profile_image_url'])(profile_image_url)
7500d90b 604 description = ''.join(
c3bab4ef 605 lmap(lambda x: x + ' ' * 4 if x == '\n' else x, description))
632c6fa5
O
606 description = color_func(c['PROFILE']['description'])(description)
607 location = 'Location : ' + color_func(c['PROFILE']['location'])(location)
608 url = 'URL : ' + (color_func(c['PROFILE']['url'])(url) if url else '')
7500d90b 609 date = parser.parse(created_at)
8b3456f9 610 lang, encode = locale.getdefaultlocale()
611 clock = arrow.get(date).to('local').humanize(locale=lang)
632c6fa5 612 clock = 'Join at ' + color_func(c['PROFILE']['clock'])(clock)
6fa09c14 613
7500d90b
VNM
614 # Format
615 line1 = u"{u:>{uw}}".format(
616 u=user,
617 uw=len(user) + 2,
618 )
619 line2 = u"{p:>{pw}}".format(
620 p=profile_image_raw_url,
621 pw=len(profile_image_raw_url) + 4,
622 )
623 line3 = u"{d:>{dw}}".format(
624 d=description,
625 dw=len(description) + 4,
626 )
627 line4 = u"{l:>{lw}}".format(
628 l=location,
629 lw=len(location) + 4,
630 )
631 line5 = u"{u:>{uw}}".format(
632 u=url,
633 uw=len(url) + 4,
634 )
635 line6 = u"{c:>{cw}}".format(
636 c=clock,
637 cw=len(clock) + 4,
638 )
6fa09c14 639
7500d90b
VNM
640 # Display
641 printNicely('')
642 printNicely(line1)
fe9bb33b 643 if c['IMAGE_ON_TERM']:
17bc529d 644 try:
645 response = requests.get(profile_image_url)
c37c04a9 646 image_to_display(BytesIO(response.content))
17bc529d 647 except:
648 pass
7500d90b
VNM
649 else:
650 printNicely(line2)
651 for line in [line3, line4, line5, line6]:
652 printNicely(line)
653 printNicely('')
654
655
656def print_trends(trends):
657 """
658 Display topics
659 """
632c6fa5 660 for topic in trends[:c['TREND_MAX']]:
7500d90b
VNM
661 name = topic['name']
662 url = topic['url']
8394e34b 663 line = cycle_color(name) + ': ' + color_func(c['TREND']['url'])(url)
7500d90b
VNM
664 printNicely(line)
665 printNicely('')
2d341029
O
666
667
668def print_list(group):
669 """
670 Display a list
671 """
99b52f5f 672 for grp in group:
2d341029 673 # Format
99b52f5f 674 name = grp['full_name']
2d341029 675 name = color_func(c['GROUP']['name'])(name + ' : ')
99b52f5f 676 member = str(grp['member_count'])
422dd385 677 member = color_func(c['GROUP']['member'])(member + ' member')
99b52f5f 678 subscriber = str(grp['subscriber_count'])
422dd385
O
679 subscriber = color_func(
680 c['GROUP']['subscriber'])(
681 subscriber +
682 ' subscriber')
99b52f5f 683 description = grp['description'].strip()
2d341029 684 description = color_func(c['GROUP']['description'])(description)
99b52f5f 685 mode = grp['mode']
422dd385 686 mode = color_func(c['GROUP']['mode'])('Type: ' + mode)
99b52f5f 687 created_at = grp['created_at']
2d341029 688 date = parser.parse(created_at)
8b3456f9 689 lang, encode = locale.getdefaultlocale()
690 clock = arrow.get(date).to('local').humanize(locale=lang)
2d341029
O
691 clock = 'Created at ' + color_func(c['GROUP']['clock'])(clock)
692
2d341029 693 # Create lines
422dd385
O
694 line1 = ' ' * 2 + name + member + ' ' + subscriber
695 line2 = ' ' * 4 + description
696 line3 = ' ' * 4 + mode
697 line4 = ' ' * 4 + clock
2d341029
O
698
699 # Display
700 printNicely('')
701 printNicely(line1)
702 printNicely(line2)
703 printNicely(line3)
704 printNicely(line4)
705
706 printNicely('')
59262e95
O
707
708
8141aca6 709def show_calendar(month, date, rel):
710 """
711 Show the calendar in rainbow mode
712 """
713 month = random_rainbow(month)
714 date = ' '.join([cycle_color(i) for i in date.split(' ')])
715 today = str(int(os.popen('date +\'%d\'').read().strip()))
716 # Display
717 printNicely(month)
718 printNicely(date)
719 for line in rel:
720 ary = line.split(' ')
721 ary = lmap(
722 lambda x: color_func(c['CAL']['today'])(x)
723 if x == today
724 else color_func(c['CAL']['days'])(x),
725 ary)
726 printNicely(' '.join(ary))
727
728
b7c9c570 729def format_quote(tweet):
730 """
731 Quoting format
732 """
733 # Retrieve info
734 screen_name = '@' + tweet['user']['screen_name']
735 text = tweet['text']
736 # Validate quote format
737 if '#owner' not in c['QUOTE_FORMAT']:
738 printNicely(light_magenta('Quote should contains #owner'))
739 return False
740 if '#comment' not in c['QUOTE_FORMAT']:
741 printNicely(light_magenta('Quote format should have #comment'))
742 return False
743 # Build formater
744 formater = ''
745 try:
746 formater = c['QUOTE_FORMAT']
747 formater = screen_name.join(formater.split('#owner'))
748 formater = text.join(formater.split('#tweet'))
749 formater = u2str(formater)
750 except:
751 pass
752 # Highlight like a tweet
4dc385b5
O
753 notice = formater.split()
754 notice = lmap(
b7c9c570 755 lambda x: light_green(x)
756 if x == '#comment'
757 else x,
4dc385b5
O
758 notice)
759 notice = lmap(
b7c9c570 760 lambda x: color_func(c['TWEET']['rt'])(x)
761 if x == 'RT'
762 else x,
4dc385b5
O
763 notice)
764 notice = lmap(lambda x: cycle_color(x) if x[0] == '@' else x, notice)
765 notice = lmap(
b7c9c570 766 lambda x: color_func(c['TWEET']['link'])(x)
767 if x[0:4] == 'http'
768 else x,
4dc385b5
O
769 notice)
770 notice = lmap(
b7c9c570 771 lambda x: color_func(c['TWEET']['hashtag'])(x)
772 if x.startswith('#')
773 else x,
4dc385b5
O
774 notice)
775 notice = ' '.join(notice)
b7c9c570 776 # Notice
4dc385b5 777 notice = light_magenta('Quoting: "') + notice + light_magenta('"')
b7c9c570 778 printNicely(notice)
779 return formater
780
781
59262e95 782# Start the color cycle
b2cde062 783start_cycle()