Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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