script/stream/recorder.py
changeset 11 54d7f1486ac4
parent 10 eb885a117aa0
child 12 4daf47fcf792
equal deleted inserted replaced
10:eb885a117aa0 11:54d7f1486ac4
     1 import time
       
     2 
       
     3 from getpass import getpass
       
     4 from textwrap import TextWrapper
       
     5 
       
     6 import tweepy
       
     7 import webbrowser
       
     8 
       
     9 CONSUMER_KEY = "54ThDZhpEjokcMgHJOMnQA"
       
    10 CONSUMER_SECRET = "wUoL9UL2T87tfc97R0Dff2EaqRzpJ5XGdmaN2XK3udA"
       
    11 
       
    12 class StreamWatcherListener(tweepy.StreamListener):
       
    13 
       
    14     status_wrapper = TextWrapper(width=60, initial_indent='    ', subsequent_indent='    ')
       
    15 
       
    16     def on_status(self, status):
       
    17         try:
       
    18             print self.status_wrapper.fill(status.text)
       
    19             print '\n %s  %s  via %s\n' % (status.author.screen_name, status.created_at, status.source)
       
    20         except:
       
    21             # Catch any unicode errors while printing to console
       
    22             # and just ignore them to avoid breaking application.
       
    23             pass
       
    24 
       
    25     def on_error(self, status_code):
       
    26         print 'An error has occured! Status code = %s' % status_code
       
    27         return True  # keep stream alive
       
    28 
       
    29     def on_timeout(self):
       
    30         print 'Snoozing Zzzzzz'
       
    31 
       
    32 
       
    33 
       
    34 def main():
       
    35 
       
    36     auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
       
    37     auth_url = auth.get_authorization_url()
       
    38     print 'Please authorize: ' + auth_url
       
    39     webbrowser.open(auth_url)
       
    40 
       
    41     # Prompt for login credentials and setup stream object
       
    42     verifier = raw_input('PIN: ').strip()
       
    43     auth.get_access_token(verifier)
       
    44     stream = tweepy.Stream(auth, StreamWatcherListener(), timeout=None)
       
    45 
       
    46     # Prompt for mode of streaming
       
    47     valid_modes = ['sample', 'filter']
       
    48     while True:
       
    49         mode = raw_input('Mode? [sample/filter] ')
       
    50         if mode in valid_modes:
       
    51             break
       
    52         print 'Invalid mode! Try again.'
       
    53 
       
    54     if mode == 'sample':
       
    55         stream.sample()
       
    56 
       
    57     elif mode == 'filter':
       
    58         follow_list = raw_input('Users to follow (comma separated): ').strip()
       
    59         track_list = raw_input('Keywords to track (comma seperated): ').strip()
       
    60         if follow_list:
       
    61             follow_list = [u for u in follow_list.split(',')]
       
    62         else:
       
    63             follow_list = None
       
    64         if track_list:
       
    65             track_list = [k for k in track_list.split(',')]
       
    66         else:
       
    67             track_list = None
       
    68 
       
    69         stream.filter(follow_list, track_list)
       
    70 
       
    71 
       
    72 if __name__ == '__main__':
       
    73     try:
       
    74         main()
       
    75     except KeyboardInterrupt:
       
    76         print '\nGoodbye!'