-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpage.go
55 lines (51 loc) · 1.63 KB
/
page.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
package quebec
import (
"errors"
"fmt"
"strings"
htmlquery "github.com/antchfx/xquery/html"
"github.com/whitewater-guide/gorge/core"
"golang.org/x/text/encoding/charmap"
"golang.org/x/text/transform"
)
type stationInfo struct {
code string
name string
federalCode string
isLocal bool
}
func (s *scriptQuebec) parsePage(code string) (*stationInfo, error) {
resp, err := core.Client.Get(fmt.Sprintf(s.stationURLFormat, code), &core.RequestOptions{SkipCookies: true})
if err != nil {
return nil, err
}
defer resp.Body.Close()
reader := transform.NewReader(resp.Body, charmap.Windows1252.NewDecoder())
doc, err := htmlquery.Parse(reader)
if err != nil {
return nil, err
}
infoTable := htmlquery.FindOne(doc, "//table[@class='tab']/tbody")
if infoTable == nil {
return nil, errors.New("could not find info table")
}
name := htmlquery.FindOne(infoTable, "/tr/td/font/b[contains(text(),'Nom de la station')]/../../following-sibling::*")
if name == nil {
return nil, errors.New("could not find name row")
}
fed := htmlquery.FindOne(infoTable, "/tr/td/font/b[contains(text(),'Numéro fédéral')]/../../following-sibling::*")
if fed == nil {
return nil, errors.New("could not find federal code row")
}
part := htmlquery.FindOne(infoTable, "/tr/td/div/font/b[contains(text(),'Particularité')]/../../..")
isLocal := true
if part != nil {
isLocal = !strings.Contains(htmlquery.InnerText(part), "www.eau.ec.gc.ca")
}
return &stationInfo{
code: code,
federalCode: strings.TrimSpace(htmlquery.InnerText(fed)),
isLocal: isLocal,
name: strings.TrimSpace(htmlquery.InnerText(name)),
}, nil
}