Skip to content

Commit cefaaac

Browse files
authored
Merge pull request #19 from rusq/i18
fix parsing for v1.2.24215.01 of the page
2 parents 6f64cfc + 6ca1d1a commit cefaaac

9 files changed

Lines changed: 1112 additions & 235 deletions

File tree

.github/workflows/go.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@ jobs:
1414
build:
1515
runs-on: ubuntu-latest
1616
steps:
17-
- uses: actions/checkout@v3
17+
- uses: actions/checkout@v4
1818

1919
- name: Set up Go
20-
uses: actions/setup-go@v3
20+
uses: actions/setup-go@v5
2121
with:
22-
go-version: 1.22
22+
go-version: 1.23
2323

2424
- name: Build
2525
run: go build -v ./...

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM golang:1.22-alpine AS builder
1+
FROM golang:1.23-alpine AS builder
22
LABEL maintainer="github:@rusq"
33

44
WORKDIR /build

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module github.com/rusq/aklapi
33
go 1.22
44

55
require (
6-
github.com/PuerkitoBio/goquery v1.9.1
6+
github.com/PuerkitoBio/goquery v1.9.2
77
github.com/rusq/osenv/v2 v2.0.1
88
github.com/stretchr/testify v1.8.1
99
)
@@ -12,6 +12,6 @@ require (
1212
github.com/andybalholm/cascadia v1.3.2 // indirect
1313
github.com/davecgh/go-spew v1.1.1 // indirect
1414
github.com/pmezard/go-difflib v1.0.0 // indirect
15-
golang.org/x/net v0.24.0 // indirect
15+
golang.org/x/net v0.28.0 // indirect
1616
gopkg.in/yaml.v3 v3.0.1 // indirect
1717
)

go.sum

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
1-
github.com/PuerkitoBio/goquery v1.8.1 h1:uQxhNlArOIdbrH1tr0UXwdVFgDcZDrZVdcpygAcwmWM=
2-
github.com/PuerkitoBio/goquery v1.8.1/go.mod h1:Q8ICL1kNUJ2sXGoAhPGUdYDJvgQgHzJsnnd3H7Ho5jQ=
3-
github.com/PuerkitoBio/goquery v1.9.1 h1:mTL6XjbJTZdpfL+Gwl5U2h1l9yEkJjhmlTeV9VPW7UI=
4-
github.com/PuerkitoBio/goquery v1.9.1/go.mod h1:cW1n6TmIMDoORQU5IU/P1T3tGFunOeXEpGP2WHRwkbY=
5-
github.com/andybalholm/cascadia v1.3.1/go.mod h1:R4bJ1UQfqADjvDa4P6HZHLh/3OxWWEqc0Sk8XGwHqvA=
1+
github.com/PuerkitoBio/goquery v1.9.2 h1:4/wZksC3KgkQw7SQgkKotmKljk0M6V8TUvA8Wb4yPeE=
2+
github.com/PuerkitoBio/goquery v1.9.2/go.mod h1:GHPCaP0ODyyxqcNoFGYlAprUFH81NuRPd0GX3Zu2Mvk=
63
github.com/andybalholm/cascadia v1.3.2 h1:3Xi6Dw5lHF15JtdcmAHD3i1+T8plmv7BQ/nsViSLyss=
74
github.com/andybalholm/cascadia v1.3.2/go.mod h1:7gtRlve5FxPPgIgX36uWBX58OdBsSS6lUvCFb+h7KvU=
85
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
@@ -19,28 +16,25 @@ github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
1916
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
2017
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
2118
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
19+
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
20+
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
2221
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
2322
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
2423
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
2524
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
2625
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
2726
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
2827
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
29-
golang.org/x/net v0.0.0-20210916014120-12bc252f5db8/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
3028
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
3129
golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
32-
golang.org/x/net v0.7.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
3330
golang.org/x/net v0.9.0/go.mod h1:d48xBJpPfHeWQsugry2m+kC02ZBRGRgulfHnEXEuWns=
34-
golang.org/x/net v0.19.0 h1:zTwKpTd2XuCqf8huc7Fo2iSy+4RHPd10s4KzeTnVr1c=
35-
golang.org/x/net v0.19.0/go.mod h1:CfAk/cbD4CthTvqiEl8NpboMuiuOYsAr/7NOjZJtv1U=
36-
golang.org/x/net v0.24.0 h1:1PcaxkF854Fu3+lvBIx5SYn9wRlBzzcnHZSiaFFAb0w=
37-
golang.org/x/net v0.24.0/go.mod h1:2Q7sJY5mzlzWjKtYUEXSlBWCdyaioyXzRB2RtU8KVE8=
31+
golang.org/x/net v0.28.0 h1:a9JDOJc5GMUJ0+UDqmLT86WiEy7iWyIhz8gz8E4e5hE=
32+
golang.org/x/net v0.28.0/go.mod h1:yqtgsTWOOnlGLG9GFRrK3++bGOUEkNBoHZc8MEDWPNg=
3833
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
3934
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
4035
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
4136
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
4237
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
43-
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
4438
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
4539
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
4640
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
@@ -52,7 +46,6 @@ golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
5246
golang.org/x/term v0.7.0/go.mod h1:P32HKFT3hSsZrRxla30E9HqToFYAQPCMs/zFMBUFqPY=
5347
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
5448
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
55-
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
5649
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
5750
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
5851
golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=

mocks_test.go

Lines changed: 0 additions & 15 deletions
This file was deleted.

rubbish.go

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,23 @@ type RubbishCollection struct {
3434
FoodScraps bool
3535
}
3636

37+
func (r *RubbishCollection) String() string {
38+
return fmt.Sprintf("%s: %s", r.Day, r.Type())
39+
}
40+
41+
func (r *RubbishCollection) Type() string {
42+
switch {
43+
case r.Rubbish:
44+
return "Rubbish"
45+
case r.Recycle:
46+
return "Recycle"
47+
case r.FoodScraps:
48+
return "Food Scraps"
49+
default:
50+
return "Unknown"
51+
}
52+
}
53+
3754
// CollectionDayDetailResult contains the information about Rubbish and
3855
// Recycling collection.
3956
type CollectionDayDetailResult struct {
@@ -155,11 +172,15 @@ func (p *refuseParser) parseLinks(el int, sel *goquery.Selection) {
155172
sel.Children().Children().Each(func(n int, sel *goquery.Selection) {
156173
switch n {
157174
case 0:
158-
if sel.Text() == "Rubbish" {
175+
attr, found := sel.Attr("class")
176+
if !found {
177+
return
178+
}
179+
if attr == "icon-rubbish" {
159180
p.detail[el].Rubbish = true
160-
} else if sel.Text() == "Food scraps" {
181+
} else if attr == "icon-food-waste" {
161182
p.detail[el].FoodScraps = true
162-
} else if sel.Text() == "Recycle" {
183+
} else if attr == "icon-recycle" {
163184
p.detail[el].Recycle = true
164185
} else {
165186
p.Err = fmt.Errorf("parse error: sel.Text = %q, el = %d, n = %d", sel.Text(), el, n)

rubbish_test.go

Lines changed: 105 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package aklapi
22

33
import (
4+
_ "embed"
45
"encoding/json"
56
"io"
67
"net/http"
@@ -13,6 +14,19 @@ import (
1314
"github.com/stretchr/testify/assert"
1415
)
1516

17+
//go:generate curl -L https://www.aucklandcouncil.govt.nz/rubbish-recycling/rubbish-recycling-collections/Pages/collection-day-detail.aspx?an=12342478585 -o test_assets/500-queen-street.html
18+
//go:generate curl -L https://www.aucklandcouncil.govt.nz/rubbish-recycling/rubbish-recycling-collections/Pages/collection-day-detail.aspx?an=12341511281 -o test_assets/1-luanda-drive.html
19+
20+
// Test data, run go:generate to update, then update dates in tests
21+
// accordingly.
22+
var (
23+
//go:embed "test_assets/1-luanda-drive.html"
24+
taRsd1LuandaDrive string
25+
26+
//go:embed "test_assets/500-queen-street.html"
27+
taCom500QueenStreet string
28+
)
29+
1630
func Test_parse(t *testing.T) {
1731
type args struct {
1832
r io.Reader
@@ -23,37 +37,48 @@ func Test_parse(t *testing.T) {
2337
want *CollectionDayDetailResult
2438
wantErr bool
2539
}{
26-
{"ok",
27-
args{strings.NewReader(testHTML)},
40+
{"1 Luanda Drive, Ranui",
41+
args{strings.NewReader(taRsd1LuandaDrive)},
2842
&CollectionDayDetailResult{
2943
Collections: []RubbishCollection{
30-
{Day: "Sunday 21 April",
31-
Date: adjustYear(time.Date(0, 04, 21, 0, 0, 0, 0, defaultLoc)),
44+
{
45+
Day: "Tuesday 27 August",
46+
Date: adjustYear(time.Date(0, 8, 27, 0, 0, 0, 0, defaultLoc)),
3247
Rubbish: true,
3348
Recycle: false,
34-
FoodScraps: false},
35-
{Day: "Sunday 21 April",
36-
Date: adjustYear(time.Date(0, 04, 21, 0, 0, 0, 0, defaultLoc)),
49+
FoodScraps: false,
50+
},
51+
{
52+
Day: "Tuesday 27 August",
53+
Date: adjustYear(time.Date(0, 8, 27, 0, 0, 0, 0, defaultLoc)),
54+
Rubbish: false,
55+
Recycle: false,
56+
FoodScraps: true,
57+
},
58+
{
59+
Day: "Tuesday 3 September",
60+
Date: adjustYear(time.Date(0, 9, 3, 0, 0, 0, 0, defaultLoc)),
3761
Rubbish: false,
3862
Recycle: true,
39-
FoodScraps: false},
63+
FoodScraps: false,
64+
},
4065
},
4166
Address: nil,
4267
},
4368
false},
44-
{"500 queen ok",
45-
args{strings.NewReader(testHTMLcommercial)},
69+
{"500 Queen Street, CBD",
70+
args{strings.NewReader(taCom500QueenStreet)},
4671
&CollectionDayDetailResult{
4772
Collections: []RubbishCollection{
4873
{
49-
Day: "Sunday 21 April",
50-
Date: adjustYear(time.Date(0, 04, 21, 0, 0, 0, 0, defaultLoc)),
74+
Day: "Thursday 22 August",
75+
Date: adjustYear(time.Date(0, 8, 22, 0, 0, 0, 0, defaultLoc)),
5176
Rubbish: true,
5277
Recycle: false,
5378
},
5479
{
55-
Day: "Sunday 21 April",
56-
Date: adjustYear(time.Date(0, 04, 21, 0, 0, 0, 0, defaultLoc)),
80+
Day: "Thursday 22 August",
81+
Date: adjustYear(time.Date(0, 8, 22, 0, 0, 0, 0, defaultLoc)),
5782
Rubbish: false,
5883
Recycle: true,
5984
},
@@ -74,6 +99,71 @@ func Test_parse(t *testing.T) {
7499
}
75100
}
76101

102+
func TestCollectionDayDetail(t *testing.T) {
103+
type args struct {
104+
addr string
105+
}
106+
tests := []struct {
107+
name string
108+
testSrv *httptest.Server
109+
args args
110+
want *CollectionDayDetailResult
111+
wantErr bool
112+
}{
113+
{"main branch",
114+
httptest.NewServer(testMux()),
115+
args{addr: "xxx"},
116+
&CollectionDayDetailResult{
117+
Collections: []RubbishCollection{
118+
{
119+
Day: "Tuesday 27 August",
120+
Date: adjustYear(time.Date(0, 8, 27, 0, 0, 0, 0, defaultLoc)),
121+
Rubbish: true,
122+
Recycle: false,
123+
FoodScraps: false,
124+
},
125+
{
126+
Day: "Tuesday 27 August",
127+
Date: adjustYear(time.Date(0, 8, 27, 0, 0, 0, 0, defaultLoc)),
128+
Rubbish: false,
129+
Recycle: false,
130+
FoodScraps: true,
131+
},
132+
{
133+
Day: "Tuesday 3 September",
134+
Date: adjustYear(time.Date(0, 9, 3, 0, 0, 0, 0, defaultLoc)),
135+
Rubbish: false,
136+
Recycle: true,
137+
FoodScraps: false,
138+
},
139+
},
140+
Address: &Address{
141+
ACRateAccountKey: "42",
142+
Address: "Red Square",
143+
Suggestion: "Red Square",
144+
},
145+
},
146+
false,
147+
},
148+
}
149+
for _, tt := range tests {
150+
t.Run(tt.name, func(t *testing.T) {
151+
defer tt.testSrv.Close()
152+
oldAddrURI := addrURI
153+
oldcollectionDayURI := collectionDayURI
154+
defer func() { addrURI = oldAddrURI; collectionDayURI = oldcollectionDayURI }()
155+
addrURI = tt.testSrv.URL + "/addr/"
156+
collectionDayURI = tt.testSrv.URL + "/rubbish/?an=%s"
157+
got, err := CollectionDayDetail(tt.args.addr)
158+
if (err != nil) != tt.wantErr {
159+
t.Errorf("CollectionDayDetail() error = %v, wantErr %v", err, tt.wantErr)
160+
return
161+
}
162+
assert.Equal(t, tt.want, got)
163+
})
164+
}
165+
}
166+
77167
func TestCollectionDayDetailResult_NextRubbish(t *testing.T) {
78168
type fields struct {
79169
Collections []RubbishCollection
@@ -192,64 +282,6 @@ func TestRubbishCollection_parseDate(t *testing.T) {
192282
}
193283
}
194284

195-
func TestCollectionDayDetail(t *testing.T) {
196-
type args struct {
197-
addr string
198-
}
199-
tests := []struct {
200-
name string
201-
testSrv *httptest.Server
202-
args args
203-
want *CollectionDayDetailResult
204-
wantErr bool
205-
}{
206-
{"main branch",
207-
httptest.NewServer(testMux()),
208-
args{addr: "xxx"},
209-
&CollectionDayDetailResult{
210-
Collections: []RubbishCollection{
211-
{
212-
Day: "Sunday 21 April",
213-
Date: adjustYear(time.Date(0, 4, 21, 0, 0, 0, 0, defaultLoc)),
214-
Rubbish: true,
215-
Recycle: false,
216-
FoodScraps: false,
217-
},
218-
{
219-
Day: "Sunday 21 April",
220-
Date: adjustYear(time.Date(0, 4, 21, 0, 0, 0, 0, defaultLoc)),
221-
Rubbish: false,
222-
Recycle: true,
223-
FoodScraps: false,
224-
},
225-
},
226-
Address: &Address{
227-
ACRateAccountKey: "42",
228-
Address: "Red Square",
229-
Suggestion: "Red Square",
230-
},
231-
},
232-
false,
233-
},
234-
}
235-
for _, tt := range tests {
236-
t.Run(tt.name, func(t *testing.T) {
237-
defer tt.testSrv.Close()
238-
oldAddrURI := addrURI
239-
oldcollectionDayURI := collectionDayURI
240-
defer func() { addrURI = oldAddrURI; collectionDayURI = oldcollectionDayURI }()
241-
addrURI = tt.testSrv.URL + "/addr/"
242-
collectionDayURI = tt.testSrv.URL + "/rubbish/?an=%s"
243-
got, err := CollectionDayDetail(tt.args.addr)
244-
if (err != nil) != tt.wantErr {
245-
t.Errorf("CollectionDayDetail() error = %v, wantErr %v", err, tt.wantErr)
246-
return
247-
}
248-
assert.Equal(t, tt.want, got)
249-
})
250-
}
251-
}
252-
253285
func testMux() http.Handler {
254286
mux := http.NewServeMux()
255287
mux.HandleFunc("/addr/", func(w http.ResponseWriter, r *http.Request) {
@@ -260,7 +292,7 @@ func testMux() http.Handler {
260292
w.Write(data)
261293
})
262294
mux.HandleFunc("/rubbish/", func(w http.ResponseWriter, r *http.Request) {
263-
w.Write([]byte(testHTML))
295+
w.Write([]byte(taRsd1LuandaDrive))
264296
})
265297

266298
return mux

0 commit comments

Comments
 (0)