From 2d73e0567c9ac6e03cfc8bfc62a8f24ef4fadebc Mon Sep 17 00:00:00 2001 From: Laria Carolin Chabowski Date: Tue, 11 Jan 2022 23:39:30 +0100 Subject: Use new met.no locationforecast 2.0 API The old XML API will be discontinued next month and is already returning errors occasionally (apparently to nudge lazy devs like me to update their code :D). We're also doing a much better job at caching now, as suggested by the API documentation. We're not yet showing a weather icon, I still have to figure out how to do this now (apparently the weather icons are a separate API / actually just a tag.gz archive that the weather API references?), but at least we're not panic()ing any more in the request handlers due to the unexpected error responses :/. See https://developer.yr.no/doc/GettingStarted/ for an intorduction to the new API. --- README.markdown | 5 +- config.go | 4 +- http_getter/http_getter.go | 19 ++++- main.go | 25 ++++-- templates/template.html | 4 +- weather/weather.go | 193 ++++++++++++++++++++++++++++++++++----------- 6 files changed, 189 insertions(+), 61 deletions(-) diff --git a/README.markdown b/README.markdown index aa87dbb..0a4037c 100644 --- a/README.markdown +++ b/README.markdown @@ -15,7 +15,10 @@ Here is an example with all fields filled out. { // The place for which to get the weather data. If omitted, no weather will be shown - "WeatherPlace": "Germany/Hamburg/Hamburg", + "WeatherCoords": { + "Lat": "53.6", + "Lon": "10.0" + }, // A list of links to show. Can be omitted. "Links": [ diff --git a/config.go b/config.go index 9464291..ec4f8ec 100644 --- a/config.go +++ b/config.go @@ -10,7 +10,9 @@ import ( // Config contains all configuration options that are read from .config/startpage/config.json type Config struct { // The place for which to get the weather data. If omitted, no weather will be shown - WeatherPlace string + WeatherCoords struct { + Lat, Lon string + } // A list of links to show Links []Link diff --git a/http_getter/http_getter.go b/http_getter/http_getter.go index 5df3ac2..9fdf7a5 100644 --- a/http_getter/http_getter.go +++ b/http_getter/http_getter.go @@ -4,16 +4,27 @@ import ( "net/http" ) -// Get is like http.Get, but we're sending our own user agent. -func Get(url string) (*http.Response, error) { - client := &http.Client{} - +func BuildGetRequest(url string) (*http.Request, error) { req, err := http.NewRequest("GET", url, nil) if err != nil { return nil, err } req.Header.Add("User-Agent", "github.com/slivasur/startpage") + return req, nil +} +func Do(req *http.Request) (*http.Response, error) { + client := &http.Client{} return client.Do(req) } + +// Get is like http.Get, but we're sending our own user agent. +func Get(url string) (*http.Response, error) { + req, err := BuildGetRequest(url) + if err != nil { + return nil, err + } + + return Do(req) +} diff --git a/main.go b/main.go index 01c8c60..776b1a1 100644 --- a/main.go +++ b/main.go @@ -7,6 +7,7 @@ import ( "fmt" "html/template" "log" + "math" "net/http" "os" "path" @@ -52,11 +53,11 @@ func loadTemplate(templateDir string) error { } func buildWeatherProvider(config Config) *weather.WeatherProvider { - if config.WeatherPlace == "" { + if config.WeatherCoords.Lat == "" || config.WeatherCoords.Lon == "" { return nil } - return weather.NewWeatherProvider(config.WeatherPlace) + return weather.NewWeatherProvider(config.WeatherCoords.Lat, config.WeatherCoords.Lon) } func buildRedditImageProvider(config Config) *reddit_background.RedditImageProvider { @@ -96,9 +97,23 @@ func main() { log.Fatal(http.ListenAndServe(*laddr, nil)) } +type TplWeather struct { + Temp int +} + +func convertWeather(ts *weather.TimeseriesEntry) *TplWeather { + if ts == nil { + return nil + } + + return &TplWeather{ + Temp: int(math.Round(ts.Temperature())), + } +} + type TplData struct { BgImage *reddit_background.RedditImageForAjax - Weather *weather.Weather + Weather *TplWeather Links []Link CanSaveBg bool } @@ -109,7 +124,7 @@ func startpage(config Config, redditImageProvider *reddit_background.RedditImage return func(rw http.ResponseWriter, req *http.Request) { defer req.Body.Close() - var curWeather *weather.Weather = nil + var curWeather *weather.TimeseriesEntry = nil if weatherProvider != nil { var err error if curWeather, err = weatherProvider.CurrentWeather(); err != nil { @@ -119,7 +134,7 @@ func startpage(config Config, redditImageProvider *reddit_background.RedditImage if err := tpl.Execute(rw, &TplData{ redditImageProvider.Image().ForAjax(), - curWeather, + convertWeather(curWeather), config.Links, config.BackgroundSavepath != "", }); err != nil { diff --git a/templates/template.html b/templates/template.html index 4b20536..7592b50 100644 --- a/templates/template.html +++ b/templates/template.html @@ -80,8 +80,7 @@ {{ if .Weather }}
- - {{ .Weather.Temp.Value }}° + {{ .Weather.Temp }}°
{{ end }} {{ if .Links }} @@ -94,7 +93,6 @@ {{ end }} -