The site OpenWeatherMap provides a simple REST API to get weather data. Your ass
ID: 3839959 • Letter: T
Question
The site OpenWeatherMap provides a simple REST API to get weather data. Your assignment is to write a Python script that will get the current weather data for a particular zip code and then print out some of that data in a table. Please use the zip code assigned below
For the zip code 30144, the output would look like this:
To be able to carry out this assignment you will need to register for a free account with OpenWeatherMap. You will provide an id (username) when registering. Once you have registered, you will receive an APIID, essentially your password for using the service.
The current weather data API page tells you how to create a request for the current weather at a particular zip code. Information on the page tells the structure of the data returned. The How to start page tells how to include the APIID and id in a request. Unless you care to convert from Kelvin to degrees Fahrenheit and convert from kilometers per hour to miles per hour in your script, be sure to request ‘imperial’ units from the server.
The data you get back will be encoded as JSON. You will need to decode it to access the information contained in the response.
Use the datetime class in the datetime package from the standard library to convert the time stamp included in the data to readable form. The value included in the data is the number of seconds since the epoch (standard Unix timestamp). The displayed value above is the default display of a datetime object created from the raw timestamp.
Script Structure Requirements
Please pay attention to these! These particular requirements are included so that the instructor can test your script using the instructor’s account.
At the beginning of your script assign values to variables user_id and user_apiid. These will be strings with the values of your id and apiid. Do not include your id or your apiid values directly in your code, use the variables instead. When your program is tested, the instructor’s values will be substituted.
Evaluation
Your script will run from the project directory that contains it.
The values of the variables user_id and user_apiid will be modified before running the script.
The requests HTTP package will be installed. However, you may not assume that any other package has been installed beyond the standard library.
Zip code assignment
The last two digits of your KSU ID should be in the first column. Please use the Zip code in the second column.
The purpose of this is so that your programs won’t trip alarms at the web site when being tested.
ID zip code 04 30001 07 30002 08 30003 09 30004 10 30006 12 30011 14 30017 15 30019 16 30020 21 30021 22 30025 30 30026 33 30027 39 30028 46 30030 47 30038 49 30039 52 30048 59 30050 60 30052 63 30054 64 30055 66 30056 70 30057 72 30059 75 30070 78 30072 79 30074 80 30075 82 30079 84 30080 87 30083Explanation / Answer
# -*- coding: utf-8 -*-
"""
Created on Fri Mar 10 21:40:11 2017
@author: raska
"""
import urllib.request
import datetime
import json
default_zip_code="30144"
user_apiid="0af5ba1bb7d8a0cd278d5f40a70bb60e"
url="http://api.openweathermap.org/data/2.5/weather?zip=" + default_zip_code + "&APPID="+ user_apiid + "&units=imperial"
def OpenURL(up_Url):
print("Working on..." + up_Url)
PageSrc = urllib.request.urlopen(up_Url).read()
return PageSrc
def LoadData(PageSrc):
data =json.loads(PageSrc.decode('utf-8'))
return data
if __name__=="__main__":
Src=OpenURL(url)
dictval=DataFromZipCode=LoadData(Src)
FinalVal={}
FinalVal['Name']=dictval['name']
FinalVal['Current Temperature']=dictval['main']['temp']
FinalVal['Atmospheric Pressure']=dictval['main']['pressure']
FinalVal['Wind Speed']=dictval['wind']['speed']
FinalVal['Wind Direction']=dictval['wind']['deg']
FinalVal['Time of Report']=datetime.datetime.utcfromtimestamp(dictval['dt'])
print("Completed")
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.