Careers

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 […]

Read More

Learn Python with Pj! Part 5 – Building something with the Twitter API

This is the fifth and last 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 […]

Read More

Learn Python with Pj! Part 4 – Dictionaries and Files

This is the third installment in the Learn Python with Pj! series. Make sure to read Part 1 – Getting started, Part 2 – Lists and loops, and Part 3 – Funcitons and strings. I’ve learned a lot with Python so far, but when I learned dictionaries (sometimes shortened to dicts), I was really excited about what could be done. A dictionary in Python is a series of keys and values stored inside a single object. This is kind of like a super array; one that allows you to connect keys and values together in a single easily accessible source. Creating dictionaries from arrays can actually be very simple, too. In this blog, I’ll dig into how to create dictionaries and how to read and write files in the code. Dictionaries Dictionaries in Python are indicated by using curly braces, or as I like to call them, mustaches. { } indicates that the list you’re looking at isn’t a list at all, but a dictionary. shows_and _characters = { “Bojack Horseman”: “Todd”, “My Hero Academia”: “Midoriya” “Ozark”: “Ruth” “Arrested Development”: “Tobias”, “Derry Girls”: “Sister Michael”, “Tuca & Bertie”: “Bertie” } This is a dictionary of my favorite TV shows and my favorite characters in that show. In this example, the key is on the left and the value is on the right. To access dictionaries, you use a similar call like you would for a list, except instead of an element number, you would put the key. print(shows_and_characters[“Ozark”]) would print Ruth to the console. Additionally, both the key and value in this example are strings, but that’s not a requirement. Keys can be any immutable type, like strings, ints, floats, and tuples. Values don’t have this same restriction, therefore values can be a nested dictionary or a list, in addition to the types mentioned for keys. For instance, the following dictionary is a valid dictionary. shows_with_lists = { “Bojack Horseman”: [“Todd”, “Princess Carolyn”, “Judah”, “Diane”], “My Hero Academia”: [“Midoriya”, “Shoto”, “All Might”, “Bakugo”, “Kirishima”], “Ozark”: [“Ruth”, “Jonah”, “Wyatt”], “Arrested Development”: [“Tobias”, “Gob”, “Anne”, “Maeby”], “Derry Girls”: [“Sister Michael”, “Orla”, “Erin”, “Claire”, “James”], “Tuca & Bertie”: [“Bertie”, “Speckle”, “Tuca”, “Dakota”] } In this example, each value is a list. So if we tried to print the value for the key ”Derry Girls”, we would see [“Sister Michael”, “Orla”, “Erin”, “Claire”, “James”] printed to the console. However, if we wanted the last element in the value list, we’d write shows_with_lists[“Derry Girls”] [-1]. This would print the last element in the list, which in this case is James. Dictionaries can be written manually, or, if you have two lists, you can combine the dict() and zip() methods to make the lists into a dictionary. list_of_shows = [“Bojack Horseman”, “My Hero Academia”, “Ozark”, “Arrested Development”, “Derry Girls”, “Tuca & Bertie”] list_of_characters = [[“Todd”, “Princess Carolyn”, “Judah”, “Diane”], [“Midoriya”, “Shoto”, “All Might”, “Bakugo”, “Kirishima”], [“Ruth”, “Jonah”, “Wyatt”], [“Tobias”, “Gob”, “Anne”, “Maeby”], [“Sister Michael”, “Orla”, “Erin”, “Claire”, “James”], [“Bertie”, “Speckle”, “Tuca”, “Dakota”]] combined_shows_characters = dict(zip(list_of_shows, list_of_characters)) print(combined_shows_characters) This is one way to create a dictionary. Another is called Dictionary Comprehension. This one is a little more work, but can be used in a variety of different ways, including using a bit of logic on a single list to generate a dictionary using that original list. Here’s […]

Read More

Faces of Unity – Manuel Sainsily

My parents always told me that art doesn’t pay – especially for minorities – and while I was talented and practiced different artistic hobbies, they hoped I would pursue medicine or law. But, I was obsessed with computers and design software, and was spending most of my time at home playing video games or creating worlds. So, when the time came to choose my path, I decided on a computer science program in Montreal. As a visible minority in a new country, I wanted to integrate myself and make a difference for my people. I started working in the tech industry as a designer, and always hoped to enter the gaming industry one day. Now, Unity allows me to do both design work and my own freelance projects in a very rewarding way, and I’ve found great communities and talented colleagues with similar interests. What’s a cool project you’re working on? A lot of my work involves immersive multisensory experiences and human-computer interaction, but the heart of my artistic process is my Caribbean culture, which connects me with music and nature.

Read More