Python Automation CLI | EVE Python Weather API (Part 1)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey everyone my name is hayden and welcome to my channel in this video we'll be adding a weather utility to our cli program giving us the ability to request the current and forecasted weather for any location in the world so today we're building our first utility into eve it's going to be a weather api that has two endpoints current which displays the current weather for today and forecast which displays the forecast for that week also what you're going to want to do is head over to openweathermap.org this is the api that we are going to use in order to get our forecasts and weather information all you need to do is go to the website go to api we're using the one api call and hit subscribe it's free and then click here so you can say get api key create yourself an account and it will generate you an api key so the other package we are going to use in order to interact with open api is pi open weber map this is just kind of a wrapper around the um api that allows you to call kind of like weather manager get weather at place uh the reason i chose to do this rather using the request packages because we can put in arbitrary strings like this like london gb rather than with open weather api you have to put in your longitude and latitude which is a bit fiddly so for this tutorial we're just going to use this um nice package okay so just opened up our project in pycharm so if you joined us in the last tutorial you should be up to this point where we have a cli that when you type eve and it echoes out this string in here now before we kind of start adding all these utilities and packages it's going to get this file is going to get very big and very long with all the different kind of codes and it's going to become very confusing so what i want to do before we kind of start this is just think about the long term scalability of the eve project and what we can do in order to kind of handle the easier management of all these files so what we don't want is a huge cli.py with everything in it what we kind of want is separate files or directories where we can store things so what i'm going to do is come into the eve folder create a new python package called commands this is where all our commands are going to sit and i'm also going to create another package called service and this is where all our services are going to live so what i want to do with these package structures is basically have all the commands that we can type into eve so for example like eve weather will all live under the commands and directory and all the kind of python logic code will live under the services and python directory so it's kind of a nice separation of responsibilities so our function down here has kind of no idea about the commands or services packages so what we need to do is make click aware of them and load them when this script is loaded and run so what i did is i went over to the click documentation and kind of went down to this complex examples with plugin loading and there is some code in here that basically overwrites an object so i'm going to just basically copy this paste it into our script it overwrites or uses this click multi command and overwrites two functions in it called list command and get commands um and all these do is go for a commands folder try to find any files that start with cmd and sorts them returns them and it that allows click to use those um scripts as a command and it also tries to get command is loading that file and importing cli a function from cli in that file and then making that available to the um package at runtime first of what we need to do is just kind of like edit this for our use case so i've just imported the os package there rather than having a variable here that looks for the command folder what i'm just going to do is set it to um what i'm going to say here is just os.path.join the directory of this file is in which will be eve and then join that on commands and that will um basically look inside our commands folder i'm not really that interested in uh prefixing all of our commands in the command photo with cmd um so we can kind of ignore that i'm just gonna say um doesn't start with um in it because i don't want it to load the init file inside commands i want it to load every other file uh and then all we need to do down here is just um strip out the dot p while that from every file that it loads with nothing and sort them alphabetically and return them so that is now going to um when we run our click package it's going to try to list commands and by listing commands it will loop through the commands directory in here and find anything that's a python file that doesn't start with a double underscore uh saw them alphabetically and return them so again down here this is when we actually run a command it goes off and gets it um so here we've obviously because we've ripped out of their complex example we need to change this from complex dot commands to eve dot command so it's going to load eve dot command and then rather than cmd name we're just going to call ours name and it's going to look for a function in there called cli and import that that is pretty much all we need to do and we just need to make our little function down here aware of it and also you need to do there is say that the class is a complex cli and then we can get rid of this hit pass give this a nice dock strings saying like welcome to eve and you can see if i come down here now and type eve it says welcome to eve and you can see it's tried it was not failed basically which is a good sign um but what it's not doing obviously is loading anything so what we can do now is come into commands and add a new python file called weather which is going to store all our weather commands and and in here i'm just going to import click you might not be able to see that so let me just make that a little bit bigger i'm going to import click i'm going to make a function called cli that just click echo hello everyone and this needs to be a click command with two spaces and a space at the bottom and then you can see here if i clear the screen and type eve now you can see we've got a command called weather because basically what it's done is when i've typed eve it's coming to cli it's because it knows that the class is overwrited here with complex cli it's looped through our commands loaded weather so you can see actually if i change this to for example change the name of this to weather new name for example refactor that and then type eve again you can see our command is with a new name so it's basically just loading that script at runtime and importing everything into it so you can see now i can type eve whether or new name and it will and it will run hello everyone so what i'm going to do is just change that back to obviously not that and call it uh weather so we can actually get rid of that file now and we can put all of our commands for weather in here cool so obviously our weather commands isn't very useful the minutes all it does is print out that so what we want is a one method that shows you the current weather and another one that is um a forecast of the weather so in order to do this all we need to say is um create a click group and this can just be pass and we can give this a nice stock string like uh weather info and then down here we can say that this is a cli dot command this is also a cli.command and now you should be able to see if i come down here and go eve whether it gives me two options now so i've got current and forecast so we can now kind of put all of our python logic code in here um so what i want to do here is essentially talk this this function now talk to the service the weather service that we'll create request the information and get it back so before we do that there's also something in click called a context which basically means rather than having rather than both of these endpoints to have like an option that you can pass like location from you can use a context could kind of store information about um requests or like kind of a currently executing commands and pass relative information to them so what i'm going to do is make location a um location a parameter that we pass through to all the commands um which i'll just store in an object and i'm also going to create a weather which will be our weather object that we will create in a minute um in order to do this i can i just need to type down here click pass context and also tell context to come through here and then i identify context.object equals a context object um and location because obviously we're passing location into the unit file here um but obviously how does location get in here so you can basically click uh you can give options um to say i want to take options on the command line so we can say um we can give two types options you can either type dash l or dash dash location the type of it will be a string and the help text for this will be whether at this location and obviously location will get passed in there we go um so if we go to if we now we can pass context oh i need a click past context there and it click past context there so now all of these commands will get a context um so for example if we do current we can actually ask the context the object dot location and we can actually print this out so if we do click dot echo uh it's going to complain because weather isn't imported so what i'm going to do is create a new file in service a python file called weather this is just going to be an empty class called weather which will fill out in a second um cool let's go back here and then from eve dot service import weather and this can be weather weather what's complaining about there seems to have no effect because i'm missing an equal sign um so yeah you can see here if i come now and go current uh it doesn't print out anything but for example i can well actually let's show you this eve.weather and it says i've got an option i can pass l which is the weather at this location so i can say eve weather dash l uh let's just do london and then current and then you can see it prints out london so you can see that i'm passing this information through to all the sub commands by using this past context hey guys it's hayden from the future here whilst i was editing the footage for this video i realized that setting up the project actually took quite a long time so what i'm going to do is split them out into two parts so the first part which is the part that you've just watched is setting up the entire project and we need to do this because it kind of helps us with the longevity of the product it makes it down the line a lot easier and a lot quicker to maintain and extend and in part two we're actually going to code the weather api so i'll leave a link to part two in the description but be sure to check that out apologies if you want to watch this video and you think you're going to get it all in one and just it was going to turn into a 20 25 minute video if i didn't so i think you would kind of prefer two 10-minute videos that you can skip through rather than one kind of colossal video i think it's a bit intimidating to see a 20-minute coding video and so i'll leave a link in the description be sure to check that one out and we'll actually be doing the coding for the weather api in that one thank you very much for watching guys and see you in the next video [Music] you
Info
Channel: Software Engineer Haydn
Views: 2,809
Rating: undefined out of 5
Keywords: software engineer, software developer, software developer haydn, open weather api, open weather api 5 day forecast, open weather api python, open weather api key, how to use open weather map api, how to get open weather map api key, python weather api, python open weather api, python assistant, how to make python cli, how to create python bot, pyowm tutorial, python get weather, weather forecast using python, python forecasting, python cli click, how to use openweather api
Id: Easd8LamPRE
Channel Id: undefined
Length: 12min 47sec (767 seconds)
Published: Wed Sep 02 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.