-
Notifications
You must be signed in to change notification settings - Fork 3
/
parser.go
161 lines (146 loc) · 4.02 KB
/
parser.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
package galicia2
import (
"bufio"
"fmt"
"html"
"regexp"
"strconv"
"strings"
"time"
"github.com/im7mortal/UTM"
"github.com/whitewater-guide/gorge/core"
)
const (
colOpen = "<td"
colClose = "</td>"
colCloseLen = len(colClose)
delim1 = "<!----------------------------------------------------------------------------- LINEA 1 ------------------------------------------------------------------>"
delim2 = "<!----------------------------------------------------------------------------- LINEA 2 ------------------------------------------------------------------>"
)
type item struct {
gauge core.Gauge
measurement core.Measurement
}
func splitColumns(data []byte, atEOF bool) (advance int, token []byte, err error) {
// Return nothing if at end of file and no data passed
if atEOF && len(data) == 0 {
return 0, nil, nil
}
dataStr := string(data)
if start := strings.Index(dataStr, colOpen); start >= 0 {
openTagEnd := strings.Index(dataStr[start:], ">")
if openTagEnd <= 0 {
return
}
openTagEnd += start
end := strings.Index(dataStr[start:], colClose)
//fmt.Println(start, end)
if end <= 0 {
return
}
end += start
return end + colCloseLen, data[openTagEnd+1 : end], nil
}
return
}
func prettyName(name string) string {
words := strings.Fields(html.UnescapeString(name))
for i, word := range words {
if word == "DE" || word == "EN" {
words[i] = strings.ToLower(word)
} else {
words[i] = word[:1] + strings.ToLower(word[1:])
}
}
return strings.Join(words, " ")
}
func (s *scriptGalicia2) parseTable() ([]item, error) {
var result []item
err := core.Client.EnsureCookie("http://saih.chminosil.es", false)
if err != nil {
return result, err
}
resp, err := core.Client.Get(s.listURL, nil)
if err != nil {
return result, err
}
defer resp.Body.Close()
core.Client.SaveCookies()
scanner := bufio.NewScanner(resp.Body)
scanner.Split(splitColumns)
location, err := time.LoadLocation("CET")
if err != nil {
return result, err
}
ind := 0
var gauge core.Gauge
var msm core.Measurement
stationExp := regexp.MustCompile(`([A-Z]\d{3})\s-\s(.*)`)
header := true
for scanner.Scan() {
if header {
if ind == 6 {
header = false
ind = 0
} else {
ind++
}
continue
}
// There are 7 columns in a row
switch ind {
case 0:
gauge = core.Gauge{LevelUnit: "m"}
case 1: // River name and code
station := scanner.Text()
parts := stationExp.FindStringSubmatch(station)
gauge.Script = s.name
gauge.Code = parts[1]
gauge.Name = prettyName(parts[2])
gauge.URL = fmt.Sprintf(s.gaugeURLFormat, parts[1])
case 5: // Level
levelStr := scanner.Text()
levelStr = strings.Replace(levelStr, ",", ".", 1)
msm = core.Measurement{GaugeID: gauge.GaugeID}
msm.Level.UnmarshalJSON([]byte(levelStr)) //nolint:errcheck
case 6: // timestamp
t, _ := time.ParseInLocation("02/01/2006 15:04", scanner.Text(), location)
msm.Timestamp = core.HTime{Time: t.UTC()}
result = append(result, item{gauge: gauge, measurement: msm})
}
ind = (ind + 1) % 7
}
if err := scanner.Err(); err != nil {
return result, err
}
return result, nil
}
// http://saih.chminosil.es/index.php?url=/datos/ficha/estacion:N015
func (s *scriptGalicia2) parseGaugePage(code string) (lat float64, lon float64, altitude float64) {
html, err := core.Client.GetAsString(fmt.Sprintf(s.gaugeURLFormat, code), nil)
if err != nil {
return
}
bodyInd := strings.Index(html, delim1) + len(delim1)
bodyEnd := strings.Index(html, delim2)
html = html[bodyInd:bodyEnd]
trEnd := strings.Index(html, "</tr>")
html = html[trEnd+5:]
trEnd = strings.Index(html, "</tr>")
html = html[:trEnd+5]
scanner := bufio.NewScanner(strings.NewReader(html))
scanner.Split(splitColumns)
i, coord := 0, [4]float64{}
for scanner.Scan() {
coord[i], _ = strconv.ParseFloat(scanner.Text(), 64)
i++
}
altitude = coord[3]
lat, lon, err = UTM.ToLatLon(coord[1], coord[2], int(coord[0]), "", true)
if err != nil {
fmt.Println(err)
}
lat = core.TruncCoord(lat)
lon = core.TruncCoord(lon)
return
}