2 Colorful user's timeline stream
4 from multiprocessing
import Process
15 from twitter
.stream
import TwitterStream
, Timeout
, HeartbeatTimeout
, Hangup
16 from twitter
.api
import *
17 from twitter
.oauth
import OAuth
, read_token_file
18 from twitter
.oauth_dance
import oauth_dance
19 from twitter
.util
import printNicely
24 from .consumer
import *
25 from .interactive
import *
27 from .c_image
import *
28 from .py3patch
import *
78 def parse_arguments():
82 parser
= argparse
.ArgumentParser(description
=__doc__
or "")
86 help='Timeout for the stream (seconds).')
89 '--heartbeat-timeout',
90 help='Set heartbeat timeout.',
96 help='Set stream to non-blocking.')
100 help='Search the stream for specific text.')
104 help='Filter specific screen_name.')
108 help='Ignore specific screen_name.')
113 help='Display all image on terminal.')
114 return parser
.parse_args()
119 Authenticate with Twitter OAuth
121 # When using rainbow stream you must authorize.
122 twitter_credential
= os
.environ
.get(
126 '')) + os
.sep
+ '.rainbow_oauth'
127 if not os
.path
.exists(twitter_credential
):
128 oauth_dance("Rainbow Stream",
132 oauth_token
, oauth_token_secret
= read_token_file(twitter_credential
)
145 t
= Twitter(auth
=authen())
146 name
= '@' + t
.account
.verify_credentials()['screen_name']
147 if not get_config('PREFIX'):
148 set_config('PREFIX', name
)
149 g
['original_name'] = name
[1:]
150 g
['decorated_name'] = lambda x
: color_func(
151 c
['DECORATED_NAME'])(
154 files
= os
.listdir(os
.path
.dirname(__file__
) + '/colorset')
155 themes
= [f
.split('.')[0] for f
in files
if f
.split('.')[-1] == 'json']
157 db
.theme_store(c
['THEME'])
159 db
.semaphore_store(False)
161 c
['IMAGE_ON_TERM'] = args
.image_on_term
169 target
= g
['stuff'].split()[0]
171 args
= parse_arguments()
173 if g
['stuff'].split()[-1] == '-f':
174 guide
= 'To ignore an option, just hit Enter key.'
175 printNicely(light_magenta(guide
))
176 only
= raw_input('Only nicks [Ex: @xxx,@yy]: ')
177 ignore
= raw_input('Ignore nicks [Ex: @xxx,@yy]: ')
178 args
.filter = filter(None, only
.split(','))
179 args
.ignore
= filter(None, ignore
.split(','))
180 elif g
['stuff'].split()[-1] == '-d':
181 args
.filter = c
['ONLY_LIST']
182 args
.ignore
= c
['IGNORE_LIST']
184 printNicely(red('Sorry, wrong format.'))
187 if target
== 'public':
188 keyword
= g
['stuff'].split()[1]
189 if keyword
[0] == '#':
190 keyword
= keyword
[1:]
192 os
.kill(g
['stream_pid'], signal
.SIGKILL
)
193 args
.track_keywords
= keyword
201 g
['stream_pid'] = p
.pid
203 elif target
== 'mine':
205 os
.kill(g
['stream_pid'], signal
.SIGKILL
)
214 g
['stream_pid'] = p
.pid
217 printNicely(cyan('Only: ' + str(args
.filter)))
219 printNicely(red('Ignore: ' + str(args
.ignore
)))
222 printNicely(red('Sorry I can\'t understand.'))
229 t
= Twitter(auth
=authen())
230 # Get country and town
232 country
= g
['stuff'].split()[0]
236 town
= g
['stuff'].split()[1]
239 avail
= t
.trends
.available()
242 trends
= t
.trends
.place(_id
=1)[0]['trends']
245 for location
in avail
:
246 # Search for country and Town
248 if location
['countryCode'] == country \
249 and location
['placeType']['name'] == 'Town' \
250 and location
['name'] == town
:
251 trends
= t
.trends
.place(_id
=location
['woeid'])[0]['trends']
253 # Search for country only
255 if location
['countryCode'] == country \
256 and location
['placeType']['name'] == 'Country':
257 trends
= t
.trends
.place(_id
=location
['woeid'])[0]['trends']
265 t
= Twitter(auth
=authen())
266 num
= c
['HOME_TWEET_NUM']
267 if g
['stuff'].isdigit():
268 num
= int(g
['stuff'])
269 for tweet
in reversed(t
.statuses
.home_timeline(count
=num
)):
278 t
= Twitter(auth
=authen())
279 user
= g
['stuff'].split()[0]
282 num
= int(g
['stuff'].split()[1])
284 num
= c
['HOME_TWEET_NUM']
285 for tweet
in reversed(t
.statuses
.user_timeline(count
=num
, screen_name
=user
[1:])):
289 printNicely(red('A name should begin with a \'@\''))
296 t
= Twitter(auth
=authen())
297 num
= c
['HOME_TWEET_NUM']
298 if g
['stuff'].isdigit():
299 num
= int(g
['stuff'])
300 for tweet
in reversed(t
.statuses
.mentions_timeline(count
=num
)):
309 t
= Twitter(auth
=authen())
310 t
.statuses
.update(status
=g
['stuff'])
317 t
= Twitter(auth
=authen())
319 id = int(g
['stuff'].split()[0])
321 printNicely(red('Sorry I can\'t understand.'))
323 tid
= db
.rainbow_to_tweet_query(id)[0].tweet_id
324 t
.statuses
.retweet(id=tid
, include_entities
=False, trim_user
=True)
331 t
= Twitter(auth
=authen())
333 id = int(g
['stuff'].split()[0])
335 printNicely(red('Sorry I can\'t understand.'))
337 tid
= db
.rainbow_to_tweet_query(id)[0].tweet_id
338 tweet
= t
.statuses
.show(id=tid
)
339 screen_name
= tweet
['user']['screen_name']
341 quote
= '\"@' + screen_name
+ ': ' + text
+ '\"'
342 quote
= quote
.encode('utf8')
343 notice
= light_magenta('Compose mode ')
344 notice
+= light_yellow('(Enter nothing will cancel the quote)')
345 notice
+= light_magenta(':')
347 extra
= raw_input(quote
)
349 t
.statuses
.update(status
=quote
+ extra
)
351 printNicely(light_magenta('No text added.'))
358 t
= Twitter(auth
=authen())
361 id = int(g
['stuff'].split()[0])
363 printNicely(red('Sorry I can\'t understand.'))
365 tid
= db
.rainbow_to_tweet_query(id)[0].tweet_id
366 # Get display num if exist
368 num
= int(g
['stuff'].split()[1])
370 num
= c
['RETWEETS_SHOW_NUM']
371 # Get result and display
372 rt_ary
= t
.statuses
.retweets(id=tid
, count
=num
)
374 printNicely(magenta('This tweet has no retweet.'))
376 for tweet
in reversed(rt_ary
):
385 t
= Twitter(auth
=authen())
387 id = int(g
['stuff'].split()[0])
389 printNicely(red('Sorry I can\'t understand.'))
391 tid
= db
.rainbow_to_tweet_query(id)[0].tweet_id
392 t
.favorites
.create(_id
=tid
, include_entities
=False)
393 printNicely(green('Favorited.'))
394 draw(t
.statuses
.show(id=tid
))
402 t
= Twitter(auth
=authen())
404 id = int(g
['stuff'].split()[0])
406 printNicely(red('Sorry I can\'t understand.'))
408 tid
= db
.rainbow_to_tweet_query(id)[0].tweet_id
409 user
= t
.statuses
.show(id=tid
)['user']['screen_name']
410 status
= ' '.join(g
['stuff'].split()[1:])
411 status
= '@' + user
+ ' ' + status
.decode('utf-8')
412 t
.statuses
.update(status
=status
, in_reply_to_status_id
=tid
)
419 t
= Twitter(auth
=authen())
421 rid
= int(g
['stuff'].split()[0])
423 printNicely(red('Sorry I can\'t understand.'))
425 tid
= db
.rainbow_to_tweet_query(rid
)[0].tweet_id
426 t
.statuses
.destroy(id=tid
)
427 printNicely(green('Okay it\'s gone.'))
434 t
= Twitter(auth
=authen())
436 id = int(g
['stuff'].split()[0])
438 printNicely(red('Sorry I can\'t understand.'))
440 tid
= db
.rainbow_to_tweet_query(id)[0].tweet_id
441 t
.favorites
.destroy(_id
=tid
)
442 printNicely(green('Okay it\'s unfavorited.'))
443 draw(t
.statuses
.show(id=tid
))
451 t
= Twitter(auth
=authen())
452 g
['stuff'] = g
['stuff'].strip()
453 rel
= t
.search
.tweets(q
=g
['stuff'])['statuses']
455 printNicely('Newest tweets:')
456 for i
in reversed(xrange(c
['SEARCH_MAX_RECORD'])):
461 printNicely(magenta('I\'m afraid there is no result'))
466 Send a direct message
468 t
= Twitter(auth
=authen())
469 user
= g
['stuff'].split()[0]
470 if user
[0].startswith('@'):
472 content
= g
['stuff'].split()[1]
474 printNicely(red('Sorry I can\'t understand.'))
475 t
.direct_messages
.new(
476 screen_name
=user
[1:],
479 printNicely(green('Message sent.'))
481 printNicely(red('A name should begin with a \'@\''))
488 t
= Twitter(auth
=authen())
490 target
= g
['stuff'].split()[0]
491 if target
!= 'image':
493 id = int(g
['stuff'].split()[1])
494 tid
= db
.rainbow_to_tweet_query(id)[0].tweet_id
495 tweet
= t
.statuses
.show(id=tid
)
496 media
= tweet
['entities']['media']
498 res
= requests
.get(m
['media_url'])
499 img
= Image
.open(BytesIO(res
.content
))
502 printNicely(red('Sorry I can\'t show this image.'))
509 t
= Twitter(auth
=authen())
511 if not g
['stuff'].isdigit():
513 tid
= db
.rainbow_to_tweet_query(g
['stuff'])[0].tweet_id
514 tweet
= t
.statuses
.show(id=tid
)
516 u
for u
in tweet
['text'].split() if u
.startswith('http://')]
518 printNicely(light_magenta('No url here @.@!'))
520 for link
in link_ary
:
521 webbrowser
.open(link
)
523 printNicely(red('Sorry I can\'t open url in this tweet.'))
528 List friends for followers
530 t
= Twitter(auth
=authen())
533 name
= g
['stuff'].split()[1]
534 if name
.startswith('@'):
537 printNicely(red('A name should begin with a \'@\''))
538 raise Exception('Invalid name')
540 name
= g
['original_name']
541 # Get list followers or friends
543 target
= g
['stuff'].split()[0]
545 printNicely(red('Omg some syntax is wrong.'))
547 d
= {'fl': 'followers', 'fr': 'friends'}
551 while next_cursor
!= 0:
552 list = getattr(t
, d
[target
]).list(
556 include_entities
=False,
558 for u
in list['users']:
559 rel
[u
['name']] = '@' + u
['screen_name']
560 next_cursor
= list['next_cursor']
562 printNicely('All: ' + str(len(rel
)) + ' ' + d
[target
] + '.')
564 user
= ' ' + cycle_color(name
)
565 user
+= color_func(c
['TWEET']['nick'])(' ' + rel
[name
] + ' ')
571 Inbox direct messages
573 t
= Twitter(auth
=authen())
574 num
= c
['MESSAGES_DISPLAY']
576 if g
['stuff'].isdigit():
579 # Max message per page is 20 so we have to loop
581 rel
= rel
+ t
.direct_messages(
584 include_entities
=False,
589 rel
= rel
+ t
.direct_messages(
592 include_entities
=False,
596 printNicely('Inbox: newest ' + str(len(rel
)) + ' messages.')
597 for m
in reversed(rel
):
606 t
= Twitter(auth
=authen())
607 num
= c
['MESSAGES_DISPLAY']
609 if g
['stuff'].isdigit():
610 num
= int(g
['stuff'])
612 # Max message per page is 20 so we have to loop
614 rel
= rel
+ t
.direct_messages
.sent(
617 include_entities
=False,
622 rel
= rel
+ t
.direct_messages
.sent(
625 include_entities
=False,
629 printNicely('Sent: newest ' + str(len(rel
)) + ' messages.')
630 for m
in reversed(rel
):
639 t
= Twitter(auth
=authen())
641 rid
= int(g
['stuff'].split()[0])
643 printNicely(red('Sorry I can\'t understand.'))
644 mid
= db
.rainbow_to_message_query(rid
)[0].message_id
645 t
.direct_messages
.destroy(id=mid
)
646 printNicely(green('Message deleted.'))
651 Show profile of a specific user
653 t
= Twitter(auth
=authen())
654 screen_name
= g
['stuff'].split()[0]
655 if screen_name
.startswith('@'):
658 screen_name
=screen_name
[1:],
659 include_entities
=False)
662 printNicely(red('Omg no user.'))
664 printNicely(red('A name should begin with a \'@\''))
671 t
= Twitter(auth
=authen())
672 screen_name
= g
['stuff'].split()[0]
673 if screen_name
.startswith('@'):
674 t
.friendships
.create(screen_name
=screen_name
[1:], follow
=True)
675 printNicely(green('You are following ' + screen_name
+ ' now!'))
677 printNicely(red('A name should begin with a \'@\''))
684 t
= Twitter(auth
=authen())
685 screen_name
= g
['stuff'].split()[0]
686 if screen_name
.startswith('@'):
687 t
.friendships
.destroy(
688 screen_name
=screen_name
[1:],
689 include_entities
=False)
690 printNicely(green('Unfollow ' + screen_name
+ ' success!'))
692 printNicely(red('A name should begin with a \'@\''))
699 t
= Twitter(auth
=authen())
701 screen_name
= g
['stuff'].split()[0]
703 printNicely(red('A name should be specified. '))
705 if screen_name
.startswith('@'):
706 rel
= t
.mutes
.users
.create(screen_name
=screen_name
[1:])
707 if isinstance(rel
, dict):
708 printNicely(green(screen_name
+ ' is muted.'))
710 printNicely(red(rel
))
712 printNicely(red('A name should begin with a \'@\''))
719 t
= Twitter(auth
=authen())
721 screen_name
= g
['stuff'].split()[0]
723 printNicely(red('A name should be specified. '))
725 if screen_name
.startswith('@'):
726 rel
= t
.mutes
.users
.destroy(screen_name
=screen_name
[1:])
727 if isinstance(rel
, dict):
728 printNicely(green(screen_name
+ ' is unmuted.'))
730 printNicely(red(rel
))
732 printNicely(red('A name should begin with a \'@\''))
739 t
= Twitter(auth
=authen())
744 while next_cursor
!= 0:
745 list = t
.mutes
.users
.list(
746 screen_name
=g
['original_name'],
749 include_entities
=False,
751 for u
in list['users']:
752 rel
[u
['name']] = '@' + u
['screen_name']
753 next_cursor
= list['next_cursor']
755 printNicely('All: ' + str(len(rel
)) + ' people.')
757 user
= ' ' + cycle_color(name
)
758 user
+= color_func(c
['TWEET']['nick'])(' ' + rel
[name
] + ' ')
766 t
= Twitter(auth
=authen())
767 screen_name
= g
['stuff'].split()[0]
768 if screen_name
.startswith('@'):
770 screen_name
=screen_name
[1:],
771 include_entities
=False,
773 printNicely(green('You blocked ' + screen_name
+ '.'))
775 printNicely(red('A name should begin with a \'@\''))
782 t
= Twitter(auth
=authen())
783 screen_name
= g
['stuff'].split()[0]
784 if screen_name
.startswith('@'):
786 screen_name
=screen_name
[1:],
787 include_entities
=False,
789 printNicely(green('Unblock ' + screen_name
+ ' success!'))
791 printNicely(red('A name should begin with a \'@\''))
796 Report a user as a spam account
798 t
= Twitter(auth
=authen())
799 screen_name
= g
['stuff'].split()[0]
800 if screen_name
.startswith('@'):
802 screen_name
=screen_name
[1:])
803 printNicely(green('You reported ' + screen_name
+ '.'))
805 printNicely(red('Sorry I can\'t understand.'))
813 list_name
= raw_input(light_magenta('Give me the list\'s name: '))
814 # Get list name and owner
816 owner
, slug
= list_name
.split('/')
817 if slug
.startswith('@'):
822 light_magenta('List name should follow "@owner/list_name" format.'))
823 raise Exception('Wrong list name')
830 rel
= t
.lists
.list(screen_name
=g
['original_name'])
834 printNicely(light_magenta('You belong to no lists :)'))
841 owner
, slug
= get_slug()
842 res
= t
.lists
.statuses(
844 owner_screen_name
=owner
,
846 include_entities
=False)
856 owner
, slug
= get_slug()
860 while next_cursor
!= 0:
863 owner_screen_name
=owner
,
865 include_entities
=False)
867 rel
[u
['name']] = '@' + u
['screen_name']
868 next_cursor
= m
['next_cursor']
869 printNicely('All: ' + str(len(rel
)) + ' members.')
871 user
= ' ' + cycle_color(name
)
872 user
+= color_func(c
['TWEET']['nick'])(' ' + rel
[name
] + ' ')
876 def list_subscribers(t
):
880 owner
, slug
= get_slug()
884 while next_cursor
!= 0:
885 m
= t
.lists
.subscribers(
887 owner_screen_name
=owner
,
889 include_entities
=False)
891 rel
[u
['name']] = '@' + u
['screen_name']
892 next_cursor
= m
['next_cursor']
893 printNicely('All: ' + str(len(rel
)) + ' subscribers.')
895 user
= ' ' + cycle_color(name
)
896 user
+= color_func(c
['TWEET']['nick'])(' ' + rel
[name
] + ' ')
902 Add specific user to a list
904 owner
, slug
= get_slug()
906 user_name
= raw_input(light_magenta('Give me name of the newbie: '))
907 if user_name
.startswith('@'):
908 user_name
= user_name
[1:]
910 t
.lists
.members
.create(
912 owner_screen_name
=owner
,
913 screen_name
=user_name
)
914 printNicely(light_green('Added.'))
916 printNicely(light_magenta('I\'m sorry we can not add him/her.'))
921 Remove specific user from a list
923 owner
, slug
= get_slug()
925 user_name
= raw_input(light_magenta('Give me name of the unlucky one: '))
926 if user_name
.startswith('@'):
927 user_name
= user_name
[1:]
929 t
.lists
.members
.destroy(
931 owner_screen_name
=owner
,
932 screen_name
=user_name
)
933 printNicely(light_green('Gone.'))
935 printNicely(light_magenta('I\'m sorry we can not remove him/her.'))
938 def list_subscribe(t
):
942 owner
, slug
= get_slug()
945 t
.lists
.subscribers
.create(
947 owner_screen_name
=owner
)
948 printNicely(light_green('Done.'))
951 light_magenta('I\'m sorry you can not subscribe to this list.'))
954 def list_unsubscribe(t
):
958 owner
, slug
= get_slug()
961 t
.lists
.subscribers
.destroy(
963 owner_screen_name
=owner
)
964 printNicely(light_green('Done.'))
967 light_magenta('I\'m sorry you can not unsubscribe to this list.'))
976 while next_cursor
!= 0:
977 res
= t
.lists
.ownerships(
978 screen_name
=g
['original_name'],
981 next_cursor
= res
['next_cursor']
985 printNicely(light_magenta('You own no lists :)'))
992 name
= raw_input(light_magenta('New list\'s name: '))
993 mode
= raw_input(light_magenta('New list\'s mode (public/private): '))
994 description
= raw_input(light_magenta('New list\'s description: '))
999 description
=description
)
1000 printNicely(light_green(name
+ ' list is created.'))
1002 printNicely(red('Oops something is wrong with Twitter :('))
1009 slug
= raw_input(light_magenta('Your list that you want to update: '))
1010 name
= raw_input(light_magenta('Update name (leave blank to unchange): '))
1011 mode
= raw_input(light_magenta('Update mode (public/private): '))
1012 description
= raw_input(light_magenta('Update description: '))
1016 slug
='-'.join(slug
.split()),
1017 owner_screen_name
=g
['original_name'],
1020 description
=description
)
1024 owner_screen_name
=g
['original_name'],
1026 description
=description
)
1027 printNicely(light_green(slug
+ ' list is updated.'))
1029 printNicely(red('Oops something is wrong with Twitter :('))
1036 slug
= raw_input(light_magenta('Your list that you want to update: '))
1039 slug
='-'.join(slug
.split()),
1040 owner_screen_name
=g
['original_name'])
1041 printNicely(light_green(slug
+ ' list is deleted.'))
1043 printNicely(red('Oops something is wrong with Twitter :('))
1050 t
= Twitter(auth
=authen())
1051 # List all lists or base on action
1053 g
['list_action'] = g
['stuff'].split()[0]
1060 'all_mem': list_members
,
1061 'all_sub': list_subscribers
,
1064 'sub': list_subscribe
,
1065 'unsub': list_unsubscribe
,
1068 'update': list_update
,
1072 return action_ary
[g
['list_action']](t
)
1074 printNicely(red('Please try again.'))
1079 Unix's command `cal`
1082 rel
= os
.popen('cal').read().split('\n')
1085 show_calendar(month
, date
, rel
)
1090 Browse and change config
1092 all_config
= get_all_config()
1093 g
['stuff'] = g
['stuff'].strip()
1096 for k
in all_config
:
1098 light_green(k
) + ': ' + light_yellow(str(all_config
[k
]))
1100 guide
= 'Detailed explanation can be found at ' + \
1101 color_func(c
['TWEET']['link'])(
1102 'http://rainbowstream.readthedocs.org/en/latest/#config-explanation')
1104 # Print specific config
1105 elif len(g
['stuff'].split()) == 1:
1106 if g
['stuff'] in all_config
:
1109 light_green(k
) + ': ' + light_yellow(str(all_config
[k
]))
1112 printNicely(red('No such config key.'))
1113 # Print specific config's default value
1114 elif len(g
['stuff'].split()) == 2 and g
['stuff'].split()[-1] == 'default':
1115 key
= g
['stuff'].split()[0]
1117 value
= get_default_config(key
)
1118 line
= ' ' * 2 + light_green(key
) + ': ' + light_magenta(value
)
1122 light_magenta('This config key does not exist in default.'))
1123 # Delete specific config key in config file
1124 elif len(g
['stuff'].split()) == 2 and g
['stuff'].split()[-1] == 'drop':
1125 key
= g
['stuff'].split()[0]
1128 printNicely(light_green('Config key is dropped.'))
1130 printNicely(red('No such config key.'))
1131 # Set specific config
1132 elif len(g
['stuff'].split()) == 3 and g
['stuff'].split()[1] == '=':
1133 key
= g
['stuff'].split()[0]
1134 value
= g
['stuff'].split()[-1]
1135 if key
== 'THEME' and not validate_theme(value
):
1136 printNicely(red('Invalid theme\'s value.'))
1139 set_config(key
, value
)
1140 # Apply theme immediately
1143 g
['decorated_name'] = lambda x
: color_func(
1144 c
['DECORATED_NAME'])(
1146 printNicely(light_green('Updated successfully.'))
1148 printNicely(light_magenta('Not valid value.'))
1152 printNicely(light_magenta('Sorry I can\'s understand.'))
1157 List and change theme
1161 for theme
in g
['themes']:
1162 line
= light_magenta(theme
)
1163 if c
['THEME'] == theme
:
1164 line
= ' ' * 2 + light_yellow('* ') + line
1166 line
= ' ' * 4 + line
1172 reload_theme(g
['stuff'])
1173 # Redefine decorated_name
1174 g
['decorated_name'] = lambda x
: color_func(
1175 c
['DECORATED_NAME'])(
1177 printNicely(green('Theme changed.'))
1179 printNicely(red('No such theme exists.'))
1182 def help_discover():
1187 # Discover the world
1189 usage
+= s
+ grey(u
'\u266A' + ' Discover the world \n')
1190 usage
+= s
* 2 + light_green('trend') + ' will show global trending topics. ' + \
1191 'You can try ' + light_green('trend US') + ' or ' + \
1192 light_green('trend JP Tokyo') + '.\n'
1193 usage
+= s
* 2 + light_green('home') + ' will show your timeline. ' + \
1194 light_green('home 7') + ' will show 7 tweets.\n'
1195 usage
+= s
* 2 + light_green('mentions') + ' will show mentions timeline. ' + \
1196 light_green('mentions 7') + ' will show 7 mention tweets.\n'
1197 usage
+= s
* 2 + light_green('whois @mdo') + ' will show profile of ' + \
1198 magenta('@mdo') + '.\n'
1199 usage
+= s
* 2 + light_green('view @mdo') + \
1200 ' will show ' + magenta('@mdo') + '\'s home.\n'
1201 usage
+= s
* 2 + light_green('s AKB48') + ' will search for "' + \
1202 light_yellow('AKB48') + '" and return 5 newest tweet. ' + \
1203 'Search can be performed with or without hashtag.\n'
1214 usage
+= s
+ grey(u
'\u266A' + ' Tweets \n')
1215 usage
+= s
* 2 + light_green('t oops ') + \
1216 'will tweet "' + light_yellow('oops') + '" immediately.\n'
1218 light_green('rt 12 ') + ' will retweet to tweet with ' + \
1219 light_yellow('[id=12]') + '.\n'
1221 light_green('quote 12 ') + ' will quote the tweet with ' + \
1222 light_yellow('[id=12]') + '. If no extra text is added, ' + \
1223 'the quote will be canceled.\n'
1225 light_green('allrt 12 20 ') + ' will list 20 newest retweet of the tweet with ' + \
1226 light_yellow('[id=12]') + '.\n'
1227 usage
+= s
* 2 + light_green('rep 12 oops') + ' will reply "' + \
1228 light_yellow('oops') + '" to tweet with ' + \
1229 light_yellow('[id=12]') + '.\n'
1231 light_green('fav 12 ') + ' will favorite the tweet with ' + \
1232 light_yellow('[id=12]') + '.\n'
1234 light_green('ufav 12 ') + ' will unfavorite tweet with ' + \
1235 light_yellow('[id=12]') + '.\n'
1237 light_green('del 12 ') + ' will delete tweet with ' + \
1238 light_yellow('[id=12]') + '.\n'
1239 usage
+= s
* 2 + light_green('show image 12') + ' will show image in tweet with ' + \
1240 light_yellow('[id=12]') + ' in your OS\'s image viewer.\n'
1241 usage
+= s
* 2 + light_green('open 12') + ' will open url in tweet with ' + \
1242 light_yellow('[id=12]') + ' in your OS\'s default browser.\n'
1246 def help_messages():
1253 usage
+= s
+ grey(u
'\u266A' + ' Direct messages \n')
1254 usage
+= s
* 2 + light_green('inbox') + ' will show inbox messages. ' + \
1255 light_green('inbox 7') + ' will show newest 7 messages.\n'
1256 usage
+= s
* 2 + light_green('sent') + ' will show sent messages. ' + \
1257 light_green('sent 7') + ' will show newest 7 messages.\n'
1258 usage
+= s
* 2 + light_green('mes @dtvd88 hi') + ' will send a "hi" messege to ' + \
1259 magenta('@dtvd88') + '.\n'
1260 usage
+= s
* 2 + light_green('trash 5') + ' will remove message with ' + \
1261 light_yellow('[message_id=5]') + '.\n'
1265 def help_friends_and_followers():
1267 Friends and Followers
1270 # Follower and following
1272 usage
+= s
+ grey(u
'\u266A' + ' Friends and followers \n')
1274 light_green('ls fl') + \
1275 ' will list all followers (people who are following you).\n'
1277 light_green('ls fr') + \
1278 ' will list all friends (people who you are following).\n'
1279 usage
+= s
* 2 + light_green('fl @dtvd88') + ' will follow ' + \
1280 magenta('@dtvd88') + '.\n'
1281 usage
+= s
* 2 + light_green('ufl @dtvd88') + ' will unfollow ' + \
1282 magenta('@dtvd88') + '.\n'
1283 usage
+= s
* 2 + light_green('mute @dtvd88') + ' will mute ' + \
1284 magenta('@dtvd88') + '.\n'
1285 usage
+= s
* 2 + light_green('unmute @dtvd88') + ' will unmute ' + \
1286 magenta('@dtvd88') + '.\n'
1287 usage
+= s
* 2 + light_green('muting') + ' will list muting users.\n'
1288 usage
+= s
* 2 + light_green('block @dtvd88') + ' will block ' + \
1289 magenta('@dtvd88') + '.\n'
1290 usage
+= s
* 2 + light_green('unblock @dtvd88') + ' will unblock ' + \
1291 magenta('@dtvd88') + '.\n'
1292 usage
+= s
* 2 + light_green('report @dtvd88') + ' will report ' + \
1293 magenta('@dtvd88') + ' as a spam account.\n'
1304 usage
+= s
+ grey(u
'\u266A' + ' Twitter list\n')
1305 usage
+= s
* 2 + light_green('list') + \
1306 ' will show all lists you are belong to.\n'
1307 usage
+= s
* 2 + light_green('list home') + \
1308 ' will show timeline of list. You will be asked for list\'s name.\n'
1309 usage
+= s
* 2 + light_green('list all_mem') + \
1310 ' will show list\'s all members.\n'
1311 usage
+= s
* 2 + light_green('list all_sub') + \
1312 ' will show list\'s all subscribers.\n'
1313 usage
+= s
* 2 + light_green('list add') + \
1314 ' will add specific person to a list owned by you.' + \
1315 ' You will be asked for list\'s name and person\'s name.\n'
1316 usage
+= s
* 2 + light_green('list rm') + \
1317 ' will remove specific person from a list owned by you.' + \
1318 ' You will be asked for list\'s name and person\'s name.\n'
1319 usage
+= s
* 2 + light_green('list sub') + \
1320 ' will subscribe you to a specific list.\n'
1321 usage
+= s
* 2 + light_green('list unsub') + \
1322 ' will unsubscribe you from a specific list.\n'
1323 usage
+= s
* 2 + light_green('list own') + \
1324 ' will show all list owned by you.\n'
1325 usage
+= s
* 2 + light_green('list new') + \
1326 ' will create a new list.\n'
1327 usage
+= s
* 2 + light_green('list update') + \
1328 ' will update a list owned by you.\n'
1329 usage
+= s
* 2 + light_green('list del') + \
1330 ' will delete a list owned by you.\n'
1341 usage
+= s
+ grey(u
'\u266A' + ' Switching streams \n')
1342 usage
+= s
* 2 + light_green('switch public #AKB') + \
1343 ' will switch to public stream and follow "' + \
1344 light_yellow('AKB') + '" keyword.\n'
1345 usage
+= s
* 2 + light_green('switch mine') + \
1346 ' will switch to your personal stream.\n'
1347 usage
+= s
* 2 + light_green('switch mine -f ') + \
1348 ' will prompt to enter the filter.\n'
1349 usage
+= s
* 3 + light_yellow('Only nicks') + \
1350 ' filter will decide nicks will be INCLUDE ONLY.\n'
1351 usage
+= s
* 3 + light_yellow('Ignore nicks') + \
1352 ' filter will decide nicks will be EXCLUDE.\n'
1353 usage
+= s
* 2 + light_green('switch mine -d') + \
1354 ' will use the config\'s ONLY_LIST and IGNORE_LIST.\n'
1363 h
, w
= os
.popen('stty size', 'r').read().split()
1366 usage
+= s
+ 'Hi boss! I\'m ready to serve you right now!\n'
1367 usage
+= s
+ '-' * (int(w
) - 4) + '\n'
1368 usage
+= s
+ 'You are ' + \
1369 light_yellow('already') + ' on your personal stream.\n'
1370 usage
+= s
+ 'Any update from Twitter will show up ' + \
1371 light_yellow('immediately') + '.\n'
1372 usage
+= s
+ 'In addtion, following commands are available right now:\n'
1373 # Twitter help section
1375 usage
+= s
+ grey(u
'\u266A' + ' Twitter help\n')
1376 usage
+= s
* 2 + light_green('h discover') + \
1377 ' will show help for discover commands.\n'
1378 usage
+= s
* 2 + light_green('h tweets') + \
1379 ' will show help for tweets commands.\n'
1380 usage
+= s
* 2 + light_green('h messages') + \
1381 ' will show help for messages commands.\n'
1382 usage
+= s
* 2 + light_green('h friends_and_followers') + \
1383 ' will show help for friends and followers commands.\n'
1384 usage
+= s
* 2 + light_green('h list') + \
1385 ' will show help for list commands.\n'
1386 usage
+= s
* 2 + light_green('h stream') + \
1387 ' will show help for stream commands.\n'
1390 usage
+= s
+ grey(u
'\u266A' + ' Smart shell\n')
1391 usage
+= s
* 2 + light_green('111111 * 9 / 7') + ' or any math expression ' + \
1392 'will be evaluate by Python interpreter.\n'
1393 usage
+= s
* 2 + 'Even ' + light_green('cal') + ' will show the calendar' + \
1394 ' for current month.\n'
1397 usage
+= s
+ grey(u
'\u266A' + ' Config \n')
1398 usage
+= s
* 2 + light_green('theme') + ' will list available theme. ' + \
1399 light_green('theme monokai') + ' will apply ' + light_yellow('monokai') + \
1400 ' theme immediately.\n'
1401 usage
+= s
* 2 + light_green('config') + ' will list all config.\n'
1403 light_green('config ASCII_ART') + ' will output current value of ' +\
1404 light_yellow('ASCII_ART') + ' config key.\n'
1406 light_green('config TREND_MAX default') + ' will output default value of ' + \
1407 light_yellow('TREND_MAX') + ' config key.\n'
1409 light_green('config CUSTOM_CONFIG drop') + ' will drop ' + \
1410 light_yellow('CUSTOM_CONFIG') + ' config key.\n'
1412 light_green('config IMAGE_ON_TERM = true') + ' will set value of ' + \
1413 light_yellow('IMAGE_ON_TERM') + ' config key to ' + \
1414 light_yellow('True') + '.\n'
1417 usage
+= s
+ grey(u
'\u266A' + ' Screening \n')
1418 usage
+= s
* 2 + light_green('h') + ' will show this help again.\n'
1419 usage
+= s
* 2 + light_green('c') + ' will clear the screen.\n'
1420 usage
+= s
* 2 + light_green('q') + ' will quit.\n'
1423 usage
+= s
+ '-' * (int(w
) - 4) + '\n'
1424 usage
+= s
+ 'Have fun and hang tight! \n'
1427 'discover': help_discover
,
1428 'tweets': help_tweets
,
1429 'messages': help_messages
,
1430 'friends_and_followers': help_friends_and_followers
,
1432 'stream': help_stream
,
1435 d
[g
['stuff'].strip()]()
1452 os
.system('rm -rf rainbow.db')
1453 os
.kill(g
['stream_pid'], signal
.SIGKILL
)
1459 Reset prefix of line
1462 printNicely(magenta('Need tips ? Type "h" and hit Enter key!'))
1465 printNicely(str(eval(g
['cmd'])))
1520 Listen to user's input
1525 ['public', 'mine'], # switch
1540 ['image'], # show image
1542 ['fl', 'fr'], # list
1569 [key
for key
in dict(get_all_config())], # config
1570 g
['themes'], # theme
1575 'friends_and_followers',
1583 init_interactive_shell(d
)
1588 line
= raw_input(g
['decorated_name'](c
['PREFIX']))
1592 cmd
= line
.split()[0]
1597 # Lock the semaphore
1598 db
.semaphore_update(True)
1599 # Save cmd to global variable and call process
1600 g
['stuff'] = ' '.join(line
.split()[1:])
1601 # Process the command
1604 if cmd
in ['switch', 't', 'rt', 'rep']:
1608 # Release the semaphore lock
1609 db
.semaphore_update(False)
1610 except Exception as e
:
1612 printNicely(red('OMG something is wrong with Twitter right now.'))
1615 def stream(domain
, args
, name
='Rainbow Stream'):
1621 c
['USER_DOMAIN']: name
,
1622 c
['PUBLIC_DOMAIN']: args
.track_keywords
,
1623 c
['SITE_DOMAIN']: name
,
1626 ascii_art(art_dict
[domain
])
1627 # These arguments are optional:
1629 timeout
=args
.timeout
,
1630 block
=not args
.no_block
,
1631 heartbeat_timeout
=args
.heartbeat_timeout
)
1634 if args
.track_keywords
:
1635 query_args
['track'] = args
.track_keywords
1637 stream
= TwitterStream(
1642 if domain
== c
['USER_DOMAIN']:
1643 tweet_iter
= stream
.user(**query_args
)
1644 elif domain
== c
['SITE_DOMAIN']:
1645 tweet_iter
= stream
.site(**query_args
)
1647 if args
.track_keywords
:
1648 tweet_iter
= stream
.statuses
.filter(**query_args
)
1650 tweet_iter
= stream
.statuses
.sample()
1651 for tweet
in tweet_iter
:
1653 printNicely("-- None --")
1654 elif tweet
is Timeout
:
1655 printNicely("-- Timeout --")
1656 elif tweet
is HeartbeatTimeout
:
1657 printNicely("-- Heartbeat Timeout --")
1658 elif tweet
is Hangup
:
1659 printNicely("-- Hangup --")
1660 elif tweet
.get('text'):
1663 keyword
=args
.track_keywords
,
1664 check_semaphore
=True,
1668 elif tweet
.get('direct_message'):
1669 print_message(tweet
['direct_message'])
1670 except TwitterHTTPError
:
1673 magenta("We have maximum connection problem with twitter'stream API right now :("))
1681 args
= parse_arguments()
1684 except TwitterHTTPError
:
1687 magenta("Something wrong with Twitter Oauth right now :("))
1689 magenta("Please delete ~/.rainbow_oauth and try again."))
1691 os
.system('rm -rf rainbow.db')
1693 # Spawn stream process
1699 g
['original_name']))
1701 # Start listen process
1705 g
['stream_pid'] = p
.pid