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 *
80 def parse_arguments():
84 parser
= argparse
.ArgumentParser(description
=__doc__
or "")
88 help='Timeout for the stream (seconds).')
91 '--heartbeat-timeout',
92 help='Set heartbeat timeout.',
98 help='Set stream to non-blocking.')
102 help='Search the stream for specific text.')
106 help='Filter specific screen_name.')
110 help='Ignore specific screen_name.')
115 help='Display all image on terminal.')
116 return parser
.parse_args()
121 Authenticate with Twitter OAuth
123 # When using rainbow stream you must authorize.
124 twitter_credential
= os
.environ
.get(
128 '')) + os
.sep
+ '.rainbow_oauth'
129 if not os
.path
.exists(twitter_credential
):
130 oauth_dance("Rainbow Stream",
134 oauth_token
, oauth_token_secret
= read_token_file(twitter_credential
)
147 t
= Twitter(auth
=authen())
148 name
= '@' + t
.account
.verify_credentials()['screen_name']
149 if not get_config('PREFIX'):
150 set_config('PREFIX', name
)
151 g
['original_name'] = name
[1:]
152 g
['decorated_name'] = lambda x
: color_func(
153 c
['DECORATED_NAME'])(
156 files
= os
.listdir(os
.path
.dirname(__file__
) + '/colorset')
157 themes
= [f
.split('.')[0] for f
in files
if f
.split('.')[-1] == 'json']
159 db
.theme_store(c
['THEME'])
161 db
.semaphore_store(False, False)
163 c
['IMAGE_ON_TERM'] = args
.image_on_term
171 target
= g
['stuff'].split()[0]
173 args
= parse_arguments()
175 if g
['stuff'].split()[-1] == '-f':
176 guide
= 'To ignore an option, just hit Enter key.'
177 printNicely(light_magenta(guide
))
178 only
= raw_input('Only nicks [Ex: @xxx,@yy]: ')
179 ignore
= raw_input('Ignore nicks [Ex: @xxx,@yy]: ')
180 args
.filter = filter(None, only
.split(','))
181 args
.ignore
= filter(None, ignore
.split(','))
182 elif g
['stuff'].split()[-1] == '-d':
183 args
.filter = c
['ONLY_LIST']
184 args
.ignore
= c
['IGNORE_LIST']
186 printNicely(red('Sorry, wrong format.'))
189 if target
== 'public':
190 keyword
= g
['stuff'].split()[1]
191 if keyword
[0] == '#':
192 keyword
= keyword
[1:]
194 os
.kill(g
['stream_pid'], signal
.SIGKILL
)
195 args
.track_keywords
= keyword
203 g
['stream_pid'] = p
.pid
205 elif target
== 'mine':
207 os
.kill(g
['stream_pid'], signal
.SIGKILL
)
216 g
['stream_pid'] = p
.pid
219 printNicely(cyan('Only: ' + str(args
.filter)))
221 printNicely(red('Ignore: ' + str(args
.ignore
)))
224 printNicely(red('Sorry I can\'t understand.'))
231 t
= Twitter(auth
=authen())
232 # Get country and town
234 country
= g
['stuff'].split()[0]
238 town
= g
['stuff'].split()[1]
241 avail
= t
.trends
.available()
244 trends
= t
.trends
.place(_id
=1)[0]['trends']
247 for location
in avail
:
248 # Search for country and Town
250 if location
['countryCode'] == country \
251 and location
['placeType']['name'] == 'Town' \
252 and location
['name'] == town
:
253 trends
= t
.trends
.place(_id
=location
['woeid'])[0]['trends']
255 # Search for country only
257 if location
['countryCode'] == country \
258 and location
['placeType']['name'] == 'Country':
259 trends
= t
.trends
.place(_id
=location
['woeid'])[0]['trends']
267 t
= Twitter(auth
=authen())
268 num
= c
['HOME_TWEET_NUM']
269 if g
['stuff'].isdigit():
270 num
= int(g
['stuff'])
271 for tweet
in reversed(t
.statuses
.home_timeline(count
=num
)):
280 t
= Twitter(auth
=authen())
281 user
= g
['stuff'].split()[0]
284 num
= int(g
['stuff'].split()[1])
286 num
= c
['HOME_TWEET_NUM']
287 for tweet
in reversed(t
.statuses
.user_timeline(count
=num
, screen_name
=user
[1:])):
291 printNicely(red('A name should begin with a \'@\''))
298 t
= Twitter(auth
=authen())
299 num
= c
['HOME_TWEET_NUM']
300 if g
['stuff'].isdigit():
301 num
= int(g
['stuff'])
302 for tweet
in reversed(t
.statuses
.mentions_timeline(count
=num
)):
311 t
= Twitter(auth
=authen())
312 t
.statuses
.update(status
=g
['stuff'])
319 t
= Twitter(auth
=authen())
321 id = int(g
['stuff'].split()[0])
323 printNicely(red('Sorry I can\'t understand.'))
325 tid
= db
.rainbow_to_tweet_query(id)[0].tweet_id
326 t
.statuses
.retweet(id=tid
, include_entities
=False, trim_user
=True)
333 t
= Twitter(auth
=authen())
335 id = int(g
['stuff'].split()[0])
337 printNicely(red('Sorry I can\'t understand.'))
339 tid
= db
.rainbow_to_tweet_query(id)[0].tweet_id
340 tweet
= t
.statuses
.show(id=tid
)
341 screen_name
= tweet
['user']['screen_name']
343 quote
= '\"@' + screen_name
+ ': ' + text
+ '\"'
344 quote
= quote
.encode('utf8')
345 notice
= light_magenta('Compose mode ')
346 notice
+= light_yellow('(Enter nothing will cancel the quote)')
347 notice
+= light_magenta(':')
349 extra
= raw_input(quote
)
351 t
.statuses
.update(status
=quote
+ extra
)
353 printNicely(light_magenta('No text added.'))
360 t
= Twitter(auth
=authen())
363 id = int(g
['stuff'].split()[0])
365 printNicely(red('Sorry I can\'t understand.'))
367 tid
= db
.rainbow_to_tweet_query(id)[0].tweet_id
368 # Get display num if exist
370 num
= int(g
['stuff'].split()[1])
372 num
= c
['RETWEETS_SHOW_NUM']
373 # Get result and display
374 rt_ary
= t
.statuses
.retweets(id=tid
, count
=num
)
376 printNicely(magenta('This tweet has no retweet.'))
378 for tweet
in reversed(rt_ary
):
387 t
= Twitter(auth
=authen())
389 id = int(g
['stuff'].split()[0])
391 printNicely(red('Sorry I can\'t understand.'))
393 tid
= db
.rainbow_to_tweet_query(id)[0].tweet_id
394 t
.favorites
.create(_id
=tid
, include_entities
=False)
395 printNicely(green('Favorited.'))
396 draw(t
.statuses
.show(id=tid
))
404 t
= Twitter(auth
=authen())
406 id = int(g
['stuff'].split()[0])
408 printNicely(red('Sorry I can\'t understand.'))
410 tid
= db
.rainbow_to_tweet_query(id)[0].tweet_id
411 user
= t
.statuses
.show(id=tid
)['user']['screen_name']
412 status
= ' '.join(g
['stuff'].split()[1:])
413 status
= '@' + user
+ ' ' + status
.decode('utf-8')
414 t
.statuses
.update(status
=status
, in_reply_to_status_id
=tid
)
421 t
= Twitter(auth
=authen())
423 rid
= int(g
['stuff'].split()[0])
425 printNicely(red('Sorry I can\'t understand.'))
427 tid
= db
.rainbow_to_tweet_query(rid
)[0].tweet_id
428 t
.statuses
.destroy(id=tid
)
429 printNicely(green('Okay it\'s gone.'))
436 t
= Twitter(auth
=authen())
438 id = int(g
['stuff'].split()[0])
440 printNicely(red('Sorry I can\'t understand.'))
442 tid
= db
.rainbow_to_tweet_query(id)[0].tweet_id
443 t
.favorites
.destroy(_id
=tid
)
444 printNicely(green('Okay it\'s unfavorited.'))
445 draw(t
.statuses
.show(id=tid
))
453 t
= Twitter(auth
=authen())
454 g
['stuff'] = g
['stuff'].strip()
455 rel
= t
.search
.tweets(q
=g
['stuff'])['statuses']
457 printNicely('Newest tweets:')
458 for i
in reversed(xrange(c
['SEARCH_MAX_RECORD'])):
463 printNicely(magenta('I\'m afraid there is no result'))
468 Send a direct message
470 t
= Twitter(auth
=authen())
471 user
= g
['stuff'].split()[0]
472 if user
[0].startswith('@'):
474 content
= g
['stuff'].split()[1]
476 printNicely(red('Sorry I can\'t understand.'))
477 t
.direct_messages
.new(
478 screen_name
=user
[1:],
481 printNicely(green('Message sent.'))
483 printNicely(red('A name should begin with a \'@\''))
490 t
= Twitter(auth
=authen())
492 target
= g
['stuff'].split()[0]
493 if target
!= 'image':
495 id = int(g
['stuff'].split()[1])
496 tid
= db
.rainbow_to_tweet_query(id)[0].tweet_id
497 tweet
= t
.statuses
.show(id=tid
)
498 media
= tweet
['entities']['media']
500 res
= requests
.get(m
['media_url'])
501 img
= Image
.open(BytesIO(res
.content
))
504 printNicely(red('Sorry I can\'t show this image.'))
511 t
= Twitter(auth
=authen())
513 if not g
['stuff'].isdigit():
515 tid
= db
.rainbow_to_tweet_query(g
['stuff'])[0].tweet_id
516 tweet
= t
.statuses
.show(id=tid
)
518 u
for u
in tweet
['text'].split() if u
.startswith('http://')]
520 printNicely(light_magenta('No url here @.@!'))
522 for link
in link_ary
:
523 webbrowser
.open(link
)
525 printNicely(red('Sorry I can\'t open url in this tweet.'))
530 List friends for followers
532 t
= Twitter(auth
=authen())
535 name
= g
['stuff'].split()[1]
536 if name
.startswith('@'):
539 printNicely(red('A name should begin with a \'@\''))
540 raise Exception('Invalid name')
542 name
= g
['original_name']
543 # Get list followers or friends
545 target
= g
['stuff'].split()[0]
547 printNicely(red('Omg some syntax is wrong.'))
549 d
= {'fl': 'followers', 'fr': 'friends'}
553 while next_cursor
!= 0:
554 list = getattr(t
, d
[target
]).list(
558 include_entities
=False,
560 for u
in list['users']:
561 rel
[u
['name']] = '@' + u
['screen_name']
562 next_cursor
= list['next_cursor']
564 printNicely('All: ' + str(len(rel
)) + ' ' + d
[target
] + '.')
566 user
= ' ' + cycle_color(name
)
567 user
+= color_func(c
['TWEET']['nick'])(' ' + rel
[name
] + ' ')
573 Inbox direct messages
575 t
= Twitter(auth
=authen())
576 num
= c
['MESSAGES_DISPLAY']
578 if g
['stuff'].isdigit():
581 # Max message per page is 20 so we have to loop
583 rel
= rel
+ t
.direct_messages(
586 include_entities
=False,
591 rel
= rel
+ t
.direct_messages(
594 include_entities
=False,
598 printNicely('Inbox: newest ' + str(len(rel
)) + ' messages.')
599 for m
in reversed(rel
):
608 t
= Twitter(auth
=authen())
609 num
= c
['MESSAGES_DISPLAY']
611 if g
['stuff'].isdigit():
612 num
= int(g
['stuff'])
614 # Max message per page is 20 so we have to loop
616 rel
= rel
+ t
.direct_messages
.sent(
619 include_entities
=False,
624 rel
= rel
+ t
.direct_messages
.sent(
627 include_entities
=False,
631 printNicely('Sent: newest ' + str(len(rel
)) + ' messages.')
632 for m
in reversed(rel
):
641 t
= Twitter(auth
=authen())
643 rid
= int(g
['stuff'].split()[0])
645 printNicely(red('Sorry I can\'t understand.'))
646 mid
= db
.rainbow_to_message_query(rid
)[0].message_id
647 t
.direct_messages
.destroy(id=mid
)
648 printNicely(green('Message deleted.'))
653 Show profile of a specific user
655 t
= Twitter(auth
=authen())
656 screen_name
= g
['stuff'].split()[0]
657 if screen_name
.startswith('@'):
660 screen_name
=screen_name
[1:],
661 include_entities
=False)
664 printNicely(red('Omg no user.'))
666 printNicely(red('A name should begin with a \'@\''))
673 t
= Twitter(auth
=authen())
674 screen_name
= g
['stuff'].split()[0]
675 if screen_name
.startswith('@'):
676 t
.friendships
.create(screen_name
=screen_name
[1:], follow
=True)
677 printNicely(green('You are following ' + screen_name
+ ' now!'))
679 printNicely(red('A name should begin with a \'@\''))
686 t
= Twitter(auth
=authen())
687 screen_name
= g
['stuff'].split()[0]
688 if screen_name
.startswith('@'):
689 t
.friendships
.destroy(
690 screen_name
=screen_name
[1:],
691 include_entities
=False)
692 printNicely(green('Unfollow ' + screen_name
+ ' success!'))
694 printNicely(red('A name should begin with a \'@\''))
701 t
= Twitter(auth
=authen())
703 screen_name
= g
['stuff'].split()[0]
705 printNicely(red('A name should be specified. '))
707 if screen_name
.startswith('@'):
708 rel
= t
.mutes
.users
.create(screen_name
=screen_name
[1:])
709 if isinstance(rel
, dict):
710 printNicely(green(screen_name
+ ' is muted.'))
712 printNicely(red(rel
))
714 printNicely(red('A name should begin with a \'@\''))
721 t
= Twitter(auth
=authen())
723 screen_name
= g
['stuff'].split()[0]
725 printNicely(red('A name should be specified. '))
727 if screen_name
.startswith('@'):
728 rel
= t
.mutes
.users
.destroy(screen_name
=screen_name
[1:])
729 if isinstance(rel
, dict):
730 printNicely(green(screen_name
+ ' is unmuted.'))
732 printNicely(red(rel
))
734 printNicely(red('A name should begin with a \'@\''))
741 t
= Twitter(auth
=authen())
746 while next_cursor
!= 0:
747 list = t
.mutes
.users
.list(
748 screen_name
=g
['original_name'],
751 include_entities
=False,
753 for u
in list['users']:
754 rel
[u
['name']] = '@' + u
['screen_name']
755 next_cursor
= list['next_cursor']
757 printNicely('All: ' + str(len(rel
)) + ' people.')
759 user
= ' ' + cycle_color(name
)
760 user
+= color_func(c
['TWEET']['nick'])(' ' + rel
[name
] + ' ')
768 t
= Twitter(auth
=authen())
769 screen_name
= g
['stuff'].split()[0]
770 if screen_name
.startswith('@'):
772 screen_name
=screen_name
[1:],
773 include_entities
=False,
775 printNicely(green('You blocked ' + screen_name
+ '.'))
777 printNicely(red('A name should begin with a \'@\''))
784 t
= Twitter(auth
=authen())
785 screen_name
= g
['stuff'].split()[0]
786 if screen_name
.startswith('@'):
788 screen_name
=screen_name
[1:],
789 include_entities
=False,
791 printNicely(green('Unblock ' + screen_name
+ ' success!'))
793 printNicely(red('A name should begin with a \'@\''))
798 Report a user as a spam account
800 t
= Twitter(auth
=authen())
801 screen_name
= g
['stuff'].split()[0]
802 if screen_name
.startswith('@'):
804 screen_name
=screen_name
[1:])
805 printNicely(green('You reported ' + screen_name
+ '.'))
807 printNicely(red('Sorry I can\'t understand.'))
815 list_name
= raw_input(light_magenta('Give me the list\'s name: '))
816 # Get list name and owner
818 owner
, slug
= list_name
.split('/')
819 if slug
.startswith('@'):
824 light_magenta('List name should follow "@owner/list_name" format.'))
825 raise Exception('Wrong list name')
832 rel
= t
.lists
.list(screen_name
=g
['original_name'])
836 printNicely(light_magenta('You belong to no lists :)'))
843 owner
, slug
= get_slug()
844 res
= t
.lists
.statuses(
846 owner_screen_name
=owner
,
848 include_entities
=False)
858 owner
, slug
= get_slug()
862 while next_cursor
!= 0:
865 owner_screen_name
=owner
,
867 include_entities
=False)
869 rel
[u
['name']] = '@' + u
['screen_name']
870 next_cursor
= m
['next_cursor']
871 printNicely('All: ' + str(len(rel
)) + ' members.')
873 user
= ' ' + cycle_color(name
)
874 user
+= color_func(c
['TWEET']['nick'])(' ' + rel
[name
] + ' ')
878 def list_subscribers(t
):
882 owner
, slug
= get_slug()
886 while next_cursor
!= 0:
887 m
= t
.lists
.subscribers(
889 owner_screen_name
=owner
,
891 include_entities
=False)
893 rel
[u
['name']] = '@' + u
['screen_name']
894 next_cursor
= m
['next_cursor']
895 printNicely('All: ' + str(len(rel
)) + ' subscribers.')
897 user
= ' ' + cycle_color(name
)
898 user
+= color_func(c
['TWEET']['nick'])(' ' + rel
[name
] + ' ')
904 Add specific user to a list
906 owner
, slug
= get_slug()
908 user_name
= raw_input(light_magenta('Give me name of the newbie: '))
909 if user_name
.startswith('@'):
910 user_name
= user_name
[1:]
912 t
.lists
.members
.create(
914 owner_screen_name
=owner
,
915 screen_name
=user_name
)
916 printNicely(green('Added.'))
918 printNicely(light_magenta('I\'m sorry we can not add him/her.'))
923 Remove specific user from a list
925 owner
, slug
= get_slug()
927 user_name
= raw_input(light_magenta('Give me name of the unlucky one: '))
928 if user_name
.startswith('@'):
929 user_name
= user_name
[1:]
931 t
.lists
.members
.destroy(
933 owner_screen_name
=owner
,
934 screen_name
=user_name
)
935 printNicely(green('Gone.'))
937 printNicely(light_magenta('I\'m sorry we can not remove him/her.'))
940 def list_subscribe(t
):
944 owner
, slug
= get_slug()
947 t
.lists
.subscribers
.create(
949 owner_screen_name
=owner
)
950 printNicely(green('Done.'))
953 light_magenta('I\'m sorry you can not subscribe to this list.'))
956 def list_unsubscribe(t
):
960 owner
, slug
= get_slug()
963 t
.lists
.subscribers
.destroy(
965 owner_screen_name
=owner
)
966 printNicely(green('Done.'))
969 light_magenta('I\'m sorry you can not unsubscribe to this list.'))
978 while next_cursor
!= 0:
979 res
= t
.lists
.ownerships(
980 screen_name
=g
['original_name'],
983 next_cursor
= res
['next_cursor']
987 printNicely(light_magenta('You own no lists :)'))
994 name
= raw_input(light_magenta('New list\'s name: '))
995 mode
= raw_input(light_magenta('New list\'s mode (public/private): '))
996 description
= raw_input(light_magenta('New list\'s description: '))
1001 description
=description
)
1002 printNicely(green(name
+ ' list is created.'))
1004 printNicely(red('Oops something is wrong with Twitter :('))
1011 slug
= raw_input(light_magenta('Your list that you want to update: '))
1012 name
= raw_input(light_magenta('Update name (leave blank to unchange): '))
1013 mode
= raw_input(light_magenta('Update mode (public/private): '))
1014 description
= raw_input(light_magenta('Update description: '))
1018 slug
='-'.join(slug
.split()),
1019 owner_screen_name
=g
['original_name'],
1022 description
=description
)
1026 owner_screen_name
=g
['original_name'],
1028 description
=description
)
1029 printNicely(green(slug
+ ' list is updated.'))
1031 printNicely(red('Oops something is wrong with Twitter :('))
1038 slug
= raw_input(light_magenta('Your list that you want to update: '))
1041 slug
='-'.join(slug
.split()),
1042 owner_screen_name
=g
['original_name'])
1043 printNicely(green(slug
+ ' list is deleted.'))
1045 printNicely(red('Oops something is wrong with Twitter :('))
1052 t
= Twitter(auth
=authen())
1053 # List all lists or base on action
1055 g
['list_action'] = g
['stuff'].split()[0]
1062 'all_mem': list_members
,
1063 'all_sub': list_subscribers
,
1066 'sub': list_subscribe
,
1067 'unsub': list_unsubscribe
,
1070 'update': list_update
,
1074 return action_ary
[g
['list_action']](t
)
1076 printNicely(red('Please try again.'))
1081 Unix's command `cal`
1084 rel
= os
.popen('cal').read().split('\n')
1087 show_calendar(month
, date
, rel
)
1092 Browse and change config
1094 all_config
= get_all_config()
1095 g
['stuff'] = g
['stuff'].strip()
1098 for k
in all_config
:
1100 green(k
) + ': ' + light_yellow(str(all_config
[k
]))
1102 guide
= 'Detailed explanation can be found at ' + \
1103 color_func(c
['TWEET']['link'])(
1104 'http://rainbowstream.readthedocs.org/en/latest/#config-explanation')
1106 # Print specific config
1107 elif len(g
['stuff'].split()) == 1:
1108 if g
['stuff'] in all_config
:
1111 green(k
) + ': ' + light_yellow(str(all_config
[k
]))
1114 printNicely(red('No such config key.'))
1115 # Print specific config's default value
1116 elif len(g
['stuff'].split()) == 2 and g
['stuff'].split()[-1] == 'default':
1117 key
= g
['stuff'].split()[0]
1119 value
= get_default_config(key
)
1120 line
= ' ' * 2 + green(key
) + ': ' + light_magenta(value
)
1124 light_magenta('This config key does not exist in default.'))
1125 # Delete specific config key in config file
1126 elif len(g
['stuff'].split()) == 2 and g
['stuff'].split()[-1] == 'drop':
1127 key
= g
['stuff'].split()[0]
1130 printNicely(green('Config key is dropped.'))
1132 printNicely(red('No such config key.'))
1133 # Set specific config
1134 elif len(g
['stuff'].split()) == 3 and g
['stuff'].split()[1] == '=':
1135 key
= g
['stuff'].split()[0]
1136 value
= g
['stuff'].split()[-1]
1137 if key
== 'THEME' and not validate_theme(value
):
1138 printNicely(red('Invalid theme\'s value.'))
1141 set_config(key
, value
)
1142 # Apply theme immediately
1145 g
['decorated_name'] = lambda x
: color_func(
1146 c
['DECORATED_NAME'])(
1148 printNicely(green('Updated successfully.'))
1150 printNicely(light_magenta('Not valid value.'))
1154 printNicely(light_magenta('Sorry I can\'s understand.'))
1159 List and change theme
1163 for theme
in g
['themes']:
1164 line
= light_magenta(theme
)
1165 if c
['THEME'] == theme
:
1166 line
= ' ' * 2 + light_yellow('* ') + line
1168 line
= ' ' * 4 + line
1174 reload_theme(g
['stuff'])
1175 # Redefine decorated_name
1176 g
['decorated_name'] = lambda x
: color_func(
1177 c
['DECORATED_NAME'])(
1179 printNicely(green('Theme changed.'))
1181 printNicely(red('No such theme exists.'))
1184 def help_discover():
1189 # Discover the world
1191 usage
+= s
+ grey(u
'\u266A' + ' Discover the world \n')
1192 usage
+= s
* 2 + light_green('trend') + ' will show global trending topics. ' + \
1193 'You can try ' + light_green('trend US') + ' or ' + \
1194 light_green('trend JP Tokyo') + '.\n'
1195 usage
+= s
* 2 + light_green('home') + ' will show your timeline. ' + \
1196 light_green('home 7') + ' will show 7 tweets.\n'
1197 usage
+= s
* 2 + light_green('mentions') + ' will show mentions timeline. ' + \
1198 light_green('mentions 7') + ' will show 7 mention tweets.\n'
1199 usage
+= s
* 2 + light_green('whois @mdo') + ' will show profile of ' + \
1200 magenta('@mdo') + '.\n'
1201 usage
+= s
* 2 + light_green('view @mdo') + \
1202 ' will show ' + magenta('@mdo') + '\'s home.\n'
1203 usage
+= s
* 2 + light_green('s AKB48') + ' will search for "' + \
1204 light_yellow('AKB48') + '" and return 5 newest tweet. ' + \
1205 'Search can be performed with or without hashtag.\n'
1216 usage
+= s
+ grey(u
'\u266A' + ' Tweets \n')
1217 usage
+= s
* 2 + light_green('t oops ') + \
1218 'will tweet "' + light_yellow('oops') + '" immediately.\n'
1220 light_green('rt 12 ') + ' will retweet to tweet with ' + \
1221 light_yellow('[id=12]') + '.\n'
1223 light_green('quote 12 ') + ' will quote the tweet with ' + \
1224 light_yellow('[id=12]') + '. If no extra text is added, ' + \
1225 'the quote will be canceled.\n'
1227 light_green('allrt 12 20 ') + ' will list 20 newest retweet of the tweet with ' + \
1228 light_yellow('[id=12]') + '.\n'
1229 usage
+= s
* 2 + light_green('rep 12 oops') + ' will reply "' + \
1230 light_yellow('oops') + '" to tweet with ' + \
1231 light_yellow('[id=12]') + '.\n'
1233 light_green('fav 12 ') + ' will favorite the tweet with ' + \
1234 light_yellow('[id=12]') + '.\n'
1236 light_green('ufav 12 ') + ' will unfavorite tweet with ' + \
1237 light_yellow('[id=12]') + '.\n'
1239 light_green('del 12 ') + ' will delete tweet with ' + \
1240 light_yellow('[id=12]') + '.\n'
1241 usage
+= s
* 2 + light_green('show image 12') + ' will show image in tweet with ' + \
1242 light_yellow('[id=12]') + ' in your OS\'s image viewer.\n'
1243 usage
+= s
* 2 + light_green('open 12') + ' will open url in tweet with ' + \
1244 light_yellow('[id=12]') + ' in your OS\'s default browser.\n'
1248 def help_messages():
1255 usage
+= s
+ grey(u
'\u266A' + ' Direct messages \n')
1256 usage
+= s
* 2 + light_green('inbox') + ' will show inbox messages. ' + \
1257 light_green('inbox 7') + ' will show newest 7 messages.\n'
1258 usage
+= s
* 2 + light_green('sent') + ' will show sent messages. ' + \
1259 light_green('sent 7') + ' will show newest 7 messages.\n'
1260 usage
+= s
* 2 + light_green('mes @dtvd88 hi') + ' will send a "hi" messege to ' + \
1261 magenta('@dtvd88') + '.\n'
1262 usage
+= s
* 2 + light_green('trash 5') + ' will remove message with ' + \
1263 light_yellow('[message_id=5]') + '.\n'
1267 def help_friends_and_followers():
1269 Friends and Followers
1272 # Follower and following
1274 usage
+= s
+ grey(u
'\u266A' + ' Friends and followers \n')
1276 light_green('ls fl') + \
1277 ' will list all followers (people who are following you).\n'
1279 light_green('ls fr') + \
1280 ' will list all friends (people who you are following).\n'
1281 usage
+= s
* 2 + light_green('fl @dtvd88') + ' will follow ' + \
1282 magenta('@dtvd88') + '.\n'
1283 usage
+= s
* 2 + light_green('ufl @dtvd88') + ' will unfollow ' + \
1284 magenta('@dtvd88') + '.\n'
1285 usage
+= s
* 2 + light_green('mute @dtvd88') + ' will mute ' + \
1286 magenta('@dtvd88') + '.\n'
1287 usage
+= s
* 2 + light_green('unmute @dtvd88') + ' will unmute ' + \
1288 magenta('@dtvd88') + '.\n'
1289 usage
+= s
* 2 + light_green('muting') + ' will list muting users.\n'
1290 usage
+= s
* 2 + light_green('block @dtvd88') + ' will block ' + \
1291 magenta('@dtvd88') + '.\n'
1292 usage
+= s
* 2 + light_green('unblock @dtvd88') + ' will unblock ' + \
1293 magenta('@dtvd88') + '.\n'
1294 usage
+= s
* 2 + light_green('report @dtvd88') + ' will report ' + \
1295 magenta('@dtvd88') + ' as a spam account.\n'
1306 usage
+= s
+ grey(u
'\u266A' + ' Twitter list\n')
1307 usage
+= s
* 2 + light_green('list') + \
1308 ' will show all lists you are belong to.\n'
1309 usage
+= s
* 2 + light_green('list home') + \
1310 ' will show timeline of list. You will be asked for list\'s name.\n'
1311 usage
+= s
* 2 + light_green('list all_mem') + \
1312 ' will show list\'s all members.\n'
1313 usage
+= s
* 2 + light_green('list all_sub') + \
1314 ' will show list\'s all subscribers.\n'
1315 usage
+= s
* 2 + light_green('list add') + \
1316 ' will add specific person to a list owned by you.' + \
1317 ' You will be asked for list\'s name and person\'s name.\n'
1318 usage
+= s
* 2 + light_green('list rm') + \
1319 ' will remove specific person from a list owned by you.' + \
1320 ' You will be asked for list\'s name and person\'s name.\n'
1321 usage
+= s
* 2 + light_green('list sub') + \
1322 ' will subscribe you to a specific list.\n'
1323 usage
+= s
* 2 + light_green('list unsub') + \
1324 ' will unsubscribe you from a specific list.\n'
1325 usage
+= s
* 2 + light_green('list own') + \
1326 ' will show all list owned by you.\n'
1327 usage
+= s
* 2 + light_green('list new') + \
1328 ' will create a new list.\n'
1329 usage
+= s
* 2 + light_green('list update') + \
1330 ' will update a list owned by you.\n'
1331 usage
+= s
* 2 + light_green('list del') + \
1332 ' will delete a list owned by you.\n'
1343 usage
+= s
+ grey(u
'\u266A' + ' Switching streams \n')
1344 usage
+= s
* 2 + light_green('switch public #AKB') + \
1345 ' will switch to public stream and follow "' + \
1346 light_yellow('AKB') + '" keyword.\n'
1347 usage
+= s
* 2 + light_green('switch mine') + \
1348 ' will switch to your personal stream.\n'
1349 usage
+= s
* 2 + light_green('switch mine -f ') + \
1350 ' will prompt to enter the filter.\n'
1351 usage
+= s
* 3 + light_yellow('Only nicks') + \
1352 ' filter will decide nicks will be INCLUDE ONLY.\n'
1353 usage
+= s
* 3 + light_yellow('Ignore nicks') + \
1354 ' filter will decide nicks will be EXCLUDE.\n'
1355 usage
+= s
* 2 + light_green('switch mine -d') + \
1356 ' will use the config\'s ONLY_LIST and IGNORE_LIST.\n'
1365 h
, w
= os
.popen('stty size', 'r').read().split()
1368 usage
+= s
+ 'Hi boss! I\'m ready to serve you right now!\n'
1369 usage
+= s
+ '-' * (int(w
) - 4) + '\n'
1370 usage
+= s
+ 'You are ' + \
1371 light_yellow('already') + ' on your personal stream.\n'
1372 usage
+= s
+ 'Any update from Twitter will show up ' + \
1373 light_yellow('immediately') + '.\n'
1374 usage
+= s
+ 'In addtion, following commands are available right now:\n'
1375 # Twitter help section
1377 usage
+= s
+ grey(u
'\u266A' + ' Twitter help\n')
1378 usage
+= s
* 2 + light_green('h discover') + \
1379 ' will show help for discover commands.\n'
1380 usage
+= s
* 2 + light_green('h tweets') + \
1381 ' will show help for tweets commands.\n'
1382 usage
+= s
* 2 + light_green('h messages') + \
1383 ' will show help for messages commands.\n'
1384 usage
+= s
* 2 + light_green('h friends_and_followers') + \
1385 ' will show help for friends and followers commands.\n'
1386 usage
+= s
* 2 + light_green('h list') + \
1387 ' will show help for list commands.\n'
1388 usage
+= s
* 2 + light_green('h stream') + \
1389 ' will show help for stream commands.\n'
1392 usage
+= s
+ grey(u
'\u266A' + ' Smart shell\n')
1393 usage
+= s
* 2 + light_green('111111 * 9 / 7') + ' or any math expression ' + \
1394 'will be evaluate by Python interpreter.\n'
1395 usage
+= s
* 2 + 'Even ' + light_green('cal') + ' will show the calendar' + \
1396 ' for current month.\n'
1399 usage
+= s
+ grey(u
'\u266A' + ' Config \n')
1400 usage
+= s
* 2 + light_green('theme') + ' will list available theme. ' + \
1401 light_green('theme monokai') + ' will apply ' + light_yellow('monokai') + \
1402 ' theme immediately.\n'
1403 usage
+= s
* 2 + light_green('config') + ' will list all config.\n'
1405 light_green('config ASCII_ART') + ' will output current value of ' +\
1406 light_yellow('ASCII_ART') + ' config key.\n'
1408 light_green('config TREND_MAX default') + ' will output default value of ' + \
1409 light_yellow('TREND_MAX') + ' config key.\n'
1411 light_green('config CUSTOM_CONFIG drop') + ' will drop ' + \
1412 light_yellow('CUSTOM_CONFIG') + ' config key.\n'
1414 light_green('config IMAGE_ON_TERM = true') + ' will set value of ' + \
1415 light_yellow('IMAGE_ON_TERM') + ' config key to ' + \
1416 light_yellow('True') + '.\n'
1419 usage
+= s
+ grey(u
'\u266A' + ' Screening \n')
1420 usage
+= s
* 2 + light_green('h') + ' will show this help again.\n'
1421 usage
+= s
* 2 + light_green('p') + ' will pause the stream.\n'
1422 usage
+= s
* 2 + light_green('r') + ' will unpause the stream.\n'
1423 usage
+= s
* 2 + light_green('c') + ' will clear the screen.\n'
1424 usage
+= s
* 2 + light_green('q') + ' will quit.\n'
1427 usage
+= s
+ '-' * (int(w
) - 4) + '\n'
1428 usage
+= s
+ 'Have fun and hang tight! \n'
1431 'discover': help_discover
,
1432 'tweets': help_tweets
,
1433 'messages': help_messages
,
1434 'friends_and_followers': help_friends_and_followers
,
1436 'stream': help_stream
,
1439 d
[g
['stuff'].strip()]()
1446 Pause stream display
1448 db
.semaphore_update_pause(True)
1449 printNicely(green('Stream is paused'))
1456 db
.semaphore_update_pause(False)
1457 printNicely(green('Stream is running back now'))
1472 os
.system('rm -rf rainbow.db')
1473 os
.kill(g
['stream_pid'], signal
.SIGKILL
)
1479 Reset prefix of line
1482 printNicely(magenta('Need tips ? Type "h" and hit Enter key!'))
1485 printNicely(str(eval(g
['cmd'])))
1542 Listen to user's input
1547 ['public', 'mine'], # switch
1562 ['image'], # show image
1564 ['fl', 'fr'], # list
1591 [key
for key
in dict(get_all_config())], # config
1592 g
['themes'], # theme
1597 'friends_and_followers',
1607 init_interactive_shell(d
)
1612 line
= raw_input(g
['decorated_name'](c
['PREFIX']))
1616 cmd
= line
.split()[0]
1621 # Lock the semaphore
1622 db
.semaphore_update_lock(True)
1623 # Save cmd to global variable and call process
1624 g
['stuff'] = ' '.join(line
.split()[1:])
1625 # Process the command
1628 if cmd
in ['switch', 't', 'rt', 'rep']:
1632 # Release the semaphore lock
1633 db
.semaphore_update_lock(False)
1635 printNicely(red('OMG something is wrong with Twitter right now.'))
1638 def stream(domain
, args
, name
='Rainbow Stream'):
1644 c
['USER_DOMAIN']: name
,
1645 c
['PUBLIC_DOMAIN']: args
.track_keywords
,
1646 c
['SITE_DOMAIN']: name
,
1649 ascii_art(art_dict
[domain
])
1650 # These arguments are optional:
1652 timeout
=args
.timeout
,
1653 block
=not args
.no_block
,
1654 heartbeat_timeout
=args
.heartbeat_timeout
)
1657 if args
.track_keywords
:
1658 query_args
['track'] = args
.track_keywords
1660 stream
= TwitterStream(
1665 if domain
== c
['USER_DOMAIN']:
1666 tweet_iter
= stream
.user(**query_args
)
1667 elif domain
== c
['SITE_DOMAIN']:
1668 tweet_iter
= stream
.site(**query_args
)
1670 if args
.track_keywords
:
1671 tweet_iter
= stream
.statuses
.filter(**query_args
)
1673 tweet_iter
= stream
.statuses
.sample()
1674 for tweet
in tweet_iter
:
1676 printNicely("-- None --")
1677 elif tweet
is Timeout
:
1678 printNicely("-- Timeout --")
1679 elif tweet
is HeartbeatTimeout
:
1680 printNicely("-- Heartbeat Timeout --")
1681 elif tweet
is Hangup
:
1682 printNicely("-- Hangup --")
1683 elif tweet
.get('text'):
1686 keyword
=args
.track_keywords
,
1687 check_semaphore
=True,
1691 elif tweet
.get('direct_message'):
1692 print_message(tweet
['direct_message'],check_semaphore
=True)
1693 except TwitterHTTPError
:
1696 magenta("We have maximum connection problem with twitter'stream API right now :("))
1704 args
= parse_arguments()
1707 except TwitterHTTPError
:
1710 magenta("Something wrong with Twitter Oauth right now :("))
1712 magenta("Please delete ~/.rainbow_oauth and try again."))
1714 os
.system('rm -rf rainbow.db')
1716 # Spawn stream process
1722 g
['original_name']))
1724 # Start listen process
1728 g
['stream_pid'] = p
.pid