Weather API Tutorial in Python

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] what is going on guys welcome back in today's video we're going to learn how to use a weather api in python in order to get current information about the weather situation in a certain city for example new york who can get information about the temperature we can get information about the humidity the time the sun rises and sets and so on and so forth so this is what we're going to do today let us get right into it all right now for today's tutorial we're going to make use of the open weather map api which means that we need to visit openweathermap.org and create a new account here without an account we cannot get an api key and without an api key we cannot authorize a request so we're not going to get any responses because of that we need to create an account uh very simple process basically if you're not logged in on the top right here you're going to see a sign in button or a sign up button you click on it you create an account it's not rocket science once you have an account you click on your account name you go to my api keys and hopefully if i don't forget it this is now censored because you should not see my api key but you should have one by default so you don't need to generate one manually however if you want to or if you have to for some reason you can just generate a new key here as well and this is what you're going to need in order to access the api so you're going to have to use this in your python script if you're not recording a video and you're not sharing the code you can just put this directly into your python script i put this in a text file and then loaded it in my code so that i don't have to show the key in the video so you can see here in my development environment pycharm that i have two files the main.py file which will contain all the program logic and the api key text file which contains the api key from the openweathermap api and the first thing we want to do now in the script is we want to start by importing datetime as dt and we also want to import requests now request is not part of the core python stack even though i claimed in a past video that it is um because i thought it was but it's not so we need to install it by opening up a command line cmd on windows the terminal on linux and mac and we type pip install requests like that now requests we're going to use it obviously to send requests to the api to get the responses to use the responses and so on and the datetime module the datetime library is going to be used to take the time stamps which are just numbers for sunrise and sunset and convert them or parse them into a humanly readable format that's the idea of that and we're going to start by defining a couple of constants here because we need to specify the url that we're going to send requests to and for that we need to first know the base url of the api and this is http colon slash api dot open weather weathermap.org slash data slash 2.5 i think this is the current version slash weather and then question mark in order to be able to add a couple of more parameters and those parameters are going gonna be the api key and the city that we're interested in so the api key is whatever it is in your case you can just uh type it here if you don't have to share the code with anyone since i'm recording i'm gonna say open api key in reading mode and then read the content of the file and then the city is going to be finally let's say let's start with london there you go so the final url is essentially just going to be url equals the base url then plus app id equals this app id is the api key plus and because we need to to combine the parameters q equals and q is the city that we're looking for so in order to send a request now all we need to do is we need to say response equals requests dot get a url and we're going to turn this immediately into a json so into a dictionary and we can print the structure of the whole thing now to see what we're working with and you can see here that we essentially have a dictionary with coordinates uh key value pairs you basically so coordinate is a key then we have another dictionary as a value we have weather here we have a bunch of different keys and values here interesting for us could be the weather description so weather and then description is broken clouds in london right now um then we have something like main here where we get the temperature in kelvin this is important because that is a standardized unit we don't have celsius or fahrenheit um then we also have like a minimum and a maximum temperature we have a humidity in percent we have wind speed we have um what do we have here from cis we have the sunrise sunset and also the time zone time zone basically meaning how much do we need to add to this number to get the local time because by default we get the london time so if i change this now to uh new york you're gonna see that we get a different time zone where is this um there you go and this means that when we subtract 14 400 from that number we're going to get the local time in new york because by default we always get the timestamp for the london time so utc i think um so that is that now let's keep this simple here we're just going to make a simple program that prints some information and the first thing is probably the most important one is the temperature so the temperature in kelvin is what we get here from the api so i'm going to call this temp kelvin and actually before we do that i'm going to define a function that is going to allow us to convert kelvin into celsius and fahrenheit so kelvin 2 celsius and fahrenheit given kelvin is going to be return or actually let's use variables for that we're going to say celsius is going to be uh what was it kelvin minus two seven three point one five i think that's the definition and then fahrenheit is celsius times nine divided by five plus thirty two don't ask why i don't know why americans use this even this makes a bit more sense i don't know why americans use fahrenheit to be honest it's like arbitrary uh but yeah okay so we're gonna return a tuple of celsius and fahrenheit that's the basic idea of this function here what's the problem here oh yeah duplicate because i have this code prepared already um but essentially this is going to just convert the unit so we can say temp underscore kelvin is equal to response and it was located at main and then so the key was main and then we had another dictionary and inside of that the key was temp so this is going to be the kelvin value and now the temp celsius and the temp fahrenheit is going to be kelvin a kelvin to celsius fahrenheit temp kelvin there you go i'm going to do the same thing with another one so i can print again the response and you're going to see that there is a value called uh inside of main it's called feels like and feels like is i don't know how it's exactly determined but this basically gives us a temperature um the temperature that it feels like to be in london right now which is maybe not exactly the same as the actual temperature so we're going to say feels underscore like underscore kelvin and that is going to be response main [Music] and then feels like and then we're gonna say feels like celsius feels like fahrenheit is gonna be equal to kelvin to celsius fahrenheit temp ah actually not temp sorry feels like kelvin there you go so that's it for the temperature now let's get to humidity the humidity is essentially part of the temperature as far as i remember or no actually it's part of main as well so we need to say response main humidity and then we're going to say the description this is the basically in words the description of the weather so cloudy or not cloudy or sunny or whatever description is going to be a response and this was now part of weather and this remember this was a list of dictionaries even though it only had one dictionary so zero and then we need to get uh the description and then we have now the sunrise underscore time and this is in response sys sunrise and now in order to get the actual timestamp locally now if you want to have the london time you can have london time if you want to get the local time for sunrise you add response time zone and this also works for negative values obviously because if you add a negative value you basically subtract the value um and all this now is a number and in order to get the actual date time uh from that you type dt datetime from timestamp and i think we should use utc from timestamp because that's the utc time by default um [Music] or actually do we yeah this is how we do it right this is how i did it yeah so then we copy that and we do the same thing for the sunset and the sunset time is going to be the same thing just that we replace this here by set and last but not least but maybe let's put this up here we're going to get the wind speed as well just as a nice fun fact we're going to say response wind is its own dictionary and it has the key value pair speed there you go what's the problem here duplicate code fragment again that's fine now we have all these values and the only thing we need to do is we need to print them nicely so we can say okay print f string temperature in [Music] and now city is temperature celsius point to f as the formatting here and then degrees celsius or and then temperature fahrenheit degree fahrenheit we can copy that temperature in city feels like and then we can replace this with fields like celsius feels like fahrenheit then um actually let's copy this or maybe that's too much let's copy this here the next one is humidity in city and here we only need humidity without a formatter and this is not celsius but percent there you go um i think yeah i'm missing a quotation mark here then let's do the same thing with the wind speed and this is gonna be wind speed and i think it's in kilometers per hour i'm not sure maybe it's in meters per second uh so i don't know actually you you can look that up i think it's actually meters per second isn't it don't you specify wind in meters per second but maybe it's a different unit so you can look that up if you want to um now we can also get a general weather in city and this is going to be the description of the api or from the api so description like that and then last but not least what we're going to display is when the sun rises in the sun sets so sun rises in city and and then sun rise time local time like that and then we can copy that sun sets in city at sunset time local time that should be it now if we run this we can see here the formatting doesn't seem to have worked because we didn't do it so point to f here as well don't forget this one here point to f and now you can see okay we have in new york temperature of 13.23 degrees celsius uh we have humidity of 64 we have a wind speed of hopefully 2.57 meters per second uh or kilometers per hour i'm not sure what the unit here is uh i think it's a metric system though so i don't think that we use something like miles per hour um yeah then general weather into your clear sky we can also change that now to um something i can confirm because i am in vienna so i can tell you if that's true right now we have a temperature of 17.57 uh degrees celsius which seems to be kind of right uh we have clear sky which is also true and we have a sunset at 18 13 which is in an hour and 13 minutes so or 12 minutes so it should be right um yeah it's not yeah seems like the sun will set in an hour um so this works and this can also be combined of course with different videos on this channel for example i have a video on a virtual voice assistant i have a video on a virtual assistant in general without voice and this can be used for example to create a feature in that voice assistant tell me the weather of my city or tell me the weather off a different city and then you can also specify some parameters like maybe you want to get certain values certain values not maybe you want to get a different time zone and so on and so forth but this is how you do api weather calls in python so that's it for today's video hope you enjoyed it hope you learned something if so let me know by hitting the like button leaving a comment in the comment section down below and of course don't forget to subscribe to this channel and hit the notification bell to not miss a single future video for free other than that thank you much for watching see you next video and bye [Music] you
Info
Channel: NeuralNine
Views: 99,287
Rating: undefined out of 5
Keywords: python, weather api, python weather api, weather, temperature
Id: 9P5MY_2i7K8
Channel Id: undefined
Length: 15min 33sec (933 seconds)
Published: Mon Mar 28 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.