96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192 | class WeatherCommands(commands.Cog):
"""
Class that holds weather commands
This class extends `commands.Cog` from discord.
Args:
bot: Discord API client
logger: Logger object for logging purposes
Attributes:
bot: Discord API client
logger: Logger object for logging purposes
hidden (bool): Attribute that determines if this list of
command should show in the help command or not.
If `false`, will show in help.
__cog_name__ (str): Command designation for the help command
"""
def __init__(self, bot, logger):
self.bot = bot
self.logger = logger
self.hidden = False
self.__cog_name__ = "Weather Commands"
self.logger.info(f"Loaded {self.__cog_name__}")
@commands.cooldown(2, 30, commands.BucketType.user)
@commands.command(name='forecast')
async def forecast(self, ctx, *, location: str):
"""
Command that shows weather forecast given a city and optionally a country.
Args:
location: city name, and optionally country (e.g., "Paris, France")
Example:
`!forecast Paris, France`
Usage:
`forecast <city_name>, <country (optional)>`
"""
# Split the location into city and country (if available)
location_parts = location.split(',')
if len(location_parts) < 1:
# Display error message if user does not provide a location
error_embed = skywizz.specific_error('Please provide a location '
'to get the forecast')
await ctx.send(embed=error_embed)
return
city_name = location_parts[0].strip()
country_name = location_parts[1].strip() if len(location_parts) > 1 else None
try:
latitude, longitude = await tools.get_coordinates(city_name=city_name,
country_name=country_name)
city, country, country_code = await tools.reverse_gps(latitude, longitude)
self.logger.debug(f"RETRIEVED: {city}, {country}, {country_code}")
except Exception:
# Display error message if API response fails
error_embed = skywizz.error()
await ctx.send(embed=error_embed)
return
# Construct the API URL
api_url = f"https://api.open-meteo.com/v1/forecast?" \
f"latitude={latitude}" \
f"&longitude={longitude}" \
f"&daily=weathercode," \
f"temperature_2m_max," \
f"temperature_2m_min," \
f"sunrise,sunset," \
f"uv_index_max," \
f"precipitation_sum," \
f"precipitation_probability_max," \
f"windspeed_10m_max," \
f"winddirection_10m_dominant" \
f"&timezone=auto" \
f"&forecast_days=1"
# Make the HTTP request
response = requests.get(api_url)
try:
tools.check_request_status(response)
except skywizz.tools.APIRequestError:
# Display error message if API response fails
error_embed = skywizz.specific_error('Oops! Looks like there '
'was an error fetching '
'weather information '
'for that city...')
await ctx.send(embed=error_embed)
return
data = response.json()
embed = create_forecast_embed(data, city, country, country_code,
latitude, longitude)
await ctx.send(embed=embed)
|