Twitter Streaming API - Python & Code!

Published: 31 October 2016
on channel: Flavor Of The Month
4,817
26

CODE POSTED BELOW! Click "show more"

Python 3.4 (probably compatible with other versions)

Original Guide & extra Python program options (3rd party website):
http://adilmoujahid.com/posts/2014/07...


Follow the Twitter API instructions on the guide above. This link will point you to where you'll need to set up a free Twitter account and get access to their free Twitter Python streaming API.
  / overview  


The Code! Be sure to change the "APIrelatedkeys" to your API keys from Twitter Streaming API:


#Import the necessary methods from tweepy library
from tweepy.streaming import StreamListener
from tweepy import OAuthHandler
from tweepy import Stream
import json

#Variables that contains the user credentials to access Twitter API
access_token = "APIrelatedkeys"
access_token_secret = "APIrelatedkeys"
consumer_key = "APIrelatedkeys"
consumer_secret = "APIrelatedkeys"


#This is a basic listener that just prints received tweets to stdout.
class StdOutListener(StreamListener):

def on_data(self, data):
decoded = json.loads(data)
print (decoded['user']['screen_name'], decoded['text'].encode('ascii', 'ignore'))
return True

def on_error(self, status):
print(status)


if _name_ == '__main__':

#This handles Twitter authetification and the connection to Twitter Streaming API
l = StdOutListener()
auth = OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
stream = Stream(auth, l)

#This line filter Twitter Streams to capture data by the keyword: 'python'
stream.filter(track=['python'])


0:00 - Introduction to setting up Twitter streaming API via Python
0:24 - Setting up a Twitter streaming API account and obtaining API keys
1:36 - Code modification for cleaner tweet output
2:10 - Customizing search keywords in the program
2:32 - Running the program and concluding remarks


On this page of the site you can watch the video online Twitter Streaming API - Python & Code! with a duration of hours minute second in good quality, which was uploaded by the user Flavor Of The Month 31 October 2016, share the link with friends and acquaintances, this video has already been watched 4,817 times on youtube and it was liked by 26 viewers. Enjoy your viewing!