Learn Python with Pj! Part 5 – Build a hashtag tracker with the Twitter API
This is the fifth and final installment in the Learn Python with Pj! series. Make sure to read: Putting it all together I’ve completed my Python course on Codecademy, and am excited to put the skills I learned into building something practical. I’ve worked with the Twitter API before; I wrote a few bots in Node.js to make them tweet and respond to tweets they’re tagged in. I thought it’d be fun to work with the API again, but this time do it in Python. I didn’t just want to make another bot, so I had to figure out something else. In this case, I made a bot that can track hashtags being used in real time on Twitter. Here’s my repo containing a few different files, but live_tweets.py is what we’ll focus on for this blog. Let’s talk about how I built it and what it does. import tweepy import config auth = tweepy.OAuth1UserHandler(config.consumer_key, config.consumer_secret, config.access_token, config.access_token_secret ) api = tweepy.API(auth) #prints the text of the tweet using hashtag designated in stream.filter(track=[]) class LogTweets(tweepy.Stream): def on_status(self, status): date = status.created_at username = status.user.screen_name try: tweet = status.extended_tweet[“full_text”] except AttributeError: tweet = status.text print(“**Tweet info**”) print(f”Date: {date}”) print(f”Username: {username}”) print(f”Tweet: {tweet}”) print(“*********”) print(“********* n”) if __name__ == “__main__”: #creates instance of LogTweets with authentication stream = LogTweets(config.consumer_key, config.consumer_secret, config.access_token, config.access_token_secret) #hashtags as str in list will be watched live on twitter. hashtags = [] print(“Looking for Hashtags…”) stream.filter(track=hashtags) Here’s how this all works. First, we import two modules: Tweepy and config. Tweepy is a wrapper that makes using the Twitter API very easy. Config allows us to use config files and keep our secrets safe. This is important since using the Twitter API involves four keys that are specific to your Twitter developer account. Getting these keys is covered in this Twitter documentation. We’ll talk about what’s in the config file and how it works later. The next line defines the variable auth using tweepy’s built in authorization handler. Normally, you’d put in the keys directly here, but since we’re trying to keep secrets safe, we handle those through the config file. In order to call those variables hosted in the config file, we type config.variable_name. Finally, in order to access the tweepy api, we create the variable api with the auth variable from the line above passed into tweepy.API(). Now, the variable api will give us access to all the features in Tweepy’s Twitter API library. You’re invited! Join us on June 23rd for the GitLab 15 launch event with DevOps guru Gene Kim and several GitLab leaders. They’ll show you what they see for the future of DevOps and The One DevOps Platform. For our purposes, we want to find a hashtag being used, then collect the tweet that used it and print some information about the tweet to the console. To make this happen, we’ve created a class called LogTweets that takes an input tweepy.Stream. Stream is a Twitter API term that refers to all of the tweets being posted on Twitter at any given moment. Think of it as opening a window looking out onto every single tweet as it’s posted. We have to make this open connection in order to be able to find tweets that are using our hashtag. Inside LogTweets, we define a […]
