cleaned a few things up

This commit is contained in:
Mark McIntyre 2022-09-17 17:50:55 -04:00
parent 74a5f5a710
commit 7ec849b463

View File

@ -36,33 +36,6 @@ class WeatherApi:
api_key = fp.read() api_key = fp.read()
self.api_key = api_key.strip() 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): def get_user_preferences(self, nick):
try: try:
with open(self.user_preferences_filename) as fp: with open(self.user_preferences_filename) as fp:
@ -92,12 +65,42 @@ class WeatherApi:
with open(self.user_preferences_filename, 'w') as fp: with open(self.user_preferences_filename, 'w') as fp:
json.dump(user_preferences, 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) user_preferences = self.get_user_preferences(nick)
units = user_preferences[nick]['units']
if not location: if not location:
location = user_preferences[nick]['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 # URL for current conditions
forecast_url = f"{self.weather_api_url}/forecast.json?key={self.api_key}&q={location}&days={days}&qai=no" forecast_url = f"{self.weather_api_url}/forecast.json?key={self.api_key}&q={location}&days={days}&qai=no"