 OK, everybody, welcome to Python API requests using JSON. JSON in this case means JavaScript Object Notation. Quick shout out to my channel members. Thank you so much for supporting the channel directly. Click Like, Subscribe, and join if you can, everyone. So today, what we're going to be taking a look at is using the request module and JSON module to do web API requests. Now, an API is an application programming interface. And the website that we're going to be interacting with is called TVMaze.com, and they have information about TV programs. Now, real quick, JSON is something called, I guess I mentioned earlier, JavaScript Object Notation. It is a way for information to be exchanged between programs. It is text-based, but it looks a lot, well, you'll see later. It is basically text-based, but it has a very specific format that is easy to use, especially in Python. Let's go ahead and get started. Now, I am assuming that you've already seen my request demo. If you don't know how the request module works, go watch that first and then come back here. So what I want to do is I want to connect to the TVMaze API. Now, there's a link here. I'll put it down below. It says TVMaze.com slash API brings you to this page. And in this page, it talks about the various API calls that you can do. So it tells you here the root URL is right here. And here are some of the different types of API searches you can do. And there's some other information here as well. Now, for just an average person, you don't really need to worry about all this crazy stuff over here. Let's just focus on the easy stuff over here. Now, I've chosen to use the show single search as an example, just because it's the simplest one that I could figure out. So this is the URL. Now, notice it's not a complete URL because you have to add the HTTP API.TVMaze.com part to it. But if I click this example, I'm going to go ahead and open that in a new page. And what it does is it returns JSON. So I'm going to take a look at this is the raw data for that. It's a little bit hard to see here. But if I click here, now this is something that, I guess Firefox does automatically. So it gives you kind of an idea of what you're doing. Now, JSON is a lot like a dictionary in Python. You've got a key and some sort of value. So ID is 139, URL is this. The name is Girls. This is the example they have. And really good show. I haven't seen it for a while, but it's a really good show. If you get a chance, check it out. Anyway, so this is what the information I should say that we can get back from TVMaze.com about the show Girls. And now, instead of doing this in the browser, what we're going to do is we're going to do it over here in Python, which is pretty cool. So let's go ahead and get started with that. So I'm going to go ahead and use my import requests. Again, this is from my other tutorial. If you haven't seen that, check it out. I'm going to need the URL. And I'm just going to go ahead and basically just copy that from here. So I'm going to go ahead and copy the example. So I know the example works. I'm going to cut off the question equals girls part. Let me go ahead and make this a little bit wider so we have a little bit more to refer to here. And the parameters, the params, you can call it whatever you want, but I'm using params. And so an example that we got earlier was girls. OK, so again, I'm just copying what is over here. I put a capital G here. I don't think that matters. So what I want to do is I want to get a response using the request module. So I'm going to use requests.get and its URL params. Now this response is not text. It is a response object. Again, I explained that in my request demo. Check that out if you haven't. And what I'm going to do is I'm going to say if response.status code equals 200. 200 means that everything went through perfectly. There's no problems. I'm going to go ahead and print the response.text. And that should be our JSON code, or I guess code response that we saw over here. And this is raw data. It's not going to be nicely formatted like this. It's going to be raw data. So I should see that down below. So I'm going to do else print fstring error, and then I'm going to put the status code in here. I hope we won't have that, but just in case response.status code, in case I got the URL mixed up or something like that. So I'm going to go ahead and run this and see what happens. I see there's a little bit of a delay. And now you can see here is the JSON code, or the JSON response. So ID139 URL, blah, blah, blah, blah, which is pretty cool. So very easy, surprisingly easy, actually, if you think about it. So we were able to just using a few lines of code. I didn't even have to use this if stuff here. But let's see, 1, 2, 3, 4, basically five lines of code. I was able to get that and print it out. Now the thing you have to remember is that this is actually text. It's not a Python dictionary. It looks like it, but it's not a Python dictionary. It's text. So we have to be able to pull that information out of there. Now the easiest way to do this that I know of is to convert this text into a Python dictionary, which is surprisingly straightforward. To do that, I'm going to go import JSON, J-S-O-N. And this is a Python module that will let you manipulate JSON data. So what I'm going to do here is I'm going to say the data equals JSON dot load S. Don't forget the S. Load is a different command. See where it says loads STR? So it takes the string. So response dot text. And it will convert that into a data. This data will become a dictionary. It returns a dictionary. So now I can go print data and see what we get here. Response is not defined because I spelled it wrong. Response, ribs zone, response is always embarrassing. But it's good to see everybody does this. So response. And you can see it looks very much the same, because the format's very, very similar. So what I'm going to do is I'm going to go ahead and do import pprint, which is pretty print. I'm going to go ahead and do pprint dot pprint data and run that again. This gives us the data in a formatted kind of easy-to-read method. So again, you can see some of the keys that we can use. So we've got the name of the show as girls. It premiered on a certain time, had a rating. And there's just different things that we can do with this. So let's go ahead and pull out the name and, let's say, premiered. And we'll do the summary. So we've got three keys. Name, premiered, and summary. So remember that. So I'm not going to pretty print that anymore. I'm just going to comment that out. So what I want to do is say name equals premiered equals and what was it? Summary equals. Now remember, this is a dictionary. And if you look closely, we got pretty lucky with this one. And I chose it because this one's pretty simple. These are the keys. And they're first level keys. They're not embedded down further. Now you see there are some dictionaries inside of dictionaries, which makes a little bit more complicated. But I'm sticking to the simple stuff. So the name is going to give us this data. The premier is going to give us this data. And the summary is going to give us this data. So this is just like a normal Python dictionary. So data. And this is going to be name. And I could use double quotes there. It doesn't really matter. Data and premier. Just make sure you spell that correctly. And data, summary. And what I'll do is I'll go ahead and print that out. So print f, when we use f strings, they're very convenient. Premiered on and print summary. So name, premiered on, premiered on. And again, this is a very, very common pattern, like encoding something, like something like this. First we pull out, we get the data somehow from somewhere. In this case, it's a response object. From that response object, we pull out the text that we need. Then we converted it to the data format that we needed. And then we pulled out the individual pieces of data that we need. So the name, premiered, and summary. Now again, I didn't have to, this didn't have to be name. I could have called it show name or something like that. This could have been NP or S. But again, I like to use names that make a lot of sense. So it's very easy to see what my program does. Now if I run this, you can see that I pulled out the information. So the girls, premiered on, and here's the date. And here is the summary. Now note, this summary happens to have HTML tags in it. So I assume this was meant for the web. But anyway, that's a story for another day, how to filter that stuff out. Let's see what else that I want to do. So I could change girls. I'm going to try a couple different shows, just to see what's going on here. Let's try Star Trek. And you can see here. Star Trek premiered on June 24th, 1964. Wow, it was a long time ago. And you can see it talks about James Kirk, the Enterprise, et cetera, et cetera. And let's try Next Generation. Sorry, I'm a big Star Trek fan. Sorry, Star Wars people. Let's see if that pops up. Oh, wow, OK. So Star Trek, The Next Generation, premiered on, what was that, 9? Whatever, 928, September 28th, 1987. And it's also going back, but not quite as far as the original. And yeah, so you can see that this TVMaze website has quite a lot of information, I should say. So yeah, it's pretty cool. And let's see what else. Let's try The Mentalist. Sorry, I'm a big Mentalist fan. The List, as well. Let's go ahead and run that. Oh, very cool. OK, so that is Mentalist premiered on 2008, so 923, 2008. Very, very interesting. Great show if you haven't seen it. It's definitely on Netflix. So there you have it. It's surprisingly straightforward and easy to do this. Now, again, I prepared this ahead of time. I've done this before. So I already knew kind of how to pull the data out. Probably that's the hardest part about this is every website is going to return the data. It's going to be JSON, but what are the keys going to be? How are they going to be nested and all that sort of thing? So you have to kind of play around with pulling the data out. But again, for this one, it's pretty straightforward. It only returns one possibility. And then again, that is why I chose it. Now, there are other open APIs, they're called. And you can just access to them without making any sort of account. This is a nice one for getting started. I would strongly recommend that you not put this in a loop. Because if you mess it up and you make too many requests, you can get blocked. What I might want to do with this program, just to make it fun, I might say show equals input. Please input a show name. And then in here, I would put show. So that way I don't have to, I'm not hard coding. When I run the program, it's going to ask me for a particular show. And I'm going to say billions, because I'm watching that right now. And you can see here, billions premiered on January 17, 2016. Is a complex drama. It is absolutely a complex drama, but it's really good to check that out if you have a chance. Anyway, so that is that. That is how you basically pull data out of an API on a webpage. That provides it. Not all web pages provide this, of course. And yeah, that's it. You pull it out, take the text, convert it to a dictionary using the JSON module. And then you pull the individual pieces of data out that you need, just using standard diction, you know, Python dictionary notation. And then you output the data as it's needed. I'll put all this stuff down below so you can copy it out and try it yourself. So yeah, good luck. Click like, subscribe, join, whatever you can do. And as I like to say, keep on coding. Take care.