added the weather.py file
This commit is contained in:
parent
bfd74a28f1
commit
62586e2de6
82
weather.py
Normal file
82
weather.py
Normal file
@ -0,0 +1,82 @@
|
||||
import json
|
||||
import urllib
|
||||
import re
|
||||
import datetime
|
||||
|
||||
|
||||
class WeatherApi:
|
||||
|
||||
def __init__(self):
|
||||
self.weather_api_url = "https://api.weatherapi.com/v1"
|
||||
self.api_key_filename = "./weather_api_key.txt"
|
||||
read_api_key()
|
||||
|
||||
def read_api_key(self):
|
||||
with open(self.api_key_filename) as fp:
|
||||
self.api_key = fp.read()
|
||||
|
||||
def get_current_conditions(self, units='metric'):
|
||||
# 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 = urllib.request.urlopen(current_url)
|
||||
current_data = current_request.read()
|
||||
current_json = json.loads(current_data.decode('utf-8'))
|
||||
|
||||
# Different formats for different units
|
||||
current_output_format = {}
|
||||
current_output_format['metric'] = f"{location['name']}, {location['region']}, {location['country']} | {current['temp_c']}°C | {current['humidity']}% | {current['condition']['text']} | {current['wind_dir']} {current['wind_degree']}° {current['wind_kph']} kph"
|
||||
current_output_format['imperial'] = f"{location['name']}, {location['region']}, {location['country']} | {current['temp_f']}°F | {current['humidity']}% | {current['condition']['text']} | {current['wind_dir']} {current['wind_degree']}° {current['wind_mph']} mph"
|
||||
|
||||
return current_output_format[units]
|
||||
|
||||
def get_user_preferences(self, nick):
|
||||
# Read SQLite DB for default location and units for the nick provided
|
||||
|
||||
def set_user_preferences(self, nick, location=None, units='metric'):
|
||||
# Set the default location and units for the nick in a SQLite DB
|
||||
|
||||
def get_forecast(self, days=4, units='metric'):
|
||||
# URL for current conditions
|
||||
forecast_url = f"{self.weather_api_url}/forecast.json?key={self.api_key}&q={location}&days={days}&qai=no"
|
||||
|
||||
# Make the request and get the results
|
||||
forecast_request = urllib.request.urlopen(forecast_url)
|
||||
forecast_data = forecast_request.read()
|
||||
forecast_json = json.loads(forecast_data.decode('utf-8'))
|
||||
location = forecast_json['location']
|
||||
forecast = forecast_json['forecast']['forecastday']
|
||||
|
||||
# Find the highest and lowest temperature over each 24 hour period
|
||||
forecast_temps = []
|
||||
|
||||
for day in forecast_json['forecast']['forecastday']:
|
||||
# Set the initial high and low values to absurd values
|
||||
daily_temp_range = {
|
||||
'day': day['date'],
|
||||
'high': 10000.0,
|
||||
'low': -10000.0,
|
||||
'conditions': ''
|
||||
}}
|
||||
|
||||
for hour in day['hour']:
|
||||
temp = day['hour']['temp_c'] if units == 'metric' else day['hour']['temp_f']
|
||||
|
||||
if temp > daily_temp_range['high']:
|
||||
daily_temp_range['high'] = temp
|
||||
# We only want the conditions for when the temp is the highest
|
||||
daily_temp_range['conditions'] = hour['condition']['text']
|
||||
|
||||
if temp < daily_temp_range['low']:
|
||||
daily_temp_range['low'] = temp
|
||||
|
||||
|
||||
# Different formats for different units
|
||||
forecast_output_format = {}
|
||||
forecast_output_format['metric'] = f"{location['name']}, {location['region']}, {location['country']} | {current['temp_c']}°C | {current['humidity']}% | {current['condition']['text']} | {current['wind_dir']} {current['wind_degree']}° {current['wind_kph']} kph"
|
||||
forecast_output_format['imperial'] = f"{location['name']}, {location['region']}, {location['country']} | {current['temp_f']}°F | {current['humidity']}% | {current['condition']['text']} | {current['wind_dir']} {current['wind_degree']}° {current['wind_mph']} mph"
|
||||
|
||||
return current_output_format[units]
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user