Weather Forecasts with Json

When continuing to learn how to use API’s I ran into yet another issue with a service the book recommended me to use to be able to send data back and forth with web applications. The website openweathermap.org is a website that allow free access to weather forecasts that you can include in your software. OpenWeatherMap uses JSON, JavaScript Object Notation, to be able to send weather information to your product.

The books instructs you to visit this link:

http://api.openweathermap.org/data/2.5/forecast/daily?cnt=7&units=meteric&mode=json&q=London

But once you visit it you will encounter this error:

error401.png

So, using my investigation skills, I looked up what the error meant. It’s exactly what it says in the error message. Error 401 is basically letting you know that you do not have a valid API key/id to be able to access their API for your program. When I first came across this information, to be honest, I just completely skipped this project and thought it just wasn’t going to work. Yesterday, while writing this blog post, I got an epiphany about how I was going to make this work. I went back to the site and read the error again and decided to follow the link to create an account on the openweathermap website and generate an API key. I’m glad that I took it upon myself to go back and research because the process is very similar to my last project of me hacking Twitter’s API. After creating an account and obtaining a key I did some research on YouTube about how to insert the key appropriately in the code to get weather results. I came across the YouTuber “siddhesh kadam” and in this video:

https://www.youtube.com/watch?v=lcWfSn6-m_8&t=53s

He explains how to go about getting an api key, getting weather results in your browser, and getting weather results in your program. You can follow along with his code example if necessary, but I stuck with the code in LPWRP. The code in the book is a little outdated so I had to make some adjustments for it to work. I also got some insight from my twitter friends @lord_death216, @martinblore, and @mrgrueny for figuring out some issues I was having.

 

The first part of this program shows how python can pull information from the internet using the url-lib.request module:

The API key in here has been deleted so don’t go trying to exploit anything because I will find you. Jk. (; 

The API key in here has been deleted so don’t go trying to exploit anything because I will find you. Jk. (; 

When ran you should get a similar output to the one shown below:

Screen Shot 2018-03-09 at 7.22.41 PM.png

The data is a bit hard to read because it is in string format but the JSON module can load this information and return a dictionary that contains the various parts of it making it a little easier to read. An example of this is shown below:

(It still looks like a mess to me but whatever lol)

(It still looks like a mess to me but whatever lol)

I can now get any information that I want from forecast_dict data structure. For the last part of this program the book produces this code for us to be able to print out a seven-day weather forecast:

Screen Shot 2018-03-09 at 8.03.47 PM.png

Unfortunately, I believe it was a bit outdated. When ran the stack trace reviled these errors:

city.png

A KeyError just means that specific key does not exist. In this case it would be ‘city’ which was not in the dictionary for this program. After discussing with a few of my twitter friends, @lord_death216, @martinblore, @mrgrueny, they helped me find a solution to this issue. I changed ‘city’ to ‘sys’ and tried running the code and this was output:

Screen Shot 2018-03-09 at 8.29.48 PM.png

I am currently looking for the fix this list key error. I’ve found that when I practice my research skills when I find an error help with learning more about the language. After more revisions in the code, with the help of @martinblore, we got a more simplified version of the code to give us similar results to the first two programs but with more intelligible results.

Screen Shot 2018-03-09 at 8.03.47 PM.png

This version of the program prints a description of the weather, the temperature in kelvin, the pressure, humidity, and wind speed. You can type any city into the program and it will produce the same results. This project is a really great starter project for those looking to dive into using JSON, APIs, and python and to add to your portfolio.  

Shoutout to my hometown!! I miss the weather.

Shoutout to my hometown!! I miss the weather.