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