Skip to content

Instantly share code, notes, and snippets.

@thirdtruck
Created February 19, 2021 04:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thirdtruck/dc2eb2154567affd355241f8b0793e6a to your computer and use it in GitHub Desktop.
Save thirdtruck/dc2eb2154567affd355241f8b0793e6a to your computer and use it in GitHub Desktop.
import requests, json, os
class WeatherReport:
def __init__(self, report, weather):
self.current_temperature = report["temp"]
self.feels_like = report["feels_like"]
self.high = report["temp_max"]
self.low = report["temp_min"]
self.humidity = report["humidity"]
self.weather_description = weather[0]["description"]
class WeatherError:
def __init__(self, response):
self.response = response
self.error_code = response["cod"]
self.message = response["message"]
def fetch_weather():
base_url = "http://api.openweathermap.org/data/2.5/weather"
api_key = os.environ.get('OPENWEATHER_API_KEY')
city_name = "<Your City Goes Here>"
complete_url = "{}?appid={}&q={}&units=imperial".format(base_url, api_key, city_name)
response = requests.get(complete_url).json()
if response["cod"] == 200:
weather = response["weather"]
report = WeatherReport(response["main"], weather)
print("Temp: {}F\nFeels Like: {}F\nHigh: {}F\nLow: {}F\nHumidity: {}\nDescription: {}".format(
report.current_temperature,
report.feels_like,
report.high,
report.low,
report.humidity,
report.weather_description
))
return report
else:
print("Error: {}", response)
return WeatherError(response)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment