|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
""" |
|
**Get Weather Data using OpenWeatherMap API** |
|
โ Commands Available - |
|
|
|
โข `{i}weather` <city name> |
|
Shows the Weather of Cities |
|
|
|
โข `{i}air` <city name> |
|
Shows the Air Condition of Cities |
|
""" |
|
|
|
import datetime |
|
import time |
|
from datetime import timedelta |
|
|
|
import aiohttp |
|
import pytz |
|
|
|
from . import async_searcher, get_string, udB, ultroid_cmd |
|
|
|
|
|
async def get_timezone(offset_seconds, use_utc=False): |
|
offset = timedelta(seconds=offset_seconds) |
|
hours, remainder = divmod(offset.seconds, 3600) |
|
sign = "+" if offset.total_seconds() >= 0 else "-" |
|
timezone = "UTC" if use_utc else "GMT" |
|
if use_utc: |
|
for m in pytz.all_timezones: |
|
tz = pytz.timezone(m) |
|
now = datetime.datetime.now(tz) |
|
if now.utcoffset() == offset: |
|
return f"{m} ({timezone}{sign}{hours:02d})" |
|
else: |
|
for m in pytz.all_timezones: |
|
tz = pytz.timezone(m) |
|
if m.startswith("Australia/"): |
|
now = datetime.datetime.now(tz) |
|
if now.utcoffset() == offset: |
|
return f"{m} ({timezone}{sign}{hours:02d})" |
|
for m in pytz.all_timezones: |
|
tz = pytz.timezone(m) |
|
now = datetime.datetime.now(tz) |
|
if now.utcoffset() == offset: |
|
return f"{m} ({timezone}{sign}{hours:02d})" |
|
return "Timezone not found" |
|
|
|
async def getWindinfo(speed: str, degree: str) -> str: |
|
dirs = ["N", "NE", "E", "SE", "S", "SW", "W", "NW"] |
|
ix = round(degree / (360.00 / len(dirs))) |
|
kmph = str(float(speed) * 3.6) + " km/h" |
|
return f"[{dirs[ix % len(dirs)]}] {kmph}" |
|
|
|
async def get_air_pollution_data(latitude, longitude, api_key): |
|
url = f"http://api.openweathermap.org/data/2.5/air_pollution?lat={latitude}&lon={longitude}&appid={api_key}" |
|
async with aiohttp.ClientSession() as session: |
|
async with session.get(url) as response: |
|
data = await response.json() |
|
if "list" in data: |
|
air_pollution = data["list"][0] |
|
return air_pollution |
|
else: |
|
return None |
|
|
|
|
|
@ultroid_cmd(pattern="weather ?(.*)") |
|
async def weather(event): |
|
if event.fwd_from: |
|
return |
|
msg = await event.eor(get_string("com_1")) |
|
x = udB.get_key("OPENWEATHER_API") |
|
if x is None: |
|
await event.eor( |
|
"No API found. Get One from [Here](https://api.openweathermap.org)\nAnd Add it in OPENWEATHER_API Redis Key", |
|
time=8, |
|
) |
|
return |
|
input_str = event.pattern_match.group(1) |
|
if not input_str: |
|
await event.eor("No Location was Given...", time=5) |
|
return |
|
elif input_str == "butler": |
|
await event.eor("search butler,au for australila", time=5) |
|
sample_url = f"https://api.openweathermap.org/data/2.5/weather?q={input_str}&APPID={x}&units=metric" |
|
try: |
|
response_api = await async_searcher(sample_url, re_json=True) |
|
if response_api["cod"] == 200: |
|
country_time_zone = int(response_api["timezone"]) |
|
tz = f"{await get_timezone(country_time_zone)}" |
|
sun_rise_time = int(response_api["sys"]["sunrise"]) + country_time_zone |
|
sun_set_time = int(response_api["sys"]["sunset"]) + country_time_zone |
|
await msg.edit( |
|
f"{response_api['name']}, {response_api['sys']['country']}\n\n" |
|
f"โญโโโโโโโโโโโโโโโโโข\n" |
|
f"โฐโข **๐ถ๐พ๐บ๐๐๐พ๐:** {response_api['weather'][0]['description']}\n" |
|
f"โฐโข **๐ณ๐๐๐พ๐๐๐๐พ:** {tz}\n" |
|
f"โฐโข **๐ฒ๐๐๐๐๐๐พ:** {time.strftime('%Y-%m-%d %H:%M:%S', time.gmtime(sun_rise_time))}\n" |
|
f"โฐโข **๐ฒ๐๐๐๐พ๐:** {time.strftime('%Y-%m-%d %H:%M:%S', time.gmtime(sun_set_time))}\n" |
|
f"โฐโข **๐ถ๐๐๐ฝ:** {await getWindinfo(response_api['wind']['speed'], response_api['wind']['deg'])}\n" |
|
f"โฐโข **๐ณ๐พ๐๐๐พ๐๐บ๐๐๐๐พ:** {response_api['main']['temp']}ยฐC\n" |
|
f"โฐโข **๐ฅ๐พ๐พ๐
๐ ๐
๐๐๐พ:** {response_api['main']['feels_like']}ยฐC\n" |
|
f"โฐโข **๐ฌ๐๐๐๐๐๐:** {response_api['main']['temp_min']}ยฐC\n" |
|
f"โฐโข **๐ฌ๐บ๐๐๐๐๐:** {response_api['main']['temp_max']}ยฐC\n" |
|
f"โฐโข **๐ฏ๐๐พ๐๐๐๐๐พ:** {response_api['main']['pressure']} hPa\n" |
|
f"โฐโข **๐ง๐๐๐๐ฝ๐๐๐:** {response_api['main']['humidity']}%\n" |
|
f"โฐโข **๐ต๐๐๐๐ป๐๐
๐๐๐:** {response_api['visibility']} m\n" |
|
f"โฐโข **๐ข๐
๐๐๐ฝ๐:** {response_api['clouds']['all']}%\n" |
|
f"โฐโโโโโโโโโโโโโโโโโข\n\n" |
|
) |
|
else: |
|
await msg.edit(response_api["message"]) |
|
except Exception as e: |
|
await event.eor(f"An unexpected error occurred: {str(e)}", time=5) |
|
|
|
|
|
@ultroid_cmd(pattern="air ?(.*)") |
|
async def air_pollution(event): |
|
if event.fwd_from: |
|
return |
|
msg = await event.eor(get_string("com_1")) |
|
x = udB.get_key("OPENWEATHER_API") |
|
if x is None: |
|
await event.eor( |
|
"No API found. Get One from [Here](https://api.openweathermap.org)\nAnd Add it in OPENWEATHER_API Redis Key", |
|
time=8, |
|
) |
|
return |
|
input_str = event.pattern_match.group(1) |
|
if not input_str: |
|
await event.eor("`No Location was Given...`", time=5) |
|
return |
|
if input_str.lower() == "perth": |
|
geo_url = f"https://geocode.xyz/perth%20au?json=1" |
|
else: |
|
geo_url = f"https://geocode.xyz/{input_str}?json=1" |
|
geo_data = await async_searcher(geo_url, re_json=True) |
|
try: |
|
longitude = geo_data["longt"] |
|
latitude = geo_data["latt"] |
|
except KeyError as e: |
|
LOGS.info(e) |
|
await event.eor("`Unable to find coordinates for the given location.`", time=5) |
|
return |
|
try: |
|
city = geo_data["standard"]["city"] |
|
prov = geo_data["standard"]["prov"] |
|
except KeyError as e: |
|
LOGS.info(e) |
|
await event.eor("`Unable to find city for the given coordinates.`", time=5) |
|
return |
|
air_pollution_data = await get_air_pollution_data(latitude, longitude, x) |
|
if air_pollution_data is None: |
|
await event.eor( |
|
"`Unable to fetch air pollution data for the given location.`", time=5 |
|
) |
|
return |
|
await msg.edit( |
|
f"{city}, {prov}\n\n" |
|
f"โญโโโโโโโโโโโโโโโโโข\n" |
|
f"โฐโข **๐ ๐ฐ๐จ:** {air_pollution_data['main']['aqi']}\n" |
|
f"โฐโข **๐ข๐บ๐๐ป๐๐ ๐ฌ๐๐๐๐๐๐ฝ๐พ:** {air_pollution_data['components']['co']}ยตg/mยณ\n" |
|
f"โฐโข **๐ญ๐๐๐๐๐๐๐พ๐ ๐ฌ๐๐๐๐๐๐ฝ๐พ:** {air_pollution_data['components']['no']}ยตg/mยณ\n" |
|
f"โฐโข **๐ญ๐๐๐๐๐๐พ๐ ๐ฃ๐๐๐๐๐ฝ๐พ:** {air_pollution_data['components']['no2']}ยตg/mยณ\n" |
|
f"โฐโข **๐ฎ๐๐๐๐พ:** {air_pollution_data['components']['o3']}ยตg/mยณ\n" |
|
f"โฐโข **๐ฒ๐๐
๐๐๐๐ ๐ฃ๐๐๐๐๐ฝ๐พ:** {air_pollution_data['components']['so2']}ยตg/mยณ\n" |
|
f"โฐโข **๐ ๐๐๐๐๐๐บ:** {air_pollution_data['components']['nh3']}ยตg/mยณ\n" |
|
f"โฐโข **๐ฅ๐๐๐พ ๐ฏ๐บ๐๐๐๐ผ๐
๐พ๐ (PMโ.โ
):** {air_pollution_data['components']['pm2_5']}\n" |
|
f"โฐโข **๐ข๐๐บ๐๐๐พ ๐ฏ๐บ๐๐๐๐ผ๐
๐พ๐ (PMโโ):** {air_pollution_data['components']['pm10']}\n" |
|
f"โฐโโโโโโโโโโโโโโโโโข\n\n" |
|
) |
|
|