forked from Bi0force1/Bio-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
weatherAPI.py
247 lines (200 loc) · 11.6 KB
/
weatherAPI.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
# setup imports
import discord
from discord.ext import commands
import aiohttp
import asyncio
from config import WEATHER_KEY
from config import WEATHER_KEY2
# set common variables
intents = discord.Intents.all()
client = commands.Bot(command_prefix="!", intents=intents)
class Weather(commands.Cog):
def __init__(self, client):
self.client = client
# Weather querry and response
@client.command()
async def weather(self, ctx: commands.Context, city: str):
url = "http://api.weatherapi.com/v1/current.json"
params = {
"key": WEATHER_KEY,
"q": city
}
async with aiohttp.ClientSession() as session:
async with session.get(url, params=params) as res:
data = await res.json()
location = data["location"]["name"]
region = data["location"]["region"]
localtime = data["location"]["localtime"]
temp_c = data["current"]["temp_c"]
temp_f = data["current"]["temp_f"]
humidity = data["current"]["humidity"]
wind_kph = data["current"]["wind_kph"]
wind_mph = data["current"]["wind_mph"]
condition = data["current"]["condition"]["text"]
image_url = "http:" + data["current"]["condition"]["icon"]
embed = discord.Embed(title=f"The current weather for {location}, {region}", description=f"The condition in `{location}` is `{condition}` as of {localtime}", colour=discord.Color.from_rgb(83,195,190))
embed.add_field(name="Temperature", value=f"{temp_c} C° | {temp_f} F°")
embed.add_field(name="Humidity",value=f"{humidity}%")
embed.add_field(name="Wind",value=f"{wind_kph} kph | {wind_mph} mph")
embed.set_thumbnail(url=image_url)
await ctx.send(embed=embed)
class Forecast(commands.Cog):
def __init__(self, client):
self.client = client
# Weather querry and response
@client.command()
async def forecast(self, ctx: commands.Context, city: str):
url = "http://api.weatherapi.com/v1/forecast.json"
params = {
"key": WEATHER_KEY,
"q": city,
"days": 3
}
async with aiohttp.ClientSession() as session:
async with session.get(url, params=params) as res:
data = await res.json()
location = data["location"]["name"]
region = data["location"]["region"]
forecastday = data["forecast"]["forecastday"][0]
day = forecastday["date"]
maxtemp_c = forecastday["day"]["maxtemp_c"]
maxtemp_f = forecastday["day"]["maxtemp_f"]
mintemp_c = forecastday["day"]["mintemp_c"]
mintemp_f = forecastday["day"]["mintemp_f"]
rain = forecastday["day"]["daily_chance_of_rain"]
snow = forecastday["day"]["daily_chance_of_snow"]
forecastday2 = data["forecast"]["forecastday"][1]
day2 = forecastday2["date"]
maxtemp_c2 = forecastday2["day"]["maxtemp_c"]
maxtemp_f2 = forecastday2["day"]["maxtemp_f"]
mintemp_c2 = forecastday2["day"]["mintemp_c"]
mintemp_f2 = forecastday2["day"]["mintemp_f"]
rain2 = forecastday2["day"]["daily_chance_of_rain"]
snow2 = forecastday2["day"]["daily_chance_of_snow"]
forecastday3 = data["forecast"]["forecastday"][2]
day3 = forecastday3["date"]
maxtemp_c3 = forecastday3["day"]["maxtemp_c"]
maxtemp_f3 = forecastday3["day"]["maxtemp_f"]
mintemp_c3 = forecastday3["day"]["mintemp_c"]
mintemp_f3 = forecastday3["day"]["mintemp_f"]
rain3 = forecastday3["day"]["daily_chance_of_rain"]
snow3 = forecastday3["day"]["daily_chance_of_snow"]
embed = discord.Embed(title=f"{location}, {region}", colour=discord.Color.from_rgb(83,195,190))
embed.add_field(name=" ", value=f"""**Date:** {day} \n **Low:** {mintemp_c} C° | {mintemp_f} F° \n **High:** {maxtemp_c} C° | {maxtemp_f} F°""", inline=True )
embed.add_field(name=" ",value=f"""Chance of Rain: {rain} % \n Chance of Snow: {snow} %""", inline=False)
embed.add_field(name=" ", value=f"""**Date:** {day2} \n **Low:** {mintemp_c2} C° | {mintemp_f2} F° \n **High:** {maxtemp_c2} C° | {maxtemp_f2} F°""", inline=True )
embed.add_field(name=" ",value=f"""Chance of Rain: {rain2} % \n Chance of Snow: {snow2} %""", inline=False)
embed.add_field(name=" ", value=f"""**Date:** {day3} \n **Low:** {mintemp_c3} C° | {mintemp_f3} F° \n **High:** {maxtemp_c3} C° | {maxtemp_f3} F°""", inline=True )
embed.add_field(name=" ",value=f"""Chance of Rain: {rain3} % \n Chance of Snow: {snow3} %""", inline=False)
await ctx.send(embed=embed)
class Weather2(commands.Cog):
def __init__(self, client):
self.client = client
@commands.command()
async def weather2(self, ctx: commands.Context, city: str):
geo_url = "http://api.openweathermap.org/geo/1.0/direct"
weather_url = "http://api.openweathermap.org/data/2.5/weather"
params_geo = {
"q": city,
"limit": 1,
"appid": WEATHER_KEY2
}
async with aiohttp.ClientSession() as session:
async with session.get(geo_url, params=params_geo) as res_geo:
geo_data = await res_geo.json()
if not geo_data:
await ctx.send(f"Could not find geolocation for city: {city}")
return
lat = geo_data[0]['lat']
lon = geo_data[0]['lon']
params_weather = {
"lat": lat,
"lon": lon,
"appid": WEATHER_KEY2,
"units": "metric" # Change to 'imperial' for Fahrenheit
}
async with session.get(weather_url, params=params_weather) as res_weather:
weather_data = await res_weather.json()
if res_weather.status != 200:
await ctx.send(f"Failed to retrieve weather data for {city}")
return
location = f"{geo_data[0]['name']}, {geo_data[0]['state']}" if 'state' in geo_data[0] else geo_data[0]['name']
localtime = weather_data["dt"]
temp_c = weather_data["main"]["temp"]
temp_f = temp_c * 9/5 + 32
humidity = weather_data["main"]["humidity"]
wind_speed = weather_data["wind"]["speed"]
condition = weather_data["weather"][0]["description"]
image_url = f"http://openweathermap.org/img/wn/{weather_data['weather'][0]['icon']}@2x.png"
embed = discord.Embed(title=f"The current weather for {location}", description=f"The condition in `{location}` is `{condition}`.", colour=discord.Color.from_rgb(83, 195, 190))
embed.add_field(name="Temperature", value=f"{temp_c} °C | {temp_f} °F")
embed.add_field(name="Humidity", value=f"{humidity}%")
embed.add_field(name="Wind Speed", value=f"{wind_speed} m/s")
embed.set_thumbnail(url=image_url)
await ctx.send(embed=embed)
class Forecast2(commands.Cog):
def __init__(self, client):
self.client = client
@commands.command()
async def forecast2(self, ctx: commands.Context, city: str):
geo_url = "http://api.openweathermap.org/geo/1.0/direct"
forecast_url = "http://api.openweathermap.org/data/2.5/forecast"
params_geo = {
"q": city,
"limit": 1,
"appid": WEATHER_KEY2
}
async with aiohttp.ClientSession() as session:
async with session.get(geo_url, params=params_geo) as res_geo:
geo_data = await res_geo.json()
if not geo_data:
await ctx.send(f"Could not find geolocation for city: {city}")
return
lat = geo_data[0]['lat']
lon = geo_data[0]['lon']
params_forecast = {
"lat": lat,
"lon": lon,
"appid": WEATHER_KEY2,
"units": "metric", # Change to 'imperial' for Fahrenheit
"cnt": 24 * 3 # Get forecast for next 3 days (8 intervals per day)
}
async with session.get(forecast_url, params=params_forecast) as res_forecast:
forecast_data = await res_forecast.json()
if res_forecast.status != 200:
await ctx.send(f"Failed to retrieve forecast data for {city}")
return
daily_forecasts = {}
for forecast in forecast_data['list']:
date = forecast['dt_txt'].split(' ')[0]
temp_max = forecast['main']['temp_max']
temp_min = forecast['main']['temp_min']
if date not in daily_forecasts:
daily_forecasts[date] = {'max': temp_max, 'min': temp_min, 'pop': forecast['pop']}
else:
daily_forecasts[date]['max'] = max(daily_forecasts[date]['max'], temp_max)
daily_forecasts[date]['min'] = min(daily_forecasts[date]['min'], temp_min)
daily_forecasts[date]['pop'] = max(daily_forecasts[date]['pop'], forecast['pop']) # Use max pop for chance of rain
embed = discord.Embed(title=f"{city} 3-day Forecast", colour=discord.Color.from_rgb(83, 195, 190))
for i, (date, temps) in enumerate(daily_forecasts.items()):
if i >= 3:
break
maxtemp_c = temps['max']
mintemp_c = temps['min']
maxtemp_f = maxtemp_c * 9/5 + 32
mintemp_f = mintemp_c * 9/5 + 32
rain = temps['pop'] * 100 # Probability of precipitation
# Retrieve condition and image_url for the first forecast entry of each day
condition = forecast_data['list'][i * 8]['weather'][0]['description']
image_url = f"http://openweathermap.org/img/wn/{forecast_data['list'][i * 8]['weather'][0]['icon']}@2x.png"
embed.add_field(name=f"Date: {date}",
value=f"**Condition:** {condition}\n**Low:** {mintemp_c} °C | {mintemp_f} °F\n**High:** {maxtemp_c} °C | {maxtemp_f} °F\n**Chance of Rain:** {rain}%",
inline=False)
if i == 0:
embed.set_thumbnail(url=image_url)
await ctx.send(embed=embed)
async def setup(client):
await client.add_cog(Weather(client))
await client.add_cog(Forecast(client))
await client.add_cog(Weather2(client))
await client.add_cog(Forecast2(client))