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']
160 g
['prev_theme'] = c
['THEME']
164 # Init tweet dict and message dict
166 c
['message_dict'] = []
168 c
['IMAGE_ON_TERM'] = args
.image_on_term
176 target
= g
['stuff'].split()[0]
178 args
= parse_arguments()
180 if g
['stuff'].split()[-1] == '-f':
181 guide
= 'To ignore an option, just hit Enter key.'
182 printNicely(light_magenta(guide
))
183 only
= raw_input('Only nicks [Ex: @xxx,@yy]: ')
184 ignore
= raw_input('Ignore nicks [Ex: @xxx,@yy]: ')
185 args
.filter = filter(None, only
.split(','))
186 args
.ignore
= filter(None, ignore
.split(','))
187 elif g
['stuff'].split()[-1] == '-d':
188 args
.filter = c
['ONLY_LIST']
189 args
.ignore
= c
['IGNORE_LIST']
191 printNicely(red('Sorry, wrong format.'))
194 if target
== 'public':
195 keyword
= g
['stuff'].split()[1]
196 if keyword
[0] == '#':
197 keyword
= keyword
[1:]
199 g
['stream_stop'] = True
200 args
.track_keywords
= keyword
202 th
= threading
.Thread(target
=stream
, args
=(c
['PUBLIC_DOMAIN'], args
))
206 elif target
== 'mine':
208 g
['stream_stop'] = True
210 th
= threading
.Thread(target
=stream
, args
=(c
['USER_DOMAIN'], args
, g
['original_name']))
215 printNicely(cyan('Only: ' + str(args
.filter)))
217 printNicely(red('Ignore: ' + str(args
.ignore
)))
220 printNicely(red('Sorry I can\'t understand.'))
227 t
= Twitter(auth
=authen())
228 # Get country and town
230 country
= g
['stuff'].split()[0]
234 town
= g
['stuff'].split()[1]
237 avail
= t
.trends
.available()
240 trends
= t
.trends
.place(_id
=1)[0]['trends']
243 for location
in avail
:
244 # Search for country and Town
246 if location
['countryCode'] == country \
247 and location
['placeType']['name'] == 'Town' \
248 and location
['name'] == town
:
249 trends
= t
.trends
.place(_id
=location
['woeid'])[0]['trends']
251 # Search for country only
253 if location
['countryCode'] == country \
254 and location
['placeType']['name'] == 'Country':
255 trends
= t
.trends
.place(_id
=location
['woeid'])[0]['trends']
263 t
= Twitter(auth
=authen())
264 num
= c
['HOME_TWEET_NUM']
265 if g
['stuff'].isdigit():
266 num
= int(g
['stuff'])
267 for tweet
in reversed(t
.statuses
.home_timeline(count
=num
)):
276 t
= Twitter(auth
=authen())
277 user
= g
['stuff'].split()[0]
280 num
= int(g
['stuff'].split()[1])
282 num
= c
['HOME_TWEET_NUM']
283 for tweet
in reversed(t
.statuses
.user_timeline(count
=num
, screen_name
=user
[1:])):
287 printNicely(red('A name should begin with a \'@\''))
294 t
= Twitter(auth
=authen())
295 num
= c
['HOME_TWEET_NUM']
296 if g
['stuff'].isdigit():
297 num
= int(g
['stuff'])
298 for tweet
in reversed(t
.statuses
.mentions_timeline(count
=num
)):
307 t
= Twitter(auth
=authen())
308 t
.statuses
.update(status
=g
['stuff'])
315 t
= Twitter(auth
=authen())
317 id = int(g
['stuff'].split()[0])
319 printNicely(red('Sorry I can\'t understand.'))
321 tid
= c
['tweet_dict'][id]
322 t
.statuses
.retweet(id=tid
, include_entities
=False, trim_user
=True)
329 t
= Twitter(auth
=authen())
331 id = int(g
['stuff'].split()[0])
333 printNicely(red('Sorry I can\'t understand.'))
335 tid
= c
['tweet_dict'][id]
336 tweet
= t
.statuses
.show(id=tid
)
337 screen_name
= tweet
['user']['screen_name']
339 quote
= '\"@' + screen_name
+ ': ' + text
+ '\"'
340 quote
= quote
.encode('utf8')
341 notice
= light_magenta('Compose mode ')
342 notice
+= light_yellow('(Enter nothing will cancel the quote)')
343 notice
+= light_magenta(':')
345 extra
= raw_input(quote
)
347 t
.statuses
.update(status
=quote
+ extra
)
349 printNicely(light_magenta('No text added.'))
356 t
= Twitter(auth
=authen())
359 id = int(g
['stuff'].split()[0])
361 printNicely(red('Sorry I can\'t understand.'))
363 tid
= c
['tweet_dict'][id]
364 # Get display num if exist
366 num
= int(g
['stuff'].split()[1])
368 num
= c
['RETWEETS_SHOW_NUM']
369 # Get result and display
370 rt_ary
= t
.statuses
.retweets(id=tid
, count
=num
)
372 printNicely(magenta('This tweet has no retweet.'))
374 for tweet
in reversed(rt_ary
):
383 t
= Twitter(auth
=authen())
385 id = int(g
['stuff'].split()[0])
387 printNicely(red('Sorry I can\'t understand.'))
389 tid
= c
['tweet_dict'][id]
390 t
.favorites
.create(_id
=tid
, include_entities
=False)
391 printNicely(green('Favorited.'))
392 draw(t
.statuses
.show(id=tid
))
400 t
= Twitter(auth
=authen())
402 id = int(g
['stuff'].split()[0])
404 printNicely(red('Sorry I can\'t understand.'))
406 tid
= c
['tweet_dict'][id]
407 user
= t
.statuses
.show(id=tid
)['user']['screen_name']
408 status
= ' '.join(g
['stuff'].split()[1:])
409 status
= '@' + user
+ ' ' + status
.decode('utf-8')
410 t
.statuses
.update(status
=status
, in_reply_to_status_id
=tid
)
417 t
= Twitter(auth
=authen())
419 id = int(g
['stuff'].split()[0])
421 printNicely(red('Sorry I can\'t understand.'))
423 tid
= c
['tweet_dict'][id]
424 t
.statuses
.destroy(id=tid
)
425 printNicely(green('Okay it\'s gone.'))
432 t
= Twitter(auth
=authen())
434 id = int(g
['stuff'].split()[0])
436 printNicely(red('Sorry I can\'t understand.'))
438 tid
= c
['tweet_dict'][id]
439 t
.favorites
.destroy(_id
=tid
)
440 printNicely(green('Okay it\'s unfavorited.'))
441 draw(t
.statuses
.show(id=tid
))
449 t
= Twitter(auth
=authen())
450 g
['stuff'] = g
['stuff'].strip()
451 rel
= t
.search
.tweets(q
=g
['stuff'])['statuses']
453 printNicely('Newest tweets:')
454 for i
in reversed(xrange(c
['SEARCH_MAX_RECORD'])):
459 printNicely(magenta('I\'m afraid there is no result'))
464 Send a direct message
466 t
= Twitter(auth
=authen())
467 user
= g
['stuff'].split()[0]
468 if user
[0].startswith('@'):
470 content
= g
['stuff'].split()[1]
472 printNicely(red('Sorry I can\'t understand.'))
473 t
.direct_messages
.new(
474 screen_name
=user
[1:],
477 printNicely(green('Message sent.'))
479 printNicely(red('A name should begin with a \'@\''))
486 t
= Twitter(auth
=authen())
488 target
= g
['stuff'].split()[0]
489 if target
!= 'image':
491 id = int(g
['stuff'].split()[1])
492 tid
= c
['tweet_dict'][id]
493 tweet
= t
.statuses
.show(id=tid
)
494 media
= tweet
['entities']['media']
496 res
= requests
.get(m
['media_url'])
497 img
= Image
.open(BytesIO(res
.content
))
500 printNicely(red('Sorry I can\'t show this image.'))
507 t
= Twitter(auth
=authen())
509 if not g
['stuff'].isdigit():
511 tid
= c
['tweet_dict'][g
['stuff']]
512 tweet
= t
.statuses
.show(id=tid
)
514 u
for u
in tweet
['text'].split() if u
.startswith('http://')]
516 printNicely(light_magenta('No url here @.@!'))
518 for link
in link_ary
:
519 webbrowser
.open(link
)
521 printNicely(red('Sorry I can\'t open url in this tweet.'))
526 List friends for followers
528 t
= Twitter(auth
=authen())
531 name
= g
['stuff'].split()[1]
532 if name
.startswith('@'):
535 printNicely(red('A name should begin with a \'@\''))
536 raise Exception('Invalid name')
538 name
= g
['original_name']
539 # Get list followers or friends
541 target
= g
['stuff'].split()[0]
543 printNicely(red('Omg some syntax is wrong.'))
545 d
= {'fl': 'followers', 'fr': 'friends'}
549 while next_cursor
!= 0:
550 list = getattr(t
, d
[target
]).list(
554 include_entities
=False,
556 for u
in list['users']:
557 rel
[u
['name']] = '@' + u
['screen_name']
558 next_cursor
= list['next_cursor']
560 printNicely('All: ' + str(len(rel
)) + ' ' + d
[target
] + '.')
562 user
= ' ' + cycle_color(name
)
563 user
+= color_func(c
['TWEET']['nick'])(' ' + rel
[name
] + ' ')
569 Inbox direct messages
571 t
= Twitter(auth
=authen())
572 num
= c
['MESSAGES_DISPLAY']
574 if g
['stuff'].isdigit():
577 # Max message per page is 20 so we have to loop
579 rel
= rel
+ t
.direct_messages(
582 include_entities
=False,
587 rel
= rel
+ t
.direct_messages(
590 include_entities
=False,
594 printNicely('Inbox: newest ' + str(len(rel
)) + ' messages.')
595 for m
in reversed(rel
):
604 t
= Twitter(auth
=authen())
605 num
= c
['MESSAGES_DISPLAY']
607 if g
['stuff'].isdigit():
608 num
= int(g
['stuff'])
610 # Max message per page is 20 so we have to loop
612 rel
= rel
+ t
.direct_messages
.sent(
615 include_entities
=False,
620 rel
= rel
+ t
.direct_messages
.sent(
623 include_entities
=False,
627 printNicely('Sent: newest ' + str(len(rel
)) + ' messages.')
628 for m
in reversed(rel
):
637 t
= Twitter(auth
=authen())
639 id = int(g
['stuff'].split()[0])
641 printNicely(red('Sorry I can\'t understand.'))
642 mid
= c
['message_dict'][id]
643 t
.direct_messages
.destroy(id=mid
)
644 printNicely(green('Message deleted.'))
649 Show profile of a specific user
651 t
= Twitter(auth
=authen())
652 screen_name
= g
['stuff'].split()[0]
653 if screen_name
.startswith('@'):
656 screen_name
=screen_name
[1:],
657 include_entities
=False)
660 printNicely(red('Omg no user.'))
662 printNicely(red('A name should begin with a \'@\''))
669 t
= Twitter(auth
=authen())
670 screen_name
= g
['stuff'].split()[0]
671 if screen_name
.startswith('@'):
672 t
.friendships
.create(screen_name
=screen_name
[1:], follow
=True)
673 printNicely(green('You are following ' + screen_name
+ ' now!'))
675 printNicely(red('A name should begin with a \'@\''))
682 t
= Twitter(auth
=authen())
683 screen_name
= g
['stuff'].split()[0]
684 if screen_name
.startswith('@'):
685 t
.friendships
.destroy(
686 screen_name
=screen_name
[1:],
687 include_entities
=False)
688 printNicely(green('Unfollow ' + screen_name
+ ' success!'))
690 printNicely(red('A name should begin with a \'@\''))
697 t
= Twitter(auth
=authen())
699 screen_name
= g
['stuff'].split()[0]
701 printNicely(red('A name should be specified. '))
703 if screen_name
.startswith('@'):
704 rel
= t
.mutes
.users
.create(screen_name
=screen_name
[1:])
705 if isinstance(rel
, dict):
706 printNicely(green(screen_name
+ ' is muted.'))
708 printNicely(red(rel
))
710 printNicely(red('A name should begin with a \'@\''))
717 t
= Twitter(auth
=authen())
719 screen_name
= g
['stuff'].split()[0]
721 printNicely(red('A name should be specified. '))
723 if screen_name
.startswith('@'):
724 rel
= t
.mutes
.users
.destroy(screen_name
=screen_name
[1:])
725 if isinstance(rel
, dict):
726 printNicely(green(screen_name
+ ' is unmuted.'))
728 printNicely(red(rel
))
730 printNicely(red('A name should begin with a \'@\''))
737 t
= Twitter(auth
=authen())
742 while next_cursor
!= 0:
743 list = t
.mutes
.users
.list(
744 screen_name
=g
['original_name'],
747 include_entities
=False,
749 for u
in list['users']:
750 rel
[u
['name']] = '@' + u
['screen_name']
751 next_cursor
= list['next_cursor']
753 printNicely('All: ' + str(len(rel
)) + ' people.')
755 user
= ' ' + cycle_color(name
)
756 user
+= color_func(c
['TWEET']['nick'])(' ' + rel
[name
] + ' ')
764 t
= Twitter(auth
=authen())
765 screen_name
= g
['stuff'].split()[0]
766 if screen_name
.startswith('@'):
768 screen_name
=screen_name
[1:],
769 include_entities
=False,
771 printNicely(green('You blocked ' + screen_name
+ '.'))
773 printNicely(red('A name should begin with a \'@\''))
780 t
= Twitter(auth
=authen())
781 screen_name
= g
['stuff'].split()[0]
782 if screen_name
.startswith('@'):
784 screen_name
=screen_name
[1:],
785 include_entities
=False,
787 printNicely(green('Unblock ' + screen_name
+ ' success!'))
789 printNicely(red('A name should begin with a \'@\''))
794 Report a user as a spam account
796 t
= Twitter(auth
=authen())
797 screen_name
= g
['stuff'].split()[0]
798 if screen_name
.startswith('@'):
800 screen_name
=screen_name
[1:])
801 printNicely(green('You reported ' + screen_name
+ '.'))
803 printNicely(red('Sorry I can\'t understand.'))
811 list_name
= raw_input(light_magenta('Give me the list\'s name: '))
812 # Get list name and owner
814 owner
, slug
= list_name
.split('/')
815 if slug
.startswith('@'):
820 light_magenta('List name should follow "@owner/list_name" format.'))
821 raise Exception('Wrong list name')
828 rel
= t
.lists
.list(screen_name
=g
['original_name'])
832 printNicely(light_magenta('You belong to no lists :)'))
839 owner
, slug
= get_slug()
840 res
= t
.lists
.statuses(
842 owner_screen_name
=owner
,
844 include_entities
=False)
854 owner
, slug
= get_slug()
858 while next_cursor
!= 0:
861 owner_screen_name
=owner
,
863 include_entities
=False)
865 rel
[u
['name']] = '@' + u
['screen_name']
866 next_cursor
= m
['next_cursor']
867 printNicely('All: ' + str(len(rel
)) + ' members.')
869 user
= ' ' + cycle_color(name
)
870 user
+= color_func(c
['TWEET']['nick'])(' ' + rel
[name
] + ' ')
874 def list_subscribers(t
):
878 owner
, slug
= get_slug()
882 while next_cursor
!= 0:
883 m
= t
.lists
.subscribers(
885 owner_screen_name
=owner
,
887 include_entities
=False)
889 rel
[u
['name']] = '@' + u
['screen_name']
890 next_cursor
= m
['next_cursor']
891 printNicely('All: ' + str(len(rel
)) + ' subscribers.')
893 user
= ' ' + cycle_color(name
)
894 user
+= color_func(c
['TWEET']['nick'])(' ' + rel
[name
] + ' ')
900 Add specific user to a list
902 owner
, slug
= get_slug()
904 user_name
= raw_input(light_magenta('Give me name of the newbie: '))
905 if user_name
.startswith('@'):
906 user_name
= user_name
[1:]
908 t
.lists
.members
.create(
910 owner_screen_name
=owner
,
911 screen_name
=user_name
)
912 printNicely(green('Added.'))
914 printNicely(light_magenta('I\'m sorry we can not add him/her.'))
919 Remove specific user from a list
921 owner
, slug
= get_slug()
923 user_name
= raw_input(light_magenta('Give me name of the unlucky one: '))
924 if user_name
.startswith('@'):
925 user_name
= user_name
[1:]
927 t
.lists
.members
.destroy(
929 owner_screen_name
=owner
,
930 screen_name
=user_name
)
931 printNicely(green('Gone.'))
933 printNicely(light_magenta('I\'m sorry we can not remove him/her.'))
936 def list_subscribe(t
):
940 owner
, slug
= get_slug()
943 t
.lists
.subscribers
.create(
945 owner_screen_name
=owner
)
946 printNicely(green('Done.'))
949 light_magenta('I\'m sorry you can not subscribe to this list.'))
952 def list_unsubscribe(t
):
956 owner
, slug
= get_slug()
959 t
.lists
.subscribers
.destroy(
961 owner_screen_name
=owner
)
962 printNicely(green('Done.'))
965 light_magenta('I\'m sorry you can not unsubscribe to this list.'))
974 while next_cursor
!= 0:
975 res
= t
.lists
.ownerships(
976 screen_name
=g
['original_name'],
979 next_cursor
= res
['next_cursor']
983 printNicely(light_magenta('You own no lists :)'))
990 name
= raw_input(light_magenta('New list\'s name: '))
991 mode
= raw_input(light_magenta('New list\'s mode (public/private): '))
992 description
= raw_input(light_magenta('New list\'s description: '))
997 description
=description
)
998 printNicely(green(name
+ ' list is created.'))
1000 printNicely(red('Oops something is wrong with Twitter :('))
1007 slug
= raw_input(light_magenta('Your list that you want to update: '))
1008 name
= raw_input(light_magenta('Update name (leave blank to unchange): '))
1009 mode
= raw_input(light_magenta('Update mode (public/private): '))
1010 description
= raw_input(light_magenta('Update description: '))
1014 slug
='-'.join(slug
.split()),
1015 owner_screen_name
=g
['original_name'],
1018 description
=description
)
1022 owner_screen_name
=g
['original_name'],
1024 description
=description
)
1025 printNicely(green(slug
+ ' list is updated.'))
1027 printNicely(red('Oops something is wrong with Twitter :('))
1034 slug
= raw_input(light_magenta('Your list that you want to update: '))
1037 slug
='-'.join(slug
.split()),
1038 owner_screen_name
=g
['original_name'])
1039 printNicely(green(slug
+ ' list is deleted.'))
1041 printNicely(red('Oops something is wrong with Twitter :('))
1048 t
= Twitter(auth
=authen())
1049 # List all lists or base on action
1051 g
['list_action'] = g
['stuff'].split()[0]
1058 'all_mem': list_members
,
1059 'all_sub': list_subscribers
,
1062 'sub': list_subscribe
,
1063 'unsub': list_unsubscribe
,
1066 'update': list_update
,
1070 return action_ary
[g
['list_action']](t
)
1072 printNicely(red('Please try again.'))
1077 Unix's command `cal`
1080 rel
= os
.popen('cal').read().split('\n')
1083 show_calendar(month
, date
, rel
)
1088 Browse and change config
1090 all_config
= get_all_config()
1091 g
['stuff'] = g
['stuff'].strip()
1094 for k
in all_config
:
1096 green(k
) + ': ' + light_yellow(str(all_config
[k
]))
1098 guide
= 'Detailed explanation can be found at ' + \
1099 color_func(c
['TWEET']['link'])(
1100 'http://rainbowstream.readthedocs.org/en/latest/#config-explanation')
1102 # Print specific config
1103 elif len(g
['stuff'].split()) == 1:
1104 if g
['stuff'] in all_config
:
1107 green(k
) + ': ' + light_yellow(str(all_config
[k
]))
1110 printNicely(red('No such config key.'))
1111 # Print specific config's default value
1112 elif len(g
['stuff'].split()) == 2 and g
['stuff'].split()[-1] == 'default':
1113 key
= g
['stuff'].split()[0]
1115 value
= get_default_config(key
)
1116 line
= ' ' * 2 + green(key
) + ': ' + light_magenta(value
)
1120 light_magenta('This config key does not exist in default.'))
1121 # Delete specific config key in config file
1122 elif len(g
['stuff'].split()) == 2 and g
['stuff'].split()[-1] == 'drop':
1123 key
= g
['stuff'].split()[0]
1126 printNicely(green('Config key is dropped.'))
1128 printNicely(red('No such config key.'))
1129 # Set specific config
1130 elif len(g
['stuff'].split()) == 3 and g
['stuff'].split()[1] == '=':
1131 key
= g
['stuff'].split()[0]
1132 value
= g
['stuff'].split()[-1]
1133 if key
== 'THEME' and not validate_theme(value
):
1134 printNicely(red('Invalid theme\'s value.'))
1137 set_config(key
, value
)
1138 # Apply theme immediately
1140 c
['THEME'] = reload_theme(value
,c
['THEME'])
1141 g
['decorated_name'] = lambda x
: color_func(
1142 c
['DECORATED_NAME'])(
1144 printNicely(green('Updated successfully.'))
1146 printNicely(light_magenta('Not valid value.'))
1150 printNicely(light_magenta('Sorry I can\'s understand.'))
1155 List and change theme
1159 for theme
in g
['themes']:
1160 line
= light_magenta(theme
)
1161 if c
['THEME'] == theme
:
1162 line
= ' ' * 2 + light_yellow('* ') + line
1164 line
= ' ' * 4 + line
1170 c
['THEME'] = reload_theme(g
['stuff'],c
['THEME'])
1171 # Redefine decorated_name
1172 g
['decorated_name'] = lambda x
: color_func(
1173 c
['DECORATED_NAME'])(
1175 printNicely(green('Theme changed.'))
1176 except Exception as e
:
1178 printNicely(red('No such theme exists.'))
1181 def help_discover():
1186 # Discover the world
1188 usage
+= s
+ grey(u
'\u266A' + ' Discover the world \n')
1189 usage
+= s
* 2 + light_green('trend') + ' will show global trending topics. ' + \
1190 'You can try ' + light_green('trend US') + ' or ' + \
1191 light_green('trend JP Tokyo') + '.\n'
1192 usage
+= s
* 2 + light_green('home') + ' will show your timeline. ' + \
1193 light_green('home 7') + ' will show 7 tweets.\n'
1194 usage
+= s
* 2 + light_green('mentions') + ' will show mentions timeline. ' + \
1195 light_green('mentions 7') + ' will show 7 mention tweets.\n'
1196 usage
+= s
* 2 + light_green('whois @mdo') + ' will show profile of ' + \
1197 magenta('@mdo') + '.\n'
1198 usage
+= s
* 2 + light_green('view @mdo') + \
1199 ' will show ' + magenta('@mdo') + '\'s home.\n'
1200 usage
+= s
* 2 + light_green('s AKB48') + ' will search for "' + \
1201 light_yellow('AKB48') + '" and return 5 newest tweet. ' + \
1202 'Search can be performed with or without hashtag.\n'
1213 usage
+= s
+ grey(u
'\u266A' + ' Tweets \n')
1214 usage
+= s
* 2 + light_green('t oops ') + \
1215 'will tweet "' + light_yellow('oops') + '" immediately.\n'
1217 light_green('rt 12 ') + ' will retweet to tweet with ' + \
1218 light_yellow('[id=12]') + '.\n'
1220 light_green('quote 12 ') + ' will quote the tweet with ' + \
1221 light_yellow('[id=12]') + '. If no extra text is added, ' + \
1222 'the quote will be canceled.\n'
1224 light_green('allrt 12 20 ') + ' will list 20 newest retweet of the tweet with ' + \
1225 light_yellow('[id=12]') + '.\n'
1226 usage
+= s
* 2 + light_green('rep 12 oops') + ' will reply "' + \
1227 light_yellow('oops') + '" to tweet with ' + \
1228 light_yellow('[id=12]') + '.\n'
1230 light_green('fav 12 ') + ' will favorite the tweet with ' + \
1231 light_yellow('[id=12]') + '.\n'
1233 light_green('ufav 12 ') + ' will unfavorite tweet with ' + \
1234 light_yellow('[id=12]') + '.\n'
1236 light_green('del 12 ') + ' will delete tweet with ' + \
1237 light_yellow('[id=12]') + '.\n'
1238 usage
+= s
* 2 + light_green('show image 12') + ' will show image in tweet with ' + \
1239 light_yellow('[id=12]') + ' in your OS\'s image viewer.\n'
1240 usage
+= s
* 2 + light_green('open 12') + ' will open url in tweet with ' + \
1241 light_yellow('[id=12]') + ' in your OS\'s default browser.\n'
1245 def help_messages():
1252 usage
+= s
+ grey(u
'\u266A' + ' Direct messages \n')
1253 usage
+= s
* 2 + light_green('inbox') + ' will show inbox messages. ' + \
1254 light_green('inbox 7') + ' will show newest 7 messages.\n'
1255 usage
+= s
* 2 + light_green('sent') + ' will show sent messages. ' + \
1256 light_green('sent 7') + ' will show newest 7 messages.\n'
1257 usage
+= s
* 2 + light_green('mes @dtvd88 hi') + ' will send a "hi" messege to ' + \
1258 magenta('@dtvd88') + '.\n'
1259 usage
+= s
* 2 + light_green('trash 5') + ' will remove message with ' + \
1260 light_yellow('[message_id=5]') + '.\n'
1264 def help_friends_and_followers():
1266 Friends and Followers
1269 # Follower and following
1271 usage
+= s
+ grey(u
'\u266A' + ' Friends and followers \n')
1273 light_green('ls fl') + \
1274 ' will list all followers (people who are following you).\n'
1276 light_green('ls fr') + \
1277 ' will list all friends (people who you are following).\n'
1278 usage
+= s
* 2 + light_green('fl @dtvd88') + ' will follow ' + \
1279 magenta('@dtvd88') + '.\n'
1280 usage
+= s
* 2 + light_green('ufl @dtvd88') + ' will unfollow ' + \
1281 magenta('@dtvd88') + '.\n'
1282 usage
+= s
* 2 + light_green('mute @dtvd88') + ' will mute ' + \
1283 magenta('@dtvd88') + '.\n'
1284 usage
+= s
* 2 + light_green('unmute @dtvd88') + ' will unmute ' + \
1285 magenta('@dtvd88') + '.\n'
1286 usage
+= s
* 2 + light_green('muting') + ' will list muting users.\n'
1287 usage
+= s
* 2 + light_green('block @dtvd88') + ' will block ' + \
1288 magenta('@dtvd88') + '.\n'
1289 usage
+= s
* 2 + light_green('unblock @dtvd88') + ' will unblock ' + \
1290 magenta('@dtvd88') + '.\n'
1291 usage
+= s
* 2 + light_green('report @dtvd88') + ' will report ' + \
1292 magenta('@dtvd88') + ' as a spam account.\n'
1303 usage
+= s
+ grey(u
'\u266A' + ' Twitter list\n')
1304 usage
+= s
* 2 + light_green('list') + \
1305 ' will show all lists you are belong to.\n'
1306 usage
+= s
* 2 + light_green('list home') + \
1307 ' will show timeline of list. You will be asked for list\'s name.\n'
1308 usage
+= s
* 2 + light_green('list all_mem') + \
1309 ' will show list\'s all members.\n'
1310 usage
+= s
* 2 + light_green('list all_sub') + \
1311 ' will show list\'s all subscribers.\n'
1312 usage
+= s
* 2 + light_green('list add') + \
1313 ' will add specific person to a list owned by you.' + \
1314 ' You will be asked for list\'s name and person\'s name.\n'
1315 usage
+= s
* 2 + light_green('list rm') + \
1316 ' will remove specific person from 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 sub') + \
1319 ' will subscribe you to a specific list.\n'
1320 usage
+= s
* 2 + light_green('list unsub') + \
1321 ' will unsubscribe you from a specific list.\n'
1322 usage
+= s
* 2 + light_green('list own') + \
1323 ' will show all list owned by you.\n'
1324 usage
+= s
* 2 + light_green('list new') + \
1325 ' will create a new list.\n'
1326 usage
+= s
* 2 + light_green('list update') + \
1327 ' will update a list owned by you.\n'
1328 usage
+= s
* 2 + light_green('list del') + \
1329 ' will delete a list owned by you.\n'
1340 usage
+= s
+ grey(u
'\u266A' + ' Switching streams \n')
1341 usage
+= s
* 2 + light_green('switch public #AKB') + \
1342 ' will switch to public stream and follow "' + \
1343 light_yellow('AKB') + '" keyword.\n'
1344 usage
+= s
* 2 + light_green('switch mine') + \
1345 ' will switch to your personal stream.\n'
1346 usage
+= s
* 2 + light_green('switch mine -f ') + \
1347 ' will prompt to enter the filter.\n'
1348 usage
+= s
* 3 + light_yellow('Only nicks') + \
1349 ' filter will decide nicks will be INCLUDE ONLY.\n'
1350 usage
+= s
* 3 + light_yellow('Ignore nicks') + \
1351 ' filter will decide nicks will be EXCLUDE.\n'
1352 usage
+= s
* 2 + light_green('switch mine -d') + \
1353 ' will use the config\'s ONLY_LIST and IGNORE_LIST.\n'
1362 h
, w
= os
.popen('stty size', 'r').read().split()
1365 usage
+= s
+ 'Hi boss! I\'m ready to serve you right now!\n'
1366 usage
+= s
+ '-' * (int(w
) - 4) + '\n'
1367 usage
+= s
+ 'You are ' + \
1368 light_yellow('already') + ' on your personal stream.\n'
1369 usage
+= s
+ 'Any update from Twitter will show up ' + \
1370 light_yellow('immediately') + '.\n'
1371 usage
+= s
+ 'In addtion, following commands are available right now:\n'
1372 # Twitter help section
1374 usage
+= s
+ grey(u
'\u266A' + ' Twitter help\n')
1375 usage
+= s
* 2 + light_green('h discover') + \
1376 ' will show help for discover commands.\n'
1377 usage
+= s
* 2 + light_green('h tweets') + \
1378 ' will show help for tweets commands.\n'
1379 usage
+= s
* 2 + light_green('h messages') + \
1380 ' will show help for messages commands.\n'
1381 usage
+= s
* 2 + light_green('h friends_and_followers') + \
1382 ' will show help for friends and followers commands.\n'
1383 usage
+= s
* 2 + light_green('h list') + \
1384 ' will show help for list commands.\n'
1385 usage
+= s
* 2 + light_green('h stream') + \
1386 ' will show help for stream commands.\n'
1389 usage
+= s
+ grey(u
'\u266A' + ' Smart shell\n')
1390 usage
+= s
* 2 + light_green('111111 * 9 / 7') + ' or any math expression ' + \
1391 'will be evaluate by Python interpreter.\n'
1392 usage
+= s
* 2 + 'Even ' + light_green('cal') + ' will show the calendar' + \
1393 ' for current month.\n'
1396 usage
+= s
+ grey(u
'\u266A' + ' Config \n')
1397 usage
+= s
* 2 + light_green('theme') + ' will list available theme. ' + \
1398 light_green('theme monokai') + ' will apply ' + light_yellow('monokai') + \
1399 ' theme immediately.\n'
1400 usage
+= s
* 2 + light_green('config') + ' will list all config.\n'
1402 light_green('config ASCII_ART') + ' will output current value of ' +\
1403 light_yellow('ASCII_ART') + ' config key.\n'
1405 light_green('config TREND_MAX default') + ' will output default value of ' + \
1406 light_yellow('TREND_MAX') + ' config key.\n'
1408 light_green('config CUSTOM_CONFIG drop') + ' will drop ' + \
1409 light_yellow('CUSTOM_CONFIG') + ' config key.\n'
1411 light_green('config IMAGE_ON_TERM = true') + ' will set value of ' + \
1412 light_yellow('IMAGE_ON_TERM') + ' config key to ' + \
1413 light_yellow('True') + '.\n'
1416 usage
+= s
+ grey(u
'\u266A' + ' Screening \n')
1417 usage
+= s
* 2 + light_green('h') + ' will show this help again.\n'
1418 usage
+= s
* 2 + light_green('p') + ' will pause the stream.\n'
1419 usage
+= s
* 2 + light_green('r') + ' will unpause the stream.\n'
1420 usage
+= s
* 2 + light_green('c') + ' will clear the screen.\n'
1421 usage
+= s
* 2 + light_green('q') + ' will quit.\n'
1424 usage
+= s
+ '-' * (int(w
) - 4) + '\n'
1425 usage
+= s
+ 'Have fun and hang tight! \n'
1428 'discover': help_discover
,
1429 'tweets': help_tweets
,
1430 'messages': help_messages
,
1431 'friends_and_followers': help_friends_and_followers
,
1433 'stream': help_stream
,
1436 d
[g
['stuff'].strip()]()
1443 Pause stream display
1446 printNicely(green('Stream is paused'))
1454 printNicely(green('Stream is running back now'))
1470 printNicely(green('See you next time :)'))
1478 Reset prefix of line
1481 printNicely(magenta('Need tips ? Type "h" and hit Enter key!'))
1484 printNicely(str(eval(g
['cmd'])))
1541 Listen to user's input
1546 ['public', 'mine'], # switch
1561 ['image'], # show image
1563 ['fl', 'fr'], # list
1590 [key
for key
in dict(get_all_config())], # config
1591 g
['themes'], # theme
1596 'friends_and_followers',
1606 init_interactive_shell(d
)
1610 # Prompt redraw is needed when user is typing
1613 line
= raw_input(g
['decorated_name'](c
['PREFIX']))
1617 cmd
= line
.split()[0]
1621 # Prompt redraw not need when raw_input done
1623 # Lock the semaphore
1625 # Save cmd to global variable and call process
1626 g
['stuff'] = ' '.join(line
.split()[1:])
1627 # Process the command
1630 if cmd
in ['switch', 't', 'rt', 'rep']:
1634 # Release the semaphore lock
1637 printNicely(red('OMG something is wrong with Twitter right now.'))
1640 def stream(domain
, args
, name
='Rainbow Stream'):
1646 c
['USER_DOMAIN']: name
,
1647 c
['PUBLIC_DOMAIN']: args
.track_keywords
,
1648 c
['SITE_DOMAIN']: name
,
1651 ascii_art(art_dict
[domain
])
1652 # These arguments are optional:
1654 timeout
=args
.timeout
,
1656 heartbeat_timeout
=args
.heartbeat_timeout
)
1659 if args
.track_keywords
:
1660 query_args
['track'] = args
.track_keywords
1662 stream
= TwitterStream(
1667 if domain
== c
['USER_DOMAIN']:
1668 tweet_iter
= stream
.user(**query_args
)
1669 elif domain
== c
['SITE_DOMAIN']:
1670 tweet_iter
= stream
.site(**query_args
)
1672 if args
.track_keywords
:
1673 tweet_iter
= stream
.statuses
.filter(**query_args
)
1675 tweet_iter
= stream
.statuses
.sample()
1676 # Block new stream until other one exits
1677 StreamLock
.acquire()
1678 g
['stream_stop'] = False
1679 for tweet
in tweet_iter
:
1680 if(g
['stream_stop']):
1681 StreamLock
.release()
1685 elif tweet
is Timeout
:
1686 printNicely("-- Timeout --")
1687 elif tweet
is HeartbeatTimeout
:
1688 printNicely("-- Heartbeat Timeout --")
1689 elif tweet
is Hangup
:
1690 printNicely("-- Hangup --")
1691 elif tweet
.get('text'):
1694 keyword
=args
.track_keywords
,
1695 check_semaphore
=True,
1699 elif tweet
.get('direct_message'):
1700 print_message(tweet
['direct_message'], check_semaphore
=True)
1701 except TwitterHTTPError
:
1704 magenta("We have maximum connection problem with twitter'stream API right now :("))
1712 args
= parse_arguments()
1715 except TwitterHTTPError
:
1718 magenta("We have maximum connection problem with twitter'stream API right now :("))
1719 printNicely(magenta("Let's try again later."))
1722 # Spawn stream thread
1723 th
= threading
.Thread(target
=stream
, args
=(c
['USER_DOMAIN'], args
, g
['original_name']))
1726 # Start listen process