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
20 from StringIO
import StringIO
25 from .consumer
import *
26 from .interactive
import *
28 from .c_image
import *
71 def parse_arguments():
75 parser
= argparse
.ArgumentParser(description
=__doc__
or "")
79 help='Timeout for the stream (seconds).')
82 '--heartbeat-timeout',
83 help='Set heartbeat timeout.',
89 help='Set stream to non-blocking.')
93 help='Search the stream for specific text.')
97 help='Filter specific screen_name.')
101 help='Ignore specific screen_name.')
106 help='Display all image on terminal.')
107 return parser
.parse_args()
112 Authenticate with Twitter OAuth
114 # When using rainbow stream you must authorize.
115 twitter_credential
= os
.environ
.get(
119 '')) + os
.sep
+ '.rainbow_oauth'
120 if not os
.path
.exists(twitter_credential
):
121 oauth_dance("Rainbow Stream",
125 oauth_token
, oauth_token_secret
= read_token_file(twitter_credential
)
133 def get_decorated_name():
135 Beginning of every line
137 t
= Twitter(auth
=authen())
138 name
= '@' + t
.account
.verify_credentials()['screen_name']
139 g
['original_name'] = name
[1:]
140 g
['decorated_name'] = color_func(c
['DECORATED_NAME'])('[' + name
+ ']: ')
141 g
['ascii_art'] = True
143 files
= os
.listdir(os
.path
.dirname(__file__
)+'/colorset')
144 themes
= [f
.split('.')[0] for f
in files
if f
.split('.')[-1] == 'json']
147 db
.theme_store(c
['theme'])
155 target
= g
['stuff'].split()[0]
158 args
= parse_arguments()
160 if g
['stuff'].split()[-1] == '-f':
161 only
= raw_input('Only nicks: ')
162 ignore
= raw_input('Ignore nicks: ')
163 args
.filter = filter(None, only
.split(','))
164 args
.ignore
= filter(None, ignore
.split(','))
165 elif g
['stuff'].split()[-1] == '-d':
166 args
.filter = c
['ONLY_LIST']
167 args
.ignore
= c
['IGNORE_LIST']
169 printNicely(red('Sorry, wrong format.'))
173 if target
== 'public':
174 keyword
= g
['stuff'].split()[1]
175 if keyword
[0] == '#':
176 keyword
= keyword
[1:]
178 os
.kill(g
['stream_pid'], signal
.SIGKILL
)
179 args
.track_keywords
= keyword
187 g
['stream_pid'] = p
.pid
190 elif target
== 'mine':
192 os
.kill(g
['stream_pid'], signal
.SIGKILL
)
201 g
['stream_pid'] = p
.pid
204 printNicely(cyan('Only: ' + str(args
.filter)))
206 printNicely(red('Ignore: ' + str(args
.ignore
)))
208 g
['ascii_art'] = True
210 printNicely(red('Sorry I can\'t understand.'))
217 t
= Twitter(auth
=authen())
218 # Get country and town
220 country
= g
['stuff'].split()[0]
224 town
= g
['stuff'].split()[1]
228 avail
= t
.trends
.available()
231 trends
= t
.trends
.place(_id
=1)[0]['trends']
234 for location
in avail
:
235 # Search for country and Town
237 if location
['countryCode'] == country \
238 and location
['placeType']['name'] == 'Town' \
239 and location
['name'] == town
:
240 trends
= t
.trends
.place(_id
=location
['woeid'])[0]['trends']
242 # Search for country only
244 if location
['countryCode'] == country \
245 and location
['placeType']['name'] == 'Country':
246 trends
= t
.trends
.place(_id
=location
['woeid'])[0]['trends']
254 t
= Twitter(auth
=authen())
255 num
= c
['HOME_TWEET_NUM']
256 if g
['stuff'].isdigit():
257 num
= int(g
['stuff'])
258 for tweet
in reversed(t
.statuses
.home_timeline(count
=num
)):
259 draw(t
=tweet
, iot
=g
['iot'])
267 t
= Twitter(auth
=authen())
268 user
= g
['stuff'].split()[0]
271 num
= int(g
['stuff'].split()[1])
273 num
= c
['HOME_TWEET_NUM']
274 for tweet
in reversed(t
.statuses
.user_timeline(count
=num
, screen_name
=user
[1:])):
275 draw(t
=tweet
, iot
=g
['iot'])
278 printNicely(red('A name should begin with a \'@\''))
285 t
= Twitter(auth
=authen())
286 num
= c
['HOME_TWEET_NUM']
287 if g
['stuff'].isdigit():
288 num
= int(g
['stuff'])
289 for tweet
in reversed(t
.statuses
.mentions_timeline(count
=num
)):
290 draw(t
=tweet
, iot
=g
['iot'])
298 t
= Twitter(auth
=authen())
299 t
.statuses
.update(status
=g
['stuff'])
306 t
= Twitter(auth
=authen())
308 id = int(g
['stuff'].split()[0])
310 printNicely(red('Sorry I can\'t understand.'))
312 tid
= db
.rainbow_to_tweet_query(id)[0].tweet_id
313 t
.statuses
.retweet(id=tid
, include_entities
=False, trim_user
=True)
320 t
= Twitter(auth
=authen())
322 id = int(g
['stuff'].split()[0])
324 printNicely(red('Sorry I can\'t understand.'))
326 tid
= db
.rainbow_to_tweet_query(id)[0].tweet_id
327 tweet
= t
.statuses
.show(id=tid
)
328 screen_name
= tweet
['user']['screen_name']
330 quote
= '\"@' + screen_name
+ ': ' + text
+ '\"'
331 quote
= quote
.encode('utf8')
332 notice
= light_magenta('Compose mode ')
333 notice
+= light_yellow('(Enter nothing will cancel the quote)')
334 notice
+= light_magenta(':')
336 extra
= raw_input(quote
)
338 t
.statuses
.update(status
=quote
+extra
)
340 printNicely(light_magenta('No text added.'))
347 t
= Twitter(auth
=authen())
350 id = int(g
['stuff'].split()[0])
352 printNicely(red('Sorry I can\'t understand.'))
354 tid
= db
.rainbow_to_tweet_query(id)[0].tweet_id
355 # Get display num if exist
357 num
= int(g
['stuff'].split()[1])
359 num
= c
['RETWEETS_SHOW_NUM']
360 # Get result and display
361 rt_ary
= t
.statuses
.retweets(id=tid
, count
=num
)
363 printNicely(magenta('This tweet has no retweet.'))
365 for tweet
in reversed(rt_ary
):
366 draw(t
=tweet
, iot
=g
['iot'])
374 t
= Twitter(auth
=authen())
376 id = int(g
['stuff'].split()[0])
378 printNicely(red('Sorry I can\'t understand.'))
380 tid
= db
.rainbow_to_tweet_query(id)[0].tweet_id
381 t
.favorites
.create(_id
=tid
, include_entities
=False)
382 printNicely(green('Favorited.'))
383 draw(t
.statuses
.show(id=tid
), iot
=g
['iot'])
391 t
= Twitter(auth
=authen())
393 id = int(g
['stuff'].split()[0])
395 printNicely(red('Sorry I can\'t understand.'))
397 tid
= db
.rainbow_to_tweet_query(id)[0].tweet_id
398 user
= t
.statuses
.show(id=tid
)['user']['screen_name']
399 status
= ' '.join(g
['stuff'].split()[1:])
400 status
= '@' + user
+ ' ' + status
.decode('utf-8')
401 t
.statuses
.update(status
=status
, in_reply_to_status_id
=tid
)
408 t
= Twitter(auth
=authen())
410 rid
= int(g
['stuff'].split()[0])
412 printNicely(red('Sorry I can\'t understand.'))
414 tid
= db
.rainbow_to_tweet_query(rid
)[0].tweet_id
415 t
.statuses
.destroy(id=tid
)
416 printNicely(green('Okay it\'s gone.'))
423 t
= Twitter(auth
=authen())
425 id = int(g
['stuff'].split()[0])
427 printNicely(red('Sorry I can\'t understand.'))
429 tid
= db
.rainbow_to_tweet_query(id)[0].tweet_id
430 t
.favorites
.destroy(_id
=tid
)
431 printNicely(green('Okay it\'s unfavorited.'))
432 draw(t
.statuses
.show(id=tid
), iot
=g
['iot'])
440 t
= Twitter(auth
=authen())
441 if g
['stuff'].startswith('#'):
442 rel
= t
.search
.tweets(q
=g
['stuff'])['statuses']
444 printNicely('Newest tweets:')
445 for i
in reversed(xrange(c
['SEARCH_MAX_RECORD'])):
448 keyword
=g
['stuff'].strip()[1:])
451 printNicely(magenta('I\'m afraid there is no result'))
453 printNicely(red('A keyword should be a hashtag (like \'#AKB48\')'))
458 Send a direct message
460 t
= Twitter(auth
=authen())
461 user
= g
['stuff'].split()[0]
462 if user
[0].startswith('@'):
464 content
= g
['stuff'].split()[1]
466 printNicely(red('Sorry I can\'t understand.'))
467 t
.direct_messages
.new(
468 screen_name
=user
[1:],
471 printNicely(green('Message sent.'))
473 printNicely(red('A name should begin with a \'@\''))
480 t
= Twitter(auth
=authen())
482 target
= g
['stuff'].split()[0]
483 if target
!= 'image':
485 id = int(g
['stuff'].split()[1])
486 tid
= db
.rainbow_to_tweet_query(id)[0].tweet_id
487 tweet
= t
.statuses
.show(id=tid
)
488 media
= tweet
['entities']['media']
490 res
= requests
.get(m
['media_url'])
491 img
= Image
.open(StringIO(res
.content
))
494 printNicely(red('Sorry I can\'t show this image.'))
501 t
= Twitter(auth
=authen())
503 if not g
['stuff'].isdigit():
505 tid
= db
.rainbow_to_tweet_query(g
['stuff'])[0].tweet_id
506 tweet
= t
.statuses
.show(id=tid
)
507 link_ary
= [u
for u
in tweet
['text'].split() if u
.startswith('http://')]
509 printNicely(light_magenta('No url here @.@!'))
511 for link
in link_ary
:
512 webbrowser
.open(link
)
514 printNicely(red('Sorry I can\'t open url in this tweet.'))
519 List friends for followers
521 t
= Twitter(auth
=authen())
524 name
= g
['stuff'].split()[1]
525 if name
.startswith('@'):
528 printNicely(red('A name should begin with a \'@\''))
529 raise Exception('Invalid name')
531 name
= g
['original_name']
532 # Get list followers or friends
534 target
= g
['stuff'].split()[0]
536 printNicely(red('Omg some syntax is wrong.'))
538 d
= {'fl': 'followers', 'fr': 'friends'}
542 while next_cursor
!= 0:
543 list = getattr(t
, d
[target
]).list(
547 include_entities
=False,
549 for u
in list['users']:
550 rel
[u
['name']] = '@' + u
['screen_name']
551 next_cursor
= list['next_cursor']
553 printNicely('All: ' + str(len(rel
)) + ' people.')
555 user
= ' ' + cycle_color(name
) + grey(' ' + rel
[name
] + ' ')
561 Inbox direct messages
563 t
= Twitter(auth
=authen())
564 num
= c
['MESSAGES_DISPLAY']
566 if g
['stuff'].isdigit():
569 # Max message per page is 20 so we have to loop
571 rel
= rel
+ t
.direct_messages(
574 include_entities
=False,
579 rel
= rel
+ t
.direct_messages(
582 include_entities
=False,
586 printNicely('Inbox: newest ' + str(len(rel
)) + ' messages.')
587 for m
in reversed(rel
):
596 t
= Twitter(auth
=authen())
597 num
= c
['MESSAGES_DISPLAY']
599 if g
['stuff'].isdigit():
600 num
= int(g
['stuff'])
602 # Max message per page is 20 so we have to loop
604 rel
= rel
+ t
.direct_messages
.sent(
607 include_entities
=False,
612 rel
= rel
+ t
.direct_messages
.sent(
615 include_entities
=False,
619 printNicely('Sent: newest ' + str(len(rel
)) + ' messages.')
620 for m
in reversed(rel
):
629 t
= Twitter(auth
=authen())
631 rid
= int(g
['stuff'].split()[0])
633 printNicely(red('Sorry I can\'t understand.'))
634 mid
= db
.rainbow_to_message_query(rid
)[0].message_id
635 t
.direct_messages
.destroy(id=mid
)
636 printNicely(green('Message deleted.'))
641 Show profile of a specific user
643 t
= Twitter(auth
=authen())
644 screen_name
= g
['stuff'].split()[0]
645 if screen_name
.startswith('@'):
648 screen_name
=screen_name
[1:],
649 include_entities
=False)
650 show_profile(user
, g
['iot'])
652 printNicely(red('Omg no user.'))
654 printNicely(red('A name should begin with a \'@\''))
661 t
= Twitter(auth
=authen())
662 screen_name
= g
['stuff'].split()[0]
663 if screen_name
.startswith('@'):
664 t
.friendships
.create(screen_name
=screen_name
[1:], follow
=True)
665 printNicely(green('You are following ' + screen_name
+ ' now!'))
667 printNicely(red('A name should begin with a \'@\''))
674 t
= Twitter(auth
=authen())
675 screen_name
= g
['stuff'].split()[0]
676 if screen_name
.startswith('@'):
677 t
.friendships
.destroy(
678 screen_name
=screen_name
[1:],
679 include_entities
=False)
680 printNicely(green('Unfollow ' + screen_name
+ ' success!'))
682 printNicely(red('A name should begin with a \'@\''))
689 t
= Twitter(auth
=authen())
691 screen_name
= g
['stuff'].split()[0]
693 printNicely(red('A name should be specified. '))
695 if screen_name
.startswith('@'):
696 rel
= t
.mutes
.users
.create(screen_name
=screen_name
[1:])
697 if isinstance(rel
, dict):
698 printNicely(green(screen_name
+ ' is muted.'))
700 printNicely(red(rel
))
702 printNicely(red('A name should begin with a \'@\''))
709 t
= Twitter(auth
=authen())
711 screen_name
= g
['stuff'].split()[0]
713 printNicely(red('A name should be specified. '))
715 if screen_name
.startswith('@'):
716 rel
= t
.mutes
.users
.destroy(screen_name
=screen_name
[1:])
717 if isinstance(rel
, dict):
718 printNicely(green(screen_name
+ ' is unmuted.'))
720 printNicely(red(rel
))
722 printNicely(red('A name should begin with a \'@\''))
729 t
= Twitter(auth
=authen())
734 while next_cursor
!= 0:
735 list = t
.mutes
.users
.list(
736 screen_name
=g
['original_name'],
739 include_entities
=False,
741 for u
in list['users']:
742 rel
[u
['name']] = '@' + u
['screen_name']
743 next_cursor
= list['next_cursor']
745 printNicely('All: ' + str(len(rel
)) + ' people.')
747 user
= ' ' + cycle_color(name
) + grey(' ' + rel
[name
] + ' ')
755 t
= Twitter(auth
=authen())
756 screen_name
= g
['stuff'].split()[0]
757 if screen_name
.startswith('@'):
759 screen_name
=screen_name
[1:],
760 include_entities
=False,
762 printNicely(green('You blocked ' + screen_name
+ '.'))
764 printNicely(red('A name should begin with a \'@\''))
771 t
= Twitter(auth
=authen())
772 screen_name
= g
['stuff'].split()[0]
773 if screen_name
.startswith('@'):
775 screen_name
=screen_name
[1:],
776 include_entities
=False,
778 printNicely(green('Unblock ' + screen_name
+ ' success!'))
780 printNicely(red('A name should begin with a \'@\''))
785 Report a user as a spam account
787 t
= Twitter(auth
=authen())
788 screen_name
= g
['stuff'].split()[0]
789 if screen_name
.startswith('@'):
791 screen_name
=screen_name
[1:])
792 printNicely(green('You reported ' + screen_name
+ '.'))
794 printNicely(red('Sorry I can\'t understand.'))
802 rel
= os
.popen('cal').read().split('\n')
805 show_calendar(month
, date
, rel
)
810 List and change theme
814 for theme
in g
['themes']:
816 # Detect custom config
817 if theme
== 'custom':
818 line
+= light_magenta('custom')
819 custom_path
= os
.environ
.get(
821 os
.environ
.get('USERPROFILE',
822 '')) + os
.sep
+ '.rainbow_config.json'
823 if not os
.path
.exists(custom_path
):
824 line
+= light_magenta(' (create your own config file at ~/.rainbow_config.json)')
826 line
+= light_magenta(' (loaded)')
828 line
+= light_magenta(theme
)
829 if c
['theme'] == theme
:
830 line
= ' '*2 + light_yellow('* ') + line
834 elif g
['stuff'] == 'current_as_default':
836 path
= os
.path
.dirname(__file__
) + '/colorset/init'
840 os
.system('chmod 777 ' + path
)
841 printNicely(light_green('Okay it will be applied from next time :)'))
846 if g
['stuff'] != 'custom':
847 new_config
= os
.path
.dirname(__file__
) + '/colorset/' + g
['stuff'] + '.json'
849 new_config
= os
.environ
.get(
850 'HOME',os
.environ
.get(
852 '')) + os
.sep
+ '.rainbow_config.json'
853 new_config
= load_config(new_config
)
855 for nc
in new_config
:
856 c
[nc
] = new_config
[nc
]
857 # Update db and reset colors
858 db
.theme_update(g
['stuff'])
859 c
['theme'] = g
['stuff']
861 g
['decorated_name'] = color_func(
862 c
['DECORATED_NAME'])(
863 '[@' + g
['original_name'] + ']: ')
864 printNicely(green('Theme changed.'))
866 if g
['stuff'] == 'custom':
867 printNicely(red('~/.rainbow_config.json is not exists!'))
869 printNicely(red('No such theme exists.'))
877 h
, w
= os
.popen('stty size', 'r').read().split()
881 usage
+= s
+ 'Hi boss! I\'m ready to serve you right now!\n'
882 usage
+= s
+ '-' * (int(w
) - 4) + '\n'
883 usage
+= s
+ 'You are ' + \
884 light_yellow('already') + ' on your personal stream.\n'
885 usage
+= s
+ 'Any update from Twitter will show up ' + \
886 light_yellow('immediately') + '.\n'
887 usage
+= s
+ 'In addtion, following commands are available right now:\n'
891 usage
+= s
+ grey(u
'\u266A' + ' Discover the world \n')
892 usage
+= s
* 2 + light_green('trend') + ' will show global trending topics. ' + \
893 'You can try ' + light_green('trend US') + ' or ' + \
894 light_green('trend JP Tokyo') + '.\n'
895 usage
+= s
* 2 + light_green('home') + ' will show your timeline. ' + \
896 light_green('home 7') + ' will show 7 tweets.\n'
897 usage
+= s
* 2 + light_green('mentions') + ' will show mentions timeline. ' + \
898 light_green('mentions 7') + ' will show 7 mention tweets.\n'
899 usage
+= s
* 2 + light_green('whois @mdo') + ' will show profile of ' + \
900 magenta('@mdo') + '.\n'
901 usage
+= s
* 2 + light_green('view @mdo') + \
902 ' will show ' + magenta('@mdo') + '\'s home.\n'
903 usage
+= s
* 2 + light_green('s #AKB48') + ' will search for "' + \
904 light_yellow('AKB48') + '" and return 5 newest tweet.\n'
908 usage
+= s
+ grey(u
'\u266A' + ' Tweets \n')
909 usage
+= s
* 2 + light_green('t oops ') + \
910 'will tweet "' + light_yellow('oops') + '" immediately.\n'
912 light_green('rt 12 ') + ' will retweet to tweet with ' + \
913 light_yellow('[id=12]') + '.\n'
915 light_green('quote 12 ') + ' will quote the tweet with ' + \
916 light_yellow('[id=12]') + '. If no extra text is added, ' + \
917 'the quote will be canceled.\n'
919 light_green('allrt 12 20 ') + ' will list 20 newest retweet of the tweet with ' + \
920 light_yellow('[id=12]') + '.\n'
921 usage
+= s
* 2 + light_green('rep 12 oops') + ' will reply "' + \
922 light_yellow('oops') + '" to tweet with ' + \
923 light_yellow('[id=12]') + '.\n'
925 light_green('fav 12 ') + ' will favorite the tweet with ' + \
926 light_yellow('[id=12]') + '.\n'
928 light_green('ufav 12 ') + ' will unfavorite tweet with ' + \
929 light_yellow('[id=12]') + '.\n'
931 light_green('del 12 ') + ' will delete tweet with ' + \
932 light_yellow('[id=12]') + '.\n'
933 usage
+= s
* 2 + light_green('show image 12') + ' will show image in tweet with ' + \
934 light_yellow('[id=12]') + ' in your OS\'s image viewer.\n'
935 usage
+= s
* 2 + light_green('open 12') + ' will open url in tweet with ' + \
936 light_yellow('[id=12]') + ' in your OS\'s default browser.\n'
940 usage
+= s
+ grey(u
'\u266A' + ' Direct messages \n')
941 usage
+= s
* 2 + light_green('inbox') + ' will show inbox messages. ' + \
942 light_green('inbox 7') + ' will show newest 7 messages.\n'
943 usage
+= s
* 2 + light_green('sent') + ' will show sent messages. ' + \
944 light_green('sent 7') + ' will show newest 7 messages.\n'
945 usage
+= s
* 2 + light_green('mes @dtvd88 hi') + ' will send a "hi" messege to ' + \
946 magenta('@dtvd88') + '.\n'
947 usage
+= s
* 2 + light_green('trash 5') + ' will remove message with ' + \
948 light_yellow('[message_id=5]') + '.\n'
950 # Follower and following
952 usage
+= s
+ grey(u
'\u266A' + ' Friends and followers \n')
954 light_green('ls fl') + \
955 ' will list all followers (people who are following you).\n'
957 light_green('ls fr') + \
958 ' will list all friends (people who you are following).\n'
959 usage
+= s
* 2 + light_green('fl @dtvd88') + ' will follow ' + \
960 magenta('@dtvd88') + '.\n'
961 usage
+= s
* 2 + light_green('ufl @dtvd88') + ' will unfollow ' + \
962 magenta('@dtvd88') + '.\n'
963 usage
+= s
* 2 + light_green('mute @dtvd88') + ' will mute ' + \
964 magenta('@dtvd88') + '.\n'
965 usage
+= s
* 2 + light_green('unmute @dtvd88') + ' will unmute ' + \
966 magenta('@dtvd88') + '.\n'
967 usage
+= s
* 2 + light_green('muting') + ' will list muting users.\n'
968 usage
+= s
* 2 + light_green('block @dtvd88') + ' will block ' + \
969 magenta('@dtvd88') + '.\n'
970 usage
+= s
* 2 + light_green('unblock @dtvd88') + ' will unblock ' + \
971 magenta('@dtvd88') + '.\n'
972 usage
+= s
* 2 + light_green('report @dtvd88') + ' will report ' + \
973 magenta('@dtvd88') + ' as a spam account.\n'
977 usage
+= s
+ grey(u
'\u266A' + ' Switching streams \n')
978 usage
+= s
* 2 + light_green('switch public #AKB') + \
979 ' will switch to public stream and follow "' + \
980 light_yellow('AKB') + '" keyword.\n'
981 usage
+= s
* 2 + light_green('switch mine') + \
982 ' will switch to your personal stream.\n'
983 usage
+= s
* 2 + light_green('switch mine -f ') + \
984 ' will prompt to enter the filter.\n'
985 usage
+= s
* 3 + light_yellow('Only nicks') + \
986 ' filter will decide nicks will be INCLUDE ONLY.\n'
987 usage
+= s
* 3 + light_yellow('Ignore nicks') + \
988 ' filter will decide nicks will be EXCLUDE.\n'
989 usage
+= s
* 2 + light_green('switch mine -d') + \
990 ' will use the config\'s ONLY_LIST and IGNORE_LIST.\n'
994 usage
+= s
+ grey(u
'\u266A' + ' Smart shell\n')
995 usage
+= s
* 2 + light_green('111111 * 9 / 7') + ' or any math expression ' + \
996 'will be evaluate by Python interpreter.\n'
997 usage
+= s
* 2 + 'Even ' + light_green('cal') + ' will show the calendar' + \
998 ' for current month.\n'
1002 usage
+= s
+ grey(u
'\u266A' + ' Screening \n')
1003 usage
+= s
* 2 + light_green('theme') + ' will list available theme.' + \
1004 light_green('theme monokai') + ' will apply ' + light_yellow('monokai') + \
1005 ' theme immediately.\n'
1006 usage
+= s
* 2 + light_green('h') + ' will show this help again.\n'
1007 usage
+= s
* 2 + light_green('c') + ' will clear the screen.\n'
1008 usage
+= s
* 2 + light_green('q') + ' will quit.\n'
1012 usage
+= s
+ '-' * (int(w
) - 4) + '\n'
1013 usage
+= s
+ 'Have fun and hang tight! \n'
1029 os
.system('rm -rf rainbow.db')
1030 os
.kill(g
['stream_pid'], signal
.SIGKILL
)
1036 Reset prefix of line
1039 printNicely(magenta('Need tips ? Type "h" and hit Enter key!'))
1042 printNicely(str(eval(g
['cmd'])))
1095 Listen to user's input
1100 ['public', 'mine'], # switch
1115 ['image'], # show image
1117 ['fl', 'fr'], # list
1131 g
['themes'] + ['current_as_default'], # theme
1137 init_interactive_shell(d
)
1142 line
= raw_input(g
['decorated_name'])
1146 cmd
= line
.split()[0]
1150 # Save cmd to global variable and call process
1152 g
['stuff'] = ' '.join(line
.split()[1:])
1154 except Exception,e
:
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