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(
209 elif target
== 'mine':
211 g
['stream_stop'] = True
213 th
= threading
.Thread(
223 printNicely(cyan('Only: ' + str(args
.filter)))
225 printNicely(red('Ignore: ' + str(args
.ignore
)))
228 printNicely(red('Sorry I can\'t understand.'))
235 t
= Twitter(auth
=authen())
236 # Get country and town
238 country
= g
['stuff'].split()[0]
242 town
= g
['stuff'].split()[1]
245 avail
= t
.trends
.available()
248 trends
= t
.trends
.place(_id
=1)[0]['trends']
251 for location
in avail
:
252 # Search for country and Town
254 if location
['countryCode'] == country \
255 and location
['placeType']['name'] == 'Town' \
256 and location
['name'] == town
:
257 trends
= t
.trends
.place(_id
=location
['woeid'])[0]['trends']
259 # Search for country only
261 if location
['countryCode'] == country \
262 and location
['placeType']['name'] == 'Country':
263 trends
= t
.trends
.place(_id
=location
['woeid'])[0]['trends']
271 t
= Twitter(auth
=authen())
272 num
= c
['HOME_TWEET_NUM']
273 if g
['stuff'].isdigit():
274 num
= int(g
['stuff'])
275 for tweet
in reversed(t
.statuses
.home_timeline(count
=num
)):
284 t
= Twitter(auth
=authen())
285 user
= g
['stuff'].split()[0]
288 num
= int(g
['stuff'].split()[1])
290 num
= c
['HOME_TWEET_NUM']
291 for tweet
in reversed(t
.statuses
.user_timeline(count
=num
, screen_name
=user
[1:])):
295 printNicely(red('A name should begin with a \'@\''))
302 t
= Twitter(auth
=authen())
303 num
= c
['HOME_TWEET_NUM']
304 if g
['stuff'].isdigit():
305 num
= int(g
['stuff'])
306 for tweet
in reversed(t
.statuses
.mentions_timeline(count
=num
)):
315 t
= Twitter(auth
=authen())
316 t
.statuses
.update(status
=g
['stuff'])
323 t
= Twitter(auth
=authen())
325 id = int(g
['stuff'].split()[0])
327 printNicely(red('Sorry I can\'t understand.'))
329 tid
= c
['tweet_dict'][id]
330 t
.statuses
.retweet(id=tid
, include_entities
=False, trim_user
=True)
337 t
= Twitter(auth
=authen())
339 id = int(g
['stuff'].split()[0])
341 printNicely(red('Sorry I can\'t understand.'))
343 tid
= c
['tweet_dict'][id]
344 tweet
= t
.statuses
.show(id=tid
)
345 screen_name
= tweet
['user']['screen_name']
347 quote
= '\"@' + screen_name
+ ': ' + text
+ '\"'
348 quote
= quote
.encode('utf8')
349 notice
= light_magenta('Compose mode ')
350 notice
+= light_yellow('(Enter nothing will cancel the quote)')
351 notice
+= light_magenta(':')
353 extra
= raw_input(quote
)
355 t
.statuses
.update(status
=quote
+ extra
)
357 printNicely(light_magenta('No text added.'))
364 t
= Twitter(auth
=authen())
367 id = int(g
['stuff'].split()[0])
369 printNicely(red('Sorry I can\'t understand.'))
371 tid
= c
['tweet_dict'][id]
372 # Get display num if exist
374 num
= int(g
['stuff'].split()[1])
376 num
= c
['RETWEETS_SHOW_NUM']
377 # Get result and display
378 rt_ary
= t
.statuses
.retweets(id=tid
, count
=num
)
380 printNicely(magenta('This tweet has no retweet.'))
382 for tweet
in reversed(rt_ary
):
391 t
= Twitter(auth
=authen())
393 id = int(g
['stuff'].split()[0])
395 printNicely(red('Sorry I can\'t understand.'))
397 tid
= c
['tweet_dict'][id]
398 t
.favorites
.create(_id
=tid
, include_entities
=False)
399 printNicely(green('Favorited.'))
400 draw(t
.statuses
.show(id=tid
))
408 t
= Twitter(auth
=authen())
410 id = int(g
['stuff'].split()[0])
412 printNicely(red('Sorry I can\'t understand.'))
414 tid
= c
['tweet_dict'][id]
415 user
= t
.statuses
.show(id=tid
)['user']['screen_name']
416 status
= ' '.join(g
['stuff'].split()[1:])
417 status
= '@' + user
+ ' ' + status
.decode('utf-8')
418 t
.statuses
.update(status
=status
, in_reply_to_status_id
=tid
)
425 t
= Twitter(auth
=authen())
427 id = int(g
['stuff'].split()[0])
429 printNicely(red('Sorry I can\'t understand.'))
431 tid
= c
['tweet_dict'][id]
432 t
.statuses
.destroy(id=tid
)
433 printNicely(green('Okay it\'s gone.'))
440 t
= Twitter(auth
=authen())
442 id = int(g
['stuff'].split()[0])
444 printNicely(red('Sorry I can\'t understand.'))
446 tid
= c
['tweet_dict'][id]
447 t
.favorites
.destroy(_id
=tid
)
448 printNicely(green('Okay it\'s unfavorited.'))
449 draw(t
.statuses
.show(id=tid
))
457 t
= Twitter(auth
=authen())
458 g
['stuff'] = g
['stuff'].strip()
459 rel
= t
.search
.tweets(q
=g
['stuff'])['statuses']
461 printNicely('Newest tweets:')
462 for i
in reversed(xrange(c
['SEARCH_MAX_RECORD'])):
467 printNicely(magenta('I\'m afraid there is no result'))
472 Send a direct message
474 t
= Twitter(auth
=authen())
475 user
= g
['stuff'].split()[0]
476 if user
[0].startswith('@'):
478 content
= g
['stuff'].split()[1]
480 printNicely(red('Sorry I can\'t understand.'))
481 t
.direct_messages
.new(
482 screen_name
=user
[1:],
485 printNicely(green('Message sent.'))
487 printNicely(red('A name should begin with a \'@\''))
494 t
= Twitter(auth
=authen())
496 target
= g
['stuff'].split()[0]
497 if target
!= 'image':
499 id = int(g
['stuff'].split()[1])
500 tid
= c
['tweet_dict'][id]
501 tweet
= t
.statuses
.show(id=tid
)
502 media
= tweet
['entities']['media']
504 res
= requests
.get(m
['media_url'])
505 img
= Image
.open(BytesIO(res
.content
))
508 printNicely(red('Sorry I can\'t show this image.'))
515 t
= Twitter(auth
=authen())
517 if not g
['stuff'].isdigit():
519 tid
= c
['tweet_dict'][int(g
['stuff'])]
520 tweet
= t
.statuses
.show(id=tid
)
522 u
for u
in tweet
['text'].split() if u
.startswith('http://')]
524 printNicely(light_magenta('No url here @.@!'))
526 for link
in link_ary
:
527 webbrowser
.open(link
)
529 printNicely(red('Sorry I can\'t open url in this tweet.'))
534 List friends for followers
536 t
= Twitter(auth
=authen())
539 name
= g
['stuff'].split()[1]
540 if name
.startswith('@'):
543 printNicely(red('A name should begin with a \'@\''))
544 raise Exception('Invalid name')
546 name
= g
['original_name']
547 # Get list followers or friends
549 target
= g
['stuff'].split()[0]
551 printNicely(red('Omg some syntax is wrong.'))
553 d
= {'fl': 'followers', 'fr': 'friends'}
557 while next_cursor
!= 0:
558 list = getattr(t
, d
[target
]).list(
562 include_entities
=False,
564 for u
in list['users']:
565 rel
[u
['name']] = '@' + u
['screen_name']
566 next_cursor
= list['next_cursor']
568 printNicely('All: ' + str(len(rel
)) + ' ' + d
[target
] + '.')
570 user
= ' ' + cycle_color(name
)
571 user
+= color_func(c
['TWEET']['nick'])(' ' + rel
[name
] + ' ')
577 Inbox direct messages
579 t
= Twitter(auth
=authen())
580 num
= c
['MESSAGES_DISPLAY']
582 if g
['stuff'].isdigit():
585 # Max message per page is 20 so we have to loop
587 rel
= rel
+ t
.direct_messages(
590 include_entities
=False,
595 rel
= rel
+ t
.direct_messages(
598 include_entities
=False,
602 printNicely('Inbox: newest ' + str(len(rel
)) + ' messages.')
603 for m
in reversed(rel
):
612 t
= Twitter(auth
=authen())
613 num
= c
['MESSAGES_DISPLAY']
615 if g
['stuff'].isdigit():
616 num
= int(g
['stuff'])
618 # Max message per page is 20 so we have to loop
620 rel
= rel
+ t
.direct_messages
.sent(
623 include_entities
=False,
628 rel
= rel
+ t
.direct_messages
.sent(
631 include_entities
=False,
635 printNicely('Sent: newest ' + str(len(rel
)) + ' messages.')
636 for m
in reversed(rel
):
645 t
= Twitter(auth
=authen())
647 id = int(g
['stuff'].split()[0])
649 printNicely(red('Sorry I can\'t understand.'))
650 mid
= c
['message_dict'][id]
651 t
.direct_messages
.destroy(id=mid
)
652 printNicely(green('Message deleted.'))
657 Show profile of a specific user
659 t
= Twitter(auth
=authen())
660 screen_name
= g
['stuff'].split()[0]
661 if screen_name
.startswith('@'):
664 screen_name
=screen_name
[1:],
665 include_entities
=False)
668 printNicely(red('Omg no user.'))
670 printNicely(red('A name should begin with a \'@\''))
677 t
= Twitter(auth
=authen())
678 screen_name
= g
['stuff'].split()[0]
679 if screen_name
.startswith('@'):
680 t
.friendships
.create(screen_name
=screen_name
[1:], follow
=True)
681 printNicely(green('You are following ' + screen_name
+ ' now!'))
683 printNicely(red('A name should begin with a \'@\''))
690 t
= Twitter(auth
=authen())
691 screen_name
= g
['stuff'].split()[0]
692 if screen_name
.startswith('@'):
693 t
.friendships
.destroy(
694 screen_name
=screen_name
[1:],
695 include_entities
=False)
696 printNicely(green('Unfollow ' + screen_name
+ ' success!'))
698 printNicely(red('A name should begin with a \'@\''))
705 t
= Twitter(auth
=authen())
707 screen_name
= g
['stuff'].split()[0]
709 printNicely(red('A name should be specified. '))
711 if screen_name
.startswith('@'):
712 rel
= t
.mutes
.users
.create(screen_name
=screen_name
[1:])
713 if isinstance(rel
, dict):
714 printNicely(green(screen_name
+ ' is muted.'))
716 printNicely(red(rel
))
718 printNicely(red('A name should begin with a \'@\''))
725 t
= Twitter(auth
=authen())
727 screen_name
= g
['stuff'].split()[0]
729 printNicely(red('A name should be specified. '))
731 if screen_name
.startswith('@'):
732 rel
= t
.mutes
.users
.destroy(screen_name
=screen_name
[1:])
733 if isinstance(rel
, dict):
734 printNicely(green(screen_name
+ ' is unmuted.'))
736 printNicely(red(rel
))
738 printNicely(red('A name should begin with a \'@\''))
745 t
= Twitter(auth
=authen())
750 while next_cursor
!= 0:
751 list = t
.mutes
.users
.list(
752 screen_name
=g
['original_name'],
755 include_entities
=False,
757 for u
in list['users']:
758 rel
[u
['name']] = '@' + u
['screen_name']
759 next_cursor
= list['next_cursor']
761 printNicely('All: ' + str(len(rel
)) + ' people.')
763 user
= ' ' + cycle_color(name
)
764 user
+= color_func(c
['TWEET']['nick'])(' ' + rel
[name
] + ' ')
772 t
= Twitter(auth
=authen())
773 screen_name
= g
['stuff'].split()[0]
774 if screen_name
.startswith('@'):
776 screen_name
=screen_name
[1:],
777 include_entities
=False,
779 printNicely(green('You blocked ' + screen_name
+ '.'))
781 printNicely(red('A name should begin with a \'@\''))
788 t
= Twitter(auth
=authen())
789 screen_name
= g
['stuff'].split()[0]
790 if screen_name
.startswith('@'):
792 screen_name
=screen_name
[1:],
793 include_entities
=False,
795 printNicely(green('Unblock ' + screen_name
+ ' success!'))
797 printNicely(red('A name should begin with a \'@\''))
802 Report a user as a spam account
804 t
= Twitter(auth
=authen())
805 screen_name
= g
['stuff'].split()[0]
806 if screen_name
.startswith('@'):
808 screen_name
=screen_name
[1:])
809 printNicely(green('You reported ' + screen_name
+ '.'))
811 printNicely(red('Sorry I can\'t understand.'))
819 list_name
= raw_input(light_magenta('Give me the list\'s name: '))
820 # Get list name and owner
822 owner
, slug
= list_name
.split('/')
823 if slug
.startswith('@'):
828 light_magenta('List name should follow "@owner/list_name" format.'))
829 raise Exception('Wrong list name')
836 rel
= t
.lists
.list(screen_name
=g
['original_name'])
840 printNicely(light_magenta('You belong to no lists :)'))
847 owner
, slug
= get_slug()
848 res
= t
.lists
.statuses(
850 owner_screen_name
=owner
,
852 include_entities
=False)
862 owner
, slug
= get_slug()
866 while next_cursor
!= 0:
869 owner_screen_name
=owner
,
871 include_entities
=False)
873 rel
[u
['name']] = '@' + u
['screen_name']
874 next_cursor
= m
['next_cursor']
875 printNicely('All: ' + str(len(rel
)) + ' members.')
877 user
= ' ' + cycle_color(name
)
878 user
+= color_func(c
['TWEET']['nick'])(' ' + rel
[name
] + ' ')
882 def list_subscribers(t
):
886 owner
, slug
= get_slug()
890 while next_cursor
!= 0:
891 m
= t
.lists
.subscribers(
893 owner_screen_name
=owner
,
895 include_entities
=False)
897 rel
[u
['name']] = '@' + u
['screen_name']
898 next_cursor
= m
['next_cursor']
899 printNicely('All: ' + str(len(rel
)) + ' subscribers.')
901 user
= ' ' + cycle_color(name
)
902 user
+= color_func(c
['TWEET']['nick'])(' ' + rel
[name
] + ' ')
908 Add specific user to a list
910 owner
, slug
= get_slug()
912 user_name
= raw_input(light_magenta('Give me name of the newbie: '))
913 if user_name
.startswith('@'):
914 user_name
= user_name
[1:]
916 t
.lists
.members
.create(
918 owner_screen_name
=owner
,
919 screen_name
=user_name
)
920 printNicely(green('Added.'))
922 printNicely(light_magenta('I\'m sorry we can not add him/her.'))
927 Remove specific user from a list
929 owner
, slug
= get_slug()
931 user_name
= raw_input(light_magenta('Give me name of the unlucky one: '))
932 if user_name
.startswith('@'):
933 user_name
= user_name
[1:]
935 t
.lists
.members
.destroy(
937 owner_screen_name
=owner
,
938 screen_name
=user_name
)
939 printNicely(green('Gone.'))
941 printNicely(light_magenta('I\'m sorry we can not remove him/her.'))
944 def list_subscribe(t
):
948 owner
, slug
= get_slug()
951 t
.lists
.subscribers
.create(
953 owner_screen_name
=owner
)
954 printNicely(green('Done.'))
957 light_magenta('I\'m sorry you can not subscribe to this list.'))
960 def list_unsubscribe(t
):
964 owner
, slug
= get_slug()
967 t
.lists
.subscribers
.destroy(
969 owner_screen_name
=owner
)
970 printNicely(green('Done.'))
973 light_magenta('I\'m sorry you can not unsubscribe to this list.'))
982 while next_cursor
!= 0:
983 res
= t
.lists
.ownerships(
984 screen_name
=g
['original_name'],
987 next_cursor
= res
['next_cursor']
991 printNicely(light_magenta('You own no lists :)'))
998 name
= raw_input(light_magenta('New list\'s name: '))
999 mode
= raw_input(light_magenta('New list\'s mode (public/private): '))
1000 description
= raw_input(light_magenta('New list\'s description: '))
1005 description
=description
)
1006 printNicely(green(name
+ ' list is created.'))
1008 printNicely(red('Oops something is wrong with Twitter :('))
1015 slug
= raw_input(light_magenta('Your list that you want to update: '))
1016 name
= raw_input(light_magenta('Update name (leave blank to unchange): '))
1017 mode
= raw_input(light_magenta('Update mode (public/private): '))
1018 description
= raw_input(light_magenta('Update description: '))
1022 slug
='-'.join(slug
.split()),
1023 owner_screen_name
=g
['original_name'],
1026 description
=description
)
1030 owner_screen_name
=g
['original_name'],
1032 description
=description
)
1033 printNicely(green(slug
+ ' list is updated.'))
1035 printNicely(red('Oops something is wrong with Twitter :('))
1042 slug
= raw_input(light_magenta('Your list that you want to update: '))
1045 slug
='-'.join(slug
.split()),
1046 owner_screen_name
=g
['original_name'])
1047 printNicely(green(slug
+ ' list is deleted.'))
1049 printNicely(red('Oops something is wrong with Twitter :('))
1056 t
= Twitter(auth
=authen())
1057 # List all lists or base on action
1059 g
['list_action'] = g
['stuff'].split()[0]
1066 'all_mem': list_members
,
1067 'all_sub': list_subscribers
,
1070 'sub': list_subscribe
,
1071 'unsub': list_unsubscribe
,
1074 'update': list_update
,
1078 return action_ary
[g
['list_action']](t
)
1080 printNicely(red('Please try again.'))
1085 Unix's command `cal`
1088 rel
= os
.popen('cal').read().split('\n')
1091 show_calendar(month
, date
, rel
)
1096 Browse and change config
1098 all_config
= get_all_config()
1099 g
['stuff'] = g
['stuff'].strip()
1102 for k
in all_config
:
1104 green(k
) + ': ' + light_yellow(str(all_config
[k
]))
1106 guide
= 'Detailed explanation can be found at ' + \
1107 color_func(c
['TWEET']['link'])(
1108 'http://rainbowstream.readthedocs.org/en/latest/#config-explanation')
1110 # Print specific config
1111 elif len(g
['stuff'].split()) == 1:
1112 if g
['stuff'] in all_config
:
1115 green(k
) + ': ' + light_yellow(str(all_config
[k
]))
1118 printNicely(red('No such config key.'))
1119 # Print specific config's default value
1120 elif len(g
['stuff'].split()) == 2 and g
['stuff'].split()[-1] == 'default':
1121 key
= g
['stuff'].split()[0]
1123 value
= get_default_config(key
)
1124 line
= ' ' * 2 + green(key
) + ': ' + light_magenta(value
)
1128 light_magenta('This config key does not exist in default.'))
1129 # Delete specific config key in config file
1130 elif len(g
['stuff'].split()) == 2 and g
['stuff'].split()[-1] == 'drop':
1131 key
= g
['stuff'].split()[0]
1134 printNicely(green('Config key is dropped.'))
1136 printNicely(red('No such config key.'))
1137 # Set specific config
1138 elif len(g
['stuff'].split()) == 3 and g
['stuff'].split()[1] == '=':
1139 key
= g
['stuff'].split()[0]
1140 value
= g
['stuff'].split()[-1]
1141 if key
== 'THEME' and not validate_theme(value
):
1142 printNicely(red('Invalid theme\'s value.'))
1145 set_config(key
, value
)
1146 # Apply theme immediately
1148 c
['THEME'] = reload_theme(value
, c
['THEME'])
1149 g
['decorated_name'] = lambda x
: color_func(
1150 c
['DECORATED_NAME'])(
1152 printNicely(green('Updated successfully.'))
1154 printNicely(light_magenta('Not valid value.'))
1158 printNicely(light_magenta('Sorry I can\'s understand.'))
1163 List and change theme
1167 for theme
in g
['themes']:
1168 line
= light_magenta(theme
)
1169 if c
['THEME'] == theme
:
1170 line
= ' ' * 2 + light_yellow('* ') + line
1172 line
= ' ' * 4 + line
1178 c
['THEME'] = reload_theme(g
['stuff'], c
['THEME'])
1179 # Redefine decorated_name
1180 g
['decorated_name'] = lambda x
: color_func(
1181 c
['DECORATED_NAME'])(
1183 printNicely(green('Theme changed.'))
1185 printNicely(red('No such theme exists.'))
1188 def help_discover():
1193 # Discover the world
1195 usage
+= s
+ grey(u
'\u266A' + ' Discover the world \n')
1196 usage
+= s
* 2 + light_green('trend') + ' will show global trending topics. ' + \
1197 'You can try ' + light_green('trend US') + ' or ' + \
1198 light_green('trend JP Tokyo') + '.\n'
1199 usage
+= s
* 2 + light_green('home') + ' will show your timeline. ' + \
1200 light_green('home 7') + ' will show 7 tweets.\n'
1201 usage
+= s
* 2 + light_green('mentions') + ' will show mentions timeline. ' + \
1202 light_green('mentions 7') + ' will show 7 mention tweets.\n'
1203 usage
+= s
* 2 + light_green('whois @mdo') + ' will show profile of ' + \
1204 magenta('@mdo') + '.\n'
1205 usage
+= s
* 2 + light_green('view @mdo') + \
1206 ' will show ' + magenta('@mdo') + '\'s home.\n'
1207 usage
+= s
* 2 + light_green('s AKB48') + ' will search for "' + \
1208 light_yellow('AKB48') + '" and return 5 newest tweet. ' + \
1209 'Search can be performed with or without hashtag.\n'
1220 usage
+= s
+ grey(u
'\u266A' + ' Tweets \n')
1221 usage
+= s
* 2 + light_green('t oops ') + \
1222 'will tweet "' + light_yellow('oops') + '" immediately.\n'
1224 light_green('rt 12 ') + ' will retweet to tweet with ' + \
1225 light_yellow('[id=12]') + '.\n'
1227 light_green('quote 12 ') + ' will quote the tweet with ' + \
1228 light_yellow('[id=12]') + '. If no extra text is added, ' + \
1229 'the quote will be canceled.\n'
1231 light_green('allrt 12 20 ') + ' will list 20 newest retweet of the tweet with ' + \
1232 light_yellow('[id=12]') + '.\n'
1233 usage
+= s
* 2 + light_green('rep 12 oops') + ' will reply "' + \
1234 light_yellow('oops') + '" to tweet with ' + \
1235 light_yellow('[id=12]') + '.\n'
1237 light_green('fav 12 ') + ' will favorite the tweet with ' + \
1238 light_yellow('[id=12]') + '.\n'
1240 light_green('ufav 12 ') + ' will unfavorite tweet with ' + \
1241 light_yellow('[id=12]') + '.\n'
1243 light_green('del 12 ') + ' will delete tweet with ' + \
1244 light_yellow('[id=12]') + '.\n'
1245 usage
+= s
* 2 + light_green('show image 12') + ' will show image in tweet with ' + \
1246 light_yellow('[id=12]') + ' in your OS\'s image viewer.\n'
1247 usage
+= s
* 2 + light_green('open 12') + ' will open url in tweet with ' + \
1248 light_yellow('[id=12]') + ' in your OS\'s default browser.\n'
1252 def help_messages():
1259 usage
+= s
+ grey(u
'\u266A' + ' Direct messages \n')
1260 usage
+= s
* 2 + light_green('inbox') + ' will show inbox messages. ' + \
1261 light_green('inbox 7') + ' will show newest 7 messages.\n'
1262 usage
+= s
* 2 + light_green('sent') + ' will show sent messages. ' + \
1263 light_green('sent 7') + ' will show newest 7 messages.\n'
1264 usage
+= s
* 2 + light_green('mes @dtvd88 hi') + ' will send a "hi" messege to ' + \
1265 magenta('@dtvd88') + '.\n'
1266 usage
+= s
* 2 + light_green('trash 5') + ' will remove message with ' + \
1267 light_yellow('[message_id=5]') + '.\n'
1271 def help_friends_and_followers():
1273 Friends and Followers
1276 # Follower and following
1278 usage
+= s
+ grey(u
'\u266A' + ' Friends and followers \n')
1280 light_green('ls fl') + \
1281 ' will list all followers (people who are following you).\n'
1283 light_green('ls fr') + \
1284 ' will list all friends (people who you are following).\n'
1285 usage
+= s
* 2 + light_green('fl @dtvd88') + ' will follow ' + \
1286 magenta('@dtvd88') + '.\n'
1287 usage
+= s
* 2 + light_green('ufl @dtvd88') + ' will unfollow ' + \
1288 magenta('@dtvd88') + '.\n'
1289 usage
+= s
* 2 + light_green('mute @dtvd88') + ' will mute ' + \
1290 magenta('@dtvd88') + '.\n'
1291 usage
+= s
* 2 + light_green('unmute @dtvd88') + ' will unmute ' + \
1292 magenta('@dtvd88') + '.\n'
1293 usage
+= s
* 2 + light_green('muting') + ' will list muting users.\n'
1294 usage
+= s
* 2 + light_green('block @dtvd88') + ' will block ' + \
1295 magenta('@dtvd88') + '.\n'
1296 usage
+= s
* 2 + light_green('unblock @dtvd88') + ' will unblock ' + \
1297 magenta('@dtvd88') + '.\n'
1298 usage
+= s
* 2 + light_green('report @dtvd88') + ' will report ' + \
1299 magenta('@dtvd88') + ' as a spam account.\n'
1310 usage
+= s
+ grey(u
'\u266A' + ' Twitter list\n')
1311 usage
+= s
* 2 + light_green('list') + \
1312 ' will show all lists you are belong to.\n'
1313 usage
+= s
* 2 + light_green('list home') + \
1314 ' will show timeline of list. You will be asked for list\'s name.\n'
1315 usage
+= s
* 2 + light_green('list all_mem') + \
1316 ' will show list\'s all members.\n'
1317 usage
+= s
* 2 + light_green('list all_sub') + \
1318 ' will show list\'s all subscribers.\n'
1319 usage
+= s
* 2 + light_green('list add') + \
1320 ' will add specific person to a list owned by you.' + \
1321 ' You will be asked for list\'s name and person\'s name.\n'
1322 usage
+= s
* 2 + light_green('list rm') + \
1323 ' will remove specific person from a list owned by you.' + \
1324 ' You will be asked for list\'s name and person\'s name.\n'
1325 usage
+= s
* 2 + light_green('list sub') + \
1326 ' will subscribe you to a specific list.\n'
1327 usage
+= s
* 2 + light_green('list unsub') + \
1328 ' will unsubscribe you from a specific list.\n'
1329 usage
+= s
* 2 + light_green('list own') + \
1330 ' will show all list owned by you.\n'
1331 usage
+= s
* 2 + light_green('list new') + \
1332 ' will create a new list.\n'
1333 usage
+= s
* 2 + light_green('list update') + \
1334 ' will update a list owned by you.\n'
1335 usage
+= s
* 2 + light_green('list del') + \
1336 ' will delete a list owned by you.\n'
1347 usage
+= s
+ grey(u
'\u266A' + ' Switching streams \n')
1348 usage
+= s
* 2 + light_green('switch public #AKB') + \
1349 ' will switch to public stream and follow "' + \
1350 light_yellow('AKB') + '" keyword.\n'
1351 usage
+= s
* 2 + light_green('switch mine') + \
1352 ' will switch to your personal stream.\n'
1353 usage
+= s
* 2 + light_green('switch mine -f ') + \
1354 ' will prompt to enter the filter.\n'
1355 usage
+= s
* 3 + light_yellow('Only nicks') + \
1356 ' filter will decide nicks will be INCLUDE ONLY.\n'
1357 usage
+= s
* 3 + light_yellow('Ignore nicks') + \
1358 ' filter will decide nicks will be EXCLUDE.\n'
1359 usage
+= s
* 2 + light_green('switch mine -d') + \
1360 ' will use the config\'s ONLY_LIST and IGNORE_LIST.\n'
1369 h
, w
= os
.popen('stty size', 'r').read().split()
1372 usage
+= s
+ 'Hi boss! I\'m ready to serve you right now!\n'
1373 usage
+= s
+ '-' * (int(w
) - 4) + '\n'
1374 usage
+= s
+ 'You are ' + \
1375 light_yellow('already') + ' on your personal stream.\n'
1376 usage
+= s
+ 'Any update from Twitter will show up ' + \
1377 light_yellow('immediately') + '.\n'
1378 usage
+= s
+ 'In addtion, following commands are available right now:\n'
1379 # Twitter help section
1381 usage
+= s
+ grey(u
'\u266A' + ' Twitter help\n')
1382 usage
+= s
* 2 + light_green('h discover') + \
1383 ' will show help for discover commands.\n'
1384 usage
+= s
* 2 + light_green('h tweets') + \
1385 ' will show help for tweets commands.\n'
1386 usage
+= s
* 2 + light_green('h messages') + \
1387 ' will show help for messages commands.\n'
1388 usage
+= s
* 2 + light_green('h friends_and_followers') + \
1389 ' will show help for friends and followers commands.\n'
1390 usage
+= s
* 2 + light_green('h list') + \
1391 ' will show help for list commands.\n'
1392 usage
+= s
* 2 + light_green('h stream') + \
1393 ' will show help for stream commands.\n'
1396 usage
+= s
+ grey(u
'\u266A' + ' Smart shell\n')
1397 usage
+= s
* 2 + light_green('111111 * 9 / 7') + ' or any math expression ' + \
1398 'will be evaluate by Python interpreter.\n'
1399 usage
+= s
* 2 + 'Even ' + light_green('cal') + ' will show the calendar' + \
1400 ' for current month.\n'
1403 usage
+= s
+ grey(u
'\u266A' + ' Config \n')
1404 usage
+= s
* 2 + light_green('theme') + ' will list available theme. ' + \
1405 light_green('theme monokai') + ' will apply ' + light_yellow('monokai') + \
1406 ' theme immediately.\n'
1407 usage
+= s
* 2 + light_green('config') + ' will list all config.\n'
1409 light_green('config ASCII_ART') + ' will output current value of ' +\
1410 light_yellow('ASCII_ART') + ' config key.\n'
1412 light_green('config TREND_MAX default') + ' will output default value of ' + \
1413 light_yellow('TREND_MAX') + ' config key.\n'
1415 light_green('config CUSTOM_CONFIG drop') + ' will drop ' + \
1416 light_yellow('CUSTOM_CONFIG') + ' config key.\n'
1418 light_green('config IMAGE_ON_TERM = true') + ' will set value of ' + \
1419 light_yellow('IMAGE_ON_TERM') + ' config key to ' + \
1420 light_yellow('True') + '.\n'
1423 usage
+= s
+ grey(u
'\u266A' + ' Screening \n')
1424 usage
+= s
* 2 + light_green('h') + ' will show this help again.\n'
1425 usage
+= s
* 2 + light_green('p') + ' will pause the stream.\n'
1426 usage
+= s
* 2 + light_green('r') + ' will unpause the stream.\n'
1427 usage
+= s
* 2 + light_green('c') + ' will clear the screen.\n'
1428 usage
+= s
* 2 + light_green('q') + ' will quit.\n'
1431 usage
+= s
+ '-' * (int(w
) - 4) + '\n'
1432 usage
+= s
+ 'Have fun and hang tight! \n'
1435 'discover': help_discover
,
1436 'tweets': help_tweets
,
1437 'messages': help_messages
,
1438 'friends_and_followers': help_friends_and_followers
,
1440 'stream': help_stream
,
1445 lambda: printNicely(red('No such command.'))
1453 Pause stream display
1456 printNicely(green('Stream is paused'))
1464 printNicely(green('Stream is running back now'))
1480 printNicely(green('See you next time :)'))
1488 Reset prefix of line
1491 printNicely(magenta('Need tips ? Type "h" and hit Enter key!'))
1494 printNicely(str(eval(g
['cmd'])))
1551 Listen to user's input
1556 ['public', 'mine'], # switch
1571 ['image'], # show image
1573 ['fl', 'fr'], # list
1600 [key
for key
in dict(get_all_config())], # config
1601 g
['themes'], # theme
1606 'friends_and_followers',
1616 init_interactive_shell(d
)
1622 line
= raw_input(g
['decorated_name'](c
['PREFIX']))
1626 cmd
= line
.split()[0]
1631 # Lock the semaphore
1633 # Save cmd to global variable and call process
1634 g
['stuff'] = ' '.join(line
.split()[1:])
1635 # Process the command
1638 if cmd
in ['switch', 't', 'rt', 'rep']:
1642 # Release the semaphore lock
1645 printNicely(red('OMG something is wrong with Twitter right now.'))
1648 def stream(domain
, args
, name
='Rainbow Stream'):
1654 c
['USER_DOMAIN']: name
,
1655 c
['PUBLIC_DOMAIN']: args
.track_keywords
,
1656 c
['SITE_DOMAIN']: name
,
1659 ascii_art(art_dict
[domain
])
1660 # These arguments are optional:
1662 timeout
=args
.timeout
,
1664 heartbeat_timeout
=args
.heartbeat_timeout
)
1667 if args
.track_keywords
:
1668 query_args
['track'] = args
.track_keywords
1670 stream
= TwitterStream(
1675 if domain
== c
['USER_DOMAIN']:
1676 tweet_iter
= stream
.user(**query_args
)
1677 elif domain
== c
['SITE_DOMAIN']:
1678 tweet_iter
= stream
.site(**query_args
)
1680 if args
.track_keywords
:
1681 tweet_iter
= stream
.statuses
.filter(**query_args
)
1683 tweet_iter
= stream
.statuses
.sample()
1684 # Block new stream until other one exits
1685 StreamLock
.acquire()
1686 g
['stream_stop'] = False
1687 for tweet
in tweet_iter
:
1688 if(g
['stream_stop']):
1689 StreamLock
.release()
1693 elif tweet
is Timeout
:
1694 printNicely("-- Timeout --")
1695 elif tweet
is HeartbeatTimeout
:
1696 printNicely("-- Heartbeat Timeout --")
1697 elif tweet
is Hangup
:
1698 printNicely("-- Hangup --")
1699 elif tweet
.get('text'):
1702 keyword
=args
.track_keywords
,
1703 check_semaphore
=True,
1707 elif tweet
.get('direct_message'):
1708 print_message(tweet
['direct_message'], check_semaphore
=True)
1709 except TwitterHTTPError
:
1712 magenta("We have maximum connection problem with twitter'stream API right now :("))
1720 args
= parse_arguments()
1723 except TwitterHTTPError
:
1726 magenta("We have maximum connection problem with twitter'stream API right now :("))
1727 printNicely(magenta("Let's try again later."))
1730 # Spawn stream thread
1731 th
= threading
.Thread(
1736 g
['original_name']))
1739 # Start listen process