IE-555 / fall2023

10 stars 5 forks source link

National Weather Service API #17

Open cmurray3 opened 10 months ago

cmurray3 commented 10 months ago
import json
import urllib3
from time import strftime, localtime

def getWeather(lat, lon):

    '''
    Call National Weather Service API
    See https://www.weather.gov/documentation/services-web-api for more info
    '''

    url = (f'https://api.weather.gov/points/{lat},{lon}')
    print(f'{url=}')

    try:
        http = urllib3.PoolManager()

        response = http.request('GET', url)
        print(response.data)
        print(response.status)

        http_status = response.status

        if (http_status != 200):
            # Error of some kind
            http_status_description = response[http_status]
            print(f'Error getting weather: {http_status_description}')
            return 

        data = json.loads(response.data.decode('utf-8'))

    except Exception as e:
        print(f'Error in getWeather function: {e}')

getWeather(39.7456, -97.0892)
cmurray3 commented 10 months ago

I've attached the Jupyter notebook containing the functions we wrote together in class on Wednesday, Nov. 15. (Github won't allow uploads of .ipynb files, so I had to wrap it in a .zip archive)