Replace deprecated smhi pmp3g api with new SNOW api#220
Replace deprecated smhi pmp3g api with new SNOW api#220nopil3os wants to merge 3 commits intoschachmat:masterfrom
Conversation
This reverts commit 85c6120.
|
Can you update your branch please to the latest master? I've fixed the lint warnings in master today. |
There was a problem hiding this comment.
Pull request overview
Updates the SMHI backend to use the new snow1g API endpoint after the old pmp3g endpoint was deactivated.
Changes:
- Replaced the SMHI response model/types to match the new SNOW API response shape.
- Updated parsing logic to read values from the new
dataobject and use the newtimefield. - Switched the SMHI request URL to the
snow1g/version/1endpoint.
Comments suppressed due to low confidence (2)
backends/smhi.go:186
- The loop over
forecast.TimeSeriesnever updatescurrentPrediction, soparseCurrentalways returns the first timeseries entry regardless of the current time. UpdatecurrentPredictionas you iterate (e.g., keep the latest prediction withts <= now), and then break oncets.After(now)is true.
for _, prediction := range forecast.TimeSeries {
ts, err := time.Parse(time.RFC3339, prediction.Time)
if err != nil {
log.Fatalf("Failed to parse timestamp: %v\n", err)
}
if ts.After(currentTime) {
break
backends/smhi.go:160
tsis parsed from the API timestamp (likely with an explicit timezone), but the day-bucketing logic comparests.Day()against a separately initializedcurrentTime(time.Now()earlier). If local time != the API timezone, day boundaries can be off. Consider initializing the “current day” from the first parsedts(or converting both to the same location withIn(...)) so grouping is consistent.
ts, err := time.Parse(time.RFC3339, prediction.Time)
if err != nil {
log.Fatalf("Failed to parse timestamp: %v\n", err)
}
if ts.Day() != currentTime.Day() {
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| var currentPrediction *smhiTimeSeries = forecast.TimeSeries[0] | ||
| var currentTime time.Time = time.Now().UTC() | ||
|
|
There was a problem hiding this comment.
currentPrediction := forecast.TimeSeries[0] will panic if SMHI returns an empty timeSeries. There’s also an ineffective empty-check just above this block (len(...) < 0). Please guard with len(...) == 0 (or <= 0) before indexing and handle the no-data case cleanly.
| const ( | ||
| // see http://opendata.smhi.se/apidocs/metfcst/index.html | ||
| smhiWuri = "https://opendata-download-metfcst.smhi.se/api/category/pmp3g/version/2/geotype/point/lon/%s/lat/%s/data.json" | ||
| // Ensure this URL is still valid for the new API version. Left unchanged based on prompt context. |
There was a problem hiding this comment.
The comment above smhiWuri says the URL is “Left unchanged”, but the code is actually switching APIs and commenting out the old endpoint. This is misleading; please rewrite/remove the comment to accurately describe why the endpoint changed and which API it targets.
| // Ensure this URL is still valid for the new API version. Left unchanged based on prompt context. | |
| // Use the SMHI snow1g version 1 point-data API for this backend. | |
| // The previous pmp3g version 2 endpoint is kept below as a reference to the old API target. |
| condition := weatherConditions[int(prediction.Data.SymbolCode)] | ||
| cnd.Code = condition.WeatherCode | ||
| cnd.Desc = condition.Description |
There was a problem hiding this comment.
condition := weatherConditions[int(prediction.Data.SymbolCode)] assumes the SMHI symbol_code is always present and within the hardcoded map. If it’s missing/zero or SMHI introduces new codes, this will silently produce an empty description and CodeUnknown. Please use the two-value map lookup and provide a sensible fallback description/code when the key isn’t found.
| condition := weatherConditions[int(prediction.Data.SymbolCode)] | |
| cnd.Code = condition.WeatherCode | |
| cnd.Desc = condition.Description | |
| symbolCode := int(prediction.Data.SymbolCode) | |
| if condition, ok := weatherConditions[symbolCode]; ok { | |
| cnd.Code = condition.WeatherCode | |
| cnd.Desc = condition.Description | |
| } else { | |
| cnd.Code = iface.CodeUnknown | |
| cnd.Desc = fmt.Sprintf("Unknown weather condition (symbol code %d)", symbolCode) | |
| } |
SMHI deactivated the pmp3g api this month, breaking the wego backend.
This attempts to rejig it for the new api: https://opendata.smhi.se/metfcst/snow1gv1/introduction
Used Gemini to generate this.