-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfetcher.go
185 lines (158 loc) · 4.86 KB
/
fetcher.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
176
177
178
179
180
181
182
183
184
185
package avurnav
import (
"bytes"
"encoding/json"
"io"
"net/http"
"net/url"
"strconv"
"strings"
"github.com/yhat/scrape"
"golang.org/x/net/html"
"golang.org/x/net/html/atom"
)
const (
BASE_PREMAR = "https://premar.antoine-augusti.fr"
)
// AVURNAVFetcher fetches AVURNAVs on the Préfet
// Maritime websites
type AVURNAVFetcher struct {
service PremarInterface
}
// AVURNAVPayload is used to decode AVURNAVs from
// the Préfet Maritime websites
type AVURNAVPayload struct {
Title string `json:"title"`
ValidFrom string `json:"valid_from"`
ValidUntil string `json:"valid_until"`
Latitude float32 `json:"latitude"`
Longitude float32 `json:"longitude"`
URL string `json:"url"`
Number string `json:"number"`
}
// AVURNAV transforms a payload to a proper AVURNAV
func (p AVURNAVPayload) AVURNAV(premar PremarInterface) AVURNAV {
from, to := p.ValidFrom, p.ValidUntil
avurnav := AVURNAV{
Number: p.Number,
Title: strings.TrimSpace(p.Title),
Latitude: p.Latitude,
Longitude: p.Longitude,
URL: p.URL,
PreMarRegion: premar.Region(),
}
if from != "" {
avurnav.ValidFrom = &from
}
if to != "" {
avurnav.ValidUntil = &to
}
return avurnav
}
func (p AVURNAVPayload) parseInt(str string) int {
s, err := strconv.Atoi(str)
if err != nil {
panic(err)
}
return s
}
// AVURNAV represents an AVURNAV
type AVURNAV struct {
// Number is the number of the AVURNAV. This is the main public identifier
Number string `json:"number"`
// Title is the title of the AVURNAV
Title string `json:"title"`
// Content is the content of the AVURNAV
Content string `json:"content"`
// Latitude gives an indication about the localisation of the AVURNAV.
// It's not super reliable for now because AVURNAVs can spawn multiple
// geographical regions but for now Préfet Maritimes only give a single point.
Latitude float32 `json:"latitude"`
// Longitude gives an indication about the localisation of the AVURNAV.
// It's not super reliable for now because AVURNAVs can spawn multiple
// geographical regions but for now Préfet Maritimes only give a single point.
Longitude float32 `json:"longitude"`
// URL gives a full URL to a Préfet Maritime website concerning this specific AVURNAV
URL string `json:"url"`
// ValidFrom tells when the AVURNAV will be in force. Format: YYYY-MM-DD
ValidFrom *string `json:"valid_from"`
// ValidUntil tells when the AVURNAV will not be valid anymore. Format: YYYY-MM-DD
ValidUntil *string `json:"valid_until"`
// PreMarRegion gives the region under the authority of this Préfet Maritime
PreMarRegion string `json:"premar_region"`
}
// ParseContent fills the content section of an AVURNAV and returns a new one
func (a AVURNAV) ParseContent(reader io.Reader) AVURNAV {
root, err := html.Parse(reader)
if err != nil {
panic(err)
}
blocks := scrape.FindAllNested(root, scrape.ByClass("col-12"))
divs := scrape.FindAllNested(blocks[1], scrape.ByTag(atom.Div))
a.Content = scrape.Text(divs[3])
return a
}
// JSON gets the JSON representation of an AVURNAV
func (a AVURNAV) JSON() string {
res, err := a.MarshalBinary()
if err != nil {
panic(err)
}
return string(res)
}
// MarshalBinary marshals the object
func (a AVURNAV) MarshalBinary() ([]byte, error) {
return json.Marshal(a)
}
// UnmarshalBinary unmarshals the object
func (a *AVURNAV) UnmarshalBinary(data []byte) error {
return json.Unmarshal(data, &a)
}
// AVURNAVs represents multiple AVURNAV
type AVURNAVs []AVURNAV
// AVURNAVPayloads represents multiple AVRUNAV payloads
type AVURNAVPayloads []AVURNAVPayload
// AVURNAVs transforms payloads to AVURNAVs
func (p AVURNAVPayloads) AVURNAVs(premar PremarInterface) AVURNAVs {
var avurnavs AVURNAVs
for _, v := range p {
avurnavs = append(avurnavs, v.AVURNAV(premar))
}
return avurnavs
}
// List lists AVURNAVs that are currently available
func (f *AVURNAVFetcher) List() (AVURNAVs, *http.Response, error) {
relative, err := url.Parse("?region=" + strings.ToLower(f.service.Region()))
if err != nil {
return nil, nil, err
}
base, _ := url.Parse(BASE_PREMAR)
theURL := base.ResolveReference(relative)
req, err := f.service.Client().NewRequest("GET", theURL, nil)
if err != nil {
return nil, nil, err
}
var payloads AVURNAVPayloads
response, err := f.service.Client().Do(req, &payloads)
if err != nil {
return nil, response, err
}
return payloads.AVURNAVs(f.service), response, err
}
// Get fetches the content of an AVURNAV from the web and returns it
func (f *AVURNAVFetcher) Get(a AVURNAV) (AVURNAV, *http.Response, error) {
url, err := url.Parse(a.URL)
if err != nil {
return AVURNAV{}, nil, err
}
req, err := f.service.Client().NewRequest("GET", url, nil)
if err != nil {
return AVURNAV{}, nil, err
}
var buf bytes.Buffer
response, err := f.service.Client().Do(req, &buf)
if err != nil {
return AVURNAV{}, response, err
}
return a.ParseContent(&buf), response, err
}