TwitterTrack — tracking tweets using Tweepy #BeginnerProject

Enfa Rose George
4 min readFeb 9, 2019

I was digging through some of my old projects last day and came across a really simple one I did when I got started with python — TwitterTrack.

You might have noticed in the last hackathon or conference that you were attending, on the big LED screen tweets tagging the event are displayed live. I built a really simple basic model of this in my first year. These were some really simple ones that I did initially which really boosted my confidence and belief that I can code too. So today in this blog we are going to try to build the same.

There is no real hard logic building exercise here. Just a simple and right usage of packages and functions.

Things you’ll be comfortable with by the end of the blog:

Functions, creating simple classes, importing and using new libraries, Familiarise yourself with developer programs and dashboards of various companies ( this case Twitter), using and understanding APIs.

Get started with Twitter

As we all know, Twitter is an American social networking service on which users post and interact with messages known as “tweets”.

So Twitter is an application and we are going to build another application to display a specific set of tweets live. So these applications, they need to talk to each other. The interface with which they talk to each other is called an API.

API is the acronym for Application Programming Interface, which is a software intermediary that allows two applications to talk to each other.

So we need to use the Twitter API for an application. Check out this official blog and head back here! This is where you would end up.

Copy your keys and tokens. These are secret passwords exchanged between our application and Twitter, so that Twitter knows at the interface whose asking and if we have the permission to do so.

Go ahead and save them so that we needn’t be bothered with it anymore.

Tweepy

Tweepy is a really simple python library that helps you use the Twitter API.

To install Tweepy

pip install tweepy

First, as we mentioned before, Twitter needs to ensure it’s us whose using the API. So we need to go through authentication. Tweepy handles authentication for us in the simplest way possible. So let’s create a Tweepy Oauthhandler object using our API KEY and API SECRET KEY from before.

auth = tweepy.OAuthHandler(API_KEY,API_SECRET_KEY)

Now tell the OAuthHandler object our secret key.

auth.set_access_token(ACCESS_TOKEN,ACCESS_TOKEN_SECRET)

Now that that’s done, lets get to the main business.

Create a tweepy object using the OAuthHandler from before.

api = tweepy.API(auth)

Let’s check if it works. Let’s print all the tweets from our home timeline.

public_tweets = api.home_timeline()
for tweet in public_tweets:
print(tweet.text)

There you go!

Streaming those live tweets

The Twitter streaming API is used to download twitter messages in real time. It is useful for obtaining a high volume of tweets, or for creating a live feed using a site stream or user stream.

Let’s see how we can do this using Tweepy.

Going by the official blog, we are first going to create a StreamListener class, see this as a person eavesdropping into the live banter in twitter.

#override tweepy.StreamListener to add logic to on_status
class MyStreamListener(tweepy.StreamListener):

def on_status(self, status):
print(status.text)

Now we need to create a stream, a live flow of these tweets.

myStreamListener = MyStreamListener()
myStream = tweepy.Stream(auth = api.auth, listener=myStreamListener)

Now let’s get tracking!

myStream.filter(track=['python'])

and this would keep continuing.

Last word

There is so much more Tweepy can do, and so much more you can do with Twitter data. From simple school projects to understanding the public sentiment to event prediction to audience analysis to understanding consumer needs to maybe even predict whose president after the next election! You have taken the first step. I hope you have lots of fun with tweepy, twitter and especially python!

Drop any comments or bugs in the comments. The code can be found here. Have fun!

--

--

Enfa Rose George

Sipping my coffee in a warm cafe, a book in hand, waiting for my DL model to finish training