Bringing data from giant sites like Twitter, Reddit, Facebook or Youtube is so simple nowadays. They give you this data to include in your apps or just for the hell of it using a tool called an API. Note: This article talks about CONSUMING APIs not creating them. That will be for another time.
The word “API” is a buzzword for most companies and devs nowadays. It’s funny because they’re relatively simple but when describing your project to a new developer it may confuse them.
“Wait I use what to link my reddit account with my twitter feed!?”
Well it’s simple. We use an Application Programming Interface! Don’t let the terminology confuse you. It’s fairly simple!
If you have seen my last post about creating bots and scripts then you know that I briefly talked about how my script connects to the Reddit API to post a gif that was made using a video I pulled off of Youtube. At the time this seemed like a complicated task. Before I even dug into an API I thought that the best way to grab a video off of Youtube using Python was to automate my mouse where to click on a google chrome tab when I was away from the computer. This could be done I’m sure but there are so many problems with this approach:
- You won’t be able to use your computer while it does it.
- If Youtube decides to move it’s search bar 10 pixels to the right some day then your whole script dies.
- Who knows what your script does in it’s spare time.
To even try and use this method is nuts but while writing this post it has gained my interest in trying to make a “You got hacked by a Russian” simulator.
If you ever checked the repo from the previous blog post than you know that I used a web scraper to grab videos from Youtube. I thought this approach would have been more efficient rather than taking 10 minutes to learn the Youtube API. Although the drawback to this approach is kind of similar to the “Russian Hacker Simulator” approach. What if Youtube decides to change the way their search engine looks and change some properties within the web document? This approach works if you are looking for something temporarily or if the site doesn’t have an API. However if you go to:
api.website.com then just forget about web scraping at all. Your life will be so much easier.
What if I told you that all the information that you need can be made with a simple HTTP request? Sounds crazy. It’s almost like going on to your internet browser and searching up
http://facebook.com and getting all the information you need. An example is going to something like
http://api.facebook.com and instead of getting back the beautiful Facebook interface with all of the information rendered in UI elements, it returns you information like this instead:
If you already know what JSON data is then that’s what it returns. For the people that don’t know, JSON is a data format that is extremely easy to parse and grab data from. Although my example is crude and doesn’t go into much detail towards authentication and rate limits, all services are different and you can easily find it on the api page provided with that service. In short, think of it when you connect to
facebook.com you get all this data rendered in a beautiful UI Facebook wants you to see it like. APIs will give you almost the same data but instead of a nice UI, Facebook knows that you just want some info and that’s it. Even if you want to post something to Facebook or your favourite social network, you can do that through an API as well. You can use a request library to do this and gather links on their documentation page which is a common way to go around it but it can get even simpler than that.
APIs can be tedious work especially when you are gathering lots of links that can look like this:
A lot of developers have made simple ways to access APIs through their favourite programming languages. These wrappers span across MANY languages but today I want to talk about using the Reddit API with Python. The best way to do this is with
praw. For this I have created a script that goes on reddit and stores people in real time who say the word ‘alot’ instead of ‘a lot’ in a list. I did this as simple as possible to give newcomers an easy understanding of what’s going on so please excuse the lack of error handling provided.
If you look at the code in the github repo, you will see that a lot of the functions
praw provides allows us to easier use the Reddit API without all of the requests and query strings being used.
praw let’s us simply run:
r.login() instead of having to authenticate ourselves, connect to reddit servers and parse a bunch of data. Modules like
praw does it for us. Most modules are extremely well documented and allow for ease of use with the devs still active on development. Plus it’s open source so if you want to add features then you can do so as well.
How Can They Help You?
Throughout my time programming apps and doing contracts for people, I was always managing data in some way. Especially 3rd party. Being able to grab open data and include it in your apps gives you great skills on how to manage data using 3rd party services as well as giving you the ideas on how APIs work and how you should include it in the lifecycle of your app as well. On a more noob friendly side, it gives you an incentive to build something cool using a service you USE. Too often do I finish courses, tutorials or books that leave you with something that is useless like a terminal calculator or a “Hello *your name*” app. Using APIs will allow you to understand how these protocols work and implementing it shows you how almost all apps: web, mobile and desktop grab data from a service. It’s a topic that goes way past this blog post but if you are still looking for some more information on APIs check these links:
I have created a slack for the coding community! If you are interested in joining then please give me a contact with your email so I can give you an invite :)
If you liked this post please click the 💚 button below. You can follow me on Twitter @LewisMenelaws