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 *
72 def parse_arguments():
76 parser
= argparse
.ArgumentParser(description
=__doc__
or "")
80 help='Timeout for the stream (seconds).')
83 '--heartbeat-timeout',
84 help='Set heartbeat timeout.',
90 help='Set stream to non-blocking.')
94 help='Search the stream for specific text.')
98 help='Filter specific screen_name.')
102 help='Ignore specific screen_name.')
107 help='Display all image on terminal.')
108 return parser
.parse_args()
113 Authenticate with Twitter OAuth
115 # When using rainbow stream you must authorize.
116 twitter_credential
= os
.environ
.get(
120 '')) + os
.sep
+ '.rainbow_oauth'
121 if not os
.path
.exists(twitter_credential
):
122 oauth_dance("Rainbow Stream",
126 oauth_token
, oauth_token_secret
= read_token_file(twitter_credential
)
134 def get_decorated_name():
136 Beginning of every line
138 t
= Twitter(auth
=authen())
139 name
= '@' + t
.account
.verify_credentials()['screen_name']
140 g
['original_name'] = name
[1:]
141 g
['decorated_name'] = color_func(c
['DECORATED_NAME'])('[' + name
+ ']: ')
142 g
['ascii_art'] = True
144 files
= os
.listdir(os
.path
.dirname(__file__
)+'/colorset')
145 themes
= [f
.split('.')[0] for f
in files
if f
.split('.')[-1] == 'json']
148 db
.theme_store(c
['theme'])
156 target
= g
['stuff'].split()[0]
159 args
= parse_arguments()
161 if g
['stuff'].split()[-1] == '-f':
162 only
= raw_input('Only nicks: ')
163 ignore
= raw_input('Ignore nicks: ')
164 args
.filter = filter(None, only
.split(','))
165 args
.ignore
= filter(None, ignore
.split(','))
166 elif g
['stuff'].split()[-1] == '-d':
167 args
.filter = c
['ONLY_LIST']
168 args
.ignore
= c
['IGNORE_LIST']
170 printNicely(red('Sorry, wrong format.'))
174 if target
== 'public':
175 keyword
= g
['stuff'].split()[1]
176 if keyword
[0] == '#':
177 keyword
= keyword
[1:]
179 os
.kill(g
['stream_pid'], signal
.SIGKILL
)
180 args
.track_keywords
= keyword
188 g
['stream_pid'] = p
.pid
191 elif target
== 'mine':
193 os
.kill(g
['stream_pid'], signal
.SIGKILL
)
202 g
['stream_pid'] = p
.pid
205 printNicely(cyan('Only: ' + str(args
.filter)))
207 printNicely(red('Ignore: ' + str(args
.ignore
)))
209 g
['ascii_art'] = True
211 printNicely(red('Sorry I can\'t understand.'))
218 t
= Twitter(auth
=authen())
219 # Get country and town
221 country
= g
['stuff'].split()[0]
225 town
= g
['stuff'].split()[1]
229 avail
= t
.trends
.available()
232 trends
= t
.trends
.place(_id
=1)[0]['trends']
235 for location
in avail
:
236 # Search for country and Town
238 if location
['countryCode'] == country \
239 and location
['placeType']['name'] == 'Town' \
240 and location
['name'] == town
:
241 trends
= t
.trends
.place(_id
=location
['woeid'])[0]['trends']
243 # Search for country only
245 if location
['countryCode'] == country \
246 and location
['placeType']['name'] == 'Country':
247 trends
= t
.trends
.place(_id
=location
['woeid'])[0]['trends']
255 t
= Twitter(auth
=authen())
256 num
= c
['HOME_TWEET_NUM']
257 if g
['stuff'].isdigit():
258 num
= int(g
['stuff'])
259 for tweet
in reversed(t
.statuses
.home_timeline(count
=num
)):
260 draw(t
=tweet
, iot
=g
['iot'])
268 t
= Twitter(auth
=authen())
269 user
= g
['stuff'].split()[0]
272 num
= int(g
['stuff'].split()[1])
274 num
= c
['HOME_TWEET_NUM']
275 for tweet
in reversed(t
.statuses
.user_timeline(count
=num
, screen_name
=user
[1:])):
276 draw(t
=tweet
, iot
=g
['iot'])
279 printNicely(red('A name should begin with a \'@\''))
286 t
= Twitter(auth
=authen())
287 num
= c
['HOME_TWEET_NUM']
288 if g
['stuff'].isdigit():
289 num
= int(g
['stuff'])
290 for tweet
in reversed(t
.statuses
.mentions_timeline(count
=num
)):
291 draw(t
=tweet
, iot
=g
['iot'])
299 t
= Twitter(auth
=authen())
300 t
.statuses
.update(status
=g
['stuff'])
307 t
= Twitter(auth
=authen())
309 id = int(g
['stuff'].split()[0])
311 printNicely(red('Sorry I can\'t understand.'))
313 tid
= db
.rainbow_to_tweet_query(id)[0].tweet_id
314 t
.statuses
.retweet(id=tid
, include_entities
=False, trim_user
=True)
321 t
= Twitter(auth
=authen())
323 id = int(g
['stuff'].split()[0])
325 printNicely(red('Sorry I can\'t understand.'))
327 tid
= db
.rainbow_to_tweet_query(id)[0].tweet_id
328 tweet
= t
.statuses
.show(id=tid
)
329 screen_name
= tweet
['user']['screen_name']
331 quote
= '\"@' + screen_name
+ ': ' + text
+ '\"'
332 quote
= quote
.encode('utf8')
333 notice
= light_magenta('Compose mode ')
334 notice
+= light_yellow('(Enter nothing will cancel the quote)')
335 notice
+= light_magenta(':')
337 extra
= raw_input(quote
)
339 t
.statuses
.update(status
=quote
+extra
)
341 printNicely(light_magenta('No text added.'))
348 t
= Twitter(auth
=authen())
351 id = int(g
['stuff'].split()[0])
353 printNicely(red('Sorry I can\'t understand.'))
355 tid
= db
.rainbow_to_tweet_query(id)[0].tweet_id
356 # Get display num if exist
358 num
= int(g
['stuff'].split()[1])
360 num
= c
['RETWEETS_SHOW_NUM']
361 # Get result and display
362 rt_ary
= t
.statuses
.retweets(id=tid
, count
=num
)
364 printNicely(magenta('This tweet has no retweet.'))
366 for tweet
in reversed(rt_ary
):
367 draw(t
=tweet
, iot
=g
['iot'])
375 t
= Twitter(auth
=authen())
377 id = int(g
['stuff'].split()[0])
379 printNicely(red('Sorry I can\'t understand.'))
381 tid
= db
.rainbow_to_tweet_query(id)[0].tweet_id
382 t
.favorites
.create(_id
=tid
, include_entities
=False)
383 printNicely(green('Favorited.'))
384 draw(t
.statuses
.show(id=tid
), iot
=g
['iot'])
392 t
= Twitter(auth
=authen())
394 id = int(g
['stuff'].split()[0])
396 printNicely(red('Sorry I can\'t understand.'))
398 tid
= db
.rainbow_to_tweet_query(id)[0].tweet_id
399 user
= t
.statuses
.show(id=tid
)['user']['screen_name']
400 status
= ' '.join(g
['stuff'].split()[1:])
401 status
= '@' + user
+ ' ' + status
.decode('utf-8')
402 t
.statuses
.update(status
=status
, in_reply_to_status_id
=tid
)
409 t
= Twitter(auth
=authen())
411 rid
= int(g
['stuff'].split()[0])
413 printNicely(red('Sorry I can\'t understand.'))
415 tid
= db
.rainbow_to_tweet_query(rid
)[0].tweet_id
416 t
.statuses
.destroy(id=tid
)
417 printNicely(green('Okay it\'s gone.'))
424 t
= Twitter(auth
=authen())
426 id = int(g
['stuff'].split()[0])
428 printNicely(red('Sorry I can\'t understand.'))
430 tid
= db
.rainbow_to_tweet_query(id)[0].tweet_id
431 t
.favorites
.destroy(_id
=tid
)
432 printNicely(green('Okay it\'s unfavorited.'))
433 draw(t
.statuses
.show(id=tid
), iot
=g
['iot'])
441 t
= Twitter(auth
=authen())
442 if g
['stuff'].startswith('#'):
443 rel
= t
.search
.tweets(q
=g
['stuff'])['statuses']
445 printNicely('Newest tweets:')
446 for i
in reversed(xrange(c
['SEARCH_MAX_RECORD'])):
449 keyword
=g
['stuff'].strip()[1:])
452 printNicely(magenta('I\'m afraid there is no result'))
454 printNicely(red('A keyword should be a hashtag (like \'#AKB48\')'))
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(StringIO(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
)
508 link_ary
= [u
for u
in tweet
['text'].split() if u
.startswith('http://')]
510 printNicely(light_magenta('No url here @.@!'))
512 for link
in link_ary
:
513 webbrowser
.open(link
)
515 printNicely(red('Sorry I can\'t open url in this tweet.'))
520 List friends for followers
522 t
= Twitter(auth
=authen())
525 name
= g
['stuff'].split()[1]
526 if name
.startswith('@'):
529 printNicely(red('A name should begin with a \'@\''))
530 raise Exception('Invalid name')
532 name
= g
['original_name']
533 # Get list followers or friends
535 target
= g
['stuff'].split()[0]
537 printNicely(red('Omg some syntax is wrong.'))
539 d
= {'fl': 'followers', 'fr': 'friends'}
543 while next_cursor
!= 0:
544 list = getattr(t
, d
[target
]).list(
548 include_entities
=False,
550 for u
in list['users']:
551 rel
[u
['name']] = '@' + u
['screen_name']
552 next_cursor
= list['next_cursor']
554 printNicely('All: ' + str(len(rel
)) + ' people.')
556 user
= ' ' + cycle_color(name
) + grey(' ' + rel
[name
] + ' ')
562 Inbox direct messages
564 t
= Twitter(auth
=authen())
565 num
= c
['MESSAGES_DISPLAY']
567 if g
['stuff'].isdigit():
570 # Max message per page is 20 so we have to loop
572 rel
= rel
+ t
.direct_messages(
575 include_entities
=False,
580 rel
= rel
+ t
.direct_messages(
583 include_entities
=False,
587 printNicely('Inbox: newest ' + str(len(rel
)) + ' messages.')
588 for m
in reversed(rel
):
597 t
= Twitter(auth
=authen())
598 num
= c
['MESSAGES_DISPLAY']
600 if g
['stuff'].isdigit():
601 num
= int(g
['stuff'])
603 # Max message per page is 20 so we have to loop
605 rel
= rel
+ t
.direct_messages
.sent(
608 include_entities
=False,
613 rel
= rel
+ t
.direct_messages
.sent(
616 include_entities
=False,
620 printNicely('Sent: newest ' + str(len(rel
)) + ' messages.')
621 for m
in reversed(rel
):
630 t
= Twitter(auth
=authen())
632 rid
= int(g
['stuff'].split()[0])
634 printNicely(red('Sorry I can\'t understand.'))
635 mid
= db
.rainbow_to_message_query(rid
)[0].message_id
636 t
.direct_messages
.destroy(id=mid
)
637 printNicely(green('Message deleted.'))
642 Show profile of a specific user
644 t
= Twitter(auth
=authen())
645 screen_name
= g
['stuff'].split()[0]
646 if screen_name
.startswith('@'):
649 screen_name
=screen_name
[1:],
650 include_entities
=False)
651 show_profile(user
, g
['iot'])
653 printNicely(red('Omg no user.'))
655 printNicely(red('A name should begin with a \'@\''))
662 t
= Twitter(auth
=authen())
663 screen_name
= g
['stuff'].split()[0]
664 if screen_name
.startswith('@'):
665 t
.friendships
.create(screen_name
=screen_name
[1:], follow
=True)
666 printNicely(green('You are following ' + screen_name
+ ' now!'))
668 printNicely(red('A name should begin with a \'@\''))
675 t
= Twitter(auth
=authen())
676 screen_name
= g
['stuff'].split()[0]
677 if screen_name
.startswith('@'):
678 t
.friendships
.destroy(
679 screen_name
=screen_name
[1:],
680 include_entities
=False)
681 printNicely(green('Unfollow ' + screen_name
+ ' success!'))
683 printNicely(red('A name should begin with a \'@\''))
690 t
= Twitter(auth
=authen())
692 screen_name
= g
['stuff'].split()[0]
694 printNicely(red('A name should be specified. '))
696 if screen_name
.startswith('@'):
697 rel
= t
.mutes
.users
.create(screen_name
=screen_name
[1:])
698 if isinstance(rel
, dict):
699 printNicely(green(screen_name
+ ' is muted.'))
701 printNicely(red(rel
))
703 printNicely(red('A name should begin with a \'@\''))
710 t
= Twitter(auth
=authen())
712 screen_name
= g
['stuff'].split()[0]
714 printNicely(red('A name should be specified. '))
716 if screen_name
.startswith('@'):
717 rel
= t
.mutes
.users
.destroy(screen_name
=screen_name
[1:])
718 if isinstance(rel
, dict):
719 printNicely(green(screen_name
+ ' is unmuted.'))
721 printNicely(red(rel
))
723 printNicely(red('A name should begin with a \'@\''))
730 t
= Twitter(auth
=authen())
735 while next_cursor
!= 0:
736 list = t
.mutes
.users
.list(
737 screen_name
=g
['original_name'],
740 include_entities
=False,
742 for u
in list['users']:
743 rel
[u
['name']] = '@' + u
['screen_name']
744 next_cursor
= list['next_cursor']
746 printNicely('All: ' + str(len(rel
)) + ' people.')
748 user
= ' ' + cycle_color(name
) + grey(' ' + rel
[name
] + ' ')
756 t
= Twitter(auth
=authen())
757 screen_name
= g
['stuff'].split()[0]
758 if screen_name
.startswith('@'):
760 screen_name
=screen_name
[1:],
761 include_entities
=False,
763 printNicely(green('You blocked ' + screen_name
+ '.'))
765 printNicely(red('A name should begin with a \'@\''))
772 t
= Twitter(auth
=authen())
773 screen_name
= g
['stuff'].split()[0]
774 if screen_name
.startswith('@'):
776 screen_name
=screen_name
[1:],
777 include_entities
=False,
779 printNicely(green('Unblock ' + screen_name
+ ' success!'))
781 printNicely(red('A name should begin with a \'@\''))
786 Report a user as a spam account
788 t
= Twitter(auth
=authen())
789 screen_name
= g
['stuff'].split()[0]
790 if screen_name
.startswith('@'):
792 screen_name
=screen_name
[1:])
793 printNicely(green('You reported ' + screen_name
+ '.'))
795 printNicely(red('Sorry I can\'t understand.'))
803 rel
= os
.popen('cal').read().split('\n')
806 show_calendar(month
, date
, rel
)
811 List and change theme
815 for theme
in g
['themes']:
817 # Detect custom config
818 if theme
== 'custom':
819 line
+= light_magenta('custom')
820 custom_path
= os
.environ
.get(
822 os
.environ
.get('USERPROFILE',
823 '')) + os
.sep
+ '.rainbow_config.json'
824 if not os
.path
.exists(custom_path
):
825 line
+= light_magenta(' (create your own config file at ~/.rainbow_config.json)')
827 line
+= light_magenta(' (loaded)')
829 line
+= light_magenta(theme
)
830 if c
['theme'] == theme
:
831 line
= ' '*2 + light_yellow('* ') + line
835 elif g
['stuff'] == 'current_as_default':
837 path
= os
.path
.dirname(__file__
) + '/colorset/init'
841 os
.system('chmod 777 ' + path
)
842 printNicely(light_green('Okay it will be applied from next time :)'))
847 if g
['stuff'] != 'custom':
848 new_config
= os
.path
.dirname(__file__
) + '/colorset/' + g
['stuff'] + '.json'
850 new_config
= os
.environ
.get(
851 'HOME',os
.environ
.get(
853 '')) + os
.sep
+ '.rainbow_config.json'
854 new_config
= load_config(new_config
)
856 for nc
in new_config
:
857 c
[nc
] = new_config
[nc
]
858 # Update db and reset colors
859 db
.theme_update(g
['stuff'])
860 c
['theme'] = g
['stuff']
862 g
['decorated_name'] = color_func(
863 c
['DECORATED_NAME'])(
864 '[@' + g
['original_name'] + ']: ')
865 printNicely(green('Theme changed.'))
867 if g
['stuff'] == 'custom':
868 printNicely(red('~/.rainbow_config.json is not exists!'))
870 printNicely(red('No such theme exists.'))
878 h
, w
= os
.popen('stty size', 'r').read().split()
882 usage
+= s
+ 'Hi boss! I\'m ready to serve you right now!\n'
883 usage
+= s
+ '-' * (int(w
) - 4) + '\n'
884 usage
+= s
+ 'You are ' + \
885 light_yellow('already') + ' on your personal stream.\n'
886 usage
+= s
+ 'Any update from Twitter will show up ' + \
887 light_yellow('immediately') + '.\n'
888 usage
+= s
+ 'In addtion, following commands are available right now:\n'
892 usage
+= s
+ grey(u
'\u266A' + ' Discover the world \n')
893 usage
+= s
* 2 + light_green('trend') + ' will show global trending topics. ' + \
894 'You can try ' + light_green('trend US') + ' or ' + \
895 light_green('trend JP Tokyo') + '.\n'
896 usage
+= s
* 2 + light_green('home') + ' will show your timeline. ' + \
897 light_green('home 7') + ' will show 7 tweets.\n'
898 usage
+= s
* 2 + light_green('mentions') + ' will show mentions timeline. ' + \
899 light_green('mentions 7') + ' will show 7 mention tweets.\n'
900 usage
+= s
* 2 + light_green('whois @mdo') + ' will show profile of ' + \
901 magenta('@mdo') + '.\n'
902 usage
+= s
* 2 + light_green('view @mdo') + \
903 ' will show ' + magenta('@mdo') + '\'s home.\n'
904 usage
+= s
* 2 + light_green('s #AKB48') + ' will search for "' + \
905 light_yellow('AKB48') + '" and return 5 newest tweet.\n'
909 usage
+= s
+ grey(u
'\u266A' + ' Tweets \n')
910 usage
+= s
* 2 + light_green('t oops ') + \
911 'will tweet "' + light_yellow('oops') + '" immediately.\n'
913 light_green('rt 12 ') + ' will retweet to tweet with ' + \
914 light_yellow('[id=12]') + '.\n'
916 light_green('quote 12 ') + ' will quote the tweet with ' + \
917 light_yellow('[id=12]') + '. If no extra text is added, ' + \
918 'the quote will be canceled.\n'
920 light_green('allrt 12 20 ') + ' will list 20 newest retweet of the tweet with ' + \
921 light_yellow('[id=12]') + '.\n'
922 usage
+= s
* 2 + light_green('rep 12 oops') + ' will reply "' + \
923 light_yellow('oops') + '" to tweet with ' + \
924 light_yellow('[id=12]') + '.\n'
926 light_green('fav 12 ') + ' will favorite the tweet with ' + \
927 light_yellow('[id=12]') + '.\n'
929 light_green('ufav 12 ') + ' will unfavorite tweet with ' + \
930 light_yellow('[id=12]') + '.\n'
932 light_green('del 12 ') + ' will delete tweet with ' + \
933 light_yellow('[id=12]') + '.\n'
934 usage
+= s
* 2 + light_green('show image 12') + ' will show image in tweet with ' + \
935 light_yellow('[id=12]') + ' in your OS\'s image viewer.\n'
936 usage
+= s
* 2 + light_green('open 12') + ' will open url in tweet with ' + \
937 light_yellow('[id=12]') + ' in your OS\'s default browser.\n'
941 usage
+= s
+ grey(u
'\u266A' + ' Direct messages \n')
942 usage
+= s
* 2 + light_green('inbox') + ' will show inbox messages. ' + \
943 light_green('inbox 7') + ' will show newest 7 messages.\n'
944 usage
+= s
* 2 + light_green('sent') + ' will show sent messages. ' + \
945 light_green('sent 7') + ' will show newest 7 messages.\n'
946 usage
+= s
* 2 + light_green('mes @dtvd88 hi') + ' will send a "hi" messege to ' + \
947 magenta('@dtvd88') + '.\n'
948 usage
+= s
* 2 + light_green('trash 5') + ' will remove message with ' + \
949 light_yellow('[message_id=5]') + '.\n'
951 # Follower and following
953 usage
+= s
+ grey(u
'\u266A' + ' Friends and followers \n')
955 light_green('ls fl') + \
956 ' will list all followers (people who are following you).\n'
958 light_green('ls fr') + \
959 ' will list all friends (people who you are following).\n'
960 usage
+= s
* 2 + light_green('fl @dtvd88') + ' will follow ' + \
961 magenta('@dtvd88') + '.\n'
962 usage
+= s
* 2 + light_green('ufl @dtvd88') + ' will unfollow ' + \
963 magenta('@dtvd88') + '.\n'
964 usage
+= s
* 2 + light_green('mute @dtvd88') + ' will mute ' + \
965 magenta('@dtvd88') + '.\n'
966 usage
+= s
* 2 + light_green('unmute @dtvd88') + ' will unmute ' + \
967 magenta('@dtvd88') + '.\n'
968 usage
+= s
* 2 + light_green('muting') + ' will list muting users.\n'
969 usage
+= s
* 2 + light_green('block @dtvd88') + ' will block ' + \
970 magenta('@dtvd88') + '.\n'
971 usage
+= s
* 2 + light_green('unblock @dtvd88') + ' will unblock ' + \
972 magenta('@dtvd88') + '.\n'
973 usage
+= s
* 2 + light_green('report @dtvd88') + ' will report ' + \
974 magenta('@dtvd88') + ' as a spam account.\n'
978 usage
+= s
+ grey(u
'\u266A' + ' Switching streams \n')
979 usage
+= s
* 2 + light_green('switch public #AKB') + \
980 ' will switch to public stream and follow "' + \
981 light_yellow('AKB') + '" keyword.\n'
982 usage
+= s
* 2 + light_green('switch mine') + \
983 ' will switch to your personal stream.\n'
984 usage
+= s
* 2 + light_green('switch mine -f ') + \
985 ' will prompt to enter the filter.\n'
986 usage
+= s
* 3 + light_yellow('Only nicks') + \
987 ' filter will decide nicks will be INCLUDE ONLY.\n'
988 usage
+= s
* 3 + light_yellow('Ignore nicks') + \
989 ' filter will decide nicks will be EXCLUDE.\n'
990 usage
+= s
* 2 + light_green('switch mine -d') + \
991 ' will use the config\'s ONLY_LIST and IGNORE_LIST.\n'
995 usage
+= s
+ grey(u
'\u266A' + ' Smart shell\n')
996 usage
+= s
* 2 + light_green('111111 * 9 / 7') + ' or any math expression ' + \
997 'will be evaluate by Python interpreter.\n'
998 usage
+= s
* 2 + 'Even ' + light_green('cal') + ' will show the calendar' + \
999 ' for current month.\n'
1003 usage
+= s
+ grey(u
'\u266A' + ' Screening \n')
1004 usage
+= s
* 2 + light_green('theme') + ' will list available theme.' + \
1005 light_green('theme monokai') + ' will apply ' + light_yellow('monokai') + \
1006 ' theme immediately.\n'
1007 usage
+= s
* 2 + light_green('h') + ' will show this help again.\n'
1008 usage
+= s
* 2 + light_green('c') + ' will clear the screen.\n'
1009 usage
+= s
* 2 + light_green('q') + ' will quit.\n'
1013 usage
+= s
+ '-' * (int(w
) - 4) + '\n'
1014 usage
+= s
+ 'Have fun and hang tight! \n'
1030 os
.system('rm -rf rainbow.db')
1031 os
.kill(g
['stream_pid'], signal
.SIGKILL
)
1037 Reset prefix of line
1040 printNicely(magenta('Need tips ? Type "h" and hit Enter key!'))
1043 printNicely(str(eval(g
['cmd'])))
1096 Listen to user's input
1101 ['public', 'mine'], # switch
1116 ['image'], # show image
1118 ['fl', 'fr'], # list
1132 g
['themes'] + ['current_as_default'], # theme
1138 init_interactive_shell(d
)
1143 line
= raw_input(g
['decorated_name'])
1147 cmd
= line
.split()[0]
1151 # Save cmd to global variable and call process
1153 g
['stuff'] = ' '.join(line
.split()[1:])
1156 printNicely(red('OMG something is wrong with Twitter right now.'))
1157 # Not redisplay prefix
1158 if cmd
in ['switch', 't', 'rt', 'rep']:
1164 def stream(domain
, args
, name
='Rainbow Stream'):
1171 c
['USER_DOMAIN']: name
,
1172 c
['PUBLIC_DOMAIN']: args
.track_keywords
,
1173 c
['SITE_DOMAIN']: 'Site Stream',
1176 ascii_art(art_dict
[domain
])
1178 # These arguments are optional:
1180 timeout
=args
.timeout
,
1181 block
=not args
.no_block
,
1182 heartbeat_timeout
=args
.heartbeat_timeout
)
1186 if args
.track_keywords
:
1187 query_args
['track'] = args
.track_keywords
1190 stream
= TwitterStream(
1196 if domain
== c
['USER_DOMAIN']:
1197 tweet_iter
= stream
.user(**query_args
)
1198 elif domain
== c
['SITE_DOMAIN']:
1199 tweet_iter
= stream
.site(**query_args
)
1201 if args
.track_keywords
:
1202 tweet_iter
= stream
.statuses
.filter(**query_args
)
1204 tweet_iter
= stream
.statuses
.sample()
1206 # Iterate over the stream.
1207 for tweet
in tweet_iter
:
1209 printNicely("-- None --")
1210 elif tweet
is Timeout
:
1211 printNicely("-- Timeout --")
1212 elif tweet
is HeartbeatTimeout
:
1213 printNicely("-- Heartbeat Timeout --")
1214 elif tweet
is Hangup
:
1215 printNicely("-- Hangup --")
1216 elif tweet
.get('text'):
1219 iot
=args
.image_on_term
,
1220 keyword
=args
.track_keywords
,
1224 except TwitterHTTPError
:
1227 magenta("We have maximum connection problem with twitter'stream API right now :("))
1234 # Spawn stream process
1235 args
= parse_arguments()
1237 get_decorated_name()
1239 except TwitterHTTPError
:
1242 magenta("I'm afraid we have maximum connection problem with twitter right now :("))
1243 printNicely(magenta("Let's try again later."))
1245 os
.system('rm -rf rainbow.db')
1253 g
['original_name']))
1256 # Start listen process
1260 g
['stream_pid'] = p
.pid
1261 g
['iot'] = args
.image_on_term