Skip to content

Commit

Permalink
feat(weather): add language support for weather data retrieval
Browse files Browse the repository at this point in the history
  • Loading branch information
HitaloM committed Dec 12, 2024
1 parent 8e97a85 commit d91a318
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 28 deletions.
20 changes: 10 additions & 10 deletions locales/bot.pot
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ msgid ""
msgstr ""
"Project-Id-Version: PROJECT VERSION\n"
"Report-Msgid-Bugs-To: EMAIL@ADDRESS\n"
"POT-Creation-Date: 2024-12-12 18:33-0300\n"
"POT-Creation-Date: 2024-12-12 18:50-0300\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <[email protected]>\n"
Expand Down Expand Up @@ -1176,7 +1176,7 @@ msgid "<b>User link</b>: <a href='tg://user?id={id}'>link</a>\n"
msgstr ""

#: src/korone/modules/weather/handlers/get.py:31
msgid "Temperature"
msgid "Temperature:"
msgstr ""

#: src/korone/modules/weather/handlers/get.py:32
Expand All @@ -1188,33 +1188,33 @@ msgid "Air humidity:"
msgstr ""

#: src/korone/modules/weather/handlers/get.py:34
msgid "Wind Speed"
msgid "Wind Speed:"
msgstr ""

#: src/korone/modules/weather/handlers/get.py:46
#: src/korone/modules/weather/handlers/get.py:45
msgid ""
"No location provided. You should provide a location. Example: "
"<code>/weather Rio de Janeiro</code>"
msgstr ""

#: src/korone/modules/weather/handlers/get.py:55
#: src/korone/modules/weather/handlers/get.py:112
#: src/korone/modules/weather/handlers/get.py:61
#: src/korone/modules/weather/handlers/get.py:125
msgid "Failed to fetch weather data."
msgstr ""

#: src/korone/modules/weather/handlers/get.py:66
#: src/korone/modules/weather/handlers/get.py:72
msgid "No locations found for the provided query."
msgstr ""

#: src/korone/modules/weather/handlers/get.py:79
#: src/korone/modules/weather/handlers/get.py:85
msgid "Please select a location:"
msgstr ""

#: src/korone/modules/weather/handlers/get.py:93
#: src/korone/modules/weather/handlers/get.py:99
msgid "Session expired. Please try again."
msgstr ""

#: src/korone/modules/weather/handlers/get.py:119
#: src/korone/modules/weather/handlers/get.py:132
msgid "Incomplete weather data received."
msgstr ""

Expand Down
39 changes: 26 additions & 13 deletions src/korone/modules/weather/handlers/get.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,29 +11,28 @@
from korone.modules.weather.utils.types import WeatherResult, WeatherSearch
from korone.modules.weather.utils.weather_icons import get_status_emoji
from korone.utils.caching import cache
from korone.utils.i18n import get_i18n
from korone.utils.i18n import gettext as _
from korone.utils.pagination import Pagination


async def fetch_location_data(command_args):
async def fetch_location_data(command_args: str, language: str) -> dict:
cache_key = f"weather_location_{command_args}"
data = await cache.get(cache_key) or await search_location(command_args)
data = await cache.get(cache_key) or await search_location(command_args, language)
if data:
await cache.set(cache_key, data, expire=3600)
return data


def format_weather_text(weather_result):
def format_weather_text(weather_result: WeatherResult) -> str:
return (
f"<b>{weather_result.city}, "
f"{weather_result.admin_district}, "
f"<b>{weather_result.city}, {weather_result.admin_district}, "
f"{weather_result.country}</b>:\n\n"
f"🌡️ <b>{_('Temperature')}</b>: {weather_result.temperature}°C\n"
f"🌡️ <b>{_('Temperature feels like:')}</b>: {weather_result.temperature_feels_like}°C\n"
f"💧 <b>{_('Air humidity:')}</b>: {weather_result.relative_humidity}%\n"
f"💨 <b>{_('Wind Speed')}</b>: {weather_result.wind_speed} km/h\n\n"
f"- {get_status_emoji(weather_result.icon_code)} "
f"<i>{weather_result.wx_phrase_long}</i>"
f"🌡️ <b>{_('Temperature:')}</b> {weather_result.temperature}°C\n"
f"🌡️ <b>{_('Temperature feels like:')}</b> {weather_result.temperature_feels_like}°C\n"
f"💧 <b>{_('Air humidity:')}</b> {weather_result.relative_humidity}%\n"
f"💨 <b>{_('Wind Speed:')}</b> {weather_result.wind_speed} km/h\n\n"
f"- {get_status_emoji(weather_result.icon_code)} <i>{weather_result.wx_phrase_long}</i>"
)


Expand All @@ -50,7 +49,14 @@ async def get_weather(client: Client, message: Message) -> None:
)
return

data = await fetch_location_data(command.args)
current_locale = get_i18n().current_locale_babel
language = (
f"{current_locale.language}-{current_locale.territory}"
if current_locale.language and current_locale.territory
else "en-US"
)

data = await fetch_location_data(command.args, language)
if not data:
await message.reply_text(_("Failed to fetch weather data."))
return
Expand Down Expand Up @@ -106,8 +112,15 @@ async def callback_weather(client: Client, callback_query: CallbackQuery) -> Non
await callback_query.edit_message_reply_markup(reply_markup=keyboard_markup)
return

current_locale = get_i18n().current_locale_babel
language = (
f"{current_locale.language}-{current_locale.territory}"
if current_locale.language and current_locale.territory
else "en-US"
)

if data.latitude and data.longitude:
weather_data = await get_weather_observations(data.latitude, data.longitude)
weather_data = await get_weather_observations(data.latitude, data.longitude, language)
if not weather_data:
await callback_query.answer(_("Failed to fetch weather data."), show_alert=True)
return
Expand Down
11 changes: 6 additions & 5 deletions src/korone/modules/weather/utils/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
WEATHER_OBSERVATIONS_URL = (
"https://api.weather.com/v3/aggcommon/v3-wx-observations-current;v3-location-point"
)
LANGUAGE = "en-US"
FORMAT = "json"
UNITS = "m"

Expand All @@ -31,21 +30,23 @@ async def fetch_json(url: str, params: dict) -> dict:


@cache(ttl=timedelta(days=1))
async def search_location(query: str) -> dict[str, Any]:
async def search_location(query: str, language: str) -> dict[str, Any]:
params = {
"apiKey": WEATHER_API_KEY,
"query": query,
"language": LANGUAGE,
"language": language,
"format": FORMAT,
}
return await fetch_json(LOCATION_SEARCH_URL, params)


async def get_weather_observations(latitude: float, longitude: float) -> dict[str, Any]:
async def get_weather_observations(
latitude: float, longitude: float, language: str
) -> dict[str, Any]:
params = {
"apiKey": WEATHER_API_KEY,
"geocode": f"{latitude},{longitude}",
"language": LANGUAGE,
"language": language,
"units": UNITS,
"format": FORMAT,
}
Expand Down

0 comments on commit d91a318

Please sign in to comment.