-
Notifications
You must be signed in to change notification settings - Fork 0
/
api_v3.py
22 lines (18 loc) · 1003 Bytes
/
api_v3.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# extract value(s) from key's in dictionary
import urllib.request
import json
import datetime as dt
def extract_data():
with urllib.request.urlopen('https://api.weather.gov/gridpoints/RAH/61%2c55/forecast') as url:
data = json.load(url)
iso_date = dt.datetime.fromisoformat(
data['properties']['periods'][0]['endTime'])
utc_date = iso_date.strftime('%b %d, %Y')
time_of_day = data['properties']['periods'][0]['name']
temp = data['properties']['periods'][0]['temperature']
sf = data['properties']['periods'][0]['shortForecast']
df = data['properties']['periods'][0]['detailedForecast']
weather_report = (
f"This is a weather report for North Raleigh: \n\nDate of forecast: {utc_date}\nTime of Day: {time_of_day} \nTemperature: {temp} \nShort Forecast: {sf} \nDetailed Forecast: {df}") #v\nRelative Humidity: {rel_humidity}
with open("weather.txt", "w") as outfile:
outfile.write(weather_report)