Skip to content

Commit

Permalink
Add option to specify language
Browse files Browse the repository at this point in the history
  • Loading branch information
eandersons committed Mar 10, 2023
1 parent 71d6881 commit 6e7fb5a
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 3 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ A simple [maubot](https://github.com/maubot/maubot) (which was based on the echo
where `<location>` can be an airport code, or a city
* `!weather <location> u:[u|m|M]` - As above, but specify unit of measure (see
below)
* `!weather <location> l:[language code]` - current weather and forecast in the
specified language; \
available languages are listed on <https://wttr.in/:translation>
* `!moon` - Display lunar phase information

### Units
Expand Down
4 changes: 4 additions & 0 deletions base-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ default_location:
# Blank will automatically determine units based on source IP
default_units:

# The default language to use for weather reports.
# Available languages are listed here: https://wttr.in/:translation
default_language:

# Show a link to the wttr.in page
show_link: false

Expand Down
2 changes: 1 addition & 1 deletion maubot.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
maubot: 0.1.0
id: com.arachnitech.weather
version: "0.3.1"
version: "0.4.0"
license: MIT
modules:
- weather
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "maubot-weather"
version = "0.3.1"
version = "0.4.0"
description = "A maubot bot to get the weather from wttr.in and post to a matrix channel"
authors = ["Alex Kelly <[email protected]>"]
license = "MIT"
Expand Down
37 changes: 36 additions & 1 deletion weather/weather.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,14 @@ def do_update(self, helper: ConfigUpdateHelper) -> None:
helper.copy("default_location")
helper.copy("show_image")
helper.copy("default_units")
helper.copy("default_language")


class WeatherBot(Plugin):
"""Maubot plugin class to get the weather and respond in a chat."""

_service_url: str = "https://wttr.in"
_stored_language: str
_stored_location: str
_stored_units: str

Expand Down Expand Up @@ -70,6 +72,13 @@ async def help(self, evt: MessageEvent) -> None:
"* `m`: metric;\n"
"* `u`: US;\n"
"* `M`: metric, but wind speed unit is m/s."
"\n\n"
"Forecast language can be specified by adding `l:<language-code>` "
"at the end of the location like:\\\n"
"`!weather Chicago l:es`.\\\n"
"Available languages are listed on <https://wttr.in/:translation>."
"\n\n"
"Options can be combined: `!weather Chicago l:es u:M`."
)

@command.new(name="moon", help="Get the moon phase")
Expand Down Expand Up @@ -137,6 +146,26 @@ async def _image(self, event: MessageEvent) -> None:
f"error getting location {self._stored_location}"
)

def _language(self) -> str:
if self._stored_language == "":
language = self._config_value("default_language")
location = self._stored_location

if "l:" in location:
match: Match[str] | None = search(
r"(\bl: *(?!u:)(\S+))", location, IGNORECASE
)

if match is not None:
matches = match.groups()
language = matches[1]
location = location.replace(matches[0], "")

self._stored_language = language.strip()
self._stored_location = location.strip()

return self._stored_language

def _location(self, location: str = "") -> str:
"""Return a cleaned-up location name"""
if self._stored_location == "":
Expand All @@ -147,6 +176,7 @@ def _location(self, location: str = "") -> str:
else self._config_value("default_location")
).strip()
self._units()
self._language()

return self._stored_location

Expand Down Expand Up @@ -206,7 +236,12 @@ def _units(self) -> str:

if match is not None:
matches = match.groups()
units = matches[1]
custom_unit = matches[1]
units = (
custom_unit
if custom_unit in ("u", "m", "M")
else units
)
location = location.replace(matches[0], "").strip()

self._stored_location = location.strip()
Expand Down

0 comments on commit 6e7fb5a

Please sign in to comment.