From 7ec849b463f41fd27fe71cd81da3ad5b182e0818 Mon Sep 17 00:00:00 2001 From: Mark McIntyre Date: Sat, 17 Sep 2022 17:50:55 -0400 Subject: [PATCH] cleaned a few things up --- weather.py | 59 ++++++++++++++++++++++++++++-------------------------- 1 file changed, 31 insertions(+), 28 deletions(-) diff --git a/weather.py b/weather.py index 0630dc8..d2919ca 100644 --- a/weather.py +++ b/weather.py @@ -36,33 +36,6 @@ class WeatherApi: api_key = fp.read() self.api_key = api_key.strip() - def get_current_conditions(self, nick, location=None, units='metric'): - user_preferences = self.get_user_preferences(nick) - - if not location: - location = user_preferences[nick]['location'] - - # URL for current conditions - current_url = f"{self.weather_api_url}/current.json?key={self.api_key}&q={location}&qai=no" - - # Make the request and get the results - current_request = requests.get(current_url) - current_data = current_request.content - current_json = json.loads(current_data.decode('utf-8')) - - # Different formats for different units - current = current_json['current'] - location = current_json['location'] - - current_output_location = f"{location['name']}, {location['region']}, {location['country']}" - - temp = current[f"temp_{self.unit_suffices[units]['temp']}"] - wind_speed = current[f"wind_{self.unit_suffices[units]['rate']}"] - - current_output_format = f"{current_output_location} | {temp}°{self.unit_suffices[units]['temp'].upper()} | {current['humidity']}% | {current['condition']['text']} | {current['wind_dir']} {current['wind_degree']}° {wind_speed} {self.unit_suffices[units]['rate']}" - - return current_output_format - def get_user_preferences(self, nick): try: with open(self.user_preferences_filename) as fp: @@ -92,12 +65,42 @@ class WeatherApi: with open(self.user_preferences_filename, 'w') as fp: json.dump(user_preferences, fp) - def get_forecast(self, nick, location=None, days=5, units='metric'): + def get_current_conditions(self, nick, location=None, units=None): user_preferences = self.get_user_preferences(nick) + units = user_preferences[nick]['units'] if not location: location = user_preferences[nick]['location'] + # URL for current conditions + current_url = f"{self.weather_api_url}/current.json?key={self.api_key}&q={location}&qai=no" + + # Make the request and get the results + current_request = requests.get(current_url) + current_data = current_request.content + current_json = json.loads(current_data.decode('utf-8')) + + # Different formats for different units + current = current_json['current'] + location = current_json['location'] + + current_output_location = f"{location['name']}, {location['region']}, {location['country']}" + + temp = current[f"temp_{self.unit_suffices[units]['temp']}"] + wind_speed = current[f"wind_{self.unit_suffices[units]['rate']}"] + + current_output_format = f"{current_output_location} | {temp}°{self.unit_suffices[units]['temp'].upper()} | {current['humidity']}% | {current['condition']['text']} | {current['wind_dir']} {current['wind_degree']}° {wind_speed} {self.unit_suffices[units]['rate']}" + + return current_output_format + + def get_forecast(self, nick, location=None, days=5, units=None): + user_preferences = self.get_user_preferences(nick) if not units else 'metric' + units = user_preferences[nick]['units'] + + if not location: + location = user_preferences[nick]['location'] + + # URL for current conditions forecast_url = f"{self.weather_api_url}/forecast.json?key={self.api_key}&q={location}&days={days}&qai=no"