2024-05-08 09:08:36 +02:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
xj "github.com/basgys/goxml2json"
|
|
|
|
"io"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
type root struct {
|
|
|
|
Base struct {
|
|
|
|
Nombre string `json:"nombre"`
|
|
|
|
Prediccion struct {
|
|
|
|
Dia []struct {
|
|
|
|
Fecha string `json:"-fecha"`
|
|
|
|
Temperatura struct {
|
|
|
|
Maxima string `json:"maxima"`
|
|
|
|
Minima string `json:"minima"`
|
2024-05-13 08:26:11 +02:00
|
|
|
Dato []struct {
|
|
|
|
Valor string `json:"#content"`
|
|
|
|
Hora string `json:"-hora"`
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Sens_Termica struct {
|
|
|
|
Maxima string `json:"maxima"`
|
|
|
|
Minima string `json:"minima"`
|
|
|
|
Dato []struct {
|
|
|
|
Valor string `json:"#content"`
|
|
|
|
Hora string `json:"-hora"`
|
|
|
|
}
|
2024-05-08 09:08:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} `json:"root"`
|
|
|
|
}
|
|
|
|
|
2024-05-08 11:14:11 +02:00
|
|
|
func client() {
|
|
|
|
jsonData, err := getJSON()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2024-05-08 09:08:36 +02:00
|
|
|
textBytes := []byte(jsonData)
|
|
|
|
aemetRequest := root{}
|
2024-05-08 11:14:11 +02:00
|
|
|
err = json.Unmarshal(textBytes, &aemetRequest)
|
2024-05-08 09:08:36 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Error occurred unmarshalling data: %v\n", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Println(aemetRequest.Base.Nombre)
|
|
|
|
fmt.Println(aemetRequest.Base.Prediccion.Dia[0].Fecha)
|
|
|
|
fmt.Printf("Temperatura máxima: %v°C\n", aemetRequest.Base.Prediccion.Dia[0].Temperatura.Maxima)
|
|
|
|
fmt.Printf("Temperatura mínima: %v°C\n", aemetRequest.Base.Prediccion.Dia[0].Temperatura.Minima)
|
|
|
|
}
|
|
|
|
|
2024-05-08 11:14:11 +02:00
|
|
|
func getJSON() (s string, err error) {
|
|
|
|
client := &http.Client{}
|
|
|
|
req, err := http.NewRequest("GET", "https://www.aemet.es/xml/municipios/localidad_46250.xml", nil)
|
|
|
|
if err != nil {
|
|
|
|
e := fmt.Errorf("Error occurred pulling data: %v\n", err)
|
|
|
|
return "", e
|
|
|
|
}
|
|
|
|
req.Header.Set("User-Agent", "AEMET-Client/1.0 (https://git.bulgariu.xyz/raul/aemet)")
|
|
|
|
|
|
|
|
resp, err := client.Do(req)
|
|
|
|
|
2024-05-08 09:08:36 +02:00
|
|
|
if err != nil {
|
2024-05-08 11:14:11 +02:00
|
|
|
e := fmt.Errorf("Error occurred pulling data: %v\n", err)
|
|
|
|
return "", e
|
2024-05-08 09:08:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
defer resp.Body.Close()
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
2024-05-08 11:14:11 +02:00
|
|
|
e := fmt.Errorf("Error occurred accessing upstream website: %v\n", resp.Status)
|
|
|
|
return "", e
|
2024-05-08 09:08:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
data, err := io.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
2024-05-08 11:14:11 +02:00
|
|
|
e := fmt.Errorf("Error occurred reading data: %v\n", err)
|
|
|
|
return "", e
|
2024-05-08 09:08:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
xml := strings.NewReader(string(data))
|
|
|
|
json, err := xj.Convert(xml)
|
|
|
|
if err != nil {
|
2024-05-08 11:14:11 +02:00
|
|
|
e := fmt.Errorf("Error occurred converting XML to JSON: %v\n", err)
|
|
|
|
return "", e
|
2024-05-08 09:08:36 +02:00
|
|
|
}
|
|
|
|
|
2024-05-08 11:14:11 +02:00
|
|
|
return json.String(), nil
|
2024-05-08 09:08:36 +02:00
|
|
|
}
|