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