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