Python Jumpstart by Building 10 Apps Transcripts
Chapter: App 6: LOLCat Factory
Lecture: Downloading cats

Login or purchase this course to watch this video and the rest of the course contents.
0:01 The next thing to do is to download the cats. So let's write a method called download_cats().
0:08 And we are going to need to pass this folder that we created. So down here I'll make some room for the method,
0:19 so just for a moment let me write pass so we can talk about it now one possibility is I could write the logic
0:26 here entirely in this method and I could maybe write a few other methods within this file, and that would solve the problem.
0:35 Downloading the cats from the internet and saving them to our folder but that would make it not very easy for other people
0:42 other programs that maybe also wanted to download cats so as we have seen in other apps let's go and actually put this into a separate module
0:51 so we'll come over here and add another Python file, I'll call it cat_service and in the cat service we are going to have a method,
0:58 we can call it whatever we want, let's just say something like get_cat(), and it's going to take a folder to save the cat too,
1:04 and it's going to take a name for the cat, for now let's just say hold on, we are not going to write the code here but use it back in this area,
1:12 so back in the program file, we'd like to call that get cat method form our cat_service so we would say something like cat_service,
1:20 and of course, just like any module we have to import it, now we could go to the top and write import cat_service,
1:26 or as you've seen PyCharm will do that for us, watch the line numbers change when I hit enter, there you go, at the top it's imported,
1:33 cat service just like any other module and so now we can say get cat and pass the folder and we are going to need to pass a name
1:40 and I am just going to leave that empty for a second. Now before we go and implement this cat_service,
1:44 let's talk about how we are going to get these cats, what we're going to do with them,
1:47 so we are going to get eight cats and let's make this configurable so I'll create a variable called cat count and let's set that to eight,
1:55 we could make this a parameter we passed to this method but we'll just sort of put it here locally,
1:59 now I'd like to loop over something and for each one of those I'd like to go and get a cat, well, what do I look over, I say for i in... what?
2:10 so Python has a couple of cool methods that will build what are called iterable sets that can be used in for in loops and more Pythonic structures
2:20 such as list comprehensions and so on, out of just parameters, so we don't have a collection
2:25 but we can go over here and say for i in range from 1 to cat count. Now before we get into this part,
2:32 let's just do a little print and see what we get back so this is going to return an integer, I want to call it i,
2:39 and one of the things we might ask is is this going to go from 1 to 8, maybe the end point is inclusive,
2:49 maybe it's sort of upper bound but not included, let's just do a little print i to find out and while we are at it,
2:55 let me show you another cool little trick here, so we'll run this and you can see it's 1 to 7 so 1 to 7, actually that's not what we wanted,
3:04 we wanted 1 to 8 so let's put a +1 for a cat count here, excellent now it goes 1 to 8 but notice, when I run this it's kind of like all up and down
3:13 like every print is a new line so there is something you can do on the print statements where you can figure the endings,
3:20 so I can come down here I can say end = normally the end is like a \n anew line character,
3:26 but I can say the end is just going to be a comma with a space, and now when I run it we actually just print this out on one line,
3:32 separated by a comma and a space, ok, so it looks like we got the right number of cats working, now the next thing to do is to come up with the name
3:40 so that should be pretty easy we won't be too creative here, we'll just call this lolcat_ and here we'll just put the number like,
3:46 lolcat_1, lolcat_2 and so on so we'll do a format and we'll do i so let's pass this name along here, right so that should be everything we need to do
3:56 to download the lol cat from the program file, now we just have to implement the details of cat_service.get_cat().
4:03 Now, before we move on to writing cat_service.get_cat() notice there is some squiggles here
4:09 and PyCharm is actually telling us lolcat is a misspelled word so PyCharm will actually find spelling errors in variables,
4:17 in methods, in strings and so on, so let me just put a string here it says I went to the house and misspell house here,
4:25 so I can go over here in PyCharm and hit alt+enter and it will say you know what, that word is a misspelled maybe you should use house
4:33 and it will do similar stuff for parameters for methods and so on, and that's really cool, sometimes,
4:39 there is a word that is actually the correct word but you know, PyCharm doesn't believe in lolcats so we need to tell it
4:45 hey this is a properly spelled word, so I can come over here and say save lolcat to my dictionary
4:50 which is just a local file associated with your PyCharm project and now it won't give me a little squiggly and say there is something wrong.
4:58 So this spelling correction stuff is really nice I actually find it super helpful for when I have misspelled the method
5:04 especially if it's like in public API or something. All right, let's write the first part of this get_cat() method,
5:10 so the first thing we are going to have to do is get the cat from the internet and we are going to return it as some kind of data
5:17 a binary stream or a byte array or something like this so we'll write a method called get_data_from_url()
5:24 and this method could be used like a binary data for many url it doesn't have to be just cats come over the url parameter here
5:32 and so we'll say the url is and I happen to know a url that we can use that will return a random lolcat.
5:39 So here we have this web service and we are going to call it and it will return a small set of random lolcats, let's go and pass this here,
5:48 now obviously, this method doesn't exist so let's go write it, so let's do what we've done before and use the requests package
5:58 from PyPi to download the data from this url, so we'll say response = requests, now again,
6:06 request is an external package we have already installed it with pip but we still need to import it at the top of this file that's easy enough to do,
6:14 then we can do a get() and we'll pass the url. On the response object we have a raw,
6:20 and the raw item is just the binary stream of data that we want to work with, so we can return this to the caller
6:27 and then they can go save it or work with it memory, whatever they want to do with that data. Now it turns out this is almost right,
6:34 there is actually a problem with this even though it might be the most straight forward way of thinking about doing it
6:39 and we are going to come back and fix that but first let's move on and talk about saving this binary data to a file.

Talk Python's Mastodon Michael Kennedy's Mastodon