2 Colorful user's timeline stream
4 from multiprocessing
import Process
16 from twitter
.stream
import TwitterStream
, Timeout
, HeartbeatTimeout
, Hangup
17 from twitter
.api
import *
18 from twitter
.oauth
import OAuth
, read_token_file
19 from twitter
.oauth_dance
import oauth_dance
20 from twitter
.util
import printNicely
25 from .consumer
import *
26 from .interactive
import *
28 from .c_image
import *
29 from .py3patch
import *
75 def parse_arguments():
79 parser
= argparse
.ArgumentParser(description
=__doc__
or "")
83 help='Timeout for the stream (seconds).')
86 '--heartbeat-timeout',
87 help='Set heartbeat timeout.',
93 help='Set stream to non-blocking.')
97 help='Search the stream for specific text.')
101 help='Filter specific screen_name.')
105 help='Ignore specific screen_name.')
110 help='Display all image on terminal.')
111 return parser
.parse_args()
116 Authenticate with Twitter OAuth
118 # When using rainbow stream you must authorize.
119 twitter_credential
= os
.environ
.get(
123 '')) + os
.sep
+ '.rainbow_oauth'
124 if not os
.path
.exists(twitter_credential
):
125 oauth_dance("Rainbow Stream",
129 oauth_token
, oauth_token_secret
= read_token_file(twitter_credential
)
137 def get_decorated_name():
142 t
= Twitter(auth
=authen())
143 name
= '@' + t
.account
.verify_credentials()['screen_name']
144 g
['original_name'] = name
[1:]
145 g
['decorated_name'] = color_func(c
['DECORATED_NAME'])('[' + name
+ ']: ')
148 files
= os
.listdir(os
.path
.dirname(__file__
) + '/colorset')
149 themes
= [f
.split('.')[0] for f
in files
if f
.split('.')[-1] == 'json']
151 db
.theme_store(c
['THEME'])
154 db
.semaphore_store(False)
162 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.'))
180 if target
== 'public':
181 keyword
= g
['stuff'].split()[1]
182 if keyword
[0] == '#':
183 keyword
= keyword
[1:]
185 os
.kill(g
['stream_pid'], signal
.SIGKILL
)
186 args
.track_keywords
= keyword
194 g
['stream_pid'] = p
.pid
197 elif target
== 'mine':
199 os
.kill(g
['stream_pid'], signal
.SIGKILL
)
208 g
['stream_pid'] = p
.pid
211 printNicely(cyan('Only: ' + str(args
.filter)))
213 printNicely(red('Ignore: ' + str(args
.ignore
)))
216 printNicely(red('Sorry I can\'t understand.'))
223 t
= Twitter(auth
=authen())
224 # Get country and town
226 country
= g
['stuff'].split()[0]
230 town
= g
['stuff'].split()[1]
234 avail
= t
.trends
.available()
237 trends
= t
.trends
.place(_id
=1)[0]['trends']
240 for location
in avail
:
241 # Search for country and Town
243 if location
['countryCode'] == country \
244 and location
['placeType']['name'] == 'Town' \
245 and location
['name'] == town
:
246 trends
= t
.trends
.place(_id
=location
['woeid'])[0]['trends']
248 # Search for country only
250 if location
['countryCode'] == country \
251 and location
['placeType']['name'] == 'Country':
252 trends
= t
.trends
.place(_id
=location
['woeid'])[0]['trends']
260 t
= Twitter(auth
=authen())
261 num
= c
['HOME_TWEET_NUM']
262 if g
['stuff'].isdigit():
263 num
= int(g
['stuff'])
264 for tweet
in reversed(t
.statuses
.home_timeline(count
=num
)):
265 draw(t
=tweet
, iot
=g
['iot'])
273 t
= Twitter(auth
=authen())
274 user
= g
['stuff'].split()[0]
277 num
= int(g
['stuff'].split()[1])
279 num
= c
['HOME_TWEET_NUM']
280 for tweet
in reversed(t
.statuses
.user_timeline(count
=num
, screen_name
=user
[1:])):
281 draw(t
=tweet
, iot
=g
['iot'])
284 printNicely(red('A name should begin with a \'@\''))
291 t
= Twitter(auth
=authen())
292 num
= c
['HOME_TWEET_NUM']
293 if g
['stuff'].isdigit():
294 num
= int(g
['stuff'])
295 for tweet
in reversed(t
.statuses
.mentions_timeline(count
=num
)):
296 draw(t
=tweet
, iot
=g
['iot'])
304 t
= Twitter(auth
=authen())
305 t
.statuses
.update(status
=g
['stuff'])
312 t
= Twitter(auth
=authen())
314 id = int(g
['stuff'].split()[0])
316 printNicely(red('Sorry I can\'t understand.'))
318 tid
= db
.rainbow_to_tweet_query(id)[0].tweet_id
319 t
.statuses
.retweet(id=tid
, include_entities
=False, trim_user
=True)
326 t
= Twitter(auth
=authen())
328 id = int(g
['stuff'].split()[0])
330 printNicely(red('Sorry I can\'t understand.'))
332 tid
= db
.rainbow_to_tweet_query(id)[0].tweet_id
333 tweet
= t
.statuses
.show(id=tid
)
334 screen_name
= tweet
['user']['screen_name']
336 quote
= '\"@' + screen_name
+ ': ' + text
+ '\"'
337 quote
= quote
.encode('utf8')
338 notice
= light_magenta('Compose mode ')
339 notice
+= light_yellow('(Enter nothing will cancel the quote)')
340 notice
+= light_magenta(':')
342 extra
= raw_input(quote
)
344 t
.statuses
.update(status
=quote
+ extra
)
346 printNicely(light_magenta('No text added.'))
353 t
= Twitter(auth
=authen())
356 id = int(g
['stuff'].split()[0])
358 printNicely(red('Sorry I can\'t understand.'))
360 tid
= db
.rainbow_to_tweet_query(id)[0].tweet_id
361 # Get display num if exist
363 num
= int(g
['stuff'].split()[1])
365 num
= c
['RETWEETS_SHOW_NUM']
366 # Get result and display
367 rt_ary
= t
.statuses
.retweets(id=tid
, count
=num
)
369 printNicely(magenta('This tweet has no retweet.'))
371 for tweet
in reversed(rt_ary
):
372 draw(t
=tweet
, iot
=g
['iot'])
380 t
= Twitter(auth
=authen())
382 id = int(g
['stuff'].split()[0])
384 printNicely(red('Sorry I can\'t understand.'))
386 tid
= db
.rainbow_to_tweet_query(id)[0].tweet_id
387 t
.favorites
.create(_id
=tid
, include_entities
=False)
388 printNicely(green('Favorited.'))
389 draw(t
.statuses
.show(id=tid
), iot
=g
['iot'])
397 t
= Twitter(auth
=authen())
399 id = int(g
['stuff'].split()[0])
401 printNicely(red('Sorry I can\'t understand.'))
403 tid
= db
.rainbow_to_tweet_query(id)[0].tweet_id
404 user
= t
.statuses
.show(id=tid
)['user']['screen_name']
405 status
= ' '.join(g
['stuff'].split()[1:])
406 status
= '@' + user
+ ' ' + status
.decode('utf-8')
407 t
.statuses
.update(status
=status
, in_reply_to_status_id
=tid
)
414 t
= Twitter(auth
=authen())
416 rid
= int(g
['stuff'].split()[0])
418 printNicely(red('Sorry I can\'t understand.'))
420 tid
= db
.rainbow_to_tweet_query(rid
)[0].tweet_id
421 t
.statuses
.destroy(id=tid
)
422 printNicely(green('Okay it\'s gone.'))
429 t
= Twitter(auth
=authen())
431 id = int(g
['stuff'].split()[0])
433 printNicely(red('Sorry I can\'t understand.'))
435 tid
= db
.rainbow_to_tweet_query(id)[0].tweet_id
436 t
.favorites
.destroy(_id
=tid
)
437 printNicely(green('Okay it\'s unfavorited.'))
438 draw(t
.statuses
.show(id=tid
), iot
=g
['iot'])
446 t
= Twitter(auth
=authen())
447 g
['stuff'] = g
['stuff'].strip()
448 rel
= t
.search
.tweets(q
=g
['stuff'])['statuses']
450 printNicely('Newest tweets:')
451 for i
in reversed(xrange(c
['SEARCH_MAX_RECORD'])):
457 printNicely(magenta('I\'m afraid there is no result'))
462 Send a direct message
464 t
= Twitter(auth
=authen())
465 user
= g
['stuff'].split()[0]
466 if user
[0].startswith('@'):
468 content
= g
['stuff'].split()[1]
470 printNicely(red('Sorry I can\'t understand.'))
471 t
.direct_messages
.new(
472 screen_name
=user
[1:],
475 printNicely(green('Message sent.'))
477 printNicely(red('A name should begin with a \'@\''))
484 t
= Twitter(auth
=authen())
486 target
= g
['stuff'].split()[0]
487 if target
!= 'image':
489 id = int(g
['stuff'].split()[1])
490 tid
= db
.rainbow_to_tweet_query(id)[0].tweet_id
491 tweet
= t
.statuses
.show(id=tid
)
492 media
= tweet
['entities']['media']
494 res
= requests
.get(m
['media_url'])
495 img
= Image
.open(BytesIO(res
.content
))
498 printNicely(red('Sorry I can\'t show this image.'))
505 t
= Twitter(auth
=authen())
507 if not g
['stuff'].isdigit():
509 tid
= db
.rainbow_to_tweet_query(g
['stuff'])[0].tweet_id
510 tweet
= t
.statuses
.show(id=tid
)
512 u
for u
in tweet
['text'].split() if u
.startswith('http://')]
514 printNicely(light_magenta('No url here @.@!'))
516 for link
in link_ary
:
517 webbrowser
.open(link
)
519 printNicely(red('Sorry I can\'t open url in this tweet.'))
524 List friends for followers
526 t
= Twitter(auth
=authen())
529 name
= g
['stuff'].split()[1]
530 if name
.startswith('@'):
533 printNicely(red('A name should begin with a \'@\''))
534 raise Exception('Invalid name')
536 name
= g
['original_name']
537 # Get list followers or friends
539 target
= g
['stuff'].split()[0]
541 printNicely(red('Omg some syntax is wrong.'))
543 d
= {'fl': 'followers', 'fr': 'friends'}
547 while next_cursor
!= 0:
548 list = getattr(t
, d
[target
]).list(
552 include_entities
=False,
554 for u
in list['users']:
555 rel
[u
['name']] = '@' + u
['screen_name']
556 next_cursor
= list['next_cursor']
558 printNicely('All: ' + str(len(rel
)) + ' ' + d
[target
] + '.')
560 user
= ' ' + cycle_color(name
)
561 user
+= color_func(c
['TWEET']['nick'])(' ' + rel
[name
] + ' ')
567 Inbox direct messages
569 t
= Twitter(auth
=authen())
570 num
= c
['MESSAGES_DISPLAY']
572 if g
['stuff'].isdigit():
575 # Max message per page is 20 so we have to loop
577 rel
= rel
+ t
.direct_messages(
580 include_entities
=False,
585 rel
= rel
+ t
.direct_messages(
588 include_entities
=False,
592 printNicely('Inbox: newest ' + str(len(rel
)) + ' messages.')
593 for m
in reversed(rel
):
602 t
= Twitter(auth
=authen())
603 num
= c
['MESSAGES_DISPLAY']
605 if g
['stuff'].isdigit():
606 num
= int(g
['stuff'])
608 # Max message per page is 20 so we have to loop
610 rel
= rel
+ t
.direct_messages
.sent(
613 include_entities
=False,
618 rel
= rel
+ t
.direct_messages
.sent(
621 include_entities
=False,
625 printNicely('Sent: newest ' + str(len(rel
)) + ' messages.')
626 for m
in reversed(rel
):
635 t
= Twitter(auth
=authen())
637 rid
= int(g
['stuff'].split()[0])
639 printNicely(red('Sorry I can\'t understand.'))
640 mid
= db
.rainbow_to_message_query(rid
)[0].message_id
641 t
.direct_messages
.destroy(id=mid
)
642 printNicely(green('Message deleted.'))
647 Show profile of a specific user
649 t
= Twitter(auth
=authen())
650 screen_name
= g
['stuff'].split()[0]
651 if screen_name
.startswith('@'):
654 screen_name
=screen_name
[1:],
655 include_entities
=False)
656 show_profile(user
, g
['iot'])
658 printNicely(red('Omg no user.'))
660 printNicely(red('A name should begin with a \'@\''))
667 t
= Twitter(auth
=authen())
668 screen_name
= g
['stuff'].split()[0]
669 if screen_name
.startswith('@'):
670 t
.friendships
.create(screen_name
=screen_name
[1:], follow
=True)
671 printNicely(green('You are following ' + screen_name
+ ' now!'))
673 printNicely(red('A name should begin with a \'@\''))
680 t
= Twitter(auth
=authen())
681 screen_name
= g
['stuff'].split()[0]
682 if screen_name
.startswith('@'):
683 t
.friendships
.destroy(
684 screen_name
=screen_name
[1:],
685 include_entities
=False)
686 printNicely(green('Unfollow ' + screen_name
+ ' success!'))
688 printNicely(red('A name should begin with a \'@\''))
695 t
= Twitter(auth
=authen())
697 screen_name
= g
['stuff'].split()[0]
699 printNicely(red('A name should be specified. '))
701 if screen_name
.startswith('@'):
702 rel
= t
.mutes
.users
.create(screen_name
=screen_name
[1:])
703 if isinstance(rel
, dict):
704 printNicely(green(screen_name
+ ' is muted.'))
706 printNicely(red(rel
))
708 printNicely(red('A name should begin with a \'@\''))
715 t
= Twitter(auth
=authen())
717 screen_name
= g
['stuff'].split()[0]
719 printNicely(red('A name should be specified. '))
721 if screen_name
.startswith('@'):
722 rel
= t
.mutes
.users
.destroy(screen_name
=screen_name
[1:])
723 if isinstance(rel
, dict):
724 printNicely(green(screen_name
+ ' is unmuted.'))
726 printNicely(red(rel
))
728 printNicely(red('A name should begin with a \'@\''))
735 t
= Twitter(auth
=authen())
740 while next_cursor
!= 0:
741 list = t
.mutes
.users
.list(
742 screen_name
=g
['original_name'],
745 include_entities
=False,
747 for u
in list['users']:
748 rel
[u
['name']] = '@' + u
['screen_name']
749 next_cursor
= list['next_cursor']
751 printNicely('All: ' + str(len(rel
)) + ' people.')
753 user
= ' ' + cycle_color(name
)
754 user
+= color_func(c
['TWEET']['nick'])(' ' + rel
[name
] + ' ')
762 t
= Twitter(auth
=authen())
763 screen_name
= g
['stuff'].split()[0]
764 if screen_name
.startswith('@'):
766 screen_name
=screen_name
[1:],
767 include_entities
=False,
769 printNicely(green('You blocked ' + screen_name
+ '.'))
771 printNicely(red('A name should begin with a \'@\''))
778 t
= Twitter(auth
=authen())
779 screen_name
= g
['stuff'].split()[0]
780 if screen_name
.startswith('@'):
782 screen_name
=screen_name
[1:],
783 include_entities
=False,
785 printNicely(green('Unblock ' + screen_name
+ ' success!'))
787 printNicely(red('A name should begin with a \'@\''))
792 Report a user as a spam account
794 t
= Twitter(auth
=authen())
795 screen_name
= g
['stuff'].split()[0]
796 if screen_name
.startswith('@'):
798 screen_name
=screen_name
[1:])
799 printNicely(green('You reported ' + screen_name
+ '.'))
801 printNicely(red('Sorry I can\'t understand.'))
809 list_name
= raw_input(light_magenta('Give me the list\'s name: '))
810 # Get list name and owner
812 owner
, slug
= list_name
.split('/')
813 if slug
.startswith('@'):
817 printNicely(light_magenta('List name should follow "@owner/list_name" format.'))
818 raise Exception('Wrong list name')
825 rel
= t
.lists
.list(screen_name
=g
['original_name'])
829 printNicely(light_magenta('You belong to no lists :)'))
836 owner
, slug
= get_slug()
837 res
= t
.lists
.statuses(
839 owner_screen_name
=owner
,
841 include_entities
=False)
851 owner
, slug
= get_slug()
855 while next_cursor
!= 0:
858 owner_screen_name
=owner
,
860 include_entities
=False)
862 rel
[u
['name']] = '@' + u
['screen_name']
863 next_cursor
= m
['next_cursor']
864 printNicely('All: ' + str(len(rel
)) + ' members.')
866 user
= ' ' + cycle_color(name
)
867 user
+= color_func(c
['TWEET']['nick'])(' ' + rel
[name
] + ' ')
871 def list_subscribers(t
):
875 owner
, slug
= get_slug()
879 while next_cursor
!= 0:
880 m
= t
.lists
.subscribers(
882 owner_screen_name
=owner
,
884 include_entities
=False)
886 rel
[u
['name']] = '@' + u
['screen_name']
887 next_cursor
= m
['next_cursor']
888 printNicely('All: ' + str(len(rel
)) + ' subscribers.')
890 user
= ' ' + cycle_color(name
)
891 user
+= color_func(c
['TWEET']['nick'])(' ' + rel
[name
] + ' ')
897 Add specific user to a list
899 owner
, slug
= get_slug()
901 user_name
= raw_input(light_magenta('Give me name of the newbie: '))
902 if user_name
.startswith('@'):
903 user_name
= user_name
[1:]
905 t
.lists
.members
.create(
907 owner_screen_name
=owner
,
908 screen_name
=user_name
)
909 printNicely(light_green('Added.'))
911 printNicely(light_magenta('I\'m sorry we can not add him/her.'))
916 Remove specific user from a list
918 owner
, slug
= get_slug()
920 user_name
= raw_input(light_magenta('Give me name of the unlucky one: '))
921 if user_name
.startswith('@'):
922 user_name
= user_name
[1:]
924 t
.lists
.members
.destroy(
926 owner_screen_name
=owner
,
927 screen_name
=user_name
)
928 printNicely(light_green('Gone.'))
930 printNicely(light_magenta('I\'m sorry we can not remove him/her.'))
933 def list_subscribe(t
):
937 owner
, slug
= get_slug()
940 t
.lists
.subscribers
.create(
942 owner_screen_name
=owner
)
943 printNicely(light_green('Done.'))
946 light_magenta('I\'m sorry you can not subscribe to this list.'))
949 def list_unsubscribe(t
):
953 owner
, slug
= get_slug()
956 t
.lists
.subscribers
.destroy(
958 owner_screen_name
=owner
)
959 printNicely(light_green('Done.'))
962 light_magenta('I\'m sorry you can not unsubscribe to this list.'))
971 while next_cursor
!= 0:
972 res
= t
.lists
.ownerships(
973 screen_name
=g
['original_name'],
976 next_cursor
= res
['next_cursor']
980 printNicely(light_magenta('You own no lists :)'))
987 name
= raw_input(light_magenta('New list\'s name: '))
988 mode
= raw_input(light_magenta('New list\'s mode (public/private): '))
989 description
= raw_input(light_magenta('New list\'s description: '))
994 description
=description
)
995 printNicely(light_green(name
+ ' list is created.'))
997 printNicely(red('Oops something is wrong with Twitter :('))
1004 slug
= raw_input(light_magenta('Your list that you want to update: '))
1005 name
= raw_input(light_magenta('Update name (leave blank to unchange): '))
1006 mode
= raw_input(light_magenta('Update mode (public/private): '))
1007 description
= raw_input(light_magenta('Update description: '))
1011 slug
='-'.join(slug
.split()),
1012 owner_screen_name
=g
['original_name'],
1015 description
=description
)
1019 owner_screen_name
=g
['original_name'],
1021 description
=description
)
1022 printNicely(light_green(slug
+ ' list is updated.'))
1024 printNicely(red('Oops something is wrong with Twitter :('))
1031 slug
= raw_input(light_magenta('Your list that you want to update: '))
1034 slug
='-'.join(slug
.split()),
1035 owner_screen_name
=g
['original_name'])
1036 printNicely(light_green(slug
+ ' list is deleted.'))
1038 printNicely(red('Oops something is wrong with Twitter :('))
1045 t
= Twitter(auth
=authen())
1046 # List all lists or base on action
1048 g
['list_action'] = g
['stuff'].split()[0]
1055 'all_mem': list_members
,
1056 'all_sub': list_subscribers
,
1059 'sub': list_subscribe
,
1060 'unsub': list_unsubscribe
,
1063 'update': list_update
,
1067 return action_ary
[g
['list_action']](t
)
1069 printNicely(red('Please try again.'))
1074 Unix's command `cal`
1077 rel
= os
.popen('cal').read().split('\n')
1080 show_calendar(month
, date
, rel
)
1085 Browse and change config
1087 all_config
= get_all_config()
1088 g
['stuff'] = g
['stuff'].strip()
1091 for k
in all_config
:
1092 line
= ' '*2 + light_green(k
) + ': ' + light_yellow(str(all_config
[k
]))
1094 guide
= 'Detailed explanation can be found at ' + \
1095 color_func(c
['TWEET']['link'])('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
:
1101 line
= ' '*2 + light_green(k
) + ': ' + light_yellow(str(all_config
[k
]))
1104 printNicely(red('No config key like this.'))
1105 # Print specific config's default value
1106 elif len(g
['stuff'].split()) == 2 and g
['stuff'].split()[-1] == 'default':
1107 key
= g
['stuff'].split()[0]
1108 value
= get_default_config(key
)
1109 line
= ' '*2 + light_green(key
) + ': ' + light_magenta(value
)
1111 # Set specific config
1112 elif len(g
['stuff'].split()) == 3 and g
['stuff'].split()[1] == '=' :
1113 key
= g
['stuff'].split()[0]
1114 value
= g
['stuff'].split()[-1]
1116 set_config(key
,value
)
1117 printNicely(light_green('Updated successfully.'))
1119 printNicely(light_magenta('Not valid value.'))
1123 printNicely(light_magenta('Sorry I can\'s understand.'))
1128 List and change theme
1132 for theme
in g
['themes']:
1133 line
= light_magenta(theme
)
1134 if c
['THEME'] == theme
:
1135 line
= ' ' * 2 + light_yellow('* ') + line
1137 line
= ' ' * 4 + line
1139 elif g
['stuff'] == 'current_as_default':
1141 set_config('THEME',c
['THEME'])
1142 printNicely(light_green('Okay it will be applied from next time :)'))
1147 if g
['stuff'] != 'custom':
1148 new_config
= os
.path
.dirname(
1149 __file__
) + '/colorset/' + g
['stuff'] + '.json'
1151 new_config
= os
.environ
.get(
1152 'HOME', os
.environ
.get(
1154 '')) + os
.sep
+ '.rainbow_config.json'
1155 new_config
= load_config(new_config
)
1157 for nc
in new_config
:
1158 c
[nc
] = new_config
[nc
]
1159 # Update db and reset colors
1160 db
.theme_update(g
['stuff'])
1161 c
['THEME'] = g
['stuff']
1163 g
['decorated_name'] = color_func(
1164 c
['DECORATED_NAME'])(
1165 '[@' + g
['original_name'] + ']: ')
1166 printNicely(green('Theme changed.'))
1168 printNicely(red('No such theme exists.'))
1171 def help_discover():
1176 # Discover the world
1178 usage
+= s
+ grey(u
'\u266A' + ' Discover the world \n')
1179 usage
+= s
* 2 + light_green('trend') + ' will show global trending topics. ' + \
1180 'You can try ' + light_green('trend US') + ' or ' + \
1181 light_green('trend JP Tokyo') + '.\n'
1182 usage
+= s
* 2 + light_green('home') + ' will show your timeline. ' + \
1183 light_green('home 7') + ' will show 7 tweets.\n'
1184 usage
+= s
* 2 + light_green('mentions') + ' will show mentions timeline. ' + \
1185 light_green('mentions 7') + ' will show 7 mention tweets.\n'
1186 usage
+= s
* 2 + light_green('whois @mdo') + ' will show profile of ' + \
1187 magenta('@mdo') + '.\n'
1188 usage
+= s
* 2 + light_green('view @mdo') + \
1189 ' will show ' + magenta('@mdo') + '\'s home.\n'
1190 usage
+= s
* 2 + light_green('s AKB48') + ' will search for "' + \
1191 light_yellow('AKB48') + '" and return 5 newest tweet. ' + \
1192 'Search can be performed with or without hashtag.\n'
1203 usage
+= s
+ grey(u
'\u266A' + ' Tweets \n')
1204 usage
+= s
* 2 + light_green('t oops ') + \
1205 'will tweet "' + light_yellow('oops') + '" immediately.\n'
1207 light_green('rt 12 ') + ' will retweet to tweet with ' + \
1208 light_yellow('[id=12]') + '.\n'
1210 light_green('quote 12 ') + ' will quote the tweet with ' + \
1211 light_yellow('[id=12]') + '. If no extra text is added, ' + \
1212 'the quote will be canceled.\n'
1214 light_green('allrt 12 20 ') + ' will list 20 newest retweet of the tweet with ' + \
1215 light_yellow('[id=12]') + '.\n'
1216 usage
+= s
* 2 + light_green('rep 12 oops') + ' will reply "' + \
1217 light_yellow('oops') + '" to tweet with ' + \
1218 light_yellow('[id=12]') + '.\n'
1220 light_green('fav 12 ') + ' will favorite the tweet with ' + \
1221 light_yellow('[id=12]') + '.\n'
1223 light_green('ufav 12 ') + ' will unfavorite tweet with ' + \
1224 light_yellow('[id=12]') + '.\n'
1226 light_green('del 12 ') + ' will delete tweet with ' + \
1227 light_yellow('[id=12]') + '.\n'
1228 usage
+= s
* 2 + light_green('show image 12') + ' will show image in tweet with ' + \
1229 light_yellow('[id=12]') + ' in your OS\'s image viewer.\n'
1230 usage
+= s
* 2 + light_green('open 12') + ' will open url in tweet with ' + \
1231 light_yellow('[id=12]') + ' in your OS\'s default browser.\n'
1235 def help_messages():
1242 usage
+= s
+ grey(u
'\u266A' + ' Direct messages \n')
1243 usage
+= s
* 2 + light_green('inbox') + ' will show inbox messages. ' + \
1244 light_green('inbox 7') + ' will show newest 7 messages.\n'
1245 usage
+= s
* 2 + light_green('sent') + ' will show sent messages. ' + \
1246 light_green('sent 7') + ' will show newest 7 messages.\n'
1247 usage
+= s
* 2 + light_green('mes @dtvd88 hi') + ' will send a "hi" messege to ' + \
1248 magenta('@dtvd88') + '.\n'
1249 usage
+= s
* 2 + light_green('trash 5') + ' will remove message with ' + \
1250 light_yellow('[message_id=5]') + '.\n'
1254 def help_friends_and_followers():
1256 Friends and Followers
1259 # Follower and following
1261 usage
+= s
+ grey(u
'\u266A' + ' Friends and followers \n')
1263 light_green('ls fl') + \
1264 ' will list all followers (people who are following you).\n'
1266 light_green('ls fr') + \
1267 ' will list all friends (people who you are following).\n'
1268 usage
+= s
* 2 + light_green('fl @dtvd88') + ' will follow ' + \
1269 magenta('@dtvd88') + '.\n'
1270 usage
+= s
* 2 + light_green('ufl @dtvd88') + ' will unfollow ' + \
1271 magenta('@dtvd88') + '.\n'
1272 usage
+= s
* 2 + light_green('mute @dtvd88') + ' will mute ' + \
1273 magenta('@dtvd88') + '.\n'
1274 usage
+= s
* 2 + light_green('unmute @dtvd88') + ' will unmute ' + \
1275 magenta('@dtvd88') + '.\n'
1276 usage
+= s
* 2 + light_green('muting') + ' will list muting users.\n'
1277 usage
+= s
* 2 + light_green('block @dtvd88') + ' will block ' + \
1278 magenta('@dtvd88') + '.\n'
1279 usage
+= s
* 2 + light_green('unblock @dtvd88') + ' will unblock ' + \
1280 magenta('@dtvd88') + '.\n'
1281 usage
+= s
* 2 + light_green('report @dtvd88') + ' will report ' + \
1282 magenta('@dtvd88') + ' as a spam account.\n'
1293 usage
+= s
+ grey(u
'\u266A' + ' Twitter list\n')
1294 usage
+= s
* 2 + light_green('list') + \
1295 ' will show all lists you are belong to.\n'
1296 usage
+= s
* 2 + light_green('list home') + \
1297 ' will show timeline of list. You will be asked for list\'s name.\n'
1298 usage
+= s
* 2 + light_green('list all_mem') + \
1299 ' will show list\'s all members.\n'
1300 usage
+= s
* 2 + light_green('list all_sub') + \
1301 ' will show list\'s all subscribers.\n'
1302 usage
+= s
* 2 + light_green('list add') + \
1303 ' will add specific person to a list owned by you.' + \
1304 ' You will be asked for list\'s name and person\'s name.\n'
1305 usage
+= s
* 2 + light_green('list rm') + \
1306 ' will remove specific person from a list owned by you.' + \
1307 ' You will be asked for list\'s name and person\'s name.\n'
1308 usage
+= s
* 2 + light_green('list sub') + \
1309 ' will subscribe you to a specific list.\n'
1310 usage
+= s
* 2 + light_green('list unsub') + \
1311 ' will unsubscribe you from a specific list.\n'
1312 usage
+= s
* 2 + light_green('list own') + \
1313 ' will show all list owned by you.\n'
1314 usage
+= s
* 2 + light_green('list new') + \
1315 ' will create a new list.\n'
1316 usage
+= s
* 2 + light_green('list update') + \
1317 ' will update a list owned by you.\n'
1318 usage
+= s
* 2 + light_green('list del') + \
1319 ' will delete a list owned by you.\n'
1330 usage
+= s
+ grey(u
'\u266A' + ' Switching streams \n')
1331 usage
+= s
* 2 + light_green('switch public #AKB') + \
1332 ' will switch to public stream and follow "' + \
1333 light_yellow('AKB') + '" keyword.\n'
1334 usage
+= s
* 2 + light_green('switch mine') + \
1335 ' will switch to your personal stream.\n'
1336 usage
+= s
* 2 + light_green('switch mine -f ') + \
1337 ' will prompt to enter the filter.\n'
1338 usage
+= s
* 3 + light_yellow('Only nicks') + \
1339 ' filter will decide nicks will be INCLUDE ONLY.\n'
1340 usage
+= s
* 3 + light_yellow('Ignore nicks') + \
1341 ' filter will decide nicks will be EXCLUDE.\n'
1342 usage
+= s
* 2 + light_green('switch mine -d') + \
1343 ' will use the config\'s ONLY_LIST and IGNORE_LIST.\n'
1352 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'
1364 # Twitter help section
1366 usage
+= s
+ grey(u
'\u266A' + ' Twitter help\n')
1367 usage
+= s
* 2 + light_green('h discover') + \
1368 ' will show help for discover commands.\n'
1369 usage
+= s
* 2 + light_green('h tweets') + \
1370 ' will show help for tweets commands.\n'
1371 usage
+= s
* 2 + light_green('h messages') + \
1372 ' will show help for messages commands.\n'
1373 usage
+= s
* 2 + light_green('h friends_and_followers') + \
1374 ' will show help for friends and followers commands.\n'
1375 usage
+= s
* 2 + light_green('h list') + \
1376 ' will show help for list commands.\n'
1377 usage
+= s
* 2 + light_green('h stream') + \
1378 ' will show help for stream commands.\n'
1382 usage
+= s
+ grey(u
'\u266A' + ' Smart shell\n')
1383 usage
+= s
* 2 + light_green('111111 * 9 / 7') + ' or any math expression ' + \
1384 'will be evaluate by Python interpreter.\n'
1385 usage
+= s
* 2 + 'Even ' + light_green('cal') + ' will show the calendar' + \
1386 ' for current month.\n'
1390 usage
+= s
+ grey(u
'\u266A' + ' Config \n')
1391 usage
+= s
* 2 + light_green('theme') + ' will list available theme. ' + \
1392 light_green('theme monokai') + ' will apply ' + light_yellow('monokai') + \
1393 ' theme immediately.\n'
1394 usage
+= s
* 2 + light_green('config') + ' will list all config.\n'
1396 light_green('config ASCII_ART') + ' will output current value of ' +\
1397 light_yellow('ASCII_ART') +' config key.\n'
1399 light_green('config ASCII_ART default') + ' will output default value of ' + \
1400 light_yellow('ASCII_ART') +' config key.\n'
1402 light_green('config ASCII_ART = False') + ' will set value of ' + \
1403 light_yellow('ASCII_ART') +' config key to ' + light_yellow('False') + '.\n'
1407 usage
+= s
+ grey(u
'\u266A' + ' Screening \n')
1408 usage
+= s
* 2 + light_green('h') + ' will show this help again.\n'
1409 usage
+= s
* 2 + light_green('c') + ' will clear the screen.\n'
1410 usage
+= s
* 2 + light_green('q') + ' will quit.\n'
1414 usage
+= s
+ '-' * (int(w
) - 4) + '\n'
1415 usage
+= s
+ 'Have fun and hang tight! \n'
1419 'discover': help_discover
,
1420 'tweets': help_tweets
,
1421 'messages': help_messages
,
1422 'friends_and_followers': help_friends_and_followers
,
1424 'stream': help_stream
,
1427 d
[g
['stuff'].strip()]()
1444 os
.system('rm -rf rainbow.db')
1445 os
.kill(g
['stream_pid'], signal
.SIGKILL
)
1451 Reset prefix of line
1454 printNicely(magenta('Need tips ? Type "h" and hit Enter key!'))
1457 printNicely(str(eval(g
['cmd'])))
1512 Listen to user's input
1517 ['public', 'mine'], # switch
1532 ['image'], # show image
1534 ['fl', 'fr'], # list
1561 [key
for key
in dict(get_all_config())], # config
1562 g
['themes'] + ['current_as_default'], # theme
1567 'friends_and_followers',
1575 init_interactive_shell(d
)
1580 line
= raw_input(g
['decorated_name'])
1584 cmd
= line
.split()[0]
1589 # Lock the semaphore
1590 db
.semaphore_update(True)
1591 # Save cmd to global variable and call process
1592 g
['stuff'] = ' '.join(line
.split()[1:])
1593 # Process the command
1596 if cmd
in ['switch', 't', 'rt', 'rep']:
1600 # Release the semaphore lock
1601 db
.semaphore_update(False)
1603 printNicely(red('OMG something is wrong with Twitter right now.'))
1606 def stream(domain
, args
, name
='Rainbow Stream'):
1613 c
['USER_DOMAIN']: name
,
1614 c
['PUBLIC_DOMAIN']: args
.track_keywords
,
1615 c
['SITE_DOMAIN']: name
,
1618 ascii_art(art_dict
[domain
])
1620 # These arguments are optional:
1622 timeout
=args
.timeout
,
1623 block
=not args
.no_block
,
1624 heartbeat_timeout
=args
.heartbeat_timeout
)
1628 if args
.track_keywords
:
1629 query_args
['track'] = args
.track_keywords
1632 stream
= TwitterStream(
1638 if domain
== c
['USER_DOMAIN']:
1639 tweet_iter
= stream
.user(**query_args
)
1640 elif domain
== c
['SITE_DOMAIN']:
1641 tweet_iter
= stream
.site(**query_args
)
1643 if args
.track_keywords
:
1644 tweet_iter
= stream
.statuses
.filter(**query_args
)
1646 tweet_iter
= stream
.statuses
.sample()
1648 # Iterate over the stream.
1649 for tweet
in tweet_iter
:
1651 printNicely("-- None --")
1652 elif tweet
is Timeout
:
1653 printNicely("-- Timeout --")
1654 elif tweet
is HeartbeatTimeout
:
1655 printNicely("-- Heartbeat Timeout --")
1656 elif tweet
is Hangup
:
1657 printNicely("-- Hangup --")
1658 elif tweet
.get('text'):
1661 iot
=args
.image_on_term
,
1662 keyword
=args
.track_keywords
,
1663 check_semaphore
=True,
1667 except TwitterHTTPError
:
1670 magenta("We have maximum connection problem with twitter'stream API right now :("))
1677 # Spawn stream process
1678 args
= parse_arguments()
1680 get_decorated_name()
1682 except TwitterHTTPError
:
1685 magenta("Something wrong with Twitter Oauth right now :("))
1687 magenta("Please delete ~/.rainbow_oauth and try again."))
1689 os
.system('rm -rf rainbow.db')
1697 g
['original_name']))
1700 # Start listen process
1704 g
['stream_pid'] = p
.pid
1705 g
['iot'] = args
.image_on_term