-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathlist.go
119 lines (110 loc) · 2.69 KB
/
list.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
package nzcan
import (
"encoding/json"
"errors"
"fmt"
"strings"
"time"
"github.com/PuerkitoBio/goquery"
"github.com/mattn/go-nulltype"
"github.com/whitewater-guide/gorge/core"
)
const (
markers0 = "var markers = ["
markers1 = "initMap(markers, options);"
)
var tz, _ = time.LoadLocation("Pacific/Auckland")
type geoloc struct {
location *core.Location
name string
}
func getTime(year int, str string) (*core.HTime, error) {
// upstream assumes current year, but for stable tests we have to parametrize it
if year == 0 {
year = time.Now().In(tz).Year()
}
ext := fmt.Sprintf("%d %s:00", year, str)
ts, err := time.ParseInLocation("2006 02-January 15:04:05", ext, tz)
if err != nil {
return nil, err
}
return &core.HTime{
Time: ts.UTC(),
}, nil
}
func (s *scriptNzcan) fetchGeo() (map[string]geoloc, error) {
txt, err := core.Client.GetAsString(s.url+"/RiverflowGeo/ALL", &core.RequestOptions{
Headers: map[string]string{
"X-Requested-With": "XMLHttpRequest",
},
})
if err != nil {
return nil, err
}
m0 := strings.Index(txt, markers0)
if m0 == -1 {
return nil, errors.New("markers not found")
}
txt = txt[m0+len(markers0)-1:]
m1 := strings.Index(txt, markers1)
if m1 == -1 {
return nil, errors.New("markers end not found")
}
txt = txt[:m1-4]
var markers markersList
err = json.Unmarshal([]byte(txt), &markers)
if err != nil {
return nil, err
}
result := make(map[string]geoloc, len(markers))
for _, m := range markers {
result[m.SiteNo] = geoloc{
location: &core.Location{
Latitude: core.TruncCoord(m.Lat),
Longitude: core.TruncCoord(m.Lng),
},
name: m.SiteName,
}
}
return result, nil
}
func (s *scriptNzcan) fetchList(suffix string, recv chan<- *core.Measurement) error {
doc, err := core.Client.GetAsDoc(s.url+"/RiverflowList/"+suffix, &core.RequestOptions{
Headers: map[string]string{
"X-Requested-With": "XMLHttpRequest",
},
})
if err != nil {
return err
}
defer doc.Close()
doc.Find("tr.riverflow-" + suffix[:1]).Each(func(i int, elem *goquery.Selection) {
tds := elem.Find("td")
link := elem.Find("th").Find("a").First()
// name := strings.TrimSpace(link.Text())
href, _ := link.Attr("href")
code := href[strings.LastIndex(href, "/")+1:]
ts, err := getTime(s.year, tds.Eq(0).Text())
if err != nil {
return
}
var level, flow nulltype.NullFloat64
levelStr, flowStr := tds.Eq(1).Text(), tds.Eq(2).Text()
if levelStr != "" {
level.Scan(levelStr) //nolint:errcheck
}
if flowStr != "" {
flow.Scan(flowStr) //nolint:errcheck
}
recv <- &core.Measurement{
GaugeID: core.GaugeID{
Script: s.name,
Code: code,
},
Timestamp: *ts,
Level: level,
Flow: flow,
}
})
return nil
}