2 Colorful user's timeline stream
14 from twitter
.stream
import TwitterStream
, Timeout
, HeartbeatTimeout
, Hangup
15 from twitter
.api
import *
16 from twitter
.oauth
import OAuth
, read_token_file
17 from twitter
.oauth_dance
import oauth_dance
18 from twitter
.util
import printNicely
23 from .consumer
import *
24 from .interactive
import *
25 from .c_image
import *
26 from .py3patch
import *
32 StreamLock
= threading
.Lock()
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 ctrl_c_handler
= lambda signum
, frame
: quit()
146 signal
.signal(signal
.SIGINT
, ctrl_c_handler
)
148 t
= Twitter(auth
=authen())
149 name
= '@' + t
.account
.verify_credentials()['screen_name']
150 if not get_config('PREFIX'):
151 set_config('PREFIX', name
)
152 g
['original_name'] = name
[1:]
153 g
['decorated_name'] = lambda x
: color_func(
154 c
['DECORATED_NAME'])(
157 files
= os
.listdir(os
.path
.dirname(__file__
) + '/colorset')
158 themes
= [f
.split('.')[0] for f
in files
if f
.split('.')[-1] == 'json']
163 # Init tweet dict and message dict
165 c
['message_dict'] = []
167 c
['IMAGE_ON_TERM'] = args
.image_on_term
175 target
= g
['stuff'].split()[0]
177 args
= parse_arguments()
179 if g
['stuff'].split()[-1] == '-f':
180 guide
= 'To ignore an option, just hit Enter key.'
181 printNicely(light_magenta(guide
))
182 only
= raw_input('Only nicks [Ex: @xxx,@yy]: ')
183 ignore
= raw_input('Ignore nicks [Ex: @xxx,@yy]: ')
184 args
.filter = filter(None, only
.split(','))
185 args
.ignore
= filter(None, ignore
.split(','))
186 elif g
['stuff'].split()[-1] == '-d':
187 args
.filter = c
['ONLY_LIST']
188 args
.ignore
= c
['IGNORE_LIST']
190 printNicely(red('Sorry, wrong format.'))
193 if target
== 'public':
194 keyword
= g
['stuff'].split()[1]
195 if keyword
[0] == '#':
196 keyword
= keyword
[1:]
198 g
['stream_stop'] = True
199 args
.track_keywords
= keyword
201 th
= threading
.Thread(target
=stream
, args
=(c
['PUBLIC_DOMAIN'], args
))
205 elif target
== 'mine':
207 g
['stream_stop'] = True
209 th
= threading
.Thread(target
=stream
, args
=(c
['USER_DOMAIN'], args
, g
['original_name']))
214 printNicely(cyan('Only: ' + str(args
.filter)))
216 printNicely(red('Ignore: ' + str(args
.ignore
)))
219 printNicely(red('Sorry I can\'t understand.'))
226 t
= Twitter(auth
=authen())
227 # Get country and town
229 country
= g
['stuff'].split()[0]
233 town
= g
['stuff'].split()[1]
236 avail
= t
.trends
.available()
239 trends
= t
.trends
.place(_id
=1)[0]['trends']
242 for location
in avail
:
243 # Search for country and Town
245 if location
['countryCode'] == country \
246 and location
['placeType']['name'] == 'Town' \
247 and location
['name'] == town
:
248 trends
= t
.trends
.place(_id
=location
['woeid'])[0]['trends']
250 # Search for country only
252 if location
['countryCode'] == country \
253 and location
['placeType']['name'] == 'Country':
254 trends
= t
.trends
.place(_id
=location
['woeid'])[0]['trends']
262 t
= Twitter(auth
=authen())
263 num
= c
['HOME_TWEET_NUM']
264 if g
['stuff'].isdigit():
265 num
= int(g
['stuff'])
266 for tweet
in reversed(t
.statuses
.home_timeline(count
=num
)):
275 t
= Twitter(auth
=authen())
276 user
= g
['stuff'].split()[0]
279 num
= int(g
['stuff'].split()[1])
281 num
= c
['HOME_TWEET_NUM']
282 for tweet
in reversed(t
.statuses
.user_timeline(count
=num
, screen_name
=user
[1:])):
286 printNicely(red('A name should begin with a \'@\''))
293 t
= Twitter(auth
=authen())
294 num
= c
['HOME_TWEET_NUM']
295 if g
['stuff'].isdigit():
296 num
= int(g
['stuff'])
297 for tweet
in reversed(t
.statuses
.mentions_timeline(count
=num
)):
306 t
= Twitter(auth
=authen())
307 t
.statuses
.update(status
=g
['stuff'])
314 t
= Twitter(auth
=authen())
316 id = int(g
['stuff'].split()[0])
318 printNicely(red('Sorry I can\'t understand.'))
320 tid
= c
['tweet_dict'][id]
321 t
.statuses
.retweet(id=tid
, include_entities
=False, trim_user
=True)
328 t
= Twitter(auth
=authen())
330 id = int(g
['stuff'].split()[0])
332 printNicely(red('Sorry I can\'t understand.'))
334 tid
= c
['tweet_dict'][id]
335 tweet
= t
.statuses
.show(id=tid
)
336 screen_name
= tweet
['user']['screen_name']
338 quote
= '\"@' + screen_name
+ ': ' + text
+ '\"'
339 quote
= quote
.encode('utf8')
340 notice
= light_magenta('Compose mode ')
341 notice
+= light_yellow('(Enter nothing will cancel the quote)')
342 notice
+= light_magenta(':')
344 extra
= raw_input(quote
)
346 t
.statuses
.update(status
=quote
+ extra
)
348 printNicely(light_magenta('No text added.'))
355 t
= Twitter(auth
=authen())
358 id = int(g
['stuff'].split()[0])
360 printNicely(red('Sorry I can\'t understand.'))
362 tid
= c
['tweet_dict'][id]
363 # Get display num if exist
365 num
= int(g
['stuff'].split()[1])
367 num
= c
['RETWEETS_SHOW_NUM']
368 # Get result and display
369 rt_ary
= t
.statuses
.retweets(id=tid
, count
=num
)
371 printNicely(magenta('This tweet has no retweet.'))
373 for tweet
in reversed(rt_ary
):
382 t
= Twitter(auth
=authen())
384 id = int(g
['stuff'].split()[0])
386 printNicely(red('Sorry I can\'t understand.'))
388 tid
= c
['tweet_dict'][id]
389 t
.favorites
.create(_id
=tid
, include_entities
=False)
390 printNicely(green('Favorited.'))
391 draw(t
.statuses
.show(id=tid
))
399 t
= Twitter(auth
=authen())
401 id = int(g
['stuff'].split()[0])
403 printNicely(red('Sorry I can\'t understand.'))
405 tid
= c
['tweet_dict'][id]
406 user
= t
.statuses
.show(id=tid
)['user']['screen_name']
407 status
= ' '.join(g
['stuff'].split()[1:])
408 status
= '@' + user
+ ' ' + status
.decode('utf-8')
409 t
.statuses
.update(status
=status
, in_reply_to_status_id
=tid
)
416 t
= Twitter(auth
=authen())
418 id = int(g
['stuff'].split()[0])
420 printNicely(red('Sorry I can\'t understand.'))
422 tid
= c
['tweet_dict'][id]
423 t
.statuses
.destroy(id=tid
)
424 printNicely(green('Okay it\'s gone.'))
431 t
= Twitter(auth
=authen())
433 id = int(g
['stuff'].split()[0])
435 printNicely(red('Sorry I can\'t understand.'))
437 tid
= c
['tweet_dict'][id]
438 t
.favorites
.destroy(_id
=tid
)
439 printNicely(green('Okay it\'s unfavorited.'))
440 draw(t
.statuses
.show(id=tid
))
448 t
= Twitter(auth
=authen())
449 g
['stuff'] = g
['stuff'].strip()
450 rel
= t
.search
.tweets(q
=g
['stuff'])['statuses']
452 printNicely('Newest tweets:')
453 for i
in reversed(xrange(c
['SEARCH_MAX_RECORD'])):
458 printNicely(magenta('I\'m afraid there is no result'))
463 Send a direct message
465 t
= Twitter(auth
=authen())
466 user
= g
['stuff'].split()[0]
467 if user
[0].startswith('@'):
469 content
= g
['stuff'].split()[1]
471 printNicely(red('Sorry I can\'t understand.'))
472 t
.direct_messages
.new(
473 screen_name
=user
[1:],
476 printNicely(green('Message sent.'))
478 printNicely(red('A name should begin with a \'@\''))
485 t
= Twitter(auth
=authen())
487 target
= g
['stuff'].split()[0]
488 if target
!= 'image':
490 id = int(g
['stuff'].split()[1])
491 tid
= c
['tweet_dict'][id]
492 tweet
= t
.statuses
.show(id=tid
)
493 media
= tweet
['entities']['media']
495 res
= requests
.get(m
['media_url'])
496 img
= Image
.open(BytesIO(res
.content
))
499 printNicely(red('Sorry I can\'t show this image.'))
506 t
= Twitter(auth
=authen())
508 if not g
['stuff'].isdigit():
510 tid
= c
['tweet_dict'][int(g
['stuff'])]
511 tweet
= t
.statuses
.show(id=tid
)
513 u
for u
in tweet
['text'].split() if u
.startswith('http://')]
515 printNicely(light_magenta('No url here @.@!'))
517 for link
in link_ary
:
518 webbrowser
.open(link
)
520 printNicely(red('Sorry I can\'t open url in this tweet.'))
525 List friends for followers
527 t
= Twitter(auth
=authen())
530 name
= g
['stuff'].split()[1]
531 if name
.startswith('@'):
534 printNicely(red('A name should begin with a \'@\''))
535 raise Exception('Invalid name')
537 name
= g
['original_name']
538 # Get list followers or friends
540 target
= g
['stuff'].split()[0]
542 printNicely(red('Omg some syntax is wrong.'))
544 d
= {'fl': 'followers', 'fr': 'friends'}
548 while next_cursor
!= 0:
549 list = getattr(t
, d
[target
]).list(
553 include_entities
=False,
555 for u
in list['users']:
556 rel
[u
['name']] = '@' + u
['screen_name']
557 next_cursor
= list['next_cursor']
559 printNicely('All: ' + str(len(rel
)) + ' ' + d
[target
] + '.')
561 user
= ' ' + cycle_color(name
)
562 user
+= color_func(c
['TWEET']['nick'])(' ' + rel
[name
] + ' ')
568 Inbox direct messages
570 t
= Twitter(auth
=authen())
571 num
= c
['MESSAGES_DISPLAY']
573 if g
['stuff'].isdigit():
576 # Max message per page is 20 so we have to loop
578 rel
= rel
+ t
.direct_messages(
581 include_entities
=False,
586 rel
= rel
+ t
.direct_messages(
589 include_entities
=False,
593 printNicely('Inbox: newest ' + str(len(rel
)) + ' messages.')
594 for m
in reversed(rel
):
603 t
= Twitter(auth
=authen())
604 num
= c
['MESSAGES_DISPLAY']
606 if g
['stuff'].isdigit():
607 num
= int(g
['stuff'])
609 # Max message per page is 20 so we have to loop
611 rel
= rel
+ t
.direct_messages
.sent(
614 include_entities
=False,
619 rel
= rel
+ t
.direct_messages
.sent(
622 include_entities
=False,
626 printNicely('Sent: newest ' + str(len(rel
)) + ' messages.')
627 for m
in reversed(rel
):
636 t
= Twitter(auth
=authen())
638 id = int(g
['stuff'].split()[0])
640 printNicely(red('Sorry I can\'t understand.'))
641 mid
= c
['message_dict'][id]
642 t
.direct_messages
.destroy(id=mid
)
643 printNicely(green('Message deleted.'))
648 Show profile of a specific user
650 t
= Twitter(auth
=authen())
651 screen_name
= g
['stuff'].split()[0]
652 if screen_name
.startswith('@'):
655 screen_name
=screen_name
[1:],
656 include_entities
=False)
659 printNicely(red('Omg no user.'))
661 printNicely(red('A name should begin with a \'@\''))
668 t
= Twitter(auth
=authen())
669 screen_name
= g
['stuff'].split()[0]
670 if screen_name
.startswith('@'):
671 t
.friendships
.create(screen_name
=screen_name
[1:], follow
=True)
672 printNicely(green('You are following ' + screen_name
+ ' now!'))
674 printNicely(red('A name should begin with a \'@\''))
681 t
= Twitter(auth
=authen())
682 screen_name
= g
['stuff'].split()[0]
683 if screen_name
.startswith('@'):
684 t
.friendships
.destroy(
685 screen_name
=screen_name
[1:],
686 include_entities
=False)
687 printNicely(green('Unfollow ' + screen_name
+ ' success!'))
689 printNicely(red('A name should begin with a \'@\''))
696 t
= Twitter(auth
=authen())
698 screen_name
= g
['stuff'].split()[0]
700 printNicely(red('A name should be specified. '))
702 if screen_name
.startswith('@'):
703 rel
= t
.mutes
.users
.create(screen_name
=screen_name
[1:])
704 if isinstance(rel
, dict):
705 printNicely(green(screen_name
+ ' is muted.'))
707 printNicely(red(rel
))
709 printNicely(red('A name should begin with a \'@\''))
716 t
= Twitter(auth
=authen())
718 screen_name
= g
['stuff'].split()[0]
720 printNicely(red('A name should be specified. '))
722 if screen_name
.startswith('@'):
723 rel
= t
.mutes
.users
.destroy(screen_name
=screen_name
[1:])
724 if isinstance(rel
, dict):
725 printNicely(green(screen_name
+ ' is unmuted.'))
727 printNicely(red(rel
))
729 printNicely(red('A name should begin with a \'@\''))
736 t
= Twitter(auth
=authen())
741 while next_cursor
!= 0:
742 list = t
.mutes
.users
.list(
743 screen_name
=g
['original_name'],
746 include_entities
=False,
748 for u
in list['users']:
749 rel
[u
['name']] = '@' + u
['screen_name']
750 next_cursor
= list['next_cursor']
752 printNicely('All: ' + str(len(rel
)) + ' people.')
754 user
= ' ' + cycle_color(name
)
755 user
+= color_func(c
['TWEET']['nick'])(' ' + rel
[name
] + ' ')
763 t
= Twitter(auth
=authen())
764 screen_name
= g
['stuff'].split()[0]
765 if screen_name
.startswith('@'):
767 screen_name
=screen_name
[1:],
768 include_entities
=False,
770 printNicely(green('You blocked ' + screen_name
+ '.'))
772 printNicely(red('A name should begin with a \'@\''))
779 t
= Twitter(auth
=authen())
780 screen_name
= g
['stuff'].split()[0]
781 if screen_name
.startswith('@'):
783 screen_name
=screen_name
[1:],
784 include_entities
=False,
786 printNicely(green('Unblock ' + screen_name
+ ' success!'))
788 printNicely(red('A name should begin with a \'@\''))
793 Report a user as a spam account
795 t
= Twitter(auth
=authen())
796 screen_name
= g
['stuff'].split()[0]
797 if screen_name
.startswith('@'):
799 screen_name
=screen_name
[1:])
800 printNicely(green('You reported ' + screen_name
+ '.'))
802 printNicely(red('Sorry I can\'t understand.'))
810 list_name
= raw_input(light_magenta('Give me the list\'s name: '))
811 # Get list name and owner
813 owner
, slug
= list_name
.split('/')
814 if slug
.startswith('@'):
819 light_magenta('List name should follow "@owner/list_name" format.'))
820 raise Exception('Wrong list name')
827 rel
= t
.lists
.list(screen_name
=g
['original_name'])
831 printNicely(light_magenta('You belong to no lists :)'))
838 owner
, slug
= get_slug()
839 res
= t
.lists
.statuses(
841 owner_screen_name
=owner
,
843 include_entities
=False)
853 owner
, slug
= get_slug()
857 while next_cursor
!= 0:
860 owner_screen_name
=owner
,
862 include_entities
=False)
864 rel
[u
['name']] = '@' + u
['screen_name']
865 next_cursor
= m
['next_cursor']
866 printNicely('All: ' + str(len(rel
)) + ' members.')
868 user
= ' ' + cycle_color(name
)
869 user
+= color_func(c
['TWEET']['nick'])(' ' + rel
[name
] + ' ')
873 def list_subscribers(t
):
877 owner
, slug
= get_slug()
881 while next_cursor
!= 0:
882 m
= t
.lists
.subscribers(
884 owner_screen_name
=owner
,
886 include_entities
=False)
888 rel
[u
['name']] = '@' + u
['screen_name']
889 next_cursor
= m
['next_cursor']
890 printNicely('All: ' + str(len(rel
)) + ' subscribers.')
892 user
= ' ' + cycle_color(name
)
893 user
+= color_func(c
['TWEET']['nick'])(' ' + rel
[name
] + ' ')
899 Add specific user to a list
901 owner
, slug
= get_slug()
903 user_name
= raw_input(light_magenta('Give me name of the newbie: '))
904 if user_name
.startswith('@'):
905 user_name
= user_name
[1:]
907 t
.lists
.members
.create(
909 owner_screen_name
=owner
,
910 screen_name
=user_name
)
911 printNicely(green('Added.'))
913 printNicely(light_magenta('I\'m sorry we can not add him/her.'))
918 Remove specific user from a list
920 owner
, slug
= get_slug()
922 user_name
= raw_input(light_magenta('Give me name of the unlucky one: '))
923 if user_name
.startswith('@'):
924 user_name
= user_name
[1:]
926 t
.lists
.members
.destroy(
928 owner_screen_name
=owner
,
929 screen_name
=user_name
)
930 printNicely(green('Gone.'))
932 printNicely(light_magenta('I\'m sorry we can not remove him/her.'))
935 def list_subscribe(t
):
939 owner
, slug
= get_slug()
942 t
.lists
.subscribers
.create(
944 owner_screen_name
=owner
)
945 printNicely(green('Done.'))
948 light_magenta('I\'m sorry you can not subscribe to this list.'))
951 def list_unsubscribe(t
):
955 owner
, slug
= get_slug()
958 t
.lists
.subscribers
.destroy(
960 owner_screen_name
=owner
)
961 printNicely(green('Done.'))
964 light_magenta('I\'m sorry you can not unsubscribe to this list.'))
973 while next_cursor
!= 0:
974 res
= t
.lists
.ownerships(
975 screen_name
=g
['original_name'],
978 next_cursor
= res
['next_cursor']
982 printNicely(light_magenta('You own no lists :)'))
989 name
= raw_input(light_magenta('New list\'s name: '))
990 mode
= raw_input(light_magenta('New list\'s mode (public/private): '))
991 description
= raw_input(light_magenta('New list\'s description: '))
996 description
=description
)
997 printNicely(green(name
+ ' list is created.'))
999 printNicely(red('Oops something is wrong with Twitter :('))
1006 slug
= raw_input(light_magenta('Your list that you want to update: '))
1007 name
= raw_input(light_magenta('Update name (leave blank to unchange): '))
1008 mode
= raw_input(light_magenta('Update mode (public/private): '))
1009 description
= raw_input(light_magenta('Update description: '))
1013 slug
='-'.join(slug
.split()),
1014 owner_screen_name
=g
['original_name'],
1017 description
=description
)
1021 owner_screen_name
=g
['original_name'],
1023 description
=description
)
1024 printNicely(green(slug
+ ' list is updated.'))
1026 printNicely(red('Oops something is wrong with Twitter :('))
1033 slug
= raw_input(light_magenta('Your list that you want to update: '))
1036 slug
='-'.join(slug
.split()),
1037 owner_screen_name
=g
['original_name'])
1038 printNicely(green(slug
+ ' list is deleted.'))
1040 printNicely(red('Oops something is wrong with Twitter :('))
1047 t
= Twitter(auth
=authen())
1048 # List all lists or base on action
1050 g
['list_action'] = g
['stuff'].split()[0]
1057 'all_mem': list_members
,
1058 'all_sub': list_subscribers
,
1061 'sub': list_subscribe
,
1062 'unsub': list_unsubscribe
,
1065 'update': list_update
,
1069 return action_ary
[g
['list_action']](t
)
1071 printNicely(red('Please try again.'))
1076 Unix's command `cal`
1079 rel
= os
.popen('cal').read().split('\n')
1082 show_calendar(month
, date
, rel
)
1087 Browse and change config
1089 all_config
= get_all_config()
1090 g
['stuff'] = g
['stuff'].strip()
1093 for k
in all_config
:
1095 green(k
) + ': ' + light_yellow(str(all_config
[k
]))
1097 guide
= 'Detailed explanation can be found at ' + \
1098 color_func(c
['TWEET']['link'])(
1099 'http://rainbowstream.readthedocs.org/en/latest/#config-explanation')
1101 # Print specific config
1102 elif len(g
['stuff'].split()) == 1:
1103 if g
['stuff'] in all_config
:
1106 green(k
) + ': ' + light_yellow(str(all_config
[k
]))
1109 printNicely(red('No such config key.'))
1110 # Print specific config's default value
1111 elif len(g
['stuff'].split()) == 2 and g
['stuff'].split()[-1] == 'default':
1112 key
= g
['stuff'].split()[0]
1114 value
= get_default_config(key
)
1115 line
= ' ' * 2 + green(key
) + ': ' + light_magenta(value
)
1119 light_magenta('This config key does not exist in default.'))
1120 # Delete specific config key in config file
1121 elif len(g
['stuff'].split()) == 2 and g
['stuff'].split()[-1] == 'drop':
1122 key
= g
['stuff'].split()[0]
1125 printNicely(green('Config key is dropped.'))
1127 printNicely(red('No such config key.'))
1128 # Set specific config
1129 elif len(g
['stuff'].split()) == 3 and g
['stuff'].split()[1] == '=':
1130 key
= g
['stuff'].split()[0]
1131 value
= g
['stuff'].split()[-1]
1132 if key
== 'THEME' and not validate_theme(value
):
1133 printNicely(red('Invalid theme\'s value.'))
1136 set_config(key
, value
)
1137 # Apply theme immediately
1139 c
['THEME'] = reload_theme(value
,c
['THEME'])
1140 g
['decorated_name'] = lambda x
: color_func(
1141 c
['DECORATED_NAME'])(
1143 printNicely(green('Updated successfully.'))
1145 printNicely(light_magenta('Not valid value.'))
1149 printNicely(light_magenta('Sorry I can\'s understand.'))
1154 List and change theme
1158 for theme
in g
['themes']:
1159 line
= light_magenta(theme
)
1160 if c
['THEME'] == theme
:
1161 line
= ' ' * 2 + light_yellow('* ') + line
1163 line
= ' ' * 4 + line
1169 c
['THEME'] = reload_theme(g
['stuff'],c
['THEME'])
1170 # Redefine decorated_name
1171 g
['decorated_name'] = lambda x
: color_func(
1172 c
['DECORATED_NAME'])(
1174 printNicely(green('Theme changed.'))
1176 printNicely(red('No such theme exists.'))
1179 def help_discover():
1184 # Discover the world
1186 usage
+= s
+ grey(u
'\u266A' + ' Discover the world \n')
1187 usage
+= s
* 2 + light_green('trend') + ' will show global trending topics. ' + \
1188 'You can try ' + light_green('trend US') + ' or ' + \
1189 light_green('trend JP Tokyo') + '.\n'
1190 usage
+= s
* 2 + light_green('home') + ' will show your timeline. ' + \
1191 light_green('home 7') + ' will show 7 tweets.\n'
1192 usage
+= s
* 2 + light_green('mentions') + ' will show mentions timeline. ' + \
1193 light_green('mentions 7') + ' will show 7 mention tweets.\n'
1194 usage
+= s
* 2 + light_green('whois @mdo') + ' will show profile of ' + \
1195 magenta('@mdo') + '.\n'
1196 usage
+= s
* 2 + light_green('view @mdo') + \
1197 ' will show ' + magenta('@mdo') + '\'s home.\n'
1198 usage
+= s
* 2 + light_green('s AKB48') + ' will search for "' + \
1199 light_yellow('AKB48') + '" and return 5 newest tweet. ' + \
1200 'Search can be performed with or without hashtag.\n'
1211 usage
+= s
+ grey(u
'\u266A' + ' Tweets \n')
1212 usage
+= s
* 2 + light_green('t oops ') + \
1213 'will tweet "' + light_yellow('oops') + '" immediately.\n'
1215 light_green('rt 12 ') + ' will retweet to tweet with ' + \
1216 light_yellow('[id=12]') + '.\n'
1218 light_green('quote 12 ') + ' will quote the tweet with ' + \
1219 light_yellow('[id=12]') + '. If no extra text is added, ' + \
1220 'the quote will be canceled.\n'
1222 light_green('allrt 12 20 ') + ' will list 20 newest retweet of the tweet with ' + \
1223 light_yellow('[id=12]') + '.\n'
1224 usage
+= s
* 2 + light_green('rep 12 oops') + ' will reply "' + \
1225 light_yellow('oops') + '" to tweet with ' + \
1226 light_yellow('[id=12]') + '.\n'
1228 light_green('fav 12 ') + ' will favorite the tweet with ' + \
1229 light_yellow('[id=12]') + '.\n'
1231 light_green('ufav 12 ') + ' will unfavorite tweet with ' + \
1232 light_yellow('[id=12]') + '.\n'
1234 light_green('del 12 ') + ' will delete tweet with ' + \
1235 light_yellow('[id=12]') + '.\n'
1236 usage
+= s
* 2 + light_green('show image 12') + ' will show image in tweet with ' + \
1237 light_yellow('[id=12]') + ' in your OS\'s image viewer.\n'
1238 usage
+= s
* 2 + light_green('open 12') + ' will open url in tweet with ' + \
1239 light_yellow('[id=12]') + ' in your OS\'s default browser.\n'
1243 def help_messages():
1250 usage
+= s
+ grey(u
'\u266A' + ' Direct messages \n')
1251 usage
+= s
* 2 + light_green('inbox') + ' will show inbox messages. ' + \
1252 light_green('inbox 7') + ' will show newest 7 messages.\n'
1253 usage
+= s
* 2 + light_green('sent') + ' will show sent messages. ' + \
1254 light_green('sent 7') + ' will show newest 7 messages.\n'
1255 usage
+= s
* 2 + light_green('mes @dtvd88 hi') + ' will send a "hi" messege to ' + \
1256 magenta('@dtvd88') + '.\n'
1257 usage
+= s
* 2 + light_green('trash 5') + ' will remove message with ' + \
1258 light_yellow('[message_id=5]') + '.\n'
1262 def help_friends_and_followers():
1264 Friends and Followers
1267 # Follower and following
1269 usage
+= s
+ grey(u
'\u266A' + ' Friends and followers \n')
1271 light_green('ls fl') + \
1272 ' will list all followers (people who are following you).\n'
1274 light_green('ls fr') + \
1275 ' will list all friends (people who you are following).\n'
1276 usage
+= s
* 2 + light_green('fl @dtvd88') + ' will follow ' + \
1277 magenta('@dtvd88') + '.\n'
1278 usage
+= s
* 2 + light_green('ufl @dtvd88') + ' will unfollow ' + \
1279 magenta('@dtvd88') + '.\n'
1280 usage
+= s
* 2 + light_green('mute @dtvd88') + ' will mute ' + \
1281 magenta('@dtvd88') + '.\n'
1282 usage
+= s
* 2 + light_green('unmute @dtvd88') + ' will unmute ' + \
1283 magenta('@dtvd88') + '.\n'
1284 usage
+= s
* 2 + light_green('muting') + ' will list muting users.\n'
1285 usage
+= s
* 2 + light_green('block @dtvd88') + ' will block ' + \
1286 magenta('@dtvd88') + '.\n'
1287 usage
+= s
* 2 + light_green('unblock @dtvd88') + ' will unblock ' + \
1288 magenta('@dtvd88') + '.\n'
1289 usage
+= s
* 2 + light_green('report @dtvd88') + ' will report ' + \
1290 magenta('@dtvd88') + ' as a spam account.\n'
1301 usage
+= s
+ grey(u
'\u266A' + ' Twitter list\n')
1302 usage
+= s
* 2 + light_green('list') + \
1303 ' will show all lists you are belong to.\n'
1304 usage
+= s
* 2 + light_green('list home') + \
1305 ' will show timeline of list. You will be asked for list\'s name.\n'
1306 usage
+= s
* 2 + light_green('list all_mem') + \
1307 ' will show list\'s all members.\n'
1308 usage
+= s
* 2 + light_green('list all_sub') + \
1309 ' will show list\'s all subscribers.\n'
1310 usage
+= s
* 2 + light_green('list add') + \
1311 ' will add specific person to a list owned by you.' + \
1312 ' You will be asked for list\'s name and person\'s name.\n'
1313 usage
+= s
* 2 + light_green('list rm') + \
1314 ' will remove specific person from 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 sub') + \
1317 ' will subscribe you to a specific list.\n'
1318 usage
+= s
* 2 + light_green('list unsub') + \
1319 ' will unsubscribe you from a specific list.\n'
1320 usage
+= s
* 2 + light_green('list own') + \
1321 ' will show all list owned by you.\n'
1322 usage
+= s
* 2 + light_green('list new') + \
1323 ' will create a new list.\n'
1324 usage
+= s
* 2 + light_green('list update') + \
1325 ' will update a list owned by you.\n'
1326 usage
+= s
* 2 + light_green('list del') + \
1327 ' will delete a list owned by you.\n'
1338 usage
+= s
+ grey(u
'\u266A' + ' Switching streams \n')
1339 usage
+= s
* 2 + light_green('switch public #AKB') + \
1340 ' will switch to public stream and follow "' + \
1341 light_yellow('AKB') + '" keyword.\n'
1342 usage
+= s
* 2 + light_green('switch mine') + \
1343 ' will switch to your personal stream.\n'
1344 usage
+= s
* 2 + light_green('switch mine -f ') + \
1345 ' will prompt to enter the filter.\n'
1346 usage
+= s
* 3 + light_yellow('Only nicks') + \
1347 ' filter will decide nicks will be INCLUDE ONLY.\n'
1348 usage
+= s
* 3 + light_yellow('Ignore nicks') + \
1349 ' filter will decide nicks will be EXCLUDE.\n'
1350 usage
+= s
* 2 + light_green('switch mine -d') + \
1351 ' will use the config\'s ONLY_LIST and IGNORE_LIST.\n'
1360 h
, w
= os
.popen('stty size', 'r').read().split()
1363 usage
+= s
+ 'Hi boss! I\'m ready to serve you right now!\n'
1364 usage
+= s
+ '-' * (int(w
) - 4) + '\n'
1365 usage
+= s
+ 'You are ' + \
1366 light_yellow('already') + ' on your personal stream.\n'
1367 usage
+= s
+ 'Any update from Twitter will show up ' + \
1368 light_yellow('immediately') + '.\n'
1369 usage
+= s
+ 'In addtion, following commands are available right now:\n'
1370 # Twitter help section
1372 usage
+= s
+ grey(u
'\u266A' + ' Twitter help\n')
1373 usage
+= s
* 2 + light_green('h discover') + \
1374 ' will show help for discover commands.\n'
1375 usage
+= s
* 2 + light_green('h tweets') + \
1376 ' will show help for tweets commands.\n'
1377 usage
+= s
* 2 + light_green('h messages') + \
1378 ' will show help for messages commands.\n'
1379 usage
+= s
* 2 + light_green('h friends_and_followers') + \
1380 ' will show help for friends and followers commands.\n'
1381 usage
+= s
* 2 + light_green('h list') + \
1382 ' will show help for list commands.\n'
1383 usage
+= s
* 2 + light_green('h stream') + \
1384 ' will show help for stream commands.\n'
1387 usage
+= s
+ grey(u
'\u266A' + ' Smart shell\n')
1388 usage
+= s
* 2 + light_green('111111 * 9 / 7') + ' or any math expression ' + \
1389 'will be evaluate by Python interpreter.\n'
1390 usage
+= s
* 2 + 'Even ' + light_green('cal') + ' will show the calendar' + \
1391 ' for current month.\n'
1394 usage
+= s
+ grey(u
'\u266A' + ' Config \n')
1395 usage
+= s
* 2 + light_green('theme') + ' will list available theme. ' + \
1396 light_green('theme monokai') + ' will apply ' + light_yellow('monokai') + \
1397 ' theme immediately.\n'
1398 usage
+= s
* 2 + light_green('config') + ' will list all config.\n'
1400 light_green('config ASCII_ART') + ' will output current value of ' +\
1401 light_yellow('ASCII_ART') + ' config key.\n'
1403 light_green('config TREND_MAX default') + ' will output default value of ' + \
1404 light_yellow('TREND_MAX') + ' config key.\n'
1406 light_green('config CUSTOM_CONFIG drop') + ' will drop ' + \
1407 light_yellow('CUSTOM_CONFIG') + ' config key.\n'
1409 light_green('config IMAGE_ON_TERM = true') + ' will set value of ' + \
1410 light_yellow('IMAGE_ON_TERM') + ' config key to ' + \
1411 light_yellow('True') + '.\n'
1414 usage
+= s
+ grey(u
'\u266A' + ' Screening \n')
1415 usage
+= s
* 2 + light_green('h') + ' will show this help again.\n'
1416 usage
+= s
* 2 + light_green('p') + ' will pause the stream.\n'
1417 usage
+= s
* 2 + light_green('r') + ' will unpause the stream.\n'
1418 usage
+= s
* 2 + light_green('c') + ' will clear the screen.\n'
1419 usage
+= s
* 2 + light_green('q') + ' will quit.\n'
1422 usage
+= s
+ '-' * (int(w
) - 4) + '\n'
1423 usage
+= s
+ 'Have fun and hang tight! \n'
1426 'discover': help_discover
,
1427 'tweets': help_tweets
,
1428 'messages': help_messages
,
1429 'friends_and_followers': help_friends_and_followers
,
1431 'stream': help_stream
,
1434 d
[g
['stuff'].strip()]()
1441 Pause stream display
1444 printNicely(green('Stream is paused'))
1452 printNicely(green('Stream is running back now'))
1468 printNicely(green('See you next time :)'))
1476 Reset prefix of line
1479 printNicely(magenta('Need tips ? Type "h" and hit Enter key!'))
1482 printNicely(str(eval(g
['cmd'])))
1539 Listen to user's input
1544 ['public', 'mine'], # switch
1559 ['image'], # show image
1561 ['fl', 'fr'], # list
1588 [key
for key
in dict(get_all_config())], # config
1589 g
['themes'], # theme
1594 'friends_and_followers',
1604 init_interactive_shell(d
)
1610 line
= raw_input(g
['decorated_name'](c
['PREFIX']))
1614 cmd
= line
.split()[0]
1619 # Lock the semaphore
1621 # Save cmd to global variable and call process
1622 g
['stuff'] = ' '.join(line
.split()[1:])
1623 # Process the command
1626 if cmd
in ['switch', 't', 'rt', 'rep']:
1630 # Release the semaphore lock
1633 printNicely(red('OMG something is wrong with Twitter right now.'))
1636 def stream(domain
, args
, name
='Rainbow Stream'):
1642 c
['USER_DOMAIN']: name
,
1643 c
['PUBLIC_DOMAIN']: args
.track_keywords
,
1644 c
['SITE_DOMAIN']: name
,
1647 ascii_art(art_dict
[domain
])
1648 # These arguments are optional:
1650 timeout
=args
.timeout
,
1652 heartbeat_timeout
=args
.heartbeat_timeout
)
1655 if args
.track_keywords
:
1656 query_args
['track'] = args
.track_keywords
1658 stream
= TwitterStream(
1663 if domain
== c
['USER_DOMAIN']:
1664 tweet_iter
= stream
.user(**query_args
)
1665 elif domain
== c
['SITE_DOMAIN']:
1666 tweet_iter
= stream
.site(**query_args
)
1668 if args
.track_keywords
:
1669 tweet_iter
= stream
.statuses
.filter(**query_args
)
1671 tweet_iter
= stream
.statuses
.sample()
1672 # Block new stream until other one exits
1673 StreamLock
.acquire()
1674 g
['stream_stop'] = False
1675 for tweet
in tweet_iter
:
1676 if(g
['stream_stop']):
1677 StreamLock
.release()
1681 elif tweet
is Timeout
:
1682 printNicely("-- Timeout --")
1683 elif tweet
is HeartbeatTimeout
:
1684 printNicely("-- Heartbeat Timeout --")
1685 elif tweet
is Hangup
:
1686 printNicely("-- Hangup --")
1687 elif tweet
.get('text'):
1690 keyword
=args
.track_keywords
,
1691 check_semaphore
=True,
1695 elif tweet
.get('direct_message'):
1696 print_message(tweet
['direct_message'], check_semaphore
=True)
1697 except TwitterHTTPError
:
1700 magenta("We have maximum connection problem with twitter'stream API right now :("))
1708 args
= parse_arguments()
1711 except TwitterHTTPError
:
1714 magenta("We have maximum connection problem with twitter'stream API right now :("))
1715 printNicely(magenta("Let's try again later."))
1718 # Spawn stream thread
1719 th
= threading
.Thread(target
=stream
, args
=(c
['USER_DOMAIN'], args
, g
['original_name']))
1722 # Start listen process