Difference between revisions of "Python: Tweepy: Twitter Streaming"

From OnnoWiki
Jump to navigation Jump to search
(Created page with " import tweepy import json consumer_key = 'FrgpflQFbXz5FbIh2qTtwqei8' consumer_secret = '0SfMrjSfKrKi1rSectp7PsEhe6lUWK7BvltyYBX9lIVz8BxqsI' access_token = '90134078-AaZ...")
 
 
Line 2: Line 2:
 
  import json
 
  import json
 
   
 
   
  consumer_key = 'FrgpflQFbXz5FbIh2qTtwqei8'
+
  consumer_key = ' '
  consumer_secret = '0SfMrjSfKrKi1rSectp7PsEhe6lUWK7BvltyYBX9lIVz8BxqsI'
+
  consumer_secret = ' '
  access_token = '90134078-AaZ70Mw0cHjJZBjySETxcTM6TY7Hp1Q5dDn6A15u5'
+
  access_token = ' '
  access_token_secret = 'txUSC0Lt9JhYx7AGLlsqrjwKypMmeqhdjYschXvTY5OzS'
+
  access_token_secret = ' '
 
   
 
   
 
  # This is the listener, resposible for receiving data
 
  # This is the listener, resposible for receiving data

Latest revision as of 11:27, 28 January 2017

import tweepy
import json

consumer_key = ' '
consumer_secret = ' '
access_token = ' '
access_token_secret = ' '

# This is the listener, resposible for receiving data
class StdOutListener(tweepy.StreamListener):
    def on_data(self, data):
        # Twitter returns data in JSON format - we need to decode it first
        decoded = json.loads(data)

        # Also, we convert UTF-8 to ASCII ignoring all bad characters sent by users
        print '@%s: %s' % (decoded['user']['screen_name'], decoded['text'].encode('ascii', 'ignore'))
        print 
        return True

    def on_error(self, status):
        print status

if __name__ == '__main__':
    l = StdOutListener()
    auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
    auth.set_access_token(access_token, access_token_secret)

    print "Showing all new tweets for #ahok:"

    # There are different kinds of streams: public stream, user stream, multi-user streams
    # In this example follow #programming tag
    # For more details refer to https://dev.twitter.com/docs/streaming-apis
    stream = tweepy.Stream(auth, l)
    stream.filter(track=['ahok'])