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 *
74 def parse_arguments():
78 parser
= argparse
.ArgumentParser(description
=__doc__
or "")
82 help='Timeout for the stream (seconds).')
85 '--heartbeat-timeout',
86 help='Set heartbeat timeout.',
92 help='Set stream to non-blocking.')
96 help='Search the stream for specific text.')
100 help='Filter specific screen_name.')
104 help='Ignore specific screen_name.')
109 help='Display all image on terminal.')
110 return parser
.parse_args()
115 Authenticate with Twitter OAuth
117 # When using rainbow stream you must authorize.
118 twitter_credential
= os
.environ
.get(
122 '')) + os
.sep
+ '.rainbow_oauth'
123 if not os
.path
.exists(twitter_credential
):
124 oauth_dance("Rainbow Stream",
128 oauth_token
, oauth_token_secret
= read_token_file(twitter_credential
)
136 def get_decorated_name():
141 t
= Twitter(auth
=authen())
142 name
= '@' + t
.account
.verify_credentials()['screen_name']
143 g
['original_name'] = name
[1:]
144 g
['decorated_name'] = color_func(c
['DECORATED_NAME'])('[' + name
+ ']: ')
147 files
= os
.listdir(os
.path
.dirname(__file__
) + '/colorset')
148 themes
= [f
.split('.')[0] for f
in files
if f
.split('.')[-1] == 'json']
150 db
.theme_store(c
['THEME'])
153 db
.semaphore_store(False)
161 target
= g
['stuff'].split()[0]
164 args
= parse_arguments()
166 if g
['stuff'].split()[-1] == '-f':
167 only
= raw_input('Only nicks: ')
168 ignore
= raw_input('Ignore nicks: ')
169 args
.filter = filter(None, only
.split(','))
170 args
.ignore
= filter(None, ignore
.split(','))
171 elif g
['stuff'].split()[-1] == '-d':
172 args
.filter = c
['ONLY_LIST']
173 args
.ignore
= c
['IGNORE_LIST']
175 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
196 elif target
== 'mine':
198 os
.kill(g
['stream_pid'], signal
.SIGKILL
)
207 g
['stream_pid'] = p
.pid
210 printNicely(cyan('Only: ' + str(args
.filter)))
212 printNicely(red('Ignore: ' + str(args
.ignore
)))
215 printNicely(red('Sorry I can\'t understand.'))
222 t
= Twitter(auth
=authen())
223 # Get country and town
225 country
= g
['stuff'].split()[0]
229 town
= g
['stuff'].split()[1]
233 avail
= t
.trends
.available()
236 trends
= t
.trends
.place(_id
=1)[0]['trends']
239 for location
in avail
:
240 # Search for country and Town
242 if location
['countryCode'] == country \
243 and location
['placeType']['name'] == 'Town' \
244 and location
['name'] == town
:
245 trends
= t
.trends
.place(_id
=location
['woeid'])[0]['trends']
247 # Search for country only
249 if location
['countryCode'] == country \
250 and location
['placeType']['name'] == 'Country':
251 trends
= t
.trends
.place(_id
=location
['woeid'])[0]['trends']
259 t
= Twitter(auth
=authen())
260 num
= c
['HOME_TWEET_NUM']
261 if g
['stuff'].isdigit():
262 num
= int(g
['stuff'])
263 for tweet
in reversed(t
.statuses
.home_timeline(count
=num
)):
264 draw(t
=tweet
, iot
=g
['iot'])
272 t
= Twitter(auth
=authen())
273 user
= g
['stuff'].split()[0]
276 num
= int(g
['stuff'].split()[1])
278 num
= c
['HOME_TWEET_NUM']
279 for tweet
in reversed(t
.statuses
.user_timeline(count
=num
, screen_name
=user
[1:])):
280 draw(t
=tweet
, iot
=g
['iot'])
283 printNicely(red('A name should begin with a \'@\''))
290 t
= Twitter(auth
=authen())
291 num
= c
['HOME_TWEET_NUM']
292 if g
['stuff'].isdigit():
293 num
= int(g
['stuff'])
294 for tweet
in reversed(t
.statuses
.mentions_timeline(count
=num
)):
295 draw(t
=tweet
, iot
=g
['iot'])
303 t
= Twitter(auth
=authen())
304 t
.statuses
.update(status
=g
['stuff'])
311 t
= Twitter(auth
=authen())
313 id = int(g
['stuff'].split()[0])
315 printNicely(red('Sorry I can\'t understand.'))
317 tid
= db
.rainbow_to_tweet_query(id)[0].tweet_id
318 t
.statuses
.retweet(id=tid
, include_entities
=False, trim_user
=True)
325 t
= Twitter(auth
=authen())
327 id = int(g
['stuff'].split()[0])
329 printNicely(red('Sorry I can\'t understand.'))
331 tid
= db
.rainbow_to_tweet_query(id)[0].tweet_id
332 tweet
= t
.statuses
.show(id=tid
)
333 screen_name
= tweet
['user']['screen_name']
335 quote
= '\"@' + screen_name
+ ': ' + text
+ '\"'
336 quote
= quote
.encode('utf8')
337 notice
= light_magenta('Compose mode ')
338 notice
+= light_yellow('(Enter nothing will cancel the quote)')
339 notice
+= light_magenta(':')
341 extra
= raw_input(quote
)
343 t
.statuses
.update(status
=quote
+ extra
)
345 printNicely(light_magenta('No text added.'))
352 t
= Twitter(auth
=authen())
355 id = int(g
['stuff'].split()[0])
357 printNicely(red('Sorry I can\'t understand.'))
359 tid
= db
.rainbow_to_tweet_query(id)[0].tweet_id
360 # Get display num if exist
362 num
= int(g
['stuff'].split()[1])
364 num
= c
['RETWEETS_SHOW_NUM']
365 # Get result and display
366 rt_ary
= t
.statuses
.retweets(id=tid
, count
=num
)
368 printNicely(magenta('This tweet has no retweet.'))
370 for tweet
in reversed(rt_ary
):
371 draw(t
=tweet
, iot
=g
['iot'])
379 t
= Twitter(auth
=authen())
381 id = int(g
['stuff'].split()[0])
383 printNicely(red('Sorry I can\'t understand.'))
385 tid
= db
.rainbow_to_tweet_query(id)[0].tweet_id
386 t
.favorites
.create(_id
=tid
, include_entities
=False)
387 printNicely(green('Favorited.'))
388 draw(t
.statuses
.show(id=tid
), iot
=g
['iot'])
396 t
= Twitter(auth
=authen())
398 id = int(g
['stuff'].split()[0])
400 printNicely(red('Sorry I can\'t understand.'))
402 tid
= db
.rainbow_to_tweet_query(id)[0].tweet_id
403 user
= t
.statuses
.show(id=tid
)['user']['screen_name']
404 status
= ' '.join(g
['stuff'].split()[1:])
405 status
= '@' + user
+ ' ' + status
.decode('utf-8')
406 t
.statuses
.update(status
=status
, in_reply_to_status_id
=tid
)
413 t
= Twitter(auth
=authen())
415 rid
= int(g
['stuff'].split()[0])
417 printNicely(red('Sorry I can\'t understand.'))
419 tid
= db
.rainbow_to_tweet_query(rid
)[0].tweet_id
420 t
.statuses
.destroy(id=tid
)
421 printNicely(green('Okay it\'s gone.'))
428 t
= Twitter(auth
=authen())
430 id = int(g
['stuff'].split()[0])
432 printNicely(red('Sorry I can\'t understand.'))
434 tid
= db
.rainbow_to_tweet_query(id)[0].tweet_id
435 t
.favorites
.destroy(_id
=tid
)
436 printNicely(green('Okay it\'s unfavorited.'))
437 draw(t
.statuses
.show(id=tid
), iot
=g
['iot'])
445 t
= Twitter(auth
=authen())
446 g
['stuff'] = g
['stuff'].strip()
447 rel
= t
.search
.tweets(q
=g
['stuff'])['statuses']
449 printNicely('Newest tweets:')
450 for i
in reversed(xrange(c
['SEARCH_MAX_RECORD'])):
456 printNicely(magenta('I\'m afraid there is no result'))
461 Send a direct message
463 t
= Twitter(auth
=authen())
464 user
= g
['stuff'].split()[0]
465 if user
[0].startswith('@'):
467 content
= g
['stuff'].split()[1]
469 printNicely(red('Sorry I can\'t understand.'))
470 t
.direct_messages
.new(
471 screen_name
=user
[1:],
474 printNicely(green('Message sent.'))
476 printNicely(red('A name should begin with a \'@\''))
483 t
= Twitter(auth
=authen())
485 target
= g
['stuff'].split()[0]
486 if target
!= 'image':
488 id = int(g
['stuff'].split()[1])
489 tid
= db
.rainbow_to_tweet_query(id)[0].tweet_id
490 tweet
= t
.statuses
.show(id=tid
)
491 media
= tweet
['entities']['media']
493 res
= requests
.get(m
['media_url'])
494 img
= Image
.open(BytesIO(res
.content
))
497 printNicely(red('Sorry I can\'t show this image.'))
504 t
= Twitter(auth
=authen())
506 if not g
['stuff'].isdigit():
508 tid
= db
.rainbow_to_tweet_query(g
['stuff'])[0].tweet_id
509 tweet
= t
.statuses
.show(id=tid
)
511 u
for u
in tweet
['text'].split() if u
.startswith('http://')]
513 printNicely(light_magenta('No url here @.@!'))
515 for link
in link_ary
:
516 webbrowser
.open(link
)
518 printNicely(red('Sorry I can\'t open url in this tweet.'))
523 List friends for followers
525 t
= Twitter(auth
=authen())
528 name
= g
['stuff'].split()[1]
529 if name
.startswith('@'):
532 printNicely(red('A name should begin with a \'@\''))
533 raise Exception('Invalid name')
535 name
= g
['original_name']
536 # Get list followers or friends
538 target
= g
['stuff'].split()[0]
540 printNicely(red('Omg some syntax is wrong.'))
542 d
= {'fl': 'followers', 'fr': 'friends'}
546 while next_cursor
!= 0:
547 list = getattr(t
, d
[target
]).list(
551 include_entities
=False,
553 for u
in list['users']:
554 rel
[u
['name']] = '@' + u
['screen_name']
555 next_cursor
= list['next_cursor']
557 printNicely('All: ' + str(len(rel
)) + ' ' + d
[target
] + '.')
559 user
= ' ' + cycle_color(name
)
560 user
+= color_func(c
['TWEET']['nick'])(' ' + rel
[name
] + ' ')
566 Inbox direct messages
568 t
= Twitter(auth
=authen())
569 num
= c
['MESSAGES_DISPLAY']
571 if g
['stuff'].isdigit():
574 # Max message per page is 20 so we have to loop
576 rel
= rel
+ t
.direct_messages(
579 include_entities
=False,
584 rel
= rel
+ t
.direct_messages(
587 include_entities
=False,
591 printNicely('Inbox: newest ' + str(len(rel
)) + ' messages.')
592 for m
in reversed(rel
):
601 t
= Twitter(auth
=authen())
602 num
= c
['MESSAGES_DISPLAY']
604 if g
['stuff'].isdigit():
605 num
= int(g
['stuff'])
607 # Max message per page is 20 so we have to loop
609 rel
= rel
+ t
.direct_messages
.sent(
612 include_entities
=False,
617 rel
= rel
+ t
.direct_messages
.sent(
620 include_entities
=False,
624 printNicely('Sent: newest ' + str(len(rel
)) + ' messages.')
625 for m
in reversed(rel
):
634 t
= Twitter(auth
=authen())
636 rid
= int(g
['stuff'].split()[0])
638 printNicely(red('Sorry I can\'t understand.'))
639 mid
= db
.rainbow_to_message_query(rid
)[0].message_id
640 t
.direct_messages
.destroy(id=mid
)
641 printNicely(green('Message deleted.'))
646 Show profile of a specific user
648 t
= Twitter(auth
=authen())
649 screen_name
= g
['stuff'].split()[0]
650 if screen_name
.startswith('@'):
653 screen_name
=screen_name
[1:],
654 include_entities
=False)
655 show_profile(user
, g
['iot'])
657 printNicely(red('Omg no user.'))
659 printNicely(red('A name should begin with a \'@\''))
666 t
= Twitter(auth
=authen())
667 screen_name
= g
['stuff'].split()[0]
668 if screen_name
.startswith('@'):
669 t
.friendships
.create(screen_name
=screen_name
[1:], follow
=True)
670 printNicely(green('You are following ' + screen_name
+ ' now!'))
672 printNicely(red('A name should begin with a \'@\''))
679 t
= Twitter(auth
=authen())
680 screen_name
= g
['stuff'].split()[0]
681 if screen_name
.startswith('@'):
682 t
.friendships
.destroy(
683 screen_name
=screen_name
[1:],
684 include_entities
=False)
685 printNicely(green('Unfollow ' + screen_name
+ ' success!'))
687 printNicely(red('A name should begin with a \'@\''))
694 t
= Twitter(auth
=authen())
696 screen_name
= g
['stuff'].split()[0]
698 printNicely(red('A name should be specified. '))
700 if screen_name
.startswith('@'):
701 rel
= t
.mutes
.users
.create(screen_name
=screen_name
[1:])
702 if isinstance(rel
, dict):
703 printNicely(green(screen_name
+ ' is muted.'))
705 printNicely(red(rel
))
707 printNicely(red('A name should begin with a \'@\''))
714 t
= Twitter(auth
=authen())
716 screen_name
= g
['stuff'].split()[0]
718 printNicely(red('A name should be specified. '))
720 if screen_name
.startswith('@'):
721 rel
= t
.mutes
.users
.destroy(screen_name
=screen_name
[1:])
722 if isinstance(rel
, dict):
723 printNicely(green(screen_name
+ ' is unmuted.'))
725 printNicely(red(rel
))
727 printNicely(red('A name should begin with a \'@\''))
734 t
= Twitter(auth
=authen())
739 while next_cursor
!= 0:
740 list = t
.mutes
.users
.list(
741 screen_name
=g
['original_name'],
744 include_entities
=False,
746 for u
in list['users']:
747 rel
[u
['name']] = '@' + u
['screen_name']
748 next_cursor
= list['next_cursor']
750 printNicely('All: ' + str(len(rel
)) + ' people.')
752 user
= ' ' + cycle_color(name
)
753 user
+= color_func(c
['TWEET']['nick'])(' ' + rel
[name
] + ' ')
761 t
= Twitter(auth
=authen())
762 screen_name
= g
['stuff'].split()[0]
763 if screen_name
.startswith('@'):
765 screen_name
=screen_name
[1:],
766 include_entities
=False,
768 printNicely(green('You blocked ' + screen_name
+ '.'))
770 printNicely(red('A name should begin with a \'@\''))
777 t
= Twitter(auth
=authen())
778 screen_name
= g
['stuff'].split()[0]
779 if screen_name
.startswith('@'):
781 screen_name
=screen_name
[1:],
782 include_entities
=False,
784 printNicely(green('Unblock ' + screen_name
+ ' success!'))
786 printNicely(red('A name should begin with a \'@\''))
791 Report a user as a spam account
793 t
= Twitter(auth
=authen())
794 screen_name
= g
['stuff'].split()[0]
795 if screen_name
.startswith('@'):
797 screen_name
=screen_name
[1:])
798 printNicely(green('You reported ' + screen_name
+ '.'))
800 printNicely(red('Sorry I can\'t understand.'))
808 list_name
= raw_input(light_magenta('Give me the list\'s name: '))
809 # Get list name and owner
811 owner
, slug
= list_name
.split('/')
812 if slug
.startswith('@'):
816 printNicely(light_magenta('List name should follow "@owner/list_name" format.'))
817 raise Exception('Wrong list name')
824 rel
= t
.lists
.list(screen_name
=g
['original_name'])
828 printNicely(light_magenta('You belong to no lists :)'))
835 owner
, slug
= get_slug()
836 res
= t
.lists
.statuses(
838 owner_screen_name
=owner
,
840 include_entities
=False)
850 owner
, slug
= get_slug()
854 while next_cursor
!= 0:
857 owner_screen_name
=owner
,
859 include_entities
=False)
861 rel
[u
['name']] = '@' + u
['screen_name']
862 next_cursor
= m
['next_cursor']
863 printNicely('All: ' + str(len(rel
)) + ' members.')
865 user
= ' ' + cycle_color(name
)
866 user
+= color_func(c
['TWEET']['nick'])(' ' + rel
[name
] + ' ')
870 def list_subscribers(t
):
874 owner
, slug
= get_slug()
878 while next_cursor
!= 0:
879 m
= t
.lists
.subscribers(
881 owner_screen_name
=owner
,
883 include_entities
=False)
885 rel
[u
['name']] = '@' + u
['screen_name']
886 next_cursor
= m
['next_cursor']
887 printNicely('All: ' + str(len(rel
)) + ' subscribers.')
889 user
= ' ' + cycle_color(name
)
890 user
+= color_func(c
['TWEET']['nick'])(' ' + rel
[name
] + ' ')
896 Add specific user to a list
898 owner
, slug
= get_slug()
900 user_name
= raw_input(light_magenta('Give me name of the newbie: '))
901 if user_name
.startswith('@'):
902 user_name
= user_name
[1:]
904 t
.lists
.members
.create(
906 owner_screen_name
=owner
,
907 screen_name
=user_name
)
908 printNicely(light_green('Added.'))
910 printNicely(light_magenta('I\'m sorry we can not add him/her.'))
915 Remove specific user from a list
917 owner
, slug
= get_slug()
919 user_name
= raw_input(light_magenta('Give me name of the unlucky one: '))
920 if user_name
.startswith('@'):
921 user_name
= user_name
[1:]
923 t
.lists
.members
.destroy(
925 owner_screen_name
=owner
,
926 screen_name
=user_name
)
927 printNicely(light_green('Gone.'))
929 printNicely(light_magenta('I\'m sorry we can not remove him/her.'))
932 def list_subscribe(t
):
936 owner
, slug
= get_slug()
939 t
.lists
.subscribers
.create(
941 owner_screen_name
=owner
)
942 printNicely(light_green('Done.'))
945 light_magenta('I\'m sorry you can not subscribe to this list.'))
948 def list_unsubscribe(t
):
952 owner
, slug
= get_slug()
955 t
.lists
.subscribers
.destroy(
957 owner_screen_name
=owner
)
958 printNicely(light_green('Done.'))
961 light_magenta('I\'m sorry you can not unsubscribe to this list.'))
970 while next_cursor
!= 0:
971 res
= t
.lists
.ownerships(
972 screen_name
=g
['original_name'],
975 next_cursor
= res
['next_cursor']
979 printNicely(light_magenta('You own no lists :)'))
986 name
= raw_input(light_magenta('New list\'s name: '))
987 mode
= raw_input(light_magenta('New list\'s mode (public/private): '))
988 description
= raw_input(light_magenta('New list\'s description: '))
993 description
=description
)
994 printNicely(light_green(name
+ ' list is created.'))
996 printNicely(red('Oops something is wrong with Twitter :('))
1003 slug
= raw_input(light_magenta('Your list that you want to update: '))
1004 name
= raw_input(light_magenta('Update name (leave blank to unchange): '))
1005 mode
= raw_input(light_magenta('Update mode (public/private): '))
1006 description
= raw_input(light_magenta('Update description: '))
1010 slug
='-'.join(slug
.split()),
1011 owner_screen_name
=g
['original_name'],
1014 description
=description
)
1018 owner_screen_name
=g
['original_name'],
1020 description
=description
)
1021 printNicely(light_green(slug
+ ' list is updated.'))
1023 printNicely(red('Oops something is wrong with Twitter :('))
1030 slug
= raw_input(light_magenta('Your list that you want to update: '))
1033 slug
='-'.join(slug
.split()),
1034 owner_screen_name
=g
['original_name'])
1035 printNicely(light_green(slug
+ ' list is deleted.'))
1037 printNicely(red('Oops something is wrong with Twitter :('))
1044 t
= Twitter(auth
=authen())
1045 # List all lists or base on action
1047 g
['list_action'] = g
['stuff'].split()[0]
1054 'all_mem': list_members
,
1055 'all_sub': list_subscribers
,
1058 'sub': list_subscribe
,
1059 'unsub': list_unsubscribe
,
1062 'update': list_update
,
1066 return action_ary
[g
['list_action']](t
)
1068 printNicely(red('Please try again.'))
1073 Unix's command `cal`
1076 rel
= os
.popen('cal').read().split('\n')
1079 show_calendar(month
, date
, rel
)
1084 List and change theme
1088 for theme
in g
['themes']:
1089 line
= light_magenta(theme
)
1090 if c
['THEME'] == theme
:
1091 line
= ' ' * 2 + light_yellow('* ') + line
1093 line
= ' ' * 4 + line
1095 elif g
['stuff'] == 'current_as_default':
1097 def fixup(adict
, k
, v
):
1098 for key
in adict
.keys():
1101 elif type(adict
[key
]) is dict:
1102 fixup(adict
[key
], k
, v
)
1104 path
= os
.environ
.get(
1108 '')) + os
.sep
+ '.rainbow_config.json'
1109 data
= load_config(rainbow_config
)
1110 fixup(data
, 'THEME', c
['THEME'])
1112 with
open(path
, 'w') as out
:
1113 json
.dump(data
, out
, indent
= 4)
1114 os
.system('chmod 777 ' + path
)
1115 printNicely(light_green('Okay it will be applied from next time :)'))
1120 if g
['stuff'] != 'custom':
1121 new_config
= os
.path
.dirname(
1122 __file__
) + '/colorset/' + g
['stuff'] + '.json'
1124 new_config
= os
.environ
.get(
1125 'HOME', os
.environ
.get(
1127 '')) + os
.sep
+ '.rainbow_config.json'
1128 new_config
= load_config(new_config
)
1130 for nc
in new_config
:
1131 c
[nc
] = new_config
[nc
]
1132 # Update db and reset colors
1133 db
.theme_update(g
['stuff'])
1134 c
['THEME'] = g
['stuff']
1136 g
['decorated_name'] = color_func(
1137 c
['DECORATED_NAME'])(
1138 '[@' + g
['original_name'] + ']: ')
1139 printNicely(green('Theme changed.'))
1141 printNicely(red('No such theme exists.'))
1144 def help_discover():
1149 # Discover the world
1151 usage
+= s
+ grey(u
'\u266A' + ' Discover the world \n')
1152 usage
+= s
* 2 + light_green('trend') + ' will show global trending topics. ' + \
1153 'You can try ' + light_green('trend US') + ' or ' + \
1154 light_green('trend JP Tokyo') + '.\n'
1155 usage
+= s
* 2 + light_green('home') + ' will show your timeline. ' + \
1156 light_green('home 7') + ' will show 7 tweets.\n'
1157 usage
+= s
* 2 + light_green('mentions') + ' will show mentions timeline. ' + \
1158 light_green('mentions 7') + ' will show 7 mention tweets.\n'
1159 usage
+= s
* 2 + light_green('whois @mdo') + ' will show profile of ' + \
1160 magenta('@mdo') + '.\n'
1161 usage
+= s
* 2 + light_green('view @mdo') + \
1162 ' will show ' + magenta('@mdo') + '\'s home.\n'
1163 usage
+= s
* 2 + light_green('s AKB48') + ' will search for "' + \
1164 light_yellow('AKB48') + '" and return 5 newest tweet. ' + \
1165 'Search can be performed with or without hashtag.\n'
1176 usage
+= s
+ grey(u
'\u266A' + ' Tweets \n')
1177 usage
+= s
* 2 + light_green('t oops ') + \
1178 'will tweet "' + light_yellow('oops') + '" immediately.\n'
1180 light_green('rt 12 ') + ' will retweet to tweet with ' + \
1181 light_yellow('[id=12]') + '.\n'
1183 light_green('quote 12 ') + ' will quote the tweet with ' + \
1184 light_yellow('[id=12]') + '. If no extra text is added, ' + \
1185 'the quote will be canceled.\n'
1187 light_green('allrt 12 20 ') + ' will list 20 newest retweet of the tweet with ' + \
1188 light_yellow('[id=12]') + '.\n'
1189 usage
+= s
* 2 + light_green('rep 12 oops') + ' will reply "' + \
1190 light_yellow('oops') + '" to tweet with ' + \
1191 light_yellow('[id=12]') + '.\n'
1193 light_green('fav 12 ') + ' will favorite the tweet with ' + \
1194 light_yellow('[id=12]') + '.\n'
1196 light_green('ufav 12 ') + ' will unfavorite tweet with ' + \
1197 light_yellow('[id=12]') + '.\n'
1199 light_green('del 12 ') + ' will delete tweet with ' + \
1200 light_yellow('[id=12]') + '.\n'
1201 usage
+= s
* 2 + light_green('show image 12') + ' will show image in tweet with ' + \
1202 light_yellow('[id=12]') + ' in your OS\'s image viewer.\n'
1203 usage
+= s
* 2 + light_green('open 12') + ' will open url in tweet with ' + \
1204 light_yellow('[id=12]') + ' in your OS\'s default browser.\n'
1208 def help_messages():
1215 usage
+= s
+ grey(u
'\u266A' + ' Direct messages \n')
1216 usage
+= s
* 2 + light_green('inbox') + ' will show inbox messages. ' + \
1217 light_green('inbox 7') + ' will show newest 7 messages.\n'
1218 usage
+= s
* 2 + light_green('sent') + ' will show sent messages. ' + \
1219 light_green('sent 7') + ' will show newest 7 messages.\n'
1220 usage
+= s
* 2 + light_green('mes @dtvd88 hi') + ' will send a "hi" messege to ' + \
1221 magenta('@dtvd88') + '.\n'
1222 usage
+= s
* 2 + light_green('trash 5') + ' will remove message with ' + \
1223 light_yellow('[message_id=5]') + '.\n'
1227 def help_friends_and_followers():
1229 Friends and Followers
1232 # Follower and following
1234 usage
+= s
+ grey(u
'\u266A' + ' Friends and followers \n')
1236 light_green('ls fl') + \
1237 ' will list all followers (people who are following you).\n'
1239 light_green('ls fr') + \
1240 ' will list all friends (people who you are following).\n'
1241 usage
+= s
* 2 + light_green('fl @dtvd88') + ' will follow ' + \
1242 magenta('@dtvd88') + '.\n'
1243 usage
+= s
* 2 + light_green('ufl @dtvd88') + ' will unfollow ' + \
1244 magenta('@dtvd88') + '.\n'
1245 usage
+= s
* 2 + light_green('mute @dtvd88') + ' will mute ' + \
1246 magenta('@dtvd88') + '.\n'
1247 usage
+= s
* 2 + light_green('unmute @dtvd88') + ' will unmute ' + \
1248 magenta('@dtvd88') + '.\n'
1249 usage
+= s
* 2 + light_green('muting') + ' will list muting users.\n'
1250 usage
+= s
* 2 + light_green('block @dtvd88') + ' will block ' + \
1251 magenta('@dtvd88') + '.\n'
1252 usage
+= s
* 2 + light_green('unblock @dtvd88') + ' will unblock ' + \
1253 magenta('@dtvd88') + '.\n'
1254 usage
+= s
* 2 + light_green('report @dtvd88') + ' will report ' + \
1255 magenta('@dtvd88') + ' as a spam account.\n'
1266 usage
+= s
+ grey(u
'\u266A' + ' Twitter list\n')
1267 usage
+= s
* 2 + light_green('list') + \
1268 ' will show all lists you are belong to.\n'
1269 usage
+= s
* 2 + light_green('list home') + \
1270 ' will show timeline of list. You will be asked for list\'s name.\n'
1271 usage
+= s
* 2 + light_green('list all_mem') + \
1272 ' will show list\'s all members.\n'
1273 usage
+= s
* 2 + light_green('list all_sub') + \
1274 ' will show list\'s all subscribers.\n'
1275 usage
+= s
* 2 + light_green('list add') + \
1276 ' will add specific person to a list owned by you.' + \
1277 ' You will be asked for list\'s name and person\'s name.\n'
1278 usage
+= s
* 2 + light_green('list rm') + \
1279 ' will remove specific person from a list owned by you.' + \
1280 ' You will be asked for list\'s name and person\'s name.\n'
1281 usage
+= s
* 2 + light_green('list sub') + \
1282 ' will subscribe you to a specific list.\n'
1283 usage
+= s
* 2 + light_green('list unsub') + \
1284 ' will unsubscribe you from a specific list.\n'
1285 usage
+= s
* 2 + light_green('list own') + \
1286 ' will show all list owned by you.\n'
1287 usage
+= s
* 2 + light_green('list new') + \
1288 ' will create a new list.\n'
1289 usage
+= s
* 2 + light_green('list update') + \
1290 ' will update a list owned by you.\n'
1291 usage
+= s
* 2 + light_green('list del') + \
1292 ' will delete a list owned by you.\n'
1303 usage
+= s
+ grey(u
'\u266A' + ' Switching streams \n')
1304 usage
+= s
* 2 + light_green('switch public #AKB') + \
1305 ' will switch to public stream and follow "' + \
1306 light_yellow('AKB') + '" keyword.\n'
1307 usage
+= s
* 2 + light_green('switch mine') + \
1308 ' will switch to your personal stream.\n'
1309 usage
+= s
* 2 + light_green('switch mine -f ') + \
1310 ' will prompt to enter the filter.\n'
1311 usage
+= s
* 3 + light_yellow('Only nicks') + \
1312 ' filter will decide nicks will be INCLUDE ONLY.\n'
1313 usage
+= s
* 3 + light_yellow('Ignore nicks') + \
1314 ' filter will decide nicks will be EXCLUDE.\n'
1315 usage
+= s
* 2 + light_green('switch mine -d') + \
1316 ' will use the config\'s ONLY_LIST and IGNORE_LIST.\n'
1325 h
, w
= os
.popen('stty size', 'r').read().split()
1329 usage
+= s
+ 'Hi boss! I\'m ready to serve you right now!\n'
1330 usage
+= s
+ '-' * (int(w
) - 4) + '\n'
1331 usage
+= s
+ 'You are ' + \
1332 light_yellow('already') + ' on your personal stream.\n'
1333 usage
+= s
+ 'Any update from Twitter will show up ' + \
1334 light_yellow('immediately') + '.\n'
1335 usage
+= s
+ 'In addtion, following commands are available right now:\n'
1337 # Twitter help section
1339 usage
+= s
+ grey(u
'\u266A' + ' Twitter help\n')
1340 usage
+= s
* 2 + light_green('h discover') + \
1341 ' will show help for discover commands.\n'
1342 usage
+= s
* 2 + light_green('h tweets') + \
1343 ' will show help for tweets commands.\n'
1344 usage
+= s
* 2 + light_green('h messages') + \
1345 ' will show help for messages commands.\n'
1346 usage
+= s
* 2 + light_green('h friends_and_followers') + \
1347 ' will show help for friends and followers commands.\n'
1348 usage
+= s
* 2 + light_green('h list') + \
1349 ' will show help for list commands.\n'
1350 usage
+= s
* 2 + light_green('h stream') + \
1351 ' will show help for stream commands.\n'
1355 usage
+= s
+ grey(u
'\u266A' + ' Smart shell\n')
1356 usage
+= s
* 2 + light_green('111111 * 9 / 7') + ' or any math expression ' + \
1357 'will be evaluate by Python interpreter.\n'
1358 usage
+= s
* 2 + 'Even ' + light_green('cal') + ' will show the calendar' + \
1359 ' for current month.\n'
1363 usage
+= s
+ grey(u
'\u266A' + ' Screening \n')
1364 usage
+= s
* 2 + light_green('theme') + ' will list available theme.' + \
1365 light_green('theme monokai') + ' will apply ' + light_yellow('monokai') + \
1366 ' theme immediately.\n'
1367 usage
+= s
* 2 + light_green('h') + ' will show this help again.\n'
1368 usage
+= s
* 2 + light_green('c') + ' will clear the screen.\n'
1369 usage
+= s
* 2 + light_green('q') + ' will quit.\n'
1373 usage
+= s
+ '-' * (int(w
) - 4) + '\n'
1374 usage
+= s
+ 'Have fun and hang tight! \n'
1378 'discover': help_discover
,
1379 'tweets': help_tweets
,
1380 'messages': help_messages
,
1381 'friends_and_followers': help_friends_and_followers
,
1383 'stream': help_stream
,
1386 d
[g
['stuff'].strip()]()
1403 os
.system('rm -rf rainbow.db')
1404 os
.kill(g
['stream_pid'], signal
.SIGKILL
)
1410 Reset prefix of line
1413 printNicely(magenta('Need tips ? Type "h" and hit Enter key!'))
1416 printNicely(str(eval(g
['cmd'])))
1470 Listen to user's input
1475 ['public', 'mine'], # switch
1490 ['image'], # show image
1492 ['fl', 'fr'], # list
1519 g
['themes'] + ['current_as_default'], # theme
1524 'friends_and_followers',
1532 init_interactive_shell(d
)
1537 line
= raw_input(g
['decorated_name'])
1541 cmd
= line
.split()[0]
1546 # Lock the semaphore
1547 db
.semaphore_update(True)
1548 # Save cmd to global variable and call process
1549 g
['stuff'] = ' '.join(line
.split()[1:])
1550 # Process the command
1553 if cmd
in ['switch', 't', 'rt', 'rep']:
1557 # Release the semaphore lock
1558 db
.semaphore_update(False)
1560 printNicely(red('OMG something is wrong with Twitter right now.'))
1563 def stream(domain
, args
, name
='Rainbow Stream'):
1570 c
['USER_DOMAIN']: name
,
1571 c
['PUBLIC_DOMAIN']: args
.track_keywords
,
1572 c
['SITE_DOMAIN']: name
,
1575 ascii_art(art_dict
[domain
])
1577 # These arguments are optional:
1579 timeout
=args
.timeout
,
1580 block
=not args
.no_block
,
1581 heartbeat_timeout
=args
.heartbeat_timeout
)
1585 if args
.track_keywords
:
1586 query_args
['track'] = args
.track_keywords
1589 stream
= TwitterStream(
1595 if domain
== c
['USER_DOMAIN']:
1596 tweet_iter
= stream
.user(**query_args
)
1597 elif domain
== c
['SITE_DOMAIN']:
1598 tweet_iter
= stream
.site(**query_args
)
1600 if args
.track_keywords
:
1601 tweet_iter
= stream
.statuses
.filter(**query_args
)
1603 tweet_iter
= stream
.statuses
.sample()
1605 # Iterate over the stream.
1606 for tweet
in tweet_iter
:
1608 printNicely("-- None --")
1609 elif tweet
is Timeout
:
1610 printNicely("-- Timeout --")
1611 elif tweet
is HeartbeatTimeout
:
1612 printNicely("-- Heartbeat Timeout --")
1613 elif tweet
is Hangup
:
1614 printNicely("-- Hangup --")
1615 elif tweet
.get('text'):
1618 iot
=args
.image_on_term
,
1619 keyword
=args
.track_keywords
,
1620 check_semaphore
=True,
1624 except TwitterHTTPError
:
1627 magenta("We have maximum connection problem with twitter'stream API right now :("))
1634 # Spawn stream process
1635 args
= parse_arguments()
1637 get_decorated_name()
1639 except TwitterHTTPError
:
1642 magenta("Something wrong with Twitter Oauth right now :("))
1644 magenta("Please delete ~/.rainbow_oauth and try again."))
1646 os
.system('rm -rf rainbow.db')
1654 g
['original_name']))
1657 # Start listen process
1661 g
['stream_pid'] = p
.pid
1662 g
['iot'] = args
.image_on_term