-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
147 lines (136 loc) · 3.67 KB
/
main.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
package main
import (
"encoding/json"
"errors"
"fmt"
"net/http"
"net/url"
"os"
"slices"
"strings"
"github.com/PuerkitoBio/goquery"
)
const (
contentTypeForm = "application/x-www-form-urlencoded"
envKeyEircode = "EIRCODE"
urlAddressLookup = "https://siro.ie/address-lookup-result"
urlSearchEircode = "https://service.siro.ie/search-eircode"
urlValueCounty = "data[county]"
urlValueEircode = "data[eircode]"
urlValuePremiseID = "data[premiseId]"
urlValueQuery = "query"
urlValueTown = "data[town]"
urlValueValue = "data[value]"
)
func main() {
eircode, ok := os.LookupEnv(envKeyEircode)
if !ok {
panic(fmt.Sprintf("environment variable %q is not set", envKeyEircode))
}
ser, err := searchEircode(eircode)
if err != nil {
var noSuggestionsErr noSuggestionsError
if errors.As(err, &noSuggestionsErr) {
fmt.Println(noSuggestionsErr)
return
}
panic(err)
}
providers, err := addressLookup(ser)
if err != nil {
var notAvailableErr notAvailableError
if errors.As(err, ¬AvailableErr) {
fmt.Println(notAvailableErr)
return
}
panic(err)
}
fmt.Println("SIRO is available via the following internet service providers:", strings.Join(providers, ", "))
os.Exit(1) // alert on success
}
func searchEircode(eircode string) (*searchEircodeResponse, error) {
parsedURL, err := url.Parse(urlSearchEircode)
if err != nil {
return nil, err
}
query := make(url.Values)
query.Set(urlValueQuery, eircode)
parsedURL.RawQuery = query.Encode()
resp, err := http.Get(parsedURL.String())
if err != nil {
return nil, err
}
defer resp.Body.Close()
var ser *searchEircodeResponse
if err = json.NewDecoder(resp.Body).Decode(&ser); err != nil {
return nil, err
}
if len(ser.Suggestions) == 0 {
return nil, noSuggestionsError{
eircode: eircode,
}
}
return ser, nil
}
func addressLookup(ser *searchEircodeResponse) ([]string, error) {
suggestion := ser.Suggestions[0]
form := make(url.Values)
form.Set(urlValueValue, suggestion.Value)
form.Set(urlValuePremiseID, suggestion.Data.PremiseID)
form.Set(urlValueCounty, suggestion.Data.County)
form.Set(urlValueTown, suggestion.Data.Town)
form.Set(urlValueEircode, suggestion.Data.Eircode)
resp, err := http.Post(urlAddressLookup, contentTypeForm, strings.NewReader(form.Encode()))
if err != nil {
return nil, err
}
defer resp.Body.Close()
doc, err := goquery.NewDocumentFromReader(resp.Body)
if err != nil {
return nil, err
}
selection := doc.Find("div .retailers_block > div")
if selection.Length() == 0 {
return nil, notAvailableError{
eircode: suggestion.Data.Eircode,
}
}
providerSet := make(map[string]struct{})
selection.Each(func(_ int, selection *goquery.Selection) {
provider, ok := selection.Attr("data-provider-name")
if !ok {
panic("could not find internet service provider attribute")
}
providerSet[provider] = struct{}{}
})
providers := make([]string, 0, len(providerSet))
for provider := range providerSet {
providers = append(providers, provider)
}
slices.Sort(providers)
return providers, nil
}
type noSuggestionsError struct {
eircode string
}
func (e noSuggestionsError) Error() string {
return fmt.Sprintf("no suggestions found for eircode %s", e.eircode)
}
type notAvailableError struct {
eircode string
}
func (e notAvailableError) Error() string {
return fmt.Sprintf("SIRO is not yet available at %s", e.eircode)
}
type searchEircodeResponse struct {
Query string `json:"query"`
Suggestions []struct {
Value string `json:"value"`
Data struct {
PremiseID string `json:"premiseId"`
County string `json:"county"`
Town string `json:"town"`
Eircode string `json:"eircode"`
} `json:"data"`
} `json:"suggestions"`
}