30 Days of Python - Day 20 - Using Google Maps Geocoding and Places API - Python TUTORIAL

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey there welcome to day 20 and this one we are going to be using the Google Maps API to extract a geo code location that is latitude and longitude to find places in that area using the places API so let's go ahead and jump in and we're gonna be using the Google Maps platform on Google cloud and for what we're doing here the cost should be nothing it should be absolutely free if for some reason your system or your calls into being a lot more than what I'm doing here then you get a free three hundred dollar credit that Google Maps platform just gives to everybody it's not like this is a sponsor post or anything like that it's just they give you this free credit just for signing up on their cloud platform so go ahead and go to cloud.google.com and sign in and once you sign in you're gonna see something like this now make sure that you create a new project up here it's probably gonna ask you to do that and then also activate your billing again so we can actually use these api's but if you for some reason are uncomfortable with keeping your billing on there after you do this just delete that card and you shouldn't be billed for anything or just contact them to make sure that that happens google cloud in my experience has been very responsive to that because they want people to learn how to use their platform no surprise there okay so with that out of the way what we need to do is actually talk about some of the requirements that we have going number one we're gonna be using the Python request library this is just to do our API calls now this is not required but it's a much easier way to do it next we're gonna be using Python pandas now this is just to put all of our data in a structure that's a lot easier to work with then like a Python dictionary and it's also super fast so those are the couple reasons that I do that the final reason for pandas is to actually really easily save our data into a CSV file for future reference if we need it so the other note that I want to say is that a lot of the things that we're gonna do here can be used in the Google Maps services client the Python client can actually do it so the purpose of this is not completely just to use the Google Maps API but also to understand a little bit more about how to actually interact with any API and sort of parse the documentation that's on there because they don't always have clients and they don't always keep their clients up to date so luckily for us Google Maps actually it does keep it mostly up to date so that is pretty nice but again we want to actually dive into this a little bit more but even if you are going to be using the Python client library you're gonna want to have to set up the API itself inside of the Google cloud platform alright so let's go ahead and set up our base project inside of 30 days of Python I'm gonna go ahead and do day 20 and I'm going to create a virtual environment inside of day 20 specifically for our Jupiter notebook and I'm gonna CD into day 20 and of course this is mint so you can pick up pretty much at any time just go into day 20 and get all the requirements there so we'll go ahead and do pipi and V Jupiter notebook or rather just Jupiter and requests and pandas so those are the three real solid requirements for this right now with that done let's go ahead and do Pippy and B run Jupiter notebook and we're going to go ahead and create our new new book Python 3 call it geocoding and places API with Google Maps hey there's a couple notes I want to make and that's this first steps we're gonna sign up for Google cloud hopefully you've already done that next we're gonna go ahead and create a project inside of Google cloud again hopefully you've already done that and then we're gonna activate the API services that we want which is geocoding API and places API and then finally we're gonna grab an API key and restrict so this is the primary thing that you need to do before you can actually use their api's all right so inside the Google cloud platform we're gonna go ahead and make sure that we have a project here now my rule of thumb is that if I'm doing research and development I create a project for that every other project that's like potentially a real live production project I create a specific project for that and the reason for this it has to do with billing but also to make sure that if I add people to that project it's not like they have access to everything that I've got and it just kind of makes it a lot easier to work with which i think is why they have projects in general so the next thing is we need to activate the API is that we want this particular project to have access to so we're gonna go ahead and do a search for Google Maps and we're not gonna find anything maybe eventually we will but right now we won't why is that well that's because a lot of the Google services like Google Maps is inside of the api's and services library so if you ever need to use the YouTube API you're gonna find this of course doing an actual Google search outside of GCP will help you find those api's as well and probably even give you this API library page too but it is important to note that going to the right place is critical here it's also important to note that GCP has a ton of services and they'll keep adding to it I don't think it's a useful time a useful use of your time to actually try and learn all of these services but let's go back into the API library and I'm gonna go ahead and search for the two services I need so the geocoding API and we go ahead and command click to open it into a new tab or control-click if you're on Windows and then I'm also gonna open up the places API now there's a couple notes I want to make before we actually enable those back into the library itself if you type out Google Maps in here you will see a bunch of interesting things right so you'll see this Maps embed API we're not going to be doing that because we're not working on a web application that's also true for the Maps JavaScript API if you want to actually visualize the map it takes a lot more programming to get there that's something we're just not going to do right now instead we're just gonna be focused on the data because we only need Python to do that so there's definitely a lot of things inside of Google Maps that allow for this one of the things that you might be like oh I accidentally did geolocation API well we can't do that either we have to use the geocoding API because what that's going to do is give us a latitude and longitude from an address city or zip code or postal code a geolocation requires you to actually have a device's physical location which means you need this to actually work on a device perhaps is a way to do it through the web but again that's a little bit more complicated okay so now clicking on our recently open tabs for geocoding API we're gonna go ahead and click enable and then also places API we'll go ahead and click enable as well so we'll use the geocoding API to say like hey I want to find food in Newport Beach California so we use geocoding to do the Newport Beach part and then places API to do the food part and so that's what we're gonna be doing okay so now that I've got those things what I need to do now is go back into api's and services and actually create my API key so inside the credentials this is where we're gonna do it now Oh auth - requires you to have a web application so some sort of coal bank place so we're not using oauth2 service counts is for various things on a Google cloud platform there are other ways to use service council as well but what we need for our API calls is going to be just a straight-up API key it's just one single key that will pass through our URLs so you might be like hey that sounds kind of kind of dangerous as in it seems like it's just gonna be open for anyone to use so we'll solve that in just a moment I'm gonna go ahead and create this API key and there it is I'm gonna first reference this notice that it says by passing it with a key equals to API key parameter I'll also put that in here as well so let's bring this into our notebook and I'll say API key equals to that string now of course you could try and copy my string and use it but it's probably gonna fail so really quickly you can always close this and you can always delete a key so if you click on the key itself you can just hit delete or regenerate that of course will actually change this key the other part is we want to have key restrictions this is critical to making sure that you don't get a crazy bill this is where those permissions come in right and so a couple of things that you can do is right here right so you can do an 8 IP address so if you actually do a search on Google what's my IP address you can actually paste that value in here and that will actually prevent access from anything but that IP address this is also very useful if you're using a server and all that um you can also use a web page itself so different web pages doing the request again we don't have a web application so I'm actually gonna leave mine is none because I'm just testing this out but I will restrict the API right so API restrictions are gonna be just the two api's that I have so if I search for the geocoding one I should see this you can also type to filter geocoding and then places now I actually will not see these api's if they are not activated at least that's how it's always been so I'm gonna go ahead and save this and now I have my API key I have my API services activated so now I'm actually ready to start doing API calls so why don't I help you actually parse the documentation to be able to use the geocoding API as well as the places API but like many api's the general rule of thumb is you grab some sort of end points some sort of URL some parameters that might go into that URL and then we actually just pass in our API key which we already have and then we actually run our request so those are the questions you need to answer when you're going through the Docs themselves okay so if we look at the docs we've got our Google Maps documentation here and if we just look for geocoding API and do a quick search for that again it's not geolocation it's just geocoding and we scroll down a little bit to the geocoding product okay and then what we're going to want to do is we're going to want to see their sample request here I'm going to go and copy this sample request and back into our no book we've pasted that one in okay so really simple we've got our base endpoint right here I have some stuff called JSON so I'm gonna go ahead and give it let's say data type and I'm gonna give it JSON and I'll pass that in as well and then it's giving me a parameter of address so I'll turn that into a dictionary a parameter address and then in this case it gave me looks like Google's address so I'm going to not leave it in as a string but rather replace those pluses with spaces as in like a more realistic you know lookup that you would do right so somebody would more likely copy and paste that address then the other one on the URL and then finally it has a API key so the key so these are the parameters so key is that API key okay so the reason I knew that these are the parameters was one I actually reviewed the docs already but also in a URL this is how it goes whenever you see that ampersand sign then that's a new parameter in there that's just a general rule of thumb when it comes to URLs so I actually need to convert the parameters themselves into being actual URL parameters so I'll call this URL params and that's going to be equal to well we actually have to import another thing in here so above this API key actually I'll put it here so it's kind of in a logical order in a way we're gonna import from URL Lib parse we're gonna import URL encode this is something I use a lot so URL and code of those params and now we can actually see what those params are so if I run this we gotta make sure we run each cell and if I run this I now see that this is either roughly identical or exactly identical to this when I say roughly is because sometimes the URL parameters don't necessarily go in the order you may expect I believe they'll probably go in alphabetical order but that also might not be true okay so now I've got my parameters set and ready I no longer need the sample of course so I can get rid of that one and actually have my new URL is equal to well we need to put this F string in here and it's gonna be our endpoint and then we need to use a question mark and then finally our URL params and put that string at the end there hit enter so I print this out so typically speaking when your URLs have parameters you need to have a question mark and that will start all of the URL parameters as we did so notice the ampere stamps in there and URL encode makes that happen for us from a simple dictionary so now we have our actual URL but this isn't quite what I want right so I'm gonna go ahead and copy this and define extract lat Ling well as in latitude and longitude so that long is probably a better way to say it okay so we've got all of that same data in there and I'm actually gonna pass the data type in here as well another option for that data type according to the docs as XML but of course I want to use JSON I think JSON is a lot easier to parse and the parameters well the real parameter is just our query string here so this is really a address or postal code all right and that will just pass in here so if it's invalid we will get a response from the back end that is invalid okay so now what I'm just going to do is return that URL and let's run that and I'll just go ahead and say the exact same URL we did again this is just a sample for a moment and that gives us that URL cool so now let's actually use Python requests really simple R equals two requests that get and URL and if our got status code in range 200 a to 99 then we're going to return the JSON response so return our JSON otherwise we'll return an empty dictionary and if you've been following along with me you know that I typically put those things in reverse and say if it's not in that range okay so if I run that you know what I should get is I get requests not find a surprise there go ahead and bring that in so import requests and run them again and now I actually get some data get some results cool so the data actually want to send back is results so our JSON and this is going to be results instead okay so those are my all of the possible results for this but really for me I actually just want to get the very first result or the result that has the latitude and longitude for me okay so this data we want to give the zeroth element here should be just one anyway and then it gives me a dictionary of items so if I actually do dot cheese as the return I can see what the various keys are so in my case I want to grab the geometry itself so let's return back those results and this time I'll just use the key of jump to geometry on that call and there we go we're actually getting the data that I was looking for which was specifically the latitude and longitude so I'm gonna go ahead and just cut this out and paste it up here and then also include the location here and that should actually return back my latitude and longitude values so I actually want to put this into my flat lawn dictionary and I'll go ahead and try to set that because in some cases it will be a successful look up but it won't find that data so I'll just go ahead and say accept so any sort of exception I'll just say pass and then we'll return that dictionary there now I could actually return those actual values right so I could do get flat and flat lank get lang or longitude okay so that might be a better way to go about doing this so let's go ahead and try this out and that gives me a couple back of those a lot of tuning longitude and if I mess it up you know by messing up the try block let's put a quote in here or something and bail how it still need good syntax let's try that again with good syntax correct syntax then it sends back none not okay so that's if it's Missy and I write it again and there we go we've got our latitude and longitude and of course that's the using the geocoding API and I did a lot of the work for you there but this actually gives us an opportunity to think about how to actually reverse a URL or reverse what we did here to really understand all of the things that are going into it so let's go ahead and copy this URL here and I'm gonna scroll to the bottom and we'll go ahead and just say to parse and there's gonna be equal to that URL and again this could be any URL itself we're also gonna go ahead and do from URL Lib parse we're gonna import URL parse as well as parse underscore qsl so qsl as in parse query string so let's go ahead and take a look here first and foremost what I'm going to do is I'm going to call URL parse to that to parse URL that we had what we get is a parsed result so this was all we can use in other ways right so if I wanted to I could say scheme equals to that URL to parse dot scheme and I could do that for each other piece net lock that will give you the root domain there including like W W or none of that finally the path the actual resource path this gives me that option I do have params in here in this case it doesn't show me params instead it has what's called a query string which is what we actually passed through here so to do the reversing of what we did up here I actually want to take that params and call them a query string so that means then I'll say query string equals to that query okay and this of course will give me you know that parsed out for on defaults right so it does it for me of course so I need to convert this into a dictionary now so to do that we do query well let's call it query tupple first and it's parse qsl of that query string so after I do that I can see that my query tupple is that and then finally my query dictionary is just calling dict on the temple itself or tuple and then we can take a look at that and there we go so we have the arguments that need to go in as the query string which I called it the URL parameters those are often used interchangeably not always but you know we can call it that so we've got our dictionary here now and I also had my endpoint to grab my endpoints let's actually turn let's add the parsed URL to just using this right here probably should have done that first anyway okay so now I have this parsed URL I can say my endpoint equals two well it's the f string here and that's going to be parse URL that's scheme right scheme and then that's gonna be colon slash slash and then parsed URL net lock and that gives us the domain path that has a leading URL there so then we can do our leading slash excuse me so parse your LDAP path this should give me that new end point so let's actually call this end point and close off string there so you can print out that in point and net lock does not have a K on it okay so there's my route in point so I now have my query dictionary and my route in point so of course I could kind of bring this full circle now and it's it's exactly this up here of course it doesn't actually parse out that data type thing but this is another way to parse a URL without using something like a regular expression you can you can actually grab what that data is and of course I do want to show you this in the documentation as well let's go ahead and do that so jumping back into the documentation so Google Maps documentation where I got that original sample URL I actually want to jump into the developer guide this will show you a little bit more as to what's going on you can see what geocoding is as an example we just showed you what it is and then it gives you you know the API request format right so here we go we've got our endpoint here it gives us that output format and parameters okay so the output format could be one of these two we talked about that and then URLs must be properly encoded okay so we did that we did that with URL in code so the actual things that we need required parameters that's what you need to look for is required parameters they give you a lot of data in here as well and it gives you ways to encode it and you can also do other kinds of things right so you've got the option to pass in address as a parameter or components which I didn't cover but that's okay and then finally definitely your API key right so actually getting better at this will then be like okay what are other parameters I can add in which is bounce language right so some sort of language if you need to pass that language in the region it's in so this is different for different places in the world and again components it has another way to add components and then it then I'll show you actual responses that come back so this is how you would break it down without going through the actual request itself is you can see all of these different responses so there's that key that I used of geometry location.latlong right in natural you can all to grab the formatted address so this would be a cool way if somebody entered their address in and you needed the correct address you could send this to the API get the formatted address suggested to your user and then come back with the actual you know address that that comes from the Google geocoding API okay so that's actually doing a couple really interesting things I think and this is why you actually want to learn how to build these clients yourself is so you can parse API documentation after you do some of your own experimenting just based off of a simple example then I think is the best way to learn it because then you can be like oh well what if I accidentally forgotten a parameter what happens then I will see that a little bit more when we go into the places API so now we're gonna go ahead and use that places API so of course in the Google Maps platform we're gonna click on documentation and just do a quick search for Places API and if we scroll down a little bit we'll see products here you could also scroll down on the platform page and not do a search and find places API as well so I'm gonna go ahead and open this up and then on the left hand side I'm gonna go into the dev guides for place search so if we scroll down a little bit what we'll see is a base endpoint here and of course it has output and parameters on there output can be JSON or XML and the parameters are listed below so I'll go ahead and copy this and bring it into my places API and I'll go ahead and say base endpoint places and I'll pass that in now my output I'll just hard coded as JSON I always want to use JSON as my outputs as I mentioned before it's just a little bit easier to work with and then I'm gonna go ahead and get rid of that question mark parameters so the params I'll set is gonna be based off of a dictionary and so I'll go ahead and look at the parameters I need well any key input like the actual search and then the input type it is which apparently we can search by phone number which is pretty cool but you have to do it in the International format so I'm not gonna do the full number part but it is cool to know that it's there and then I'm also gonna add in a few optional parameters so let's do the first three so key input and input type so we'll go ahead and say key and then of course is our API key the input itself I'm gonna just go ahead and say Mexican food because Mexican food is my favorite and then input type and the options were text query or phone number I'm going to use text query and then I'm gonna go ahead and encode these so prams encoded equals to you our URL and code and these params okay and of course we already imported URL and code a while back up here okay so then my actual places in point is going to combine all these so first off we'll do an F string or each piece and that's gonna be the base in point places and then the encoded parameters and of course I need to actually have a question mark here to make sure that my encoded parameters work yeah let's go ahead and take a look at what this looks like and make sure that there's no errors doesn't seem like there is okay so that of course is the base requirements but we want to go a little bit further than the base requirements and have a few optional parameters and one of them being the location bias and another one being the fields that we actually want from that place data and one of the reasons that we actually did latitude and longitude already was for this lookup here right so there's two different kinds of lookups that we can do and including a location bias is absolutely something we want to include so I'm gonna go ahead and first off used in that location bias and then we'll add in the fields that we want so inside here I'm going to go ahead and above the params I'm going to go ahead and add my first location bias as a point bias okay so the point bias runs like this it's point : and then some argument and another argument of course the arguments are latitude and longitude so I actually am going to set a hard code of the latitude and longitude at first and I'm gonna do it based off of mountain view at this point because that's one that I have readily available but you can use pretty much any of them and then I'll go ahead and add these down here okay so I'm gonna go ahead and pass in my latitude and longitude here and what I can say is say if use circular and of course you circular initially will go ahead and say it's false and then I'll say if you circular then I'll give a radius in meters so some sort of radius and meters I'll go ahead and say a thousand and then my location bias just changes just slightly to being circle and then my radius so the radius number itself at that and that corresponds to this right here okay and then I'll go ahead and add this into my parameters so params and it's going to be location bias and I'll set it equal to add variable of location bias so I open that up and there we've got a more structured URL based off of all this and then of course if I change this to use circular I should actually see they use circle one in there as well cool so I'm gonna go ahead and leave that as false to start the next thing is our actual fields now fields of a are the data we actually want to have come back alright so if we come down here we see that there are a number of items that we might have in here so I'm gonna go off with some of their defaults and I'm gonna add it into my default params as well not necessarily an additional argument that I do like I did with location bias but instead I'm just going to come in here and do something like this and it's fields and then address component name and geometry again the documentation is going to give you some additional options for the field categories and whatnot which is pretty cool so perhaps permanently closed would be one of them that we might want to have on there because we don't want permanently closed items so we can just pass that in here as well and now our URL lookup is quite a bit more complex but you know like we now have a pretty standardized way of formatting these URL lookups and even though just by glancing at it it's hard to parse we can still you know go backwards and parse it much like we did before and it'll still give us that same dictionary which is pretty cool so now let's go ahead and try our out our request so I'll use R equals two requests that get and then this places in point and then I'll just first print out the our status code and then we'll do our JSON to see what actually is returned so I'm getting an unsupported field name address component okay so that obviously was incorrect fields so it's coming back up here these are the basic field categories looks like I probably should have read a little bit more into what that meant so I'll use these formatted address instead and I'll put that in there and try that again and there we go so it gives me a formatted address and it gives me a place that actually exists right so this is not a list of items but rather the closest item to this itself right so this latitude and longitude so that's interesting right now if I actually use the circle circular let's go ahead and see what that does we're go ahead and say true and gives me the same place and if I open it up quite a bit more let's say five thousand radius like thousand meters it gives me another place so it's only given me one actual response back so if I wanted to have more than just one single response what I can do is the nearby search requests so this is just a different end point with roughly the same arguments in here except the location is now a required parameter so you need the latitude and longitude your keys in there and then you are going to pass in a key word of some kind as well so let's go ahead and add this one in as well to see if this might be the actual responses that we want so we'll come down here and say the places in point two equals to that end point again I'm going to use JSON and I'll remove those parameters should put an end quote in the params I need so I'll go ahead and go ahead and do params too just to differentiate it from the other one and key location radius hey Sookie is their location and I think the location is going to be in the same kind of format of before which is just laying we will just double check that in a moment so using an F string here back into a places API yeah so a lotta two longitude exactly the same as before radius how large of a search radius we want in this case I'll go ahead and use the number let's say 1500 or not 15,000 but 1500 meters okay and then the next part of keyword key word and again we'll say Mexican food that's going to be the actual search and then notice that I have a number of things that I can try out in here as well the rank by you can add in a rank by ranked by it must not be included if radius is okay so something to note ok so now I'm going to go ahead and encode these so params two encoded equals two URL encode and those parameters so places your l equals to the base URL or base in point places in points to russia mark and rams encoded or perhaps two encoded we go and then we'll finally do the request so our two equals your requests that get and that URL and then our to JSON hey so we run that and now we actually get a list of items back ok so that's pretty cool that is allowing us to do a more robust search now the difference between these is this one is looking for a specific place the first one is trying to find a specific place and it's gonna give you the best example of that place right so if we scroll up a little bit here and it returns a single place right that's the point of that right and then the other one is gonna find places nearby that general location so once you actually find either one of those places then you can actually go and get a place detail so the place detail is yet another component to this that can allow us to actually get a individual detail item here as we see with this place ID so here's a good example let's go ahead and copy that and bring it over and we'll go ahead and say detail let's call this the you know detail look up and we've got our URL here our URL example and so there's a number of things that I can do let's go ahead and do base in point and like usual I'll just go ahead and cut this out okay so we've got our place idea we've got some fields in there and then our API key so they my params are going to be place ID and you know I'll have to set that somewhere so I'll go ahead and use a variable of place ID I'll actually use their example for the moment until I actually have a way to grab that single place ID cut this out and place ID and then the fields I'll go ahead and leave these as is and changing the equals to key value pair and then of course our API key and here the comma and key is our API key okay and then our actual URL is going to be all of these things as well so let's call this actually the detail base endpoint the detail params and the detail URL detail params encoded URL encode and those params yet again we're going to use an F string down here so base in point and then those parameters and then finally let's go ahead and just do the lookup again our eco sir requests that get a teacher URL and then our JSON to see what that is and then I'm getting a formatted phone number and it's just giving me Google Australia so that's that must be the place ID that it's got so in this case I'll just actually look for one of them that was responded so that fair first one la fiesta let's actually use that actual place ID so right now I actually don't see the ID inside of that lookup based off of the fields that I gave so going back in to the details we might need to actually include the place ID on our search so going back up to our search let's go ahead and find all the parameters here for the fields okay so place ID is one of those fields that we can add in I thought that that might be the case but let's just always use the documentation and very specific things like that and there we go so now there's our place ID here so we'll go ahead and use this one now and replace it there do a search and look up now I'm getting that results so I all I got was my name my rating and my formatted number these fields I have the sneaking suspicion that these fields are identical so I can come in here and use something like formatted address and sure enough they are identical that's pretty cool okay so naturally what I still need to do is turn all of this into a more effective way for doing the lookup not just going one by one and I'll actually do that by creating our own API clients specifically for this but what we wanted to do up into this point was being a little test out the API make some changes as we needed and you know add additional information if we want to based off of their actual documentation because that's likely to change right they're likely even an add either add additional things to remove them or just change how they're formatted so it's really good to always check the documentation when you're trying to do one of these kinds of API lookups especially when it gets a little bit complex like a place because there's just a lot of data points that could be added to that place now I will say that when you add additional fields there is more cost to it I don't remember exactly how the cost works but I do know that they charged based off of how much data you are collecting or looking up and not just based off of the fact that you are doing the lookup so that is something that's interesting to research more but again the vast majority of things that we're doing here aren't ever gonna cost you anything unless they drastically change how the API works but that's that's pretty cool so that's it for this part now let's go ahead and actually turn it into an API client let's go ahead and create our Google Maps API client and I'm gonna go ahead and copy our previous one and the reason I'm doing this is so I just have some of that code to reference we're gonna rename this to my Google Maps API and you might actually want to name it closer to something pythonic so a Python module so Google Maps client something more like that because in day 19 I actually show you how to export this with one single command to turn it into an actual Python file it's not really that hard you can also export it inside of here as well so I mean if you wanted to have it as a Python file that would be the way to do it okay so now let's go ahead and just delete some of the extraneous things that we just simply don't need so some of the cells that are in here just kind of referencing other pieces but the first thing I want to do is actually put all of my imports at the top so let's cut out all of this code for the imports typically speaking that's how you're gonna do it anyway but as you code in a notebook you know sometimes you don't do it that way sometimes you do it more in a logical like hey this is when I need it so I'm gonna import it then but then once you actually get into the final piece that's when you'll change it alright so I'm gonna go ahead and call this just a Google Maps client API and what I want to do is set this as class and we'll call it Google Maps client and it takes in an object here and the object itself well we'll declare a few of those things in just a moment so the first thing that I absolutely want to get is my extracted latitude and longitude okay so I'm going to go ahead and say lat equals to none and engi or longitude equals to none so that's the initial thing that I'm going to use and now I can actually copy the method or the function that I did before and turn it into the class method in here and it will now take a postal code and it will use that same data type now in this case I'm going to go ahead and add in data type up here so every request uses the exact same data type instead of changing it and here okay so this is now going to just be self data type okay so of course the address our postal code well perhaps I want to have the location in here as well so I'm going to go ahead and do address or rather let's call this the location query and we'll give it a none initially so whatever that is I'm going to remove that and put this in as my parameter and here so self dot location query okay so that's a good start we've got our endpoint with the data type at our location query and then finally the lat Lang will be reset here so we'll go ahead and just call this self let's actually call it lat long and we'll give it equal to those things and then I can still return those values but also set them on the class itself so self dot lat equals two lat and self dot long or longitude equals two LG okay so this extraction then will allow me to then use it later okay so perhaps I want to initialize this client this way so go ahead and do in it and we'll do self and now I'm gonna go ahead and say address or the postal code will do ours and keyword arts so basically when I initialize this client I want to actually grab whatever that pulse our code would be and so I'll go ahead and run the super call on in it first and pass in args and keyword args and then I'll go ahead and call self dot extract lat lang before I do that I'll go ahead and say self dot location query equals to the address or pulsar code whatever we're passing in here and then we'll try and extract it and I'll also go ahead and say if the location query is not none because you might not need it so if location query it's not equal to none and we'll go ahead and do that so I'll go ahead and add this in as an optional argument here by passing in as none okay so since we've got that we also have this API key here that is of course another thing that I want to pass in here and this time I absolutely want to make sure that I have an API key so if API key equals to none then we'll go ahead and raise an exception and I'll just say API key is required and then of course my API key will also be a parameter to this object and then I'll go ahead and say self-taught API key equals to the past API key and then now in the future we'll go ahead and just use self dot API key for extracting this latitude and longitude all right so let's go ahead and try out our clients now I'm going to go into the kernel and restart and clear output and I'm gonna go ahead and now do each sell the API key I'm actually gonna change the name of that to Google API key and then we'll go ahead and use our map client below it so I'll insert a cell above or below the client either way it'll go ahead and say client equals to that client I hit enter and of course it's going to give me this exception that I need that API key no surprise there so I'll go ahead and do API key equals two and it's the Google API key of course and I'll hit enter okay so I can print out the LAT lang here but print client lat write client long and we get none no big surprise I actually didn't pass the address or postal code so let's go ahead and do that and this time I'll go ahead and just do Newport Beach CA and I run that and again error that has no attributes data type that's strange so this is this is where the error is happening right here to line 18 and that's this self that data type well I set the data type oh that says data data okay that's a silly error but maybe you caught it if not that's okay so then when I run it I get my client a lot of tude and longitude that's great okay so that is certainly redoing the geocoding API in this client that's a good start next what I want to do is actually do the search but before I do that I'm going to get rid of some of these old cells I no longer need these any longer let's get rid of all that and we'll go ahead and delete those cells next I will do the actual search itself now I did start out where it was finding a single place from text I'm actually not gonna use that one instead what I'm gonna use is a nearby search so that's this right here I'm gonna go ahead and copy that and we'll go ahead and paste it in so define search and it's gonna be self and keyword and I'm gonna leave in a default keyword as Mexican food and then we'll paste this in here and I get a number of things going on okay so first and foremost I'm gonna change this just to endpoint and the data type so self dot data type or the lookup type right should probably be the lookup type not data type but I'll leave it in like that and then my params and self-taught API key location well it's going to be self dot lat and self dot long okay so here's a really good opportunity to change our the way we initialize the lat latitude and longitude based off of the search potentially right so if I wanted to change the latitude and longitude perhaps this is where I would do it so if I add it in let's just add in a location being none all right so if I am going to change this if I say location is not equal to none then I'll go ahead and run that same extraction so this time I'll go ahead and say lat/long equals to what that extraction would be and so I'll actually pass in that same argument into the lat/long here and instead of doing self dot location query I will just go ahead and say if location is not equal to none then my location query as an argument here I'll actually call this lo query to not get it too confusing equals to that location and the default locally will be self dot location query so lo query equals to self dot location query so whatever that default is that I passed up here will be what the extract lat Lang will do and then I'll go ahead and come in here like that okay so that gives my search a little bit more robustness to it it will actually grab this lat/long so that also means that down here I want to actually reference the ones in the class as well so a lot long equals itself sell that and self thought on and then I set them here so instead of referencing the class I reference the local method for those latitude monsoon okay next the actual search radius this also might be an argument I want to pass into the search itself so I'll use a default of a thousand meters and then of course my keyword itself will be this okay so radius we go and params encoded is now our new encoded parameters and then finally our URL I can leave it in s places URL is going to take that endpoint and then the parameters themselves and then let's just go ahead and return the JSON data to start so the request itself I'll leave it in as our and if our status code not in range 200 to 299 will just return back an empty dictionary otherwise we'll return back the JSON perhaps we redefine that or make that a little bit more robust later but as of now this works just fine so the next thing is actually adding the detail lookup so again I'll go ahead and copy this same thing and paste it up here this one is going to be a lot more straightforward so detail and it's gonna be self and place ID and we just want to replace our original place ID so I can have a default value in here if I wanted to so I'll go ahead and do that so I've got my detail look up here now and yet again I will do this same wrist response request argument here and this time it's going to just be our detail URL okay so now I should have a working client let's go ahead and give it a shot it's certainly possible that I mixed up some things based off of my search and how I did this now the detail does not require the actual location at all just the search will okay so we'll go ahead and do client dot search and this time I'll go ahead and say tacos and hit enter I get an empty response so perhaps I have something incorrect in here I probably do and then I will try a different location so I'll do location equals to Newport each again and there we go so I actually did it have a invalid lookup in here and this should be self extract lat lang and passing in this argument here all right so let's do another search and I'm still getting an empty dictionary now this could be a valid response but I'm gonna assume that it's not so what I want to do is actually come in to my request itself and just do print R dot text like I want to see what the request is happening and perhaps I also want to see the URL to okay so right off the bat I see the problem and that's this right here this is the URL so we have a number of issues with how this URL as doing its lookup huh and hopefully you cop these things I did a really fast first thing is the endpoint we need to use that F string second thing is the parameters that I used this should be encoded parameters not just that parameters themselves now I'm going to actually get rid of the print statement and do that run again and now I actually get some responses here pretty cool and you know depending on how I change the location let's try I even wrote the wrong address or wrong search field itself and it still looked in Newport Beach got a lot of Google Maps so if I do something like a vine California it should actually give me a different location for Irvine locator vine California which is cool and of course I get to use a specific address for there as well or you've been like a zip code like the one for a new porch or part of Newport nine to six six zero this time it actually gave me a zero results so it looks like the zip code isn't necessarily working as I hoped so that's not something I really tested but the Newport Beach stuff is which is cool okay so the next thing is actually testing out this detail to just make sure that that's working I see that there's a place ID right here I'll just go ahead and grab that one with my client so insert below and client detail and my place ID equals to that okay so again I get an API key is not find so in my detail I need to reference the correct API key which is the one that's attached to this lookup or to this class itself and let's just verify your health so doing the encoding and everything like that the data type I also want to actually pass in here as if I need to change it to XML then I don't have to do too much customization so let's go ahead and run it again okay so my search worked again and my detail lookup also worked okay and it gave me the fields that I declared okay cool so naturally I can actually adjust how this is done so I can come in here and say fields equals two and use these as individual string items in here so you don't have to always write out the comma separated string I actually prefer doing it this method so then I can see what other string values that I might want to have and then down here we just say you know we just join it like that so join fields the actual argument itself and I actually don't think I need that space let's try that again and this time it seemed to work but to really verify that it's working let's go ahead and restart and clear the output and then run this again and there we go so it is working so now I have search detail and the actual laying extraction so this extraction did work for the most part it didn't actually work on our zip code zip code address there might be additional parameters that we need to look up to have a zip code or pulser code work there in my testing I thought that it actually did work just by passing the zip code itself which in this case it did not right so nine to six six zero is the postal code for oh and there you go so you actually do have a postal code and that is working let's get rid of this location here and searching tacos and again it's returning zero results based off of that latitude longitude perhaps it's just looking in too narrow of a location so you might need to you know expand this to something different or the actual region itself like the radius for the search maybe it needs to be much larger let's try that larger radius and I'll do like 25,000 think that's the max and there we go yeah so it was the actual radius itself so so the zip code did look it up fine it's just the search radius that I had was way too small so perhaps I would use something a little bit larger by default maybe 5,000 meters something like that instead of 25,000 that's kind of a big radius to search okay so 5,000 meters is now pulling through cool and then of course my client detail works based off of whatever item that's in here so now I'm going to go ahead and delete the rest of these cells because they were copied over and we'll go ahead and to edit and delete cells and there is our API client now of course this is not nearly as full featured as the official one by Google Maps right so the Python client for Google Maps not only has the places API as well as the geocoding API but it has several other api's as well and realistically that's probably the one you would end up using in the long run and they have documentation and all that but now that you know how to make your own client it's a really cool opportunity to do this for many other services that's kind of the point here is to get really comfortable with building these clients so that you're just very resilient to them changing and you can change them whenever you need to so there's absolutely a number of things that we can do to improve this client and I believe that to you one of them being how we actually formulate our endpoints themselves another one being how we actually call our requests notice that we've done roughly the same thing a number of times in here that's encoding the parameters that's doing a request now a better client would not do it multiple times instead he would do it one time and change it as it needs to right so that is definitely a nice little challenge for you the next thing is how do you actually enrich a panda's data set using this now even if you haven't used pandas enough that is something that I think you would want to work towards so make a note of that is enriching a panda's data set using this Google Maps API client that we created here so that's it for this one and thanks so much for watching day 20 hopefully got a lot out of this if you end up fixing the Google Maps API clients like the challenge I left you with please let me know in the comments the gist or the actual repo that you have on github because I would love to take a look at them and maybe give some pointers directly into your repo I can't say for instance I will do that for everyone but maybe the first few that do actually comment I'll be able to look at and give those recommendations thanks again I'll see you and the next one
Info
Channel: CodingEntrepreneurs
Views: 48,099
Rating: 4.9278555 out of 5
Keywords: djangourlshortcfe2018, install django with pip, Django Web Framework (Software), Mac OS (Operating System), Python (Software), pip, beginners tutorial, install python, python3.8, django3.0, python django, web frameworks, install python windows, windows python, mac python, install python mac, install python linux, pipenv, virtual environments, 30daysofpython, beginner python, python tutorial, google maps api, geocoding api, places api, google api library, gcp, google cloud, jupyter
Id: ckPEY2KppHc
Channel Id: undefined
Length: 63min 0sec (3780 seconds)
Published: Fri May 01 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.