regolith-linux / regolith-i3xrocks-config

Regolith customization of i3xrocks.
Other
29 stars 49 forks source link

Rewrite weather blocklet #96

Closed moritzheiber closed 3 years ago

moritzheiber commented 3 years ago

Here's a (opinionated) rewrite of the weather blocklet:

  1. Don't do our own IP "magic", wttr.in does this for us, and it's much more reliable and way easier to do. If it doesn't detect it properly you can still set weather_location to something sensible.
  2. Don't do our own temperature unit "magic", again, wttr.in will select the "right" unit for us, depending on the location you're using it from (e.g. if you're using it from somewhere inside the US it'll choose to display Fahrenheit instead of Celsius). You can still set the weather_unit variable to either u (for USCS) or m (for metric) to override this behavior.
  3. Definitely don't use our "own" urlencode function in bash. There are a thousand edge cases and I don't want to support all of them just for a small blocklet. Instead, IF (and that's a big if) the wttr.in autodetection doesn't work folks can choose to specify their location using the query parameter WorldWeatherOnline supports (which wttr.in uses for their own API). E.g. São Paulo would become weather_location=São+Paulo (yes, in Unicode, wttr.in supports that!), and if that doesn't return a satisfying result you can even specify a country (e.g. London,uk) or a UK/Canada postal/US zipcode (90201). Yes, this is a little more complicated for the individual user, but we're talking about highly unlikely edge-case here and I don't want to optimize a very simple script for these edge-cases.
  4. Don't allow for specifying a weather label. I know this might be controversial, but wttr.in already supplies a "weatherish" label for the current conditions and I just don't see the need for adding yet another label.

I've also added fairly exhaustive tests. Obviously, they don't cover all the use-cases, but my intent is to add more of them as they appear (and break the script). For now all of the test cases should be passing.

Fixes #95

cc @hendynz @p3k

moritzheiber commented 3 years ago

Okay, one thing, could you visit https://wttr.in in a regular browser and post a screenshot?

And what happens if you set the weather_location inside the blocklet configuration?

kgilmer commented 3 years ago

image

kgilmer@clavin:/tmp/regolith-i3xrocks-config/scripts$ weather_location="Lake Forest, CA" ./weather 
<span font_desc="JetBrains Mono Medium 13" color="#E6E1CF">☀️ +57°F</span>
kgilmer@clavin:/tmp/regolith-i3xrocks-config/scripts$ weather_location="Toronto, CA" ./weather 
<span font_desc="JetBrains Mono Medium 13" color="#E6E1CF">☁️ +37°F</span>
kgilmer@clavin:/tmp/regolith-i3xrocks-config/scripts$ ./weather 
<span font_desc="JetBrains Mono Medium 13" color="#E6E1CF">☀️ +50°F</span>
kgilmer@clavin:/tmp/regolith-i3xrocks-config/scripts$ 
kgilmer@clavin:/tmp/regolith-i3xrocks-config/scripts$ ./weather 
<span font_desc="JetBrains Mono Medium 13" color="#E6E1CF">☀️ +50°F</span>

It seems to be working today..? I notice though that the default location temp seems to be different than if I specify my town specifically. Given that we can't really depend on the service to return valid data, we need a better way of handing error cases. I think this was tried in the past but I guess the heuristic didn't work out.

As I recall, I think there is something volatile about wttr, like a limited amount of location resolution per day based on free API usage, although I may be dreaming that up.

moritzheiber commented 3 years ago

I've read up on wttr.in and how reliable it is and since the release of their latest API (v3) they seem to have resolved to rate limiting issues at least (they're not querying several services by default). Nonetheless, I've added a way of handling the "Unknown location" errors more gracefully. I don't think we can resolve them better with the current implementation.

I thought about switching away from wttr.in, to e.g. GWeather (which comes with its own slew of problems, mainly its lacking location database) or yr.no (which then needs accurate location information retrieved in the script, again), but found none of them attainable.

@kgilmer Does the script in its current state work for you? I'm going to add some documentation to the website to address the most common issues (e.g. setting your location manually)

kgilmer commented 3 years ago

Indeed it does! I am good w/ this change.

$ scripts/weather 
<span font_desc="JetBrains Mono Medium 13" color="#E6E1CF">☀️ +52°F</span>