112 lines
2.5 KiB
Go
112 lines
2.5 KiB
Go
package nina
|
|
|
|
import (
|
|
"ArinDash/config"
|
|
"encoding/json"
|
|
"io"
|
|
"net/http"
|
|
)
|
|
|
|
const apiBaseURL = "https://warnung.bund.de/api31"
|
|
|
|
type configFile struct {
|
|
Nina ninaConfig
|
|
}
|
|
|
|
type ninaConfig struct {
|
|
GebietsCode string
|
|
}
|
|
|
|
type Warning struct {
|
|
Id string `json:"id"`
|
|
Identifier string `json:"identifier"`
|
|
Sender string `json:"sender"`
|
|
Sent string `json:"sent"`
|
|
Status string `json:"status"`
|
|
MsgType string `json:"msgType"`
|
|
Scope string `json:"scope"`
|
|
Code []string `json:"code"`
|
|
Reference string `json:"reference"`
|
|
Info []Info `json:"info"`
|
|
Errormsg string `json:"errormsg"`
|
|
}
|
|
|
|
type Info struct {
|
|
Language string `json:"language"`
|
|
Category []string `json:"category"`
|
|
Event string `json:"event"`
|
|
Urgency string `json:"urgency"`
|
|
Severity string `json:"severity"`
|
|
Expires string `json:"expires"`
|
|
Headline string `json:"headline"`
|
|
Description string `json:"description"`
|
|
}
|
|
|
|
func FetchWarnings() []Warning {
|
|
cfg := &configFile{}
|
|
config.LoadConfig(cfg)
|
|
client := &http.Client{}
|
|
|
|
req, err := http.NewRequest("GET", apiBaseURL+"/dashboard/"+cfg.Nina.GebietsCode+".json", nil)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
resp, err := client.Do(req)
|
|
if err != nil {
|
|
return append(make([]Warning, 0), Warning{
|
|
Errormsg: err.Error(),
|
|
})
|
|
}
|
|
defer func(Body io.ReadCloser) {
|
|
err := Body.Close()
|
|
if err != nil {
|
|
return
|
|
}
|
|
}(resp.Body)
|
|
respBody, err := io.ReadAll(resp.Body)
|
|
if err != nil {
|
|
return append(make([]Warning, 0), Warning{
|
|
Errormsg: err.Error(),
|
|
})
|
|
}
|
|
|
|
warnings := make([]Warning, 0)
|
|
err = json.Unmarshal(respBody, &warnings)
|
|
if err != nil {
|
|
return append(make([]Warning, 0), Warning{
|
|
Errormsg: err.Error(),
|
|
})
|
|
}
|
|
result := make([]Warning, 0)
|
|
for _, warning := range warnings {
|
|
req, err := http.NewRequest("GET", apiBaseURL+"/warnings/"+warning.Id+".json", nil)
|
|
if err != nil {
|
|
return append(make([]Warning, 0), Warning{
|
|
Errormsg: err.Error(),
|
|
})
|
|
}
|
|
resp, err := client.Do(req)
|
|
if err != nil {
|
|
return append(make([]Warning, 0), Warning{
|
|
Errormsg: err.Error(),
|
|
})
|
|
}
|
|
defer func(Body io.ReadCloser) {
|
|
err := Body.Close()
|
|
if err != nil {
|
|
return
|
|
}
|
|
}(resp.Body)
|
|
respBody, err := io.ReadAll(resp.Body)
|
|
if err != nil {
|
|
return append(make([]Warning, 0), Warning{
|
|
Errormsg: err.Error(),
|
|
})
|
|
}
|
|
warning := Warning{}
|
|
err = json.Unmarshal(respBody, &warning)
|
|
result = append(result, warning)
|
|
}
|
|
return result
|
|
}
|