Skip to content

Commit

Permalink
feat: Allow changing units of measure for reporting
Browse files Browse the repository at this point in the history
Added "default_units" to the config file.  This globally
overrides/forces the setting which can be: metric(m) US(u) or metric except wind(M).

These values may also be specified at the end of a `!weather location`
call from a chat prefixed by 'u:" to indicate you are specifying the unit to use.
So you can do something like:

!weather Chicago, Il u:m

To get the weather in Chicago reported in Celcius.

closes #3
  • Loading branch information
kellya committed Mar 4, 2022
1 parent efb8f40 commit 8c7c74e
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 10 deletions.
12 changes: 12 additions & 0 deletions base-config.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
# Specify a location to use when just !weather is used
default_location: Columbus, Oh

# default units to use. This is directly passed to wttr.in, so it must be one of:
# m - metric (SI)
# u - US units (USCS)
# M - metric (SI), but wind in m/s
# blank will automatically determin units based on source IP
default_units:

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

# display the image of the forecast
show_image: false
39 changes: 29 additions & 10 deletions weather/weather.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ def do_update(self, helper: ConfigUpdateHelper) -> None:
helper.copy("show_link")
helper.copy("default_location")
helper.copy("show_image")
helper.copy("default_units")


class WeatherBot(Plugin):
Expand Down Expand Up @@ -44,20 +45,38 @@ async def weather_handler(self, evt: MessageEvent, location=None) -> None:
if location and location == "help":
await evt.respond(
"""
Uses wttr.in to get the weather and respond. If you
don't specify a location, it will use the IP address
of the server to figure out what the location is.
Uses wttr.in to get the weather and respond. If you
don't specify a location, it will use the IP address
of the server to figure out what the location is.
Otherwise, you may specify the location by name:
!weather Chicago
Otherwise, you may specify the location by name:
!weather Chicago
or by Airport Code
!weather SFO
"""
or by Airport Code
!weather SFO
The units may be specified with a location by adding u:<unit> to the end of the location like:
!weather Chicago u:m
Where <unit> is one of:
m = metric
u = US
M = metric, but wind in m/s
"""
)
location = self.get_location(location)
units = "" # default to nothing so that response works even if default is unset

if self.config["default_units"]:
# If units are specified in the config, use them
units = f"&{self.config['default_units']}"
if "u:" in location:
# If the location has units specified, attempt to use them
location, custom_unit = location.split("u:")
if custom_unit in ["u", "m", "M"]:
units = f"&{custom_unit}"
location = self.get_location(location.strip())

resp = await self.http.get(f"http://wttr.in/{location}?format=3")
resp = await self.http.get(f"http://wttr.in/{location}?format=3{units}")
weather = await resp.text()
message = weather
if self.config["show_link"]:
Expand Down

0 comments on commit 8c7c74e

Please sign in to comment.