Navigate back to the homepage

Using Twython To Connect To The Twitter Streaming API via OAuth

Bulkan Evcimen
August 3rd, 2013 · 1 min read

Photo by Benjamin Balázs on Unsplash

Before you can connect to the Streaming API you need create a Twitter application. This will give you the necessary OAuth credentials. To do this, go to dev.twitter.com/apps, login to your Twitter account then click the Create a new application button and follow the instructions.

To connect to the Streaming using Twython, you need create a subclass of TwythonStreamer

1from twython import TwythonStreamer
2
3class TweetStreamer(TwythonStreamer):
4 def on_success(self, data):
5 if 'text' in data:
6 print data['text'].encode('utf-8')
7
8 def on_error(self, status_code, data):
9 print status_code
10 self.disconnect()

Now we will instanstiate the TweetStreamer class and pass in the oauth details

1# replace these with the details from your Twitter Application
2consumer_key = ''
3consumer_secret = ''
4access_token = ''
5access_token_secret = ''
6
7streamer = TweetStreamer(consumer_key, consumer_secret,
8 access_token, access_token_secret)
9
10streamer.statuses.filter(track = 'python')

The method on_success on the class TweetStreamer will get called for each tweet we receive from the streaming api. The statuses.filter call, will find tweets that contain the word python. Running this script will start printing tweets to the console.

More articles from Bulkan

Building a Twitter Filter With CherryPy, Redis, and tweetstream`

Photo by Brooke Lark on Unsplash Background All the code is available at https://github.com/bulkan/queshuns Since reading this post by…

March 18th, 2010 · 3 min read

Install Shield Silent Installs

Photo by Kristina Flour on Unsplash Install Shield has this nifty feature of being able to install packages in silent mode. This means that…

November 30th, 2009 · 2 min read
© 2007–2020 Bulkan
Link to $https://twitter.com/bulkanevcimenLink to $https://github.com/bulkanLink to $https://instagram.com/bulkan.evcimen