2 Colorful user's timeline stream
4 from multiprocessing
import Process
15 from twitter
.stream
import TwitterStream
, Timeout
, HeartbeatTimeout
, Hangup
16 from twitter
.api
import *
17 from twitter
.oauth
import OAuth
, read_token_file
18 from twitter
.oauth_dance
import oauth_dance
19 from twitter
.util
import printNicely
24 from .consumer
import *
25 from .interactive
import *
27 from .c_image
import *
28 from .py3patch
import *
78 def parse_arguments():
82 parser
= argparse
.ArgumentParser(description
=__doc__
or "")
86 help='Timeout for the stream (seconds).')
89 '--heartbeat-timeout',
90 help='Set heartbeat timeout.',
96 help='Set stream to non-blocking.')
100 help='Search the stream for specific text.')
104 help='Filter specific screen_name.')
108 help='Ignore specific screen_name.')
113 help='Display all image on terminal.')
114 return parser
.parse_args()
119 Authenticate with Twitter OAuth
121 # When using rainbow stream you must authorize.
122 twitter_credential
= os
.environ
.get(
126 '')) + os
.sep
+ '.rainbow_oauth'
127 if not os
.path
.exists(twitter_credential
):
128 oauth_dance("Rainbow Stream",
132 oauth_token
, oauth_token_secret
= read_token_file(twitter_credential
)
145 t
= Twitter(auth
=authen())
146 name
= '@' + t
.account
.verify_credentials()['screen_name']
147 g
['original_name'] = name
[1:]
148 g
['decorated_name'] = color_func(c
['DECORATED_NAME'])('[' + name
+ ']: ')
150 files
= os
.listdir(os
.path
.dirname(__file__
) + '/colorset')
151 themes
= [f
.split('.')[0] for f
in files
if f
.split('.')[-1] == 'json']
153 db
.theme_store(c
['THEME'])
155 db
.semaphore_store(False)
163 target
= g
['stuff'].split()[0]
165 args
= parse_arguments()
167 if g
['stuff'].split()[-1] == '-f':
168 only
= raw_input('Only nicks: ')
169 ignore
= raw_input('Ignore nicks: ')
170 args
.filter = filter(None, only
.split(','))
171 args
.ignore
= filter(None, ignore
.split(','))
172 elif g
['stuff'].split()[-1] == '-d':
173 args
.filter = c
['ONLY_LIST']
174 args
.ignore
= c
['IGNORE_LIST']
176 printNicely(red('Sorry, wrong format.'))
179 if target
== 'public':
180 keyword
= g
['stuff'].split()[1]
181 if keyword
[0] == '#':
182 keyword
= keyword
[1:]
184 os
.kill(g
['stream_pid'], signal
.SIGKILL
)
185 args
.track_keywords
= keyword
193 g
['stream_pid'] = p
.pid
195 elif target
== 'mine':
197 os
.kill(g
['stream_pid'], signal
.SIGKILL
)
206 g
['stream_pid'] = p
.pid
209 printNicely(cyan('Only: ' + str(args
.filter)))
211 printNicely(red('Ignore: ' + str(args
.ignore
)))
214 printNicely(red('Sorry I can\'t understand.'))
221 t
= Twitter(auth
=authen())
222 # Get country and town
224 country
= g
['stuff'].split()[0]
228 town
= g
['stuff'].split()[1]
231 avail
= t
.trends
.available()
234 trends
= t
.trends
.place(_id
=1)[0]['trends']
237 for location
in avail
:
238 # Search for country and Town
240 if location
['countryCode'] == country \
241 and location
['placeType']['name'] == 'Town' \
242 and location
['name'] == town
:
243 trends
= t
.trends
.place(_id
=location
['woeid'])[0]['trends']
245 # Search for country only
247 if location
['countryCode'] == country \
248 and location
['placeType']['name'] == 'Country':
249 trends
= t
.trends
.place(_id
=location
['woeid'])[0]['trends']
257 t
= Twitter(auth
=authen())
258 num
= c
['HOME_TWEET_NUM']
259 if g
['stuff'].isdigit():
260 num
= int(g
['stuff'])
261 for tweet
in reversed(t
.statuses
.home_timeline(count
=num
)):
262 draw(t
=tweet
, iot
=g
['iot'])
270 t
= Twitter(auth
=authen())
271 user
= g
['stuff'].split()[0]
274 num
= int(g
['stuff'].split()[1])
276 num
= c
['HOME_TWEET_NUM']
277 for tweet
in reversed(t
.statuses
.user_timeline(count
=num
, screen_name
=user
[1:])):
278 draw(t
=tweet
, iot
=g
['iot'])
281 printNicely(red('A name should begin with a \'@\''))
288 t
= Twitter(auth
=authen())
289 num
= c
['HOME_TWEET_NUM']
290 if g
['stuff'].isdigit():
291 num
= int(g
['stuff'])
292 for tweet
in reversed(t
.statuses
.mentions_timeline(count
=num
)):
293 draw(t
=tweet
, iot
=g
['iot'])
301 t
= Twitter(auth
=authen())
302 t
.statuses
.update(status
=g
['stuff'])
309 t
= Twitter(auth
=authen())
311 id = int(g
['stuff'].split()[0])
313 printNicely(red('Sorry I can\'t understand.'))
315 tid
= db
.rainbow_to_tweet_query(id)[0].tweet_id
316 t
.statuses
.retweet(id=tid
, include_entities
=False, trim_user
=True)
323 t
= Twitter(auth
=authen())
325 id = int(g
['stuff'].split()[0])
327 printNicely(red('Sorry I can\'t understand.'))
329 tid
= db
.rainbow_to_tweet_query(id)[0].tweet_id
330 tweet
= t
.statuses
.show(id=tid
)
331 screen_name
= tweet
['user']['screen_name']
333 quote
= '\"@' + screen_name
+ ': ' + text
+ '\"'
334 quote
= quote
.encode('utf8')
335 notice
= light_magenta('Compose mode ')
336 notice
+= light_yellow('(Enter nothing will cancel the quote)')
337 notice
+= light_magenta(':')
339 extra
= raw_input(quote
)
341 t
.statuses
.update(status
=quote
+ extra
)
343 printNicely(light_magenta('No text added.'))
350 t
= Twitter(auth
=authen())
353 id = int(g
['stuff'].split()[0])
355 printNicely(red('Sorry I can\'t understand.'))
357 tid
= db
.rainbow_to_tweet_query(id)[0].tweet_id
358 # Get display num if exist
360 num
= int(g
['stuff'].split()[1])
362 num
= c
['RETWEETS_SHOW_NUM']
363 # Get result and display
364 rt_ary
= t
.statuses
.retweets(id=tid
, count
=num
)
366 printNicely(magenta('This tweet has no retweet.'))
368 for tweet
in reversed(rt_ary
):
369 draw(t
=tweet
, iot
=g
['iot'])
377 t
= Twitter(auth
=authen())
379 id = int(g
['stuff'].split()[0])
381 printNicely(red('Sorry I can\'t understand.'))
383 tid
= db
.rainbow_to_tweet_query(id)[0].tweet_id
384 t
.favorites
.create(_id
=tid
, include_entities
=False)
385 printNicely(green('Favorited.'))
386 draw(t
.statuses
.show(id=tid
), iot
=g
['iot'])
394 t
= Twitter(auth
=authen())
396 id = int(g
['stuff'].split()[0])
398 printNicely(red('Sorry I can\'t understand.'))
400 tid
= db
.rainbow_to_tweet_query(id)[0].tweet_id
401 user
= t
.statuses
.show(id=tid
)['user']['screen_name']
402 status
= ' '.join(g
['stuff'].split()[1:])
403 status
= '@' + user
+ ' ' + status
.decode('utf-8')
404 t
.statuses
.update(status
=status
, in_reply_to_status_id
=tid
)
411 t
= Twitter(auth
=authen())
413 rid
= int(g
['stuff'].split()[0])
415 printNicely(red('Sorry I can\'t understand.'))
417 tid
= db
.rainbow_to_tweet_query(rid
)[0].tweet_id
418 t
.statuses
.destroy(id=tid
)
419 printNicely(green('Okay it\'s gone.'))
426 t
= Twitter(auth
=authen())
428 id = int(g
['stuff'].split()[0])
430 printNicely(red('Sorry I can\'t understand.'))
432 tid
= db
.rainbow_to_tweet_query(id)[0].tweet_id
433 t
.favorites
.destroy(_id
=tid
)
434 printNicely(green('Okay it\'s unfavorited.'))
435 draw(t
.statuses
.show(id=tid
), iot
=g
['iot'])
443 t
= Twitter(auth
=authen())
444 g
['stuff'] = g
['stuff'].strip()
445 rel
= t
.search
.tweets(q
=g
['stuff'])['statuses']
447 printNicely('Newest tweets:')
448 for i
in reversed(xrange(c
['SEARCH_MAX_RECORD'])):
454 printNicely(magenta('I\'m afraid there is no result'))
459 Send a direct message
461 t
= Twitter(auth
=authen())
462 user
= g
['stuff'].split()[0]
463 if user
[0].startswith('@'):
465 content
= g
['stuff'].split()[1]
467 printNicely(red('Sorry I can\'t understand.'))
468 t
.direct_messages
.new(
469 screen_name
=user
[1:],
472 printNicely(green('Message sent.'))
474 printNicely(red('A name should begin with a \'@\''))
481 t
= Twitter(auth
=authen())
483 target
= g
['stuff'].split()[0]
484 if target
!= 'image':
486 id = int(g
['stuff'].split()[1])
487 tid
= db
.rainbow_to_tweet_query(id)[0].tweet_id
488 tweet
= t
.statuses
.show(id=tid
)
489 media
= tweet
['entities']['media']
491 res
= requests
.get(m
['media_url'])
492 img
= Image
.open(BytesIO(res
.content
))
495 printNicely(red('Sorry I can\'t show this image.'))
502 t
= Twitter(auth
=authen())
504 if not g
['stuff'].isdigit():
506 tid
= db
.rainbow_to_tweet_query(g
['stuff'])[0].tweet_id
507 tweet
= t
.statuses
.show(id=tid
)
509 u
for u
in tweet
['text'].split() if u
.startswith('http://')]
511 printNicely(light_magenta('No url here @.@!'))
513 for link
in link_ary
:
514 webbrowser
.open(link
)
516 printNicely(red('Sorry I can\'t open url in this tweet.'))
521 List friends for followers
523 t
= Twitter(auth
=authen())
526 name
= g
['stuff'].split()[1]
527 if name
.startswith('@'):
530 printNicely(red('A name should begin with a \'@\''))
531 raise Exception('Invalid name')
533 name
= g
['original_name']
534 # Get list followers or friends
536 target
= g
['stuff'].split()[0]
538 printNicely(red('Omg some syntax is wrong.'))
540 d
= {'fl': 'followers', 'fr': 'friends'}
544 while next_cursor
!= 0:
545 list = getattr(t
, d
[target
]).list(
549 include_entities
=False,
551 for u
in list['users']:
552 rel
[u
['name']] = '@' + u
['screen_name']
553 next_cursor
= list['next_cursor']
555 printNicely('All: ' + str(len(rel
)) + ' ' + d
[target
] + '.')
557 user
= ' ' + cycle_color(name
)
558 user
+= color_func(c
['TWEET']['nick'])(' ' + rel
[name
] + ' ')
564 Inbox direct messages
566 t
= Twitter(auth
=authen())
567 num
= c
['MESSAGES_DISPLAY']
569 if g
['stuff'].isdigit():
572 # Max message per page is 20 so we have to loop
574 rel
= rel
+ t
.direct_messages(
577 include_entities
=False,
582 rel
= rel
+ t
.direct_messages(
585 include_entities
=False,
589 printNicely('Inbox: newest ' + str(len(rel
)) + ' messages.')
590 for m
in reversed(rel
):
599 t
= Twitter(auth
=authen())
600 num
= c
['MESSAGES_DISPLAY']
602 if g
['stuff'].isdigit():
603 num
= int(g
['stuff'])
605 # Max message per page is 20 so we have to loop
607 rel
= rel
+ t
.direct_messages
.sent(
610 include_entities
=False,
615 rel
= rel
+ t
.direct_messages
.sent(
618 include_entities
=False,
622 printNicely('Sent: newest ' + str(len(rel
)) + ' messages.')
623 for m
in reversed(rel
):
632 t
= Twitter(auth
=authen())
634 rid
= int(g
['stuff'].split()[0])
636 printNicely(red('Sorry I can\'t understand.'))
637 mid
= db
.rainbow_to_message_query(rid
)[0].message_id
638 t
.direct_messages
.destroy(id=mid
)
639 printNicely(green('Message deleted.'))
644 Show profile of a specific user
646 t
= Twitter(auth
=authen())
647 screen_name
= g
['stuff'].split()[0]
648 if screen_name
.startswith('@'):
651 screen_name
=screen_name
[1:],
652 include_entities
=False)
653 show_profile(user
, g
['iot'])
655 printNicely(red('Omg no user.'))
657 printNicely(red('A name should begin with a \'@\''))
664 t
= Twitter(auth
=authen())
665 screen_name
= g
['stuff'].split()[0]
666 if screen_name
.startswith('@'):
667 t
.friendships
.create(screen_name
=screen_name
[1:], follow
=True)
668 printNicely(green('You are following ' + screen_name
+ ' now!'))
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
.destroy(
681 screen_name
=screen_name
[1:],
682 include_entities
=False)
683 printNicely(green('Unfollow ' + screen_name
+ ' success!'))
685 printNicely(red('A name should begin with a \'@\''))
692 t
= Twitter(auth
=authen())
694 screen_name
= g
['stuff'].split()[0]
696 printNicely(red('A name should be specified. '))
698 if screen_name
.startswith('@'):
699 rel
= t
.mutes
.users
.create(screen_name
=screen_name
[1:])
700 if isinstance(rel
, dict):
701 printNicely(green(screen_name
+ ' is muted.'))
703 printNicely(red(rel
))
705 printNicely(red('A name should begin with a \'@\''))
712 t
= Twitter(auth
=authen())
714 screen_name
= g
['stuff'].split()[0]
716 printNicely(red('A name should be specified. '))
718 if screen_name
.startswith('@'):
719 rel
= t
.mutes
.users
.destroy(screen_name
=screen_name
[1:])
720 if isinstance(rel
, dict):
721 printNicely(green(screen_name
+ ' is unmuted.'))
723 printNicely(red(rel
))
725 printNicely(red('A name should begin with a \'@\''))
732 t
= Twitter(auth
=authen())
737 while next_cursor
!= 0:
738 list = t
.mutes
.users
.list(
739 screen_name
=g
['original_name'],
742 include_entities
=False,
744 for u
in list['users']:
745 rel
[u
['name']] = '@' + u
['screen_name']
746 next_cursor
= list['next_cursor']
748 printNicely('All: ' + str(len(rel
)) + ' people.')
750 user
= ' ' + cycle_color(name
)
751 user
+= color_func(c
['TWEET']['nick'])(' ' + rel
[name
] + ' ')
759 t
= Twitter(auth
=authen())
760 screen_name
= g
['stuff'].split()[0]
761 if screen_name
.startswith('@'):
763 screen_name
=screen_name
[1:],
764 include_entities
=False,
766 printNicely(green('You blocked ' + screen_name
+ '.'))
768 printNicely(red('A name should begin with a \'@\''))
775 t
= Twitter(auth
=authen())
776 screen_name
= g
['stuff'].split()[0]
777 if screen_name
.startswith('@'):
779 screen_name
=screen_name
[1:],
780 include_entities
=False,
782 printNicely(green('Unblock ' + screen_name
+ ' success!'))
784 printNicely(red('A name should begin with a \'@\''))
789 Report a user as a spam account
791 t
= Twitter(auth
=authen())
792 screen_name
= g
['stuff'].split()[0]
793 if screen_name
.startswith('@'):
795 screen_name
=screen_name
[1:])
796 printNicely(green('You reported ' + screen_name
+ '.'))
798 printNicely(red('Sorry I can\'t understand.'))
806 list_name
= raw_input(light_magenta('Give me the list\'s name: '))
807 # Get list name and owner
809 owner
, slug
= list_name
.split('/')
810 if slug
.startswith('@'):
815 light_magenta('List name should follow "@owner/list_name" format.'))
816 raise Exception('Wrong list name')
823 rel
= t
.lists
.list(screen_name
=g
['original_name'])
827 printNicely(light_magenta('You belong to no lists :)'))
834 owner
, slug
= get_slug()
835 res
= t
.lists
.statuses(
837 owner_screen_name
=owner
,
839 include_entities
=False)
849 owner
, slug
= get_slug()
853 while next_cursor
!= 0:
856 owner_screen_name
=owner
,
858 include_entities
=False)
860 rel
[u
['name']] = '@' + u
['screen_name']
861 next_cursor
= m
['next_cursor']
862 printNicely('All: ' + str(len(rel
)) + ' members.')
864 user
= ' ' + cycle_color(name
)
865 user
+= color_func(c
['TWEET']['nick'])(' ' + rel
[name
] + ' ')
869 def list_subscribers(t
):
873 owner
, slug
= get_slug()
877 while next_cursor
!= 0:
878 m
= t
.lists
.subscribers(
880 owner_screen_name
=owner
,
882 include_entities
=False)
884 rel
[u
['name']] = '@' + u
['screen_name']
885 next_cursor
= m
['next_cursor']
886 printNicely('All: ' + str(len(rel
)) + ' subscribers.')
888 user
= ' ' + cycle_color(name
)
889 user
+= color_func(c
['TWEET']['nick'])(' ' + rel
[name
] + ' ')
895 Add specific user to a list
897 owner
, slug
= get_slug()
899 user_name
= raw_input(light_magenta('Give me name of the newbie: '))
900 if user_name
.startswith('@'):
901 user_name
= user_name
[1:]
903 t
.lists
.members
.create(
905 owner_screen_name
=owner
,
906 screen_name
=user_name
)
907 printNicely(light_green('Added.'))
909 printNicely(light_magenta('I\'m sorry we can not add him/her.'))
914 Remove specific user from a list
916 owner
, slug
= get_slug()
918 user_name
= raw_input(light_magenta('Give me name of the unlucky one: '))
919 if user_name
.startswith('@'):
920 user_name
= user_name
[1:]
922 t
.lists
.members
.destroy(
924 owner_screen_name
=owner
,
925 screen_name
=user_name
)
926 printNicely(light_green('Gone.'))
928 printNicely(light_magenta('I\'m sorry we can not remove him/her.'))
931 def list_subscribe(t
):
935 owner
, slug
= get_slug()
938 t
.lists
.subscribers
.create(
940 owner_screen_name
=owner
)
941 printNicely(light_green('Done.'))
944 light_magenta('I\'m sorry you can not subscribe to this list.'))
947 def list_unsubscribe(t
):
951 owner
, slug
= get_slug()
954 t
.lists
.subscribers
.destroy(
956 owner_screen_name
=owner
)
957 printNicely(light_green('Done.'))
960 light_magenta('I\'m sorry you can not unsubscribe to this list.'))
969 while next_cursor
!= 0:
970 res
= t
.lists
.ownerships(
971 screen_name
=g
['original_name'],
974 next_cursor
= res
['next_cursor']
978 printNicely(light_magenta('You own no lists :)'))
985 name
= raw_input(light_magenta('New list\'s name: '))
986 mode
= raw_input(light_magenta('New list\'s mode (public/private): '))
987 description
= raw_input(light_magenta('New list\'s description: '))
992 description
=description
)
993 printNicely(light_green(name
+ ' list is created.'))
995 printNicely(red('Oops something is wrong with Twitter :('))
1002 slug
= raw_input(light_magenta('Your list that you want to update: '))
1003 name
= raw_input(light_magenta('Update name (leave blank to unchange): '))
1004 mode
= raw_input(light_magenta('Update mode (public/private): '))
1005 description
= raw_input(light_magenta('Update description: '))
1009 slug
='-'.join(slug
.split()),
1010 owner_screen_name
=g
['original_name'],
1013 description
=description
)
1017 owner_screen_name
=g
['original_name'],
1019 description
=description
)
1020 printNicely(light_green(slug
+ ' list is updated.'))
1022 printNicely(red('Oops something is wrong with Twitter :('))
1029 slug
= raw_input(light_magenta('Your list that you want to update: '))
1032 slug
='-'.join(slug
.split()),
1033 owner_screen_name
=g
['original_name'])
1034 printNicely(light_green(slug
+ ' list is deleted.'))
1036 printNicely(red('Oops something is wrong with Twitter :('))
1043 t
= Twitter(auth
=authen())
1044 # List all lists or base on action
1046 g
['list_action'] = g
['stuff'].split()[0]
1053 'all_mem': list_members
,
1054 'all_sub': list_subscribers
,
1057 'sub': list_subscribe
,
1058 'unsub': list_unsubscribe
,
1061 'update': list_update
,
1065 return action_ary
[g
['list_action']](t
)
1067 printNicely(red('Please try again.'))
1072 Unix's command `cal`
1075 rel
= os
.popen('cal').read().split('\n')
1078 show_calendar(month
, date
, rel
)
1083 Browse and change config
1085 all_config
= get_all_config()
1086 g
['stuff'] = g
['stuff'].strip()
1089 for k
in all_config
:
1091 light_green(k
) + ': ' + light_yellow(str(all_config
[k
]))
1093 guide
= 'Detailed explanation can be found at ' + \
1094 color_func(c
['TWEET']['link'])(
1095 'http://rainbowstream.readthedocs.org/en/latest/#config-explanation')
1097 # Print specific config
1098 elif len(g
['stuff'].split()) == 1:
1099 if g
['stuff'] in all_config
:
1102 light_green(k
) + ': ' + light_yellow(str(all_config
[k
]))
1105 printNicely(red('No config key like this.'))
1106 # Print specific config's default value
1107 elif len(g
['stuff'].split()) == 2 and g
['stuff'].split()[-1] == 'default':
1108 key
= g
['stuff'].split()[0]
1109 value
= get_default_config(key
)
1110 line
= ' ' * 2 + light_green(key
) + ': ' + light_magenta(value
)
1112 # Set specific config
1113 elif len(g
['stuff'].split()) == 3 and g
['stuff'].split()[1] == '=':
1114 key
= g
['stuff'].split()[0]
1115 value
= g
['stuff'].split()[-1]
1117 set_config(key
, value
)
1118 printNicely(light_green('Updated successfully.'))
1120 printNicely(light_magenta('Not valid value.'))
1124 printNicely(light_magenta('Sorry I can\'s understand.'))
1129 List and change theme
1133 for theme
in g
['themes']:
1134 line
= light_magenta(theme
)
1135 if c
['THEME'] == theme
:
1136 line
= ' ' * 2 + light_yellow('* ') + line
1138 line
= ' ' * 4 + line
1140 elif g
['stuff'] == 'current_as_default':
1142 set_config('THEME', c
['THEME'])
1143 printNicely(light_green('Okay it will be applied from next time :)'))
1148 if g
['stuff'] != 'custom':
1149 new_config
= os
.path
.dirname(
1150 __file__
) + '/colorset/' + g
['stuff'] + '.json'
1152 new_config
= os
.environ
.get(
1153 'HOME', os
.environ
.get(
1155 '')) + os
.sep
+ '.rainbow_config.json'
1156 new_config
= load_config(new_config
)
1158 for nc
in new_config
:
1159 c
[nc
] = new_config
[nc
]
1160 # Update db and reset colors
1161 db
.theme_update(g
['stuff'])
1162 c
['THEME'] = g
['stuff']
1164 g
['decorated_name'] = color_func(
1165 c
['DECORATED_NAME'])(
1166 '[@' + g
['original_name'] + ']: ')
1167 printNicely(green('Theme changed.'))
1169 printNicely(red('No such theme exists.'))
1172 def help_discover():
1177 # Discover the world
1179 usage
+= s
+ grey(u
'\u266A' + ' Discover the world \n')
1180 usage
+= s
* 2 + light_green('trend') + ' will show global trending topics. ' + \
1181 'You can try ' + light_green('trend US') + ' or ' + \
1182 light_green('trend JP Tokyo') + '.\n'
1183 usage
+= s
* 2 + light_green('home') + ' will show your timeline. ' + \
1184 light_green('home 7') + ' will show 7 tweets.\n'
1185 usage
+= s
* 2 + light_green('mentions') + ' will show mentions timeline. ' + \
1186 light_green('mentions 7') + ' will show 7 mention tweets.\n'
1187 usage
+= s
* 2 + light_green('whois @mdo') + ' will show profile of ' + \
1188 magenta('@mdo') + '.\n'
1189 usage
+= s
* 2 + light_green('view @mdo') + \
1190 ' will show ' + magenta('@mdo') + '\'s home.\n'
1191 usage
+= s
* 2 + light_green('s AKB48') + ' will search for "' + \
1192 light_yellow('AKB48') + '" and return 5 newest tweet. ' + \
1193 'Search can be performed with or without hashtag.\n'
1204 usage
+= s
+ grey(u
'\u266A' + ' Tweets \n')
1205 usage
+= s
* 2 + light_green('t oops ') + \
1206 'will tweet "' + light_yellow('oops') + '" immediately.\n'
1208 light_green('rt 12 ') + ' will retweet to tweet with ' + \
1209 light_yellow('[id=12]') + '.\n'
1211 light_green('quote 12 ') + ' will quote the tweet with ' + \
1212 light_yellow('[id=12]') + '. If no extra text is added, ' + \
1213 'the quote will be canceled.\n'
1215 light_green('allrt 12 20 ') + ' will list 20 newest retweet of the tweet with ' + \
1216 light_yellow('[id=12]') + '.\n'
1217 usage
+= s
* 2 + light_green('rep 12 oops') + ' will reply "' + \
1218 light_yellow('oops') + '" to tweet with ' + \
1219 light_yellow('[id=12]') + '.\n'
1221 light_green('fav 12 ') + ' will favorite the tweet with ' + \
1222 light_yellow('[id=12]') + '.\n'
1224 light_green('ufav 12 ') + ' will unfavorite tweet with ' + \
1225 light_yellow('[id=12]') + '.\n'
1227 light_green('del 12 ') + ' will delete tweet with ' + \
1228 light_yellow('[id=12]') + '.\n'
1229 usage
+= s
* 2 + light_green('show image 12') + ' will show image in tweet with ' + \
1230 light_yellow('[id=12]') + ' in your OS\'s image viewer.\n'
1231 usage
+= s
* 2 + light_green('open 12') + ' will open url in tweet with ' + \
1232 light_yellow('[id=12]') + ' in your OS\'s default browser.\n'
1236 def help_messages():
1243 usage
+= s
+ grey(u
'\u266A' + ' Direct messages \n')
1244 usage
+= s
* 2 + light_green('inbox') + ' will show inbox messages. ' + \
1245 light_green('inbox 7') + ' will show newest 7 messages.\n'
1246 usage
+= s
* 2 + light_green('sent') + ' will show sent messages. ' + \
1247 light_green('sent 7') + ' will show newest 7 messages.\n'
1248 usage
+= s
* 2 + light_green('mes @dtvd88 hi') + ' will send a "hi" messege to ' + \
1249 magenta('@dtvd88') + '.\n'
1250 usage
+= s
* 2 + light_green('trash 5') + ' will remove message with ' + \
1251 light_yellow('[message_id=5]') + '.\n'
1255 def help_friends_and_followers():
1257 Friends and Followers
1260 # Follower and following
1262 usage
+= s
+ grey(u
'\u266A' + ' Friends and followers \n')
1264 light_green('ls fl') + \
1265 ' will list all followers (people who are following you).\n'
1267 light_green('ls fr') + \
1268 ' will list all friends (people who you are following).\n'
1269 usage
+= s
* 2 + light_green('fl @dtvd88') + ' will follow ' + \
1270 magenta('@dtvd88') + '.\n'
1271 usage
+= s
* 2 + light_green('ufl @dtvd88') + ' will unfollow ' + \
1272 magenta('@dtvd88') + '.\n'
1273 usage
+= s
* 2 + light_green('mute @dtvd88') + ' will mute ' + \
1274 magenta('@dtvd88') + '.\n'
1275 usage
+= s
* 2 + light_green('unmute @dtvd88') + ' will unmute ' + \
1276 magenta('@dtvd88') + '.\n'
1277 usage
+= s
* 2 + light_green('muting') + ' will list muting users.\n'
1278 usage
+= s
* 2 + light_green('block @dtvd88') + ' will block ' + \
1279 magenta('@dtvd88') + '.\n'
1280 usage
+= s
* 2 + light_green('unblock @dtvd88') + ' will unblock ' + \
1281 magenta('@dtvd88') + '.\n'
1282 usage
+= s
* 2 + light_green('report @dtvd88') + ' will report ' + \
1283 magenta('@dtvd88') + ' as a spam account.\n'
1294 usage
+= s
+ grey(u
'\u266A' + ' Twitter list\n')
1295 usage
+= s
* 2 + light_green('list') + \
1296 ' will show all lists you are belong to.\n'
1297 usage
+= s
* 2 + light_green('list home') + \
1298 ' will show timeline of list. You will be asked for list\'s name.\n'
1299 usage
+= s
* 2 + light_green('list all_mem') + \
1300 ' will show list\'s all members.\n'
1301 usage
+= s
* 2 + light_green('list all_sub') + \
1302 ' will show list\'s all subscribers.\n'
1303 usage
+= s
* 2 + light_green('list add') + \
1304 ' will add specific person to a list owned by you.' + \
1305 ' You will be asked for list\'s name and person\'s name.\n'
1306 usage
+= s
* 2 + light_green('list rm') + \
1307 ' will remove specific person from a list owned by you.' + \
1308 ' You will be asked for list\'s name and person\'s name.\n'
1309 usage
+= s
* 2 + light_green('list sub') + \
1310 ' will subscribe you to a specific list.\n'
1311 usage
+= s
* 2 + light_green('list unsub') + \
1312 ' will unsubscribe you from a specific list.\n'
1313 usage
+= s
* 2 + light_green('list own') + \
1314 ' will show all list owned by you.\n'
1315 usage
+= s
* 2 + light_green('list new') + \
1316 ' will create a new list.\n'
1317 usage
+= s
* 2 + light_green('list update') + \
1318 ' will update a list owned by you.\n'
1319 usage
+= s
* 2 + light_green('list del') + \
1320 ' will delete a list owned by you.\n'
1331 usage
+= s
+ grey(u
'\u266A' + ' Switching streams \n')
1332 usage
+= s
* 2 + light_green('switch public #AKB') + \
1333 ' will switch to public stream and follow "' + \
1334 light_yellow('AKB') + '" keyword.\n'
1335 usage
+= s
* 2 + light_green('switch mine') + \
1336 ' will switch to your personal stream.\n'
1337 usage
+= s
* 2 + light_green('switch mine -f ') + \
1338 ' will prompt to enter the filter.\n'
1339 usage
+= s
* 3 + light_yellow('Only nicks') + \
1340 ' filter will decide nicks will be INCLUDE ONLY.\n'
1341 usage
+= s
* 3 + light_yellow('Ignore nicks') + \
1342 ' filter will decide nicks will be EXCLUDE.\n'
1343 usage
+= s
* 2 + light_green('switch mine -d') + \
1344 ' will use the config\'s ONLY_LIST and IGNORE_LIST.\n'
1353 h
, w
= os
.popen('stty size', 'r').read().split()
1356 usage
+= s
+ 'Hi boss! I\'m ready to serve you right now!\n'
1357 usage
+= s
+ '-' * (int(w
) - 4) + '\n'
1358 usage
+= s
+ 'You are ' + \
1359 light_yellow('already') + ' on your personal stream.\n'
1360 usage
+= s
+ 'Any update from Twitter will show up ' + \
1361 light_yellow('immediately') + '.\n'
1362 usage
+= s
+ 'In addtion, following commands are available right now:\n'
1363 # Twitter help section
1365 usage
+= s
+ grey(u
'\u266A' + ' Twitter help\n')
1366 usage
+= s
* 2 + light_green('h discover') + \
1367 ' will show help for discover commands.\n'
1368 usage
+= s
* 2 + light_green('h tweets') + \
1369 ' will show help for tweets commands.\n'
1370 usage
+= s
* 2 + light_green('h messages') + \
1371 ' will show help for messages commands.\n'
1372 usage
+= s
* 2 + light_green('h friends_and_followers') + \
1373 ' will show help for friends and followers commands.\n'
1374 usage
+= s
* 2 + light_green('h list') + \
1375 ' will show help for list commands.\n'
1376 usage
+= s
* 2 + light_green('h stream') + \
1377 ' will show help for stream commands.\n'
1380 usage
+= s
+ grey(u
'\u266A' + ' Smart shell\n')
1381 usage
+= s
* 2 + light_green('111111 * 9 / 7') + ' or any math expression ' + \
1382 'will be evaluate by Python interpreter.\n'
1383 usage
+= s
* 2 + 'Even ' + light_green('cal') + ' will show the calendar' + \
1384 ' for current month.\n'
1387 usage
+= s
+ grey(u
'\u266A' + ' Config \n')
1388 usage
+= s
* 2 + light_green('theme') + ' will list available theme. ' + \
1389 light_green('theme monokai') + ' will apply ' + light_yellow('monokai') + \
1390 ' theme immediately.\n'
1391 usage
+= s
* 2 + light_green('config') + ' will list all config.\n'
1393 light_green('config ASCII_ART') + ' will output current value of ' +\
1394 light_yellow('ASCII_ART') + ' config key.\n'
1396 light_green('config ASCII_ART default') + ' will output default value of ' + \
1397 light_yellow('ASCII_ART') + ' config key.\n'
1399 light_green('config ASCII_ART = False') + ' will set value of ' + \
1400 light_yellow('ASCII_ART') + ' config key to ' + \
1401 light_yellow('False') + '.\n'
1404 usage
+= s
+ grey(u
'\u266A' + ' Screening \n')
1405 usage
+= s
* 2 + light_green('h') + ' will show this help again.\n'
1406 usage
+= s
* 2 + light_green('c') + ' will clear the screen.\n'
1407 usage
+= s
* 2 + light_green('q') + ' will quit.\n'
1410 usage
+= s
+ '-' * (int(w
) - 4) + '\n'
1411 usage
+= s
+ 'Have fun and hang tight! \n'
1414 'discover': help_discover
,
1415 'tweets': help_tweets
,
1416 'messages': help_messages
,
1417 'friends_and_followers': help_friends_and_followers
,
1419 'stream': help_stream
,
1422 d
[g
['stuff'].strip()]()
1439 os
.system('rm -rf rainbow.db')
1440 os
.kill(g
['stream_pid'], signal
.SIGKILL
)
1446 Reset prefix of line
1449 printNicely(magenta('Need tips ? Type "h" and hit Enter key!'))
1452 printNicely(str(eval(g
['cmd'])))
1507 Listen to user's input
1512 ['public', 'mine'], # switch
1527 ['image'], # show image
1529 ['fl', 'fr'], # list
1556 [key
for key
in dict(get_all_config())], # config
1557 g
['themes'] + ['current_as_default'], # theme
1562 'friends_and_followers',
1570 init_interactive_shell(d
)
1575 line
= raw_input(g
['decorated_name'])
1579 cmd
= line
.split()[0]
1584 # Lock the semaphore
1585 db
.semaphore_update(True)
1586 # Save cmd to global variable and call process
1587 g
['stuff'] = ' '.join(line
.split()[1:])
1588 # Process the command
1591 if cmd
in ['switch', 't', 'rt', 'rep']:
1595 # Release the semaphore lock
1596 db
.semaphore_update(False)
1598 printNicely(red('OMG something is wrong with Twitter right now.'))
1601 def stream(domain
, args
, name
='Rainbow Stream'):
1607 c
['USER_DOMAIN']: name
,
1608 c
['PUBLIC_DOMAIN']: args
.track_keywords
,
1609 c
['SITE_DOMAIN']: name
,
1612 ascii_art(art_dict
[domain
])
1613 # These arguments are optional:
1615 timeout
=args
.timeout
,
1616 block
=not args
.no_block
,
1617 heartbeat_timeout
=args
.heartbeat_timeout
)
1620 if args
.track_keywords
:
1621 query_args
['track'] = args
.track_keywords
1623 stream
= TwitterStream(
1628 if domain
== c
['USER_DOMAIN']:
1629 tweet_iter
= stream
.user(**query_args
)
1630 elif domain
== c
['SITE_DOMAIN']:
1631 tweet_iter
= stream
.site(**query_args
)
1633 if args
.track_keywords
:
1634 tweet_iter
= stream
.statuses
.filter(**query_args
)
1636 tweet_iter
= stream
.statuses
.sample()
1637 for tweet
in tweet_iter
:
1639 printNicely("-- None --")
1640 elif tweet
is Timeout
:
1641 printNicely("-- Timeout --")
1642 elif tweet
is HeartbeatTimeout
:
1643 printNicely("-- Heartbeat Timeout --")
1644 elif tweet
is Hangup
:
1645 printNicely("-- Hangup --")
1646 elif tweet
.get('text'):
1649 iot
=args
.image_on_term
,
1650 keyword
=args
.track_keywords
,
1651 check_semaphore
=True,
1655 except TwitterHTTPError
:
1658 magenta("We have maximum connection problem with twitter'stream API right now :("))
1666 args
= parse_arguments()
1669 except TwitterHTTPError
:
1672 magenta("Something wrong with Twitter Oauth right now :("))
1674 magenta("Please delete ~/.rainbow_oauth and try again."))
1676 os
.system('rm -rf rainbow.db')
1678 # Spawn stream process
1684 g
['original_name']))
1686 # Start listen process
1690 g
['stream_pid'] = p
.pid
1691 g
['iot'] = args
.image_on_term