adjust message
[rainbowstream.git] / rainbowstream / rainbow.py
CommitLineData
b2b933a9 1import os
2import os.path
3import sys
4import signal
5import argparse
6import time
92983945 7import threading
991c30af 8import requests
80b70d60 9import webbrowser
7a8a52fc 10import traceback
6e8fb961 11import pkg_resources
91476ec3 12
91476ec3 13from twitter.stream import TwitterStream, Timeout, HeartbeatTimeout, Hangup
54277114 14from twitter.api import *
91476ec3 15from twitter.oauth import OAuth, read_token_file
8c840a83 16from twitter.oauth_dance import oauth_dance
91476ec3 17from twitter.util import printNicely
91476ec3 18
7500d90b 19from .draw import *
2a6238f5
O
20from .colors import *
21from .config import *
777c52d4 22from .consumer import *
94a5f62e 23from .interactive import *
991c30af 24from .c_image import *
c3bab4ef 25from .py3patch import *
841260fe 26from .emoji import *
c3bab4ef 27
531f5682 28# Global values
f405a7d0 29g = {}
531f5682 30
92983945 31# Lock for streams
92983945
BS
32StreamLock = threading.Lock()
33
c075e6dc 34
91476ec3
O
35def parse_arguments():
36 """
37 Parse the arguments
38 """
91476ec3 39 parser = argparse.ArgumentParser(description=__doc__ or "")
2a6238f5
O
40 parser.add_argument(
41 '-to',
42 '--timeout',
43 help='Timeout for the stream (seconds).')
44 parser.add_argument(
2a6238f5
O
45 '-tt',
46 '--track-keywords',
47 help='Search the stream for specific text.')
d51b4107
O
48 parser.add_argument(
49 '-fil',
50 '--filter',
51 help='Filter specific screen_name.')
52 parser.add_argument(
53 '-ig',
54 '--ignore',
55 help='Ignore specific screen_name.')
88af38d8 56 parser.add_argument(
c1fa7c94
O
57 '-iot',
58 '--image-on-term',
59 action='store_true',
60 help='Display all image on terminal.')
91476ec3
O
61 return parser.parse_args()
62
63
54277114
O
64def authen():
65 """
7b674cef 66 Authenticate with Twitter OAuth
54277114 67 """
8c840a83 68 # When using rainbow stream you must authorize.
2a6238f5
O
69 twitter_credential = os.environ.get(
70 'HOME',
71 os.environ.get(
72 'USERPROFILE',
73 '')) + os.sep + '.rainbow_oauth'
8c840a83
O
74 if not os.path.exists(twitter_credential):
75 oauth_dance("Rainbow Stream",
76 CONSUMER_KEY,
77 CONSUMER_SECRET,
78 twitter_credential)
79 oauth_token, oauth_token_secret = read_token_file(twitter_credential)
54277114 80 return OAuth(
2a6238f5
O
81 oauth_token,
82 oauth_token_secret,
83 CONSUMER_KEY,
84 CONSUMER_SECRET)
91476ec3 85
54277114 86
e3927852
O
87def build_mute_dict(dict_data=False):
88 """
89 Build muting list
90 """
91 t = Twitter(auth=authen())
92 # Init cursor
93 next_cursor = -1
94 screen_name_list = []
95 name_list = []
96 # Cursor loop
97 while next_cursor != 0:
98 list = t.mutes.users.list(
99 screen_name=g['original_name'],
100 cursor=next_cursor,
101 skip_status=True,
102 include_entities=False,
103 )
104 screen_name_list += ['@' + u['screen_name'] for u in list['users']]
105 name_list += [u['name'] for u in list['users']]
106 next_cursor = list['next_cursor']
107 # Return dict or list
108 if dict_data:
109 return dict(zip(screen_name_list, name_list))
110 else:
111 return screen_name_list
112
113
7a8a52fc
O
114def debug_option():
115 """
116 Save traceback when run in debug mode
117 """
118 if g['debug']:
119 g['traceback'].append(traceback.format_exc())
120
121
6e8fb961 122def upgrade_center():
123 """
124 Check latest and notify to upgrade
125 """
126 try:
dfbc7288 127 current = pkg_resources.get_distribution("rainbowstream").version
6e8fb961 128 url = 'https://raw.githubusercontent.com/DTVD/rainbowstream/master/setup.py'
dfbc7288 129 readme = requests.get(url).content
6e8fb961 130 latest = readme.split("version = \'")[1].split("\'")[0]
131 if current != latest:
dfbc7288 132 notice = light_magenta('RainbowStream latest version is ')
6e8fb961 133 notice += light_green(latest)
134 notice += light_magenta(' while your current version is ')
135 notice += light_yellow(current) + '\n'
dfbc7288 136 notice += light_magenta('You should upgrade with ')
137 notice += light_green('pip install -U rainbowstream')
6e8fb961 138 printNicely(notice)
139 except:
140 pass
141
142
fe9bb33b 143def init(args):
54277114 144 """
9683e61d 145 Init function
54277114 146 """
64156ac4
O
147 # Handle Ctrl C
148 ctrl_c_handler = lambda signum, frame: quit()
149 signal.signal(signal.SIGINT, ctrl_c_handler)
6e8fb961 150 # Upgrade notify
151 upgrade_center()
9683e61d 152 # Get name
54277114 153 t = Twitter(auth=authen())
67c663f8
O
154 credential = t.account.verify_credentials()
155 screen_name = '@' + credential['screen_name']
156 name = credential['name']
ceec8593 157 if not get_config('PREFIX'):
67c663f8 158 set_config('PREFIX', screen_name)
841260fe 159 c['PREFIX'] = emojize(c['PREFIX'])
c285decf 160 g['PREFIX'] = u2str(c['PREFIX'])
37cf396a 161 c['original_name'] = g['original_name'] = screen_name[1:]
67c663f8 162 g['full_name'] = name
ceec8593 163 g['decorated_name'] = lambda x: color_func(
a8e71259 164 c['DECORATED_NAME'])('[' + x + ']: ')
9683e61d 165 # Theme init
422dd385 166 files = os.listdir(os.path.dirname(__file__) + '/colorset')
c075e6dc 167 themes = [f.split('.')[0] for f in files if f.split('.')[-1] == 'json']
632c6fa5 168 g['themes'] = themes
4dc385b5 169 g['pause'] = False
67c663f8 170 g['message_threads'] = {}
99cd1fba
O
171 # Events
172 g['events'] = []
4824b181 173 # Startup cmd
f1c1dfea 174 g['cmd'] = ''
6e8fb961 175 # Debug option default = True
176 g['debug'] = True
7a8a52fc 177 g['traceback'] = []
38a6dc30
O
178 # Retweet of mine events
179 c['events'] = []
9683e61d 180 # Semaphore init
99b52f5f 181 c['lock'] = False
99b52f5f
O
182 # Init tweet dict and message dict
183 c['tweet_dict'] = []
184 c['message_dict'] = []
fe9bb33b 185 # Image on term
186 c['IMAGE_ON_TERM'] = args.image_on_term
62686013 187 set_config('IMAGE_ON_TERM', str(c['IMAGE_ON_TERM']))
e3927852
O
188 # Mute dict
189 c['IGNORE_LIST'] += build_mute_dict()
f405a7d0 190
ceec8593 191
4592d231 192def trend():
193 """
194 Trend
195 """
196 t = Twitter(auth=authen())
48a25fe8 197 # Get country and town
4592d231 198 try:
199 country = g['stuff'].split()[0]
200 except:
201 country = ''
48a25fe8 202 try:
203 town = g['stuff'].split()[1]
204 except:
205 town = ''
48a25fe8 206 avail = t.trends.available()
207 # World wide
208 if not country:
209 trends = t.trends.place(_id=1)[0]['trends']
210 print_trends(trends)
211 else:
212 for location in avail:
213 # Search for country and Town
214 if town:
215 if location['countryCode'] == country \
216 and location['placeType']['name'] == 'Town' \
217 and location['name'] == town:
218 trends = t.trends.place(_id=location['woeid'])[0]['trends']
219 print_trends(trends)
220 # Search for country only
221 else:
222 if location['countryCode'] == country \
223 and location['placeType']['name'] == 'Country':
224 trends = t.trends.place(_id=location['woeid'])[0]['trends']
225 print_trends(trends)
4592d231 226
227
7b674cef 228def home():
229 """
230 Home
231 """
232 t = Twitter(auth=authen())
632c6fa5 233 num = c['HOME_TWEET_NUM']
7b674cef 234 if g['stuff'].isdigit():
305ce127 235 num = int(g['stuff'])
94a5f62e 236 for tweet in reversed(t.statuses.home_timeline(count=num)):
fe9bb33b 237 draw(t=tweet)
94a5f62e 238 printNicely('')
7b674cef 239
240
99cd1fba
O
241def notification():
242 """
243 Show notifications
244 """
38a6dc30 245 g['events'] = g['events'] + c['events']
99cd1fba
O
246 if g['events']:
247 for e in g['events']:
248 print_event(e)
249 printNicely('')
250 else:
251 printNicely(magenta('Nothing at this time.'))
252
253
fd87ddac
O
254def mentions():
255 """
256 Mentions timeline
257 """
258 t = Twitter(auth=authen())
259 num = c['HOME_TWEET_NUM']
260 if g['stuff'].isdigit():
261 num = int(g['stuff'])
262 for tweet in reversed(t.statuses.mentions_timeline(count=num)):
263 draw(t=tweet)
264 printNicely('')
265
266
267def whois():
268 """
269 Show profile of a specific user
270 """
271 t = Twitter(auth=authen())
272 screen_name = g['stuff'].split()[0]
273 if screen_name.startswith('@'):
274 try:
275 user = t.users.show(
276 screen_name=screen_name[1:],
277 include_entities=False)
278 show_profile(user)
279 except:
7a8a52fc
O
280 debug_option()
281 printNicely(red('No user.'))
fd87ddac
O
282 else:
283 printNicely(red('A name should begin with a \'@\''))
284
285
7b674cef 286def view():
287 """
288 Friend view
289 """
290 t = Twitter(auth=authen())
291 user = g['stuff'].split()[0]
b8fbcb70 292 if user[0] == '@':
293 try:
94a5f62e 294 num = int(g['stuff'].split()[1])
b8fbcb70 295 except:
632c6fa5 296 num = c['HOME_TWEET_NUM']
94a5f62e 297 for tweet in reversed(t.statuses.user_timeline(count=num, screen_name=user[1:])):
fe9bb33b 298 draw(t=tweet)
94a5f62e 299 printNicely('')
b8fbcb70 300 else:
c91f75f2 301 printNicely(red('A name should begin with a \'@\''))
7b674cef 302
303
fd87ddac 304def search():
2d0ad040 305 """
fd87ddac 306 Search
2d0ad040
J
307 """
308 t = Twitter(auth=authen())
954b3101 309 # Setup query
310 query = g['stuff'].strip()
311 type = c['SEARCH_TYPE']
312 if type not in ['mixed', 'recent', 'popular']:
313 type = 'mixed'
314 max_record = c['SEARCH_MAX_RECORD']
315 count = min(max_record, 100)
316 # Perform search
317 rel = t.search.tweets(
318 q=query,
319 type=type,
320 count=count
321 )['statuses']
322 # Return results
fd87ddac
O
323 if rel:
324 printNicely('Newest tweets:')
954b3101 325 for i in reversed(xrange(count)):
326 draw(t=rel[i], keyword=query)
fd87ddac
O
327 printNicely('')
328 else:
329 printNicely(magenta('I\'m afraid there is no result'))
2d0ad040
J
330
331
f405a7d0 332def tweet():
54277114 333 """
7b674cef 334 Tweet
54277114
O
335 """
336 t = Twitter(auth=authen())
f405a7d0 337 t.statuses.update(status=g['stuff'])
f405a7d0 338
b2b933a9 339
1ba4abfd
O
340def retweet():
341 """
342 ReTweet
343 """
344 t = Twitter(auth=authen())
345 try:
346 id = int(g['stuff'].split()[0])
1ba4abfd 347 except:
b8c1f42a
O
348 printNicely(red('Sorry I can\'t understand.'))
349 return
99b52f5f 350 tid = c['tweet_dict'][id]
b8c1f42a 351 t.statuses.retweet(id=tid, include_entities=False, trim_user=True)
1ba4abfd
O
352
353
80b70d60
O
354def quote():
355 """
356 Quote a tweet
357 """
b7c9c570 358 # Get tweet
80b70d60
O
359 t = Twitter(auth=authen())
360 try:
361 id = int(g['stuff'].split()[0])
362 except:
363 printNicely(red('Sorry I can\'t understand.'))
364 return
99b52f5f 365 tid = c['tweet_dict'][id]
80b70d60 366 tweet = t.statuses.show(id=tid)
b7c9c570 367 # Get formater
368 formater = format_quote(tweet)
369 if not formater:
7c437a0f 370 return
7c437a0f
O
371 # Get comment
372 prefix = light_magenta('Compose your ') + light_green('#comment: ')
373 comment = raw_input(prefix)
374 if comment:
375 quote = comment.join(formater.split('#comment'))
b7c9c570 376 t.statuses.update(status=quote)
80b70d60
O
377 else:
378 printNicely(light_magenta('No text added.'))
379
380
1f24a05a 381def allretweet():
382 """
383 List all retweet
384 """
385 t = Twitter(auth=authen())
386 # Get rainbow id
387 try:
388 id = int(g['stuff'].split()[0])
389 except:
390 printNicely(red('Sorry I can\'t understand.'))
391 return
99b52f5f 392 tid = c['tweet_dict'][id]
1f24a05a 393 # Get display num if exist
394 try:
395 num = int(g['stuff'].split()[1])
396 except:
632c6fa5 397 num = c['RETWEETS_SHOW_NUM']
1f24a05a 398 # Get result and display
d8e901a4 399 rt_ary = t.statuses.retweets(id=tid, count=num)
1f24a05a 400 if not rt_ary:
401 printNicely(magenta('This tweet has no retweet.'))
402 return
403 for tweet in reversed(rt_ary):
fe9bb33b 404 draw(t=tweet)
1f24a05a 405 printNicely('')
406
407
fd87ddac 408def conversation():
7e4ccbf3 409 """
fd87ddac 410 Conversation view
7e4ccbf3 411 """
412 t = Twitter(auth=authen())
413 try:
414 id = int(g['stuff'].split()[0])
7e4ccbf3 415 except:
b8c1f42a
O
416 printNicely(red('Sorry I can\'t understand.'))
417 return
99b52f5f 418 tid = c['tweet_dict'][id]
fd87ddac
O
419 tweet = t.statuses.show(id=tid)
420 limit = c['CONVERSATION_MAX']
421 thread_ref = []
422 thread_ref.append(tweet)
423 prev_tid = tweet['in_reply_to_status_id']
424 while prev_tid and limit:
425 limit -= 1
426 tweet = t.statuses.show(id=prev_tid)
427 prev_tid = tweet['in_reply_to_status_id']
428 thread_ref.append(tweet)
429
430 for tweet in reversed(thread_ref):
431 draw(t=tweet)
b8c1f42a 432 printNicely('')
7e4ccbf3 433
434
7b674cef 435def reply():
829cc2d8 436 """
7b674cef 437 Reply
829cc2d8
O
438 """
439 t = Twitter(auth=authen())
7b674cef 440 try:
441 id = int(g['stuff'].split()[0])
7b674cef 442 except:
c91f75f2 443 printNicely(red('Sorry I can\'t understand.'))
b8c1f42a 444 return
99b52f5f 445 tid = c['tweet_dict'][id]
b8c1f42a
O
446 user = t.statuses.show(id=tid)['user']['screen_name']
447 status = ' '.join(g['stuff'].split()[1:])
7c437a0f 448 status = '@' + user + ' ' + str2u(status)
b8c1f42a 449 t.statuses.update(status=status, in_reply_to_status_id=tid)
7b674cef 450
451
fd87ddac 452def favorite():
7b674cef 453 """
fd87ddac 454 Favorite
7b674cef 455 """
456 t = Twitter(auth=authen())
457 try:
99b52f5f 458 id = int(g['stuff'].split()[0])
7b674cef 459 except:
305ce127 460 printNicely(red('Sorry I can\'t understand.'))
b8c1f42a 461 return
99b52f5f 462 tid = c['tweet_dict'][id]
fd87ddac
O
463 t.favorites.create(_id=tid, include_entities=False)
464 printNicely(green('Favorited.'))
465 draw(t.statuses.show(id=tid))
466 printNicely('')
829cc2d8
O
467
468
7e4ccbf3 469def unfavorite():
470 """
471 Unfavorite
472 """
473 t = Twitter(auth=authen())
474 try:
475 id = int(g['stuff'].split()[0])
7e4ccbf3 476 except:
b8c1f42a
O
477 printNicely(red('Sorry I can\'t understand.'))
478 return
99b52f5f 479 tid = c['tweet_dict'][id]
b8c1f42a
O
480 t.favorites.destroy(_id=tid)
481 printNicely(green('Okay it\'s unfavorited.'))
fe9bb33b 482 draw(t.statuses.show(id=tid))
b8c1f42a 483 printNicely('')
7e4ccbf3 484
485
fd87ddac 486def delete():
305ce127 487 """
fd87ddac 488 Delete
305ce127 489 """
490 t = Twitter(auth=authen())
fd87ddac
O
491 try:
492 id = int(g['stuff'].split()[0])
493 except:
494 printNicely(red('Sorry I can\'t understand.'))
495 return
496 tid = c['tweet_dict'][id]
497 t.statuses.destroy(id=tid)
498 printNicely(green('Okay it\'s gone.'))
305ce127 499
500
f5677fb1 501def show():
843647ad 502 """
f5677fb1 503 Show image
843647ad
O
504 """
505 t = Twitter(auth=authen())
f5677fb1
O
506 try:
507 target = g['stuff'].split()[0]
508 if target != 'image':
509 return
510 id = int(g['stuff'].split()[1])
99b52f5f 511 tid = c['tweet_dict'][id]
f5677fb1
O
512 tweet = t.statuses.show(id=tid)
513 media = tweet['entities']['media']
514 for m in media:
515 res = requests.get(m['media_url'])
b3164e62 516 img = Image.open(BytesIO(res.content))
f5677fb1
O
517 img.show()
518 except:
7a8a52fc 519 debug_option()
f5677fb1 520 printNicely(red('Sorry I can\'t show this image.'))
843647ad
O
521
522
80bb2040 523def urlopen():
80b70d60
O
524 """
525 Open url
526 """
527 t = Twitter(auth=authen())
528 try:
529 if not g['stuff'].isdigit():
530 return
8101275e 531 tid = c['tweet_dict'][int(g['stuff'])]
80b70d60 532 tweet = t.statuses.show(id=tid)
571ea706
O
533 link_prefix = ('http://', 'https://')
534 link_ary = [u for u in tweet['text'].split()
535 if u.startswith(link_prefix)]
80b70d60
O
536 if not link_ary:
537 printNicely(light_magenta('No url here @.@!'))
538 return
539 for link in link_ary:
540 webbrowser.open(link)
541 except:
7a8a52fc 542 debug_option()
80b70d60
O
543 printNicely(red('Sorry I can\'t open url in this tweet.'))
544
545
305ce127 546def inbox():
547 """
67c663f8 548 Inbox threads
305ce127 549 """
550 t = Twitter(auth=authen())
632c6fa5 551 num = c['MESSAGES_DISPLAY']
305ce127 552 if g['stuff'].isdigit():
553 num = g['stuff']
67c663f8 554 # Get inbox messages
305ce127 555 cur_page = 1
67c663f8 556 inbox = []
305ce127 557 while num > 20:
67c663f8 558 inbox = inbox + t.direct_messages(
305ce127 559 count=20,
560 page=cur_page,
561 include_entities=False,
562 skip_status=False
48a25fe8 563 )
305ce127 564 num -= 20
565 cur_page += 1
67c663f8 566 inbox = inbox + t.direct_messages(
305ce127 567 count=num,
568 page=cur_page,
569 include_entities=False,
570 skip_status=False
48a25fe8 571 )
67c663f8 572 # Get sent messages
632c6fa5 573 num = c['MESSAGES_DISPLAY']
305ce127 574 if g['stuff'].isdigit():
67c663f8 575 num = g['stuff']
305ce127 576 cur_page = 1
67c663f8 577 sent = []
305ce127 578 while num > 20:
67c663f8 579 sent = sent + t.direct_messages.sent(
305ce127 580 count=20,
581 page=cur_page,
582 include_entities=False,
583 skip_status=False
48a25fe8 584 )
305ce127 585 num -= 20
586 cur_page += 1
67c663f8 587 sent = sent + t.direct_messages.sent(
305ce127 588 count=num,
589 page=cur_page,
590 include_entities=False,
591 skip_status=False
48a25fe8 592 )
67c663f8
O
593
594 d = {}
595 uniq_inbox = list(set(
03c0d30b 596 [(m['sender_screen_name'], m['sender']['name']) for m in inbox]
67c663f8 597 ))
03c0d30b 598 uniq_sent = list(set(
599 [(m['recipient_screen_name'], m['recipient']['name']) for m in sent]
67c663f8
O
600 ))
601 for partner in uniq_inbox:
602 inbox_ary = [m for m in inbox if m['sender_screen_name'] == partner[0]]
03c0d30b 603 sent_ary = [
604 m for m in sent if m['recipient_screen_name'] == partner[0]]
67c663f8
O
605 d[partner] = inbox_ary + sent_ary
606 for partner in uniq_sent:
607 if partner not in d:
03c0d30b 608 d[partner] = [
609 m for m in sent if m['recipient_screen_name'] == partner[0]]
67c663f8
O
610 g['message_threads'] = print_threads(d)
611
612
613def thread():
614 """
615 View a thread of message
616 """
617 try:
618 thread_id = int(g['stuff'])
03c0d30b 619 print_thread(
620 g['message_threads'][thread_id],
621 g['original_name'],
622 g['full_name'])
623 except Exception:
7a8a52fc 624 debug_option()
67c663f8 625 printNicely(red('No such thread.'))
e2b81717 626
305ce127 627
fd87ddac
O
628def message():
629 """
630 Send a direct message
631 """
632 t = Twitter(auth=authen())
03c0d30b 633 try:
634 user = g['stuff'].split()[0]
635 if user[0].startswith('@'):
636 content = ' '.join(g['stuff'].split()[1:])
637 t.direct_messages.new(
638 screen_name=user[1:],
639 text=content
640 )
641 printNicely(green('Message sent.'))
642 else:
643 printNicely(red('A name should begin with a \'@\''))
644 except:
7a8a52fc 645 debug_option()
03c0d30b 646 printNicely(red('Sorry I can\'t understand.'))
fd87ddac
O
647
648
305ce127 649def trash():
650 """
651 Remove message
652 """
653 t = Twitter(auth=authen())
654 try:
99b52f5f 655 id = int(g['stuff'].split()[0])
305ce127 656 except:
657 printNicely(red('Sorry I can\'t understand.'))
99b52f5f 658 mid = c['message_dict'][id]
b8c1f42a
O
659 t.direct_messages.destroy(id=mid)
660 printNicely(green('Message deleted.'))
305ce127 661
662
fd87ddac 663def ls():
e2b81717 664 """
fd87ddac 665 List friends for followers
e2b81717
O
666 """
667 t = Twitter(auth=authen())
fd87ddac
O
668 # Get name
669 try:
670 name = g['stuff'].split()[1]
671 if name.startswith('@'):
672 name = name[1:]
673 else:
674 printNicely(red('A name should begin with a \'@\''))
675 raise Exception('Invalid name')
676 except:
677 name = g['original_name']
678 # Get list followers or friends
679 try:
680 target = g['stuff'].split()[0]
681 except:
682 printNicely(red('Omg some syntax is wrong.'))
683 # Init cursor
684 d = {'fl': 'followers', 'fr': 'friends'}
685 next_cursor = -1
686 rel = {}
687 # Cursor loop
688 while next_cursor != 0:
689 list = getattr(t, d[target]).list(
690 screen_name=name,
691 cursor=next_cursor,
692 skip_status=True,
693 include_entities=False,
694 )
695 for u in list['users']:
696 rel[u['name']] = '@' + u['screen_name']
697 next_cursor = list['next_cursor']
698 # Print out result
699 printNicely('All: ' + str(len(rel)) + ' ' + d[target] + '.')
700 for name in rel:
701 user = ' ' + cycle_color(name)
702 user += color_func(c['TWEET']['nick'])(' ' + rel[name] + ' ')
703 printNicely(user)
e2b81717
O
704
705
f5677fb1 706def follow():
843647ad 707 """
f5677fb1 708 Follow a user
843647ad
O
709 """
710 t = Twitter(auth=authen())
f5677fb1 711 screen_name = g['stuff'].split()[0]
b8c1f42a
O
712 if screen_name.startswith('@'):
713 t.friendships.create(screen_name=screen_name[1:], follow=True)
714 printNicely(green('You are following ' + screen_name + ' now!'))
f5677fb1 715 else:
b8c1f42a 716 printNicely(red('A name should begin with a \'@\''))
f5677fb1
O
717
718
719def unfollow():
720 """
721 Unfollow a user
722 """
723 t = Twitter(auth=authen())
724 screen_name = g['stuff'].split()[0]
b8c1f42a
O
725 if screen_name.startswith('@'):
726 t.friendships.destroy(
727 screen_name=screen_name[1:],
728 include_entities=False)
729 printNicely(green('Unfollow ' + screen_name + ' success!'))
f5677fb1 730 else:
b8c1f42a 731 printNicely(red('A name should begin with a \'@\''))
843647ad
O
732
733
5b2c4faf 734def mute():
735 """
736 Mute a user
737 """
738 t = Twitter(auth=authen())
739 try:
740 screen_name = g['stuff'].split()[0]
741 except:
742 printNicely(red('A name should be specified. '))
743 return
744 if screen_name.startswith('@'):
e3927852
O
745 try:
746 rel = t.mutes.users.create(screen_name=screen_name[1:])
747 if isinstance(rel, dict):
748 printNicely(green(screen_name + ' is muted.'))
612d6863 749 c['IGNORE_LIST'] += [unc(screen_name)]
e3927852
O
750 c['IGNORE_LIST'] = list(set(c['IGNORE_LIST']))
751 else:
752 printNicely(red(rel))
753 except:
7a8a52fc 754 debug_option()
e3927852 755 printNicely(red('Something is wrong, can not mute now :('))
5b2c4faf 756 else:
757 printNicely(red('A name should begin with a \'@\''))
758
759
760def unmute():
761 """
762 Unmute a user
763 """
764 t = Twitter(auth=authen())
765 try:
766 screen_name = g['stuff'].split()[0]
767 except:
768 printNicely(red('A name should be specified. '))
769 return
770 if screen_name.startswith('@'):
e3927852
O
771 try:
772 rel = t.mutes.users.destroy(screen_name=screen_name[1:])
773 if isinstance(rel, dict):
774 printNicely(green(screen_name + ' is unmuted.'))
775 c['IGNORE_LIST'].remove(screen_name)
776 else:
777 printNicely(red(rel))
778 except:
779 printNicely(red('Maybe you are not muting this person ?'))
5b2c4faf 780 else:
781 printNicely(red('A name should begin with a \'@\''))
782
783
784def muting():
785 """
786 List muting user
787 """
e3927852
O
788 # Get dict of muting users
789 md = build_mute_dict(dict_data=True)
790 printNicely('All: ' + str(len(md)) + ' people.')
791 for name in md:
792 user = ' ' + cycle_color(md[name])
793 user += color_func(c['TWEET']['nick'])(' ' + name + ' ')
5b2c4faf 794 printNicely(user)
e3927852
O
795 # Update from Twitter
796 c['IGNORE_LIST'] = [n for n in md]
5b2c4faf 797
798
305ce127 799def block():
800 """
801 Block a user
802 """
803 t = Twitter(auth=authen())
804 screen_name = g['stuff'].split()[0]
b8c1f42a
O
805 if screen_name.startswith('@'):
806 t.blocks.create(
5b2c4faf 807 screen_name=screen_name[1:],
808 include_entities=False,
809 skip_status=True)
b8c1f42a 810 printNicely(green('You blocked ' + screen_name + '.'))
305ce127 811 else:
b8c1f42a 812 printNicely(red('A name should begin with a \'@\''))
305ce127 813
814
815def unblock():
816 """
817 Unblock a user
818 """
819 t = Twitter(auth=authen())
820 screen_name = g['stuff'].split()[0]
b8c1f42a
O
821 if screen_name.startswith('@'):
822 t.blocks.destroy(
823 screen_name=screen_name[1:],
824 include_entities=False,
825 skip_status=True)
826 printNicely(green('Unblock ' + screen_name + ' success!'))
305ce127 827 else:
b8c1f42a 828 printNicely(red('A name should begin with a \'@\''))
305ce127 829
830
831def report():
832 """
833 Report a user as a spam account
834 """
835 t = Twitter(auth=authen())
836 screen_name = g['stuff'].split()[0]
b8c1f42a
O
837 if screen_name.startswith('@'):
838 t.users.report_spam(
839 screen_name=screen_name[1:])
840 printNicely(green('You reported ' + screen_name + '.'))
305ce127 841 else:
842 printNicely(red('Sorry I can\'t understand.'))
843
844
8b8566d1
O
845def get_slug():
846 """
847 Get Slug Decorator
848 """
a8c5fce4 849 # Get list name
8b8566d1
O
850 list_name = raw_input(light_magenta('Give me the list\'s name: '))
851 # Get list name and owner
852 try:
853 owner, slug = list_name.split('/')
854 if slug.startswith('@'):
855 slug = slug[1:]
856 return owner, slug
857 except:
a8c5fce4
O
858 printNicely(
859 light_magenta('List name should follow "@owner/list_name" format.'))
8b8566d1
O
860 raise Exception('Wrong list name')
861
862
2d341029
O
863def show_lists(t):
864 """
422dd385 865 List list
2d341029
O
866 """
867 rel = t.lists.list(screen_name=g['original_name'])
868 if rel:
869 print_list(rel)
870 else:
871 printNicely(light_magenta('You belong to no lists :)'))
872
873
874def list_home(t):
875 """
876 List home
877 """
8b8566d1 878 owner, slug = get_slug()
2d341029 879 res = t.lists.statuses(
422dd385
O
880 slug=slug,
881 owner_screen_name=owner,
882 count=c['LIST_MAX'],
2d341029
O
883 include_entities=False)
884 for tweet in res:
885 draw(t=tweet)
886 printNicely('')
887
888
889def list_members(t):
890 """
891 List members
892 """
8b8566d1 893 owner, slug = get_slug()
422dd385 894 # Get members
2d341029
O
895 rel = {}
896 next_cursor = -1
422dd385 897 while next_cursor != 0:
2d341029 898 m = t.lists.members(
422dd385
O
899 slug=slug,
900 owner_screen_name=owner,
901 cursor=next_cursor,
2d341029
O
902 include_entities=False)
903 for u in m['users']:
904 rel[u['name']] = '@' + u['screen_name']
905 next_cursor = m['next_cursor']
906 printNicely('All: ' + str(len(rel)) + ' members.')
907 for name in rel:
908 user = ' ' + cycle_color(name)
422dd385 909 user += color_func(c['TWEET']['nick'])(' ' + rel[name] + ' ')
2d341029
O
910 printNicely(user)
911
912
913def list_subscribers(t):
914 """
915 List subscribers
916 """
8b8566d1 917 owner, slug = get_slug()
422dd385 918 # Get subscribers
2d341029
O
919 rel = {}
920 next_cursor = -1
422dd385 921 while next_cursor != 0:
2d341029 922 m = t.lists.subscribers(
422dd385
O
923 slug=slug,
924 owner_screen_name=owner,
925 cursor=next_cursor,
2d341029
O
926 include_entities=False)
927 for u in m['users']:
928 rel[u['name']] = '@' + u['screen_name']
929 next_cursor = m['next_cursor']
930 printNicely('All: ' + str(len(rel)) + ' subscribers.')
931 for name in rel:
932 user = ' ' + cycle_color(name)
422dd385 933 user += color_func(c['TWEET']['nick'])(' ' + rel[name] + ' ')
2d341029
O
934 printNicely(user)
935
936
422dd385
O
937def list_add(t):
938 """
939 Add specific user to a list
940 """
8b8566d1 941 owner, slug = get_slug()
422dd385
O
942 # Add
943 user_name = raw_input(light_magenta('Give me name of the newbie: '))
944 if user_name.startswith('@'):
945 user_name = user_name[1:]
946 try:
947 t.lists.members.create(
948 slug=slug,
949 owner_screen_name=owner,
950 screen_name=user_name)
d6cc4c67 951 printNicely(green('Added.'))
422dd385 952 except:
7a8a52fc 953 debug_option()
422dd385
O
954 printNicely(light_magenta('I\'m sorry we can not add him/her.'))
955
956
2d341029
O
957def list_remove(t):
958 """
959 Remove specific user from a list
960 """
8b8566d1 961 owner, slug = get_slug()
2d341029 962 # Remove
422dd385
O
963 user_name = raw_input(light_magenta('Give me name of the unlucky one: '))
964 if user_name.startswith('@'):
965 user_name = user_name[1:]
2d341029
O
966 try:
967 t.lists.members.destroy(
422dd385
O
968 slug=slug,
969 owner_screen_name=owner,
970 screen_name=user_name)
d6cc4c67 971 printNicely(green('Gone.'))
422dd385 972 except:
7a8a52fc 973 debug_option()
422dd385
O
974 printNicely(light_magenta('I\'m sorry we can not remove him/her.'))
975
976
977def list_subscribe(t):
978 """
979 Subscribe to a list
980 """
8b8566d1 981 owner, slug = get_slug()
422dd385
O
982 # Subscribe
983 try:
984 t.lists.subscribers.create(
985 slug=slug,
986 owner_screen_name=owner)
d6cc4c67 987 printNicely(green('Done.'))
422dd385 988 except:
7a8a52fc 989 debug_option()
422dd385
O
990 printNicely(
991 light_magenta('I\'m sorry you can not subscribe to this list.'))
992
993
994def list_unsubscribe(t):
995 """
996 Unsubscribe a list
997 """
8b8566d1 998 owner, slug = get_slug()
422dd385
O
999 # Subscribe
1000 try:
1001 t.lists.subscribers.destroy(
1002 slug=slug,
1003 owner_screen_name=owner)
d6cc4c67 1004 printNicely(green('Done.'))
422dd385 1005 except:
7a8a52fc 1006 debug_option()
422dd385
O
1007 printNicely(
1008 light_magenta('I\'m sorry you can not unsubscribe to this list.'))
1009
1010
1011def list_own(t):
1012 """
1013 List own
1014 """
1015 rel = []
1016 next_cursor = -1
1017 while next_cursor != 0:
1018 res = t.lists.ownerships(
1019 screen_name=g['original_name'],
1020 cursor=next_cursor)
1021 rel += res['lists']
1022 next_cursor = res['next_cursor']
1023 if rel:
1024 print_list(rel)
1025 else:
1026 printNicely(light_magenta('You own no lists :)'))
1027
1028
1029def list_new(t):
1030 """
1031 Create a new list
1032 """
1033 name = raw_input(light_magenta('New list\'s name: '))
1034 mode = raw_input(light_magenta('New list\'s mode (public/private): '))
1035 description = raw_input(light_magenta('New list\'s description: '))
1036 try:
1037 t.lists.create(
1038 name=name,
1039 mode=mode,
1040 description=description)
d6cc4c67 1041 printNicely(green(name + ' list is created.'))
422dd385 1042 except:
7a8a52fc 1043 debug_option()
422dd385
O
1044 printNicely(red('Oops something is wrong with Twitter :('))
1045
1046
1047def list_update(t):
1048 """
1049 Update a list
1050 """
1051 slug = raw_input(light_magenta('Your list that you want to update: '))
1052 name = raw_input(light_magenta('Update name (leave blank to unchange): '))
1053 mode = raw_input(light_magenta('Update mode (public/private): '))
1054 description = raw_input(light_magenta('Update description: '))
1055 try:
1056 if name:
1057 t.lists.update(
1058 slug='-'.join(slug.split()),
1059 owner_screen_name=g['original_name'],
1060 name=name,
1061 mode=mode,
1062 description=description)
1063 else:
1064 t.lists.update(
1065 slug=slug,
1066 owner_screen_name=g['original_name'],
1067 mode=mode,
1068 description=description)
d6cc4c67 1069 printNicely(green(slug + ' list is updated.'))
3c85d8fc 1070 except:
7a8a52fc 1071 debug_option()
422dd385
O
1072 printNicely(red('Oops something is wrong with Twitter :('))
1073
1074
1075def list_delete(t):
1076 """
1077 Delete a list
1078 """
8b3456f9 1079 slug = raw_input(light_magenta('Your list that you want to delete: '))
422dd385
O
1080 try:
1081 t.lists.destroy(
1082 slug='-'.join(slug.split()),
1083 owner_screen_name=g['original_name'])
d6cc4c67 1084 printNicely(green(slug + ' list is deleted.'))
2d341029 1085 except:
7a8a52fc 1086 debug_option()
422dd385 1087 printNicely(red('Oops something is wrong with Twitter :('))
2d341029
O
1088
1089
e3927852 1090def twitterlist():
2d341029
O
1091 """
1092 Twitter's list
1093 """
1094 t = Twitter(auth=authen())
1095 # List all lists or base on action
1096 try:
1097 g['list_action'] = g['stuff'].split()[0]
1098 except:
1099 show_lists(t)
1100 return
422dd385 1101 # Sub-function
2d341029
O
1102 action_ary = {
1103 'home': list_home,
1104 'all_mem': list_members,
1105 'all_sub': list_subscribers,
422dd385 1106 'add': list_add,
2d341029 1107 'rm': list_remove,
422dd385
O
1108 'sub': list_subscribe,
1109 'unsub': list_unsubscribe,
1110 'own': list_own,
1111 'new': list_new,
1112 'update': list_update,
1113 'del': list_delete,
2d341029
O
1114 }
1115 try:
1116 return action_ary[g['list_action']](t)
3c85d8fc 1117 except:
8b8566d1 1118 printNicely(red('Please try again.'))
2d341029
O
1119
1120
fd87ddac
O
1121def switch():
1122 """
1123 Switch stream
1124 """
1125 try:
1126 target = g['stuff'].split()[0]
1127 # Filter and ignore
1128 args = parse_arguments()
1129 try:
1130 if g['stuff'].split()[-1] == '-f':
1131 guide = 'To ignore an option, just hit Enter key.'
1132 printNicely(light_magenta(guide))
1133 only = raw_input('Only nicks [Ex: @xxx,@yy]: ')
1134 ignore = raw_input('Ignore nicks [Ex: @xxx,@yy]: ')
1135 args.filter = filter(None, only.split(','))
1136 args.ignore = filter(None, ignore.split(','))
1137 elif g['stuff'].split()[-1] == '-d':
1138 args.filter = c['ONLY_LIST']
1139 args.ignore = c['IGNORE_LIST']
1140 except:
1141 printNicely(red('Sorry, wrong format.'))
1142 return
1143 # Public stream
1144 if target == 'public':
1145 keyword = g['stuff'].split()[1]
1146 if keyword[0] == '#':
1147 keyword = keyword[1:]
1148 # Kill old thread
1149 g['stream_stop'] = True
1150 args.track_keywords = keyword
1151 # Start new thread
1152 th = threading.Thread(
1153 target=stream,
1154 args=(
1155 c['PUBLIC_DOMAIN'],
1156 args))
1157 th.daemon = True
1158 th.start()
1159 # Personal stream
1160 elif target == 'mine':
1161 # Kill old thread
1162 g['stream_stop'] = True
1163 # Start new thread
1164 th = threading.Thread(
1165 target=stream,
1166 args=(
1167 c['USER_DOMAIN'],
1168 args,
1169 g['original_name']))
1170 th.daemon = True
1171 th.start()
1172 printNicely('')
1173 if args.filter:
1174 printNicely(cyan('Only: ' + str(args.filter)))
1175 if args.ignore:
1176 printNicely(red('Ignore: ' + str(args.ignore)))
1177 printNicely('')
1178 except:
1179 printNicely(red('Sorry I can\'t understand.'))
1180
1181
813a5d80 1182def cal():
1183 """
1184 Unix's command `cal`
1185 """
1186 # Format
1187 rel = os.popen('cal').read().split('\n')
1188 month = rel.pop(0)
813a5d80 1189 date = rel.pop(0)
2a0cabee 1190 show_calendar(month, date, rel)
813a5d80 1191
1192
fd87ddac
O
1193def theme():
1194 """
1195 List and change theme
1196 """
1197 if not g['stuff']:
1198 # List themes
1199 for theme in g['themes']:
1200 line = light_magenta(theme)
1201 if c['THEME'] == theme:
1202 line = ' ' * 2 + light_yellow('* ') + line
1203 else:
1204 line = ' ' * 4 + line
1205 printNicely(line)
1206 else:
1207 # Change theme
1208 try:
1209 # Load new theme
1210 c['THEME'] = reload_theme(g['stuff'], c['THEME'])
1211 # Redefine decorated_name
1212 g['decorated_name'] = lambda x: color_func(
1213 c['DECORATED_NAME'])(
1214 '[' + x + ']: ')
1215 printNicely(green('Theme changed.'))
1216 except:
1217 printNicely(red('No such theme exists.'))
1218
1219
29fd0be6
O
1220def config():
1221 """
1222 Browse and change config
1223 """
1224 all_config = get_all_config()
1225 g['stuff'] = g['stuff'].strip()
1226 # List all config
1227 if not g['stuff']:
1228 for k in all_config:
a8c5fce4 1229 line = ' ' * 2 + \
d6cc4c67 1230 green(k) + ': ' + light_yellow(str(all_config[k]))
29fd0be6
O
1231 printNicely(line)
1232 guide = 'Detailed explanation can be found at ' + \
a8c5fce4
O
1233 color_func(c['TWEET']['link'])(
1234 'http://rainbowstream.readthedocs.org/en/latest/#config-explanation')
29fd0be6
O
1235 printNicely(guide)
1236 # Print specific config
1237 elif len(g['stuff'].split()) == 1:
1238 if g['stuff'] in all_config:
1239 k = g['stuff']
a8c5fce4 1240 line = ' ' * 2 + \
d6cc4c67 1241 green(k) + ': ' + light_yellow(str(all_config[k]))
29fd0be6
O
1242 printNicely(line)
1243 else:
fe9bb33b 1244 printNicely(red('No such config key.'))
29fd0be6
O
1245 # Print specific config's default value
1246 elif len(g['stuff'].split()) == 2 and g['stuff'].split()[-1] == 'default':
1247 key = g['stuff'].split()[0]
fe9bb33b 1248 try:
1249 value = get_default_config(key)
d6cc4c67 1250 line = ' ' * 2 + green(key) + ': ' + light_magenta(value)
fe9bb33b 1251 printNicely(line)
a8e71259 1252 except Exception as e:
1253 printNicely(red(e))
fe9bb33b 1254 # Delete specific config key in config file
1255 elif len(g['stuff'].split()) == 2 and g['stuff'].split()[-1] == 'drop':
1256 key = g['stuff'].split()[0]
1257 try:
1258 delete_config(key)
d6cc4c67 1259 printNicely(green('Config key is dropped.'))
a8e71259 1260 except Exception as e:
1261 printNicely(red(e))
29fd0be6 1262 # Set specific config
a8c5fce4 1263 elif len(g['stuff'].split()) == 3 and g['stuff'].split()[1] == '=':
29fd0be6
O
1264 key = g['stuff'].split()[0]
1265 value = g['stuff'].split()[-1]
ceec8593 1266 if key == 'THEME' and not validate_theme(value):
1267 printNicely(red('Invalid theme\'s value.'))
1268 return
3c01ba57 1269 try:
a8c5fce4 1270 set_config(key, value)
ceec8593 1271 # Apply theme immediately
1272 if key == 'THEME':
baec5f50 1273 c['THEME'] = reload_theme(value, c['THEME'])
ceec8593 1274 g['decorated_name'] = lambda x: color_func(
a8e71259 1275 c['DECORATED_NAME'])('[' + x + ']: ')
1276 reload_config()
d6cc4c67 1277 printNicely(green('Updated successfully.'))
a8e71259 1278 except Exception as e:
1279 printNicely(red(e))
29fd0be6
O
1280 else:
1281 printNicely(light_magenta('Sorry I can\'s understand.'))
1282
1283
2d341029 1284def help_discover():
f405a7d0 1285 """
2d341029 1286 Discover the world
f405a7d0 1287 """
7e4ccbf3 1288 s = ' ' * 2
1f24a05a 1289 # Discover the world
2d341029 1290 usage = '\n'
8bc30efd 1291 usage += s + grey(u'\u266A' + ' Discover the world \n')
c075e6dc
O
1292 usage += s * 2 + light_green('trend') + ' will show global trending topics. ' + \
1293 'You can try ' + light_green('trend US') + ' or ' + \
1294 light_green('trend JP Tokyo') + '.\n'
1295 usage += s * 2 + light_green('home') + ' will show your timeline. ' + \
1296 light_green('home 7') + ' will show 7 tweets.\n'
99cd1fba
O
1297 usage += s * 2 + \
1298 light_green('notification') + ' will show your recent notification.\n'
c075e6dc
O
1299 usage += s * 2 + light_green('mentions') + ' will show mentions timeline. ' + \
1300 light_green('mentions 7') + ' will show 7 mention tweets.\n'
1301 usage += s * 2 + light_green('whois @mdo') + ' will show profile of ' + \
8bc30efd 1302 magenta('@mdo') + '.\n'
c075e6dc 1303 usage += s * 2 + light_green('view @mdo') + \
8bc30efd 1304 ' will show ' + magenta('@mdo') + '\'s home.\n'
03e08f86
O
1305 usage += s * 2 + light_green('s AKB48') + ' will search for "' + \
1306 light_yellow('AKB48') + '" and return 5 newest tweet. ' + \
1307 'Search can be performed with or without hashtag.\n'
2d341029
O
1308 printNicely(usage)
1309
8bc30efd 1310
2d341029
O
1311def help_tweets():
1312 """
1313 Tweets
1314 """
1315 s = ' ' * 2
1f24a05a 1316 # Tweet
2d341029 1317 usage = '\n'
8bc30efd 1318 usage += s + grey(u'\u266A' + ' Tweets \n')
c075e6dc
O
1319 usage += s * 2 + light_green('t oops ') + \
1320 'will tweet "' + light_yellow('oops') + '" immediately.\n'
7e4ccbf3 1321 usage += s * 2 + \
c075e6dc
O
1322 light_green('rt 12 ') + ' will retweet to tweet with ' + \
1323 light_yellow('[id=12]') + '.\n'
1f24a05a 1324 usage += s * 2 + \
80b70d60
O
1325 light_green('quote 12 ') + ' will quote the tweet with ' + \
1326 light_yellow('[id=12]') + '. If no extra text is added, ' + \
1327 'the quote will be canceled.\n'
1328 usage += s * 2 + \
c075e6dc
O
1329 light_green('allrt 12 20 ') + ' will list 20 newest retweet of the tweet with ' + \
1330 light_yellow('[id=12]') + '.\n'
fd87ddac
O
1331 usage += s * 2 + light_green('conversation 12') + ' will show the chain of ' + \
1332 'replies prior to the tweet with ' + light_yellow('[id=12]') + '.\n'
c075e6dc
O
1333 usage += s * 2 + light_green('rep 12 oops') + ' will reply "' + \
1334 light_yellow('oops') + '" to tweet with ' + \
1335 light_yellow('[id=12]') + '.\n'
7e4ccbf3 1336 usage += s * 2 + \
c075e6dc
O
1337 light_green('fav 12 ') + ' will favorite the tweet with ' + \
1338 light_yellow('[id=12]') + '.\n'
7e4ccbf3 1339 usage += s * 2 + \
c075e6dc
O
1340 light_green('ufav 12 ') + ' will unfavorite tweet with ' + \
1341 light_yellow('[id=12]') + '.\n'
8bc30efd 1342 usage += s * 2 + \
c075e6dc
O
1343 light_green('del 12 ') + ' will delete tweet with ' + \
1344 light_yellow('[id=12]') + '.\n'
1345 usage += s * 2 + light_green('show image 12') + ' will show image in tweet with ' + \
1346 light_yellow('[id=12]') + ' in your OS\'s image viewer.\n'
80b70d60
O
1347 usage += s * 2 + light_green('open 12') + ' will open url in tweet with ' + \
1348 light_yellow('[id=12]') + ' in your OS\'s default browser.\n'
2d341029 1349 printNicely(usage)
8bc30efd 1350
2d341029
O
1351
1352def help_messages():
1353 """
1354 Messages
1355 """
1356 s = ' ' * 2
5b2c4faf 1357 # Direct message
2d341029 1358 usage = '\n'
8bc30efd 1359 usage += s + grey(u'\u266A' + ' Direct messages \n')
c075e6dc
O
1360 usage += s * 2 + light_green('inbox') + ' will show inbox messages. ' + \
1361 light_green('inbox 7') + ' will show newest 7 messages.\n'
03c0d30b 1362 usage += s * 2 + light_green('thread 2') + ' will show full thread with ' + \
1363 light_yellow('[thread_id=2]') + '.\n'
c075e6dc 1364 usage += s * 2 + light_green('mes @dtvd88 hi') + ' will send a "hi" messege to ' + \
8bc30efd 1365 magenta('@dtvd88') + '.\n'
c075e6dc
O
1366 usage += s * 2 + light_green('trash 5') + ' will remove message with ' + \
1367 light_yellow('[message_id=5]') + '.\n'
2d341029 1368 printNicely(usage)
8bc30efd 1369
2d341029
O
1370
1371def help_friends_and_followers():
1372 """
1373 Friends and Followers
1374 """
1375 s = ' ' * 2
8bc30efd 1376 # Follower and following
2d341029 1377 usage = '\n'
cdccb0d6 1378 usage += s + grey(u'\u266A' + ' Friends and followers \n')
8bc30efd 1379 usage += s * 2 + \
c075e6dc 1380 light_green('ls fl') + \
8bc30efd 1381 ' will list all followers (people who are following you).\n'
1382 usage += s * 2 + \
c075e6dc 1383 light_green('ls fr') + \
8bc30efd 1384 ' will list all friends (people who you are following).\n'
c075e6dc 1385 usage += s * 2 + light_green('fl @dtvd88') + ' will follow ' + \
305ce127 1386 magenta('@dtvd88') + '.\n'
c075e6dc 1387 usage += s * 2 + light_green('ufl @dtvd88') + ' will unfollow ' + \
305ce127 1388 magenta('@dtvd88') + '.\n'
c075e6dc 1389 usage += s * 2 + light_green('mute @dtvd88') + ' will mute ' + \
5b2c4faf 1390 magenta('@dtvd88') + '.\n'
c075e6dc 1391 usage += s * 2 + light_green('unmute @dtvd88') + ' will unmute ' + \
5b2c4faf 1392 magenta('@dtvd88') + '.\n'
c075e6dc
O
1393 usage += s * 2 + light_green('muting') + ' will list muting users.\n'
1394 usage += s * 2 + light_green('block @dtvd88') + ' will block ' + \
305ce127 1395 magenta('@dtvd88') + '.\n'
c075e6dc 1396 usage += s * 2 + light_green('unblock @dtvd88') + ' will unblock ' + \
305ce127 1397 magenta('@dtvd88') + '.\n'
c075e6dc 1398 usage += s * 2 + light_green('report @dtvd88') + ' will report ' + \
305ce127 1399 magenta('@dtvd88') + ' as a spam account.\n'
2d341029
O
1400 printNicely(usage)
1401
1402
1403def help_list():
1404 """
1405 Lists
1406 """
1407 s = ' ' * 2
1408 # Twitter list
1409 usage = '\n'
1410 usage += s + grey(u'\u266A' + ' Twitter list\n')
1411 usage += s * 2 + light_green('list') + \
1412 ' will show all lists you are belong to.\n'
1413 usage += s * 2 + light_green('list home') + \
bef33491 1414 ' will show timeline of list. You will be asked for list\'s name.\n'
a65bd34c 1415 usage += s * 2 + light_green('list all_mem') + \
2d341029 1416 ' will show list\'s all members.\n'
a65bd34c 1417 usage += s * 2 + light_green('list all_sub') + \
2d341029 1418 ' will show list\'s all subscribers.\n'
422dd385
O
1419 usage += s * 2 + light_green('list add') + \
1420 ' will add specific person to a list owned by you.' + \
1421 ' You will be asked for list\'s name and person\'s name.\n'
2d341029
O
1422 usage += s * 2 + light_green('list rm') + \
1423 ' will remove specific person from a list owned by you.' + \
1424 ' You will be asked for list\'s name and person\'s name.\n'
422dd385
O
1425 usage += s * 2 + light_green('list sub') + \
1426 ' will subscribe you to a specific list.\n'
1427 usage += s * 2 + light_green('list unsub') + \
1428 ' will unsubscribe you from a specific list.\n'
1429 usage += s * 2 + light_green('list own') + \
1430 ' will show all list owned by you.\n'
1431 usage += s * 2 + light_green('list new') + \
1432 ' will create a new list.\n'
1433 usage += s * 2 + light_green('list update') + \
1434 ' will update a list owned by you.\n'
1435 usage += s * 2 + light_green('list del') + \
1436 ' will delete a list owned by you.\n'
2d341029 1437 printNicely(usage)
8bc30efd 1438
2d341029
O
1439
1440def help_stream():
1441 """
1442 Stream switch
1443 """
1444 s = ' ' * 2
8bc30efd 1445 # Switch
2d341029 1446 usage = '\n'
8bc30efd 1447 usage += s + grey(u'\u266A' + ' Switching streams \n')
c075e6dc 1448 usage += s * 2 + light_green('switch public #AKB') + \
48a25fe8 1449 ' will switch to public stream and follow "' + \
c075e6dc
O
1450 light_yellow('AKB') + '" keyword.\n'
1451 usage += s * 2 + light_green('switch mine') + \
48a25fe8 1452 ' will switch to your personal stream.\n'
c075e6dc 1453 usage += s * 2 + light_green('switch mine -f ') + \
48a25fe8 1454 ' will prompt to enter the filter.\n'
c075e6dc 1455 usage += s * 3 + light_yellow('Only nicks') + \
48a25fe8 1456 ' filter will decide nicks will be INCLUDE ONLY.\n'
c075e6dc 1457 usage += s * 3 + light_yellow('Ignore nicks') + \
48a25fe8 1458 ' filter will decide nicks will be EXCLUDE.\n'
c075e6dc 1459 usage += s * 2 + light_green('switch mine -d') + \
48a25fe8 1460 ' will use the config\'s ONLY_LIST and IGNORE_LIST.\n'
2d341029
O
1461 printNicely(usage)
1462
1463
1464def help():
1465 """
1466 Help
1467 """
1468 s = ' ' * 2
1469 h, w = os.popen('stty size', 'r').read().split()
2d341029
O
1470 # Start
1471 usage = '\n'
1472 usage += s + 'Hi boss! I\'m ready to serve you right now!\n'
1473 usage += s + '-' * (int(w) - 4) + '\n'
1474 usage += s + 'You are ' + \
1475 light_yellow('already') + ' on your personal stream.\n'
1476 usage += s + 'Any update from Twitter will show up ' + \
1477 light_yellow('immediately') + '.\n'
37d1047f 1478 usage += s + 'In addition, following commands are available right now:\n'
2d341029
O
1479 # Twitter help section
1480 usage += '\n'
1481 usage += s + grey(u'\u266A' + ' Twitter help\n')
1482 usage += s * 2 + light_green('h discover') + \
1483 ' will show help for discover commands.\n'
1484 usage += s * 2 + light_green('h tweets') + \
1485 ' will show help for tweets commands.\n'
1486 usage += s * 2 + light_green('h messages') + \
1487 ' will show help for messages commands.\n'
1488 usage += s * 2 + light_green('h friends_and_followers') + \
1489 ' will show help for friends and followers commands.\n'
1490 usage += s * 2 + light_green('h list') + \
1491 ' will show help for list commands.\n'
1492 usage += s * 2 + light_green('h stream') + \
1493 ' will show help for stream commands.\n'
1f24a05a 1494 # Smart shell
1495 usage += '\n'
1496 usage += s + grey(u'\u266A' + ' Smart shell\n')
c075e6dc 1497 usage += s * 2 + light_green('111111 * 9 / 7') + ' or any math expression ' + \
1f24a05a 1498 'will be evaluate by Python interpreter.\n'
c075e6dc 1499 usage += s * 2 + 'Even ' + light_green('cal') + ' will show the calendar' + \
1f24a05a 1500 ' for current month.\n'
29fd0be6 1501 # Config
1f24a05a 1502 usage += '\n'
29fd0be6
O
1503 usage += s + grey(u'\u266A' + ' Config \n')
1504 usage += s * 2 + light_green('theme') + ' will list available theme. ' + \
c075e6dc 1505 light_green('theme monokai') + ' will apply ' + light_yellow('monokai') + \
632c6fa5 1506 ' theme immediately.\n'
29fd0be6
O
1507 usage += s * 2 + light_green('config') + ' will list all config.\n'
1508 usage += s * 3 + \
1509 light_green('config ASCII_ART') + ' will output current value of ' +\
a8c5fce4 1510 light_yellow('ASCII_ART') + ' config key.\n'
29fd0be6 1511 usage += s * 3 + \
fe9bb33b 1512 light_green('config TREND_MAX default') + ' will output default value of ' + \
1513 light_yellow('TREND_MAX') + ' config key.\n'
1514 usage += s * 3 + \
1515 light_green('config CUSTOM_CONFIG drop') + ' will drop ' + \
1516 light_yellow('CUSTOM_CONFIG') + ' config key.\n'
29fd0be6 1517 usage += s * 3 + \
fe9bb33b 1518 light_green('config IMAGE_ON_TERM = true') + ' will set value of ' + \
1519 light_yellow('IMAGE_ON_TERM') + ' config key to ' + \
1520 light_yellow('True') + '.\n'
29fd0be6
O
1521 # Screening
1522 usage += '\n'
1523 usage += s + grey(u'\u266A' + ' Screening \n')
c075e6dc 1524 usage += s * 2 + light_green('h') + ' will show this help again.\n'
d6cc4c67
O
1525 usage += s * 2 + light_green('p') + ' will pause the stream.\n'
1526 usage += s * 2 + light_green('r') + ' will unpause the stream.\n'
c075e6dc
O
1527 usage += s * 2 + light_green('c') + ' will clear the screen.\n'
1528 usage += s * 2 + light_green('q') + ' will quit.\n'
8bc30efd 1529 # End
1530 usage += '\n'
7e4ccbf3 1531 usage += s + '-' * (int(w) - 4) + '\n'
8bc30efd 1532 usage += s + 'Have fun and hang tight! \n'
2d341029
O
1533 # Show help
1534 d = {
422dd385
O
1535 'discover': help_discover,
1536 'tweets': help_tweets,
1537 'messages': help_messages,
1538 'friends_and_followers': help_friends_and_followers,
1539 'list': help_list,
1540 'stream': help_stream,
2d341029
O
1541 }
1542 if g['stuff']:
baec5f50 1543 d.get(
1544 g['stuff'].strip(),
1545 lambda: printNicely(red('No such command.'))
3d48702f 1546 )()
2d341029
O
1547 else:
1548 printNicely(usage)
f405a7d0
O
1549
1550
d6cc4c67
O
1551def pause():
1552 """
1553 Pause stream display
1554 """
4dc385b5 1555 g['pause'] = True
d6cc4c67
O
1556 printNicely(green('Stream is paused'))
1557
1558
1559def replay():
1560 """
1561 Replay stream
1562 """
4dc385b5 1563 g['pause'] = False
d6cc4c67
O
1564 printNicely(green('Stream is running back now'))
1565
1566
843647ad 1567def clear():
f405a7d0 1568 """
7b674cef 1569 Clear screen
f405a7d0 1570 """
843647ad 1571 os.system('clear')
f405a7d0
O
1572
1573
843647ad 1574def quit():
b8dda704
O
1575 """
1576 Exit all
1577 """
4c025026 1578 try:
1579 save_history()
4c025026 1580 printNicely(green('See you next time :)'))
1581 except:
1582 pass
843647ad 1583 sys.exit()
b8dda704
O
1584
1585
94a5f62e 1586def reset():
f405a7d0 1587 """
94a5f62e 1588 Reset prefix of line
f405a7d0 1589 """
c91f75f2 1590 if g['reset']:
a8e71259 1591 if c.get('USER_JSON_ERROR'):
1592 printNicely(red('Your ~/.rainbow_config.json is messed up:'))
1593 printNicely(red('>>> ' + c['USER_JSON_ERROR']))
1594 printNicely('')
e3885f55 1595 printNicely(magenta('Need tips ? Type "h" and hit Enter key!'))
c91f75f2 1596 g['reset'] = False
d0a726d6 1597 try:
779b0640 1598 printNicely(str(eval(g['cmd'])))
2a0cabee 1599 except Exception:
d0a726d6 1600 pass
54277114
O
1601
1602
f1c1dfea
O
1603# Command set
1604cmdset = [
1605 'switch',
1606 'trend',
1607 'home',
99cd1fba 1608 'notification',
f1c1dfea
O
1609 'view',
1610 'mentions',
1611 't',
1612 'rt',
1613 'quote',
1614 'allrt',
fd87ddac 1615 'conversation',
f1c1dfea
O
1616 'fav',
1617 'rep',
1618 'del',
1619 'ufav',
1620 's',
1621 'mes',
1622 'show',
1623 'open',
1624 'ls',
1625 'inbox',
67c663f8 1626 'thread',
f1c1dfea
O
1627 'trash',
1628 'whois',
1629 'fl',
1630 'ufl',
1631 'mute',
1632 'unmute',
1633 'muting',
1634 'block',
1635 'unblock',
1636 'report',
1637 'list',
1638 'cal',
1639 'config',
1640 'theme',
1641 'h',
1642 'p',
1643 'r',
1644 'c',
1645 'q'
1646]
1647
1648# Handle function set
1649funcset = [
1650 switch,
1651 trend,
1652 home,
99cd1fba 1653 notification,
f1c1dfea
O
1654 view,
1655 mentions,
1656 tweet,
1657 retweet,
1658 quote,
1659 allretweet,
fd87ddac 1660 conversation,
f1c1dfea
O
1661 favorite,
1662 reply,
1663 delete,
1664 unfavorite,
1665 search,
1666 message,
1667 show,
1668 urlopen,
1669 ls,
1670 inbox,
67c663f8 1671 thread,
f1c1dfea
O
1672 trash,
1673 whois,
1674 follow,
1675 unfollow,
1676 mute,
1677 unmute,
1678 muting,
1679 block,
1680 unblock,
1681 report,
1682 twitterlist,
1683 cal,
1684 config,
1685 theme,
1686 help,
1687 pause,
1688 replay,
1689 clear,
1690 quit
1691]
1692
1693
94a5f62e 1694def process(cmd):
54277114 1695 """
94a5f62e 1696 Process switch
54277114 1697 """
f1c1dfea 1698 return dict(zip(cmdset, funcset)).get(cmd, reset)
94a5f62e 1699
1700
1701def listen():
42fde775 1702 """
1703 Listen to user's input
1704 """
d51b4107
O
1705 d = dict(zip(
1706 cmdset,
1707 [
affcb149 1708 ['public', 'mine'], # switch
4592d231 1709 [], # trend
7e4ccbf3 1710 [], # home
99cd1fba 1711 [], # notification
7e4ccbf3 1712 ['@'], # view
305ce127 1713 [], # mentions
7e4ccbf3 1714 [], # tweet
1715 [], # retweet
80b70d60 1716 [], # quote
1f24a05a 1717 [], # allretweet
fd87ddac 1718 [], # conversation
f5677fb1 1719 [], # favorite
7e4ccbf3 1720 [], # reply
1721 [], # delete
f5677fb1 1722 [], # unfavorite
7e4ccbf3 1723 ['#'], # search
305ce127 1724 ['@'], # message
f5677fb1 1725 ['image'], # show image
80b70d60 1726 [''], # open url
305ce127 1727 ['fl', 'fr'], # list
1728 [], # inbox
03c0d30b 1729 [i for i in g['message_threads']], # sent
305ce127 1730 [], # trash
e2b81717 1731 ['@'], # whois
affcb149
O
1732 ['@'], # follow
1733 ['@'], # unfollow
5b2c4faf 1734 ['@'], # mute
1735 ['@'], # unmute
1736 ['@'], # muting
305ce127 1737 ['@'], # block
1738 ['@'], # unblock
1739 ['@'], # report
422dd385
O
1740 [
1741 'home',
1742 'all_mem',
1743 'all_sub',
1744 'add',
1745 'rm',
1746 'sub',
1747 'unsub',
1748 'own',
1749 'new',
1750 'update',
1751 'del'
1752 ], # list
813a5d80 1753 [], # cal
a8c5fce4 1754 [key for key in dict(get_all_config())], # config
ceec8593 1755 g['themes'], # theme
422dd385
O
1756 [
1757 'discover',
1758 'tweets',
1759 'messages',
1760 'friends_and_followers',
1761 'list',
1762 'stream'
1763 ], # help
d6cc4c67
O
1764 [], # pause
1765 [], # reconnect
7e4ccbf3 1766 [], # clear
1767 [], # quit
d51b4107 1768 ]
7e4ccbf3 1769 ))
d51b4107 1770 init_interactive_shell(d)
f5677fb1 1771 read_history()
819569e8 1772 reset()
b2b933a9 1773 while True:
b8c1f42a 1774 try:
39b8e6b3
O
1775 # raw_input
1776 if g['prefix']:
aa452ee9 1777 # Only use PREFIX as a string with raw_input
c285decf 1778 line = raw_input(g['decorated_name'](g['PREFIX']))
39b8e6b3
O
1779 else:
1780 line = raw_input()
1781 # Save cmd to compare with readline buffer
1782 g['cmd'] = line.strip()
1783 # Get short cmd to pass to handle function
1784 try:
1785 cmd = line.split()[0]
1786 except:
1787 cmd = ''
9683e61d 1788 # Lock the semaphore
99b52f5f 1789 c['lock'] = True
9683e61d 1790 # Save cmd to global variable and call process
b8c1f42a 1791 g['stuff'] = ' '.join(line.split()[1:])
9683e61d 1792 # Process the command
b8c1f42a 1793 process(cmd)()
9683e61d 1794 # Not re-display
99b52f5f 1795 if cmd in ['switch', 't', 'rt', 'rep']:
9683e61d
O
1796 g['prefix'] = False
1797 else:
1798 g['prefix'] = True
1799 # Release the semaphore lock
99b52f5f 1800 c['lock'] = False
39b8e6b3
O
1801 except EOFError:
1802 printNicely('')
eadd85a8 1803 except Exception:
7a8a52fc 1804 debug_option()
b8c1f42a 1805 printNicely(red('OMG something is wrong with Twitter right now.'))
ee444288 1806
54277114 1807
42fde775 1808def stream(domain, args, name='Rainbow Stream'):
54277114 1809 """
f405a7d0 1810 Track the stream
54277114 1811 """
54277114 1812 # The Logo
42fde775 1813 art_dict = {
632c6fa5
O
1814 c['USER_DOMAIN']: name,
1815 c['PUBLIC_DOMAIN']: args.track_keywords,
1f2f6159 1816 c['SITE_DOMAIN']: name,
42fde775 1817 }
687567eb 1818 if c['ASCII_ART']:
c075e6dc 1819 ascii_art(art_dict[domain])
91476ec3
O
1820 # These arguments are optional:
1821 stream_args = dict(
e3927852 1822 timeout=0.5, # To check g['stream_stop'] after each 0.5 s
cb45dc23 1823 block=True,
1824 heartbeat_timeout=c['HEARTBEAT_TIMEOUT'] * 60)
91476ec3
O
1825 # Track keyword
1826 query_args = dict()
1827 if args.track_keywords:
1828 query_args['track'] = args.track_keywords
91476ec3 1829 # Get stream
2a6238f5 1830 stream = TwitterStream(
22be990e 1831 auth=authen(),
42fde775 1832 domain=domain,
2a6238f5 1833 **stream_args)
2a0cabee
O
1834 try:
1835 if domain == c['USER_DOMAIN']:
1836 tweet_iter = stream.user(**query_args)
1837 elif domain == c['SITE_DOMAIN']:
1838 tweet_iter = stream.site(**query_args)
42fde775 1839 else:
2a0cabee
O
1840 if args.track_keywords:
1841 tweet_iter = stream.statuses.filter(**query_args)
1842 else:
1843 tweet_iter = stream.statuses.sample()
92983945
BS
1844 # Block new stream until other one exits
1845 StreamLock.acquire()
1846 g['stream_stop'] = False
72c02928
VNM
1847 for tweet in tweet_iter:
1848 if tweet is None:
a1222228 1849 printNicely("-- None --")
72c02928 1850 elif tweet is Timeout:
335e7803
O
1851 if(g['stream_stop']):
1852 StreamLock.release()
1853 break
72c02928
VNM
1854 elif tweet is HeartbeatTimeout:
1855 printNicely("-- Heartbeat Timeout --")
cb45dc23 1856 guide = light_magenta("You can use ") + \
1857 light_green("switch") + \
1858 light_magenta(" command to return to your stream.\n")
1859 guide += light_magenta("Type ") + \
1860 light_green("h stream") + \
1861 light_magenta(" for more details.")
1862 printNicely(guide)
aa452ee9 1863 sys.stdout.write(g['decorated_name'](c['PREFIX']))
cb45dc23 1864 sys.stdout.flush()
8715dda0
O
1865 StreamLock.release()
1866 break
72c02928
VNM
1867 elif tweet is Hangup:
1868 printNicely("-- Hangup --")
1869 elif tweet.get('text'):
4dc385b5
O
1870 # Check the semaphore pause and lock (stream process only)
1871 if g['pause']:
1872 continue
1873 while c['lock']:
1874 time.sleep(0.5)
1875 # Draw the tweet
72c02928
VNM
1876 draw(
1877 t=tweet,
72c02928 1878 keyword=args.track_keywords,
8b3456f9 1879 humanize=False,
72c02928
VNM
1880 fil=args.filter,
1881 ig=args.ignore,
1882 )
4824b181
O
1883 # Current readline buffer
1884 current_buffer = readline.get_line_buffer().strip()
335e7803 1885 # There is an unexpected behaviour in MacOSX readline + Python 2:
3d48702f
O
1886 # after completely delete a word after typing it,
1887 # somehow readline buffer still contains
1888 # the 1st character of that word
f1c1dfea 1889 if current_buffer and g['cmd'] != current_buffer:
3d48702f 1890 sys.stdout.write(
aa452ee9 1891 g['decorated_name'](c['PREFIX']) + str2u(current_buffer))
4824b181 1892 sys.stdout.flush()
335e7803 1893 elif not c['HIDE_PROMPT']:
aa452ee9 1894 sys.stdout.write(g['decorated_name'](c['PREFIX']))
335e7803 1895 sys.stdout.flush()
14db58c7 1896 elif tweet.get('direct_message'):
4dc385b5
O
1897 # Check the semaphore pause and lock (stream process only)
1898 if g['pause']:
1899 continue
1900 while c['lock']:
1901 time.sleep(0.5)
1902 print_message(tweet['direct_message'])
99cd1fba
O
1903 elif tweet.get('event'):
1904 g['events'].append(tweet)
1905 print_event(tweet)
2a0cabee
O
1906 except TwitterHTTPError:
1907 printNicely('')
c075e6dc 1908 printNicely(
2a0cabee 1909 magenta("We have maximum connection problem with twitter'stream API right now :("))
54277114
O
1910
1911
1912def fly():
1913 """
1914 Main function
1915 """
531f5682 1916 # Initial
42fde775 1917 args = parse_arguments()
2a0cabee 1918 try:
fe9bb33b 1919 init(args)
2a0cabee
O
1920 except TwitterHTTPError:
1921 printNicely('')
1922 printNicely(
e3927852 1923 magenta("We have connection problem with twitter'stream API right now :("))
4c025026 1924 printNicely(magenta("Let's try again later."))
2a0cabee 1925 save_history()
2a0cabee 1926 sys.exit()
92983945 1927 # Spawn stream thread
baec5f50 1928 th = threading.Thread(
1929 target=stream,
1930 args=(
1931 c['USER_DOMAIN'],
1932 args,
1933 g['original_name']))
92983945
BS
1934 th.daemon = True
1935 th.start()
42fde775 1936 # Start listen process
819569e8 1937 time.sleep(0.5)
c91f75f2 1938 g['reset'] = True
1dd312f5 1939 g['prefix'] = True
0f6e4daf 1940 listen()