-
Notifications
You must be signed in to change notification settings - Fork 0
/
maritime_forecast.go
175 lines (146 loc) · 3.5 KB
/
maritime_forecast.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package aemet
import (
"encoding/xml"
"errors"
"fmt"
"golang.org/x/net/html/charset"
"log"
"net/http"
"time"
)
type Result struct {
Warning Warning `xml:"aviso"`
Situation Situation `xml:"situacion"`
Forecast Forecast `xml:"prediccion"`
Trend Trend `xml:"tendencia"`
}
type Warning struct {
End Date `xml:"fin"`
Text string `xml:"texto"`
}
type Situation struct {
Name string `xml:"nombre,attr"`
Start Date `xml:"inicio"`
End Date `xml:"fin"`
Text string `xml:"texto"`
}
type Forecast struct {
Start Date `xml:"inicio"`
End Date `xml:"fin"`
Zones []struct {
Name string `xml:"nombre,attr"`
SubZones []struct {
Name string `xml:"nombre,attr"`
Text string `xml:"texto"`
} `xml:"subzona"`
} `xml:"zona"`
}
type Trend struct {
Start Date `xml:"inicio"`
End Date `xml:"fin"`
Text string `xml:"texto"`
}
type Date string
func getXML(url string) (Result, error) {
res, err := http.Get(url)
if err != nil {
return Result{}, err
}
defer res.Body.Close()
if res.StatusCode != 200 {
return Result{}, errors.New("status code error: " + string(res.StatusCode) + " / " + res.Status)
}
var result Result
decoder := xml.NewDecoder(res.Body)
decoder.CharsetReader = charset.NewReaderLabel
err = decoder.Decode(&result)
if err != nil {
return Result{}, err
}
return result, nil
}
func (d Date) formatDate() string {
EsWeeksDays := []string{"domingo", "lunes", "martes", "miércoles", "jueves", "viernes", "sábado"}
EsMonths := []string{"enero", "febrero", "marzo", "abril", "mayo", "junio", "julio", "agosto", "septiembre", "octubre", "noviembre", "diciembre"}
date, err := time.Parse("2006-01-02T15:04:05", string(d))
if err != nil {
log.Println("formatDate error: ", err)
return string(d)
}
day := EsWeeksDays[date.Weekday()]
month := EsMonths[int(date.Month())-1]
return fmt.Sprintf(
"%v, %v %v %v a las %v hora oficial",
day,
date.Day(),
month,
date.Year(),
date.Format("15:04"),
)
}
func (w Warning) formatText() string {
return fmt.Sprintf(
"<u><b>Avisos válidos hasta el %v</b></u>\n\n%v\n\n\n",
w.End.formatDate(),
w.Text,
)
}
func (s Situation) formatText() string {
return fmt.Sprintf(
"<u><b>%v</b></u>\n\nFecha de inicio: %v\nFecha de fin: %v\n\n%v\n\n\n",
s.Name,
s.Start.formatDate(),
s.End.formatDate(),
s.Text,
)
}
func (f Forecast) formatText() string {
forecastText := fmt.Sprintf(
"<u><b>Predicción</b></u>\n\nFecha de inicio: %v\nFecha de fin: %v\n\n\n",
f.Start.formatDate(),
f.End.formatDate(),
)
for _, zone := range f.Zones {
forecastText += fmt.Sprintf("<b>%v</b>\n\n", zone.Name)
if len(zone.SubZones) == 1 {
forecastText += fmt.Sprintf(
"%v\n\n",
zone.SubZones[0].Text,
)
} else {
for _, subZone := range zone.SubZones {
forecastText += fmt.Sprintf(
"•\t<b>%v</b>: %v\n\n",
subZone.Name,
subZone.Text,
)
}
}
}
return forecastText
}
func (t Trend) formatText() string {
return fmt.Sprintf(
"<u><b>Tendencia</b></u>\n\nFecha de inicio: %v\nFecha de fin: %v\n\n%v\n\n\n",
t.Start.formatDate(),
t.End.formatDate(),
t.Text,
)
}
func (r Result) formatText() string {
return fmt.Sprintf(
"%v%v%v\n%v%v",
r.Warning.formatText(),
r.Situation.formatText(),
r.Forecast.formatText(),
r.Trend.formatText(),
"© AEMET",
)
}
func GetMaritimeForecast(xmlId string) (string, error) {
result, err := getXML("http://www.aemet.es/xml/maritima/" + xmlId + ".xml")
if err != nil {
return "", err
}
return result.formatText(), nil
}