Skip to content

Commit 6ef447d

Browse files
committed
Sanitise method to get better descriptions and covers
1 parent bb5bb21 commit 6ef447d

File tree

5 files changed

+51
-35
lines changed

5 files changed

+51
-35
lines changed

.golangci.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ linters-settings:
2727

2828
- name: var-naming
2929
arguments:
30-
- ["ID", "JSON", "URL"] # Allow List
30+
- ["ID", "IDS", "JSON", "URL"] # Allow List
3131
- [] # Deny List
3232

3333
# https://golangci-lint.run/usage/linters/#stylecheck

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ require (
99
github.com/go-chi/chi/v5 v5.0.12
1010
github.com/go-chi/httplog/v2 v2.0.9
1111
github.com/jinzhu/inflection v1.0.0
12+
github.com/k3a/html2text v1.2.1
1213
github.com/oapi-codegen/nethttp-middleware v1.0.1
1314
github.com/oapi-codegen/runtime v1.1.1
1415
github.com/stretchr/testify v1.9.0

go.sum

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ github.com/go-test/deep v1.0.8 h1:TDsG77qcSprGbC6vTN8OuXp5g+J+b5Pcguhf7Zt61VM=
2525
github.com/go-test/deep v1.0.8/go.mod h1:5C2ZWiW0ErCdrYzpqxLbTX7MG14M9iiw8DgHncVwcsE=
2626
github.com/google/uuid v1.5.0 h1:1p67kYwdtXjb0gL0BPiP1Av9wiZPo5A8z2cWkTZ+eyU=
2727
github.com/google/uuid v1.5.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
28+
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1 h1:EGx4pi6eqNxGaHF6qqu48+N2wcFQ5qg5FXgOdqsJ5d8=
29+
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY=
2830
github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI=
2931
github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So=
3032
github.com/invopop/yaml v0.2.0 h1:7zky/qH+O0DwAyoobXUqvVBwgBFRxKoQ/3FjcVpjTMY=
@@ -33,7 +35,11 @@ github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD
3335
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
3436
github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY=
3537
github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y=
38+
github.com/jtolds/gls v4.20.0+incompatible h1:xdiiI2gbIgH/gLH7ADydsJ1uDOEzR8yvV7C0MuV77Wo=
39+
github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU=
3640
github.com/juju/gnuflag v0.0.0-20171113085948-2ce1bb71843d/go.mod h1:2PavIy+JPciBPrBUjwbNvtwB6RQlve+hkpll6QSNmOE=
41+
github.com/k3a/html2text v1.2.1 h1:nvnKgBvBR/myqrwfLuiqecUtaK1lB9hGziIJKatNFVY=
42+
github.com/k3a/html2text v1.2.1/go.mod h1:ieEXykM67iT8lTvEWBh6fhpH4B23kB9OMKPdIBmgUqA=
3743
github.com/kr/pretty v0.2.1 h1:Fmg33tUaq4/8ym9TJN1x7sLJnHVwhP33CNkpYV/7rwI=
3844
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
3945
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
@@ -52,6 +58,10 @@ github.com/perimeterx/marshmallow v1.1.5 h1:a2LALqQ1BlHM8PZblsDdidgv1mWi1DgC2UmX
5258
github.com/perimeterx/marshmallow v1.1.5/go.mod h1:dsXbUu8CRzfYP5a87xpp0xq9S3u0Vchtcl8we9tYaXw=
5359
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
5460
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
61+
github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d h1:zE9ykElWQ6/NYmHa3jpm/yHnI4xSofP+UP6SpjHcSeM=
62+
github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc=
63+
github.com/smartystreets/goconvey v1.6.4 h1:fv0U8FUIMPNf1L9lnHLvLhgicrIVChEkdzIKYqbNC9s=
64+
github.com/smartystreets/goconvey v1.6.4/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9QV7WQ/tjFTllLA=
5565
github.com/spkg/bom v0.0.0-20160624110644-59b7046e48ad/go.mod h1:qLr4V1qq6nMqFKkMo8ZTx3f+BZEkzsRUY10Xsm2mwU0=
5666
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
5767
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
@@ -64,14 +74,19 @@ github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsT
6474
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
6575
github.com/ugorji/go/codec v1.2.11 h1:BMaWp1Bb6fHwEtbplGBGJ498wD+LKlNSl25MjdZY4dU=
6676
github.com/ugorji/go/codec v1.2.11/go.mod h1:UNopzCgEMSXjBc6AOMqYvWC1ktqTAfzJZUZgYf6w6lg=
77+
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
6778
golang.org/x/lint v0.0.0-20210508222113-6edffad5e616 h1:VLliZ0d+/avPrXXH+OakdXhpJuEoBZuwh1m2j7U6Iug=
6879
golang.org/x/lint v0.0.0-20210508222113-6edffad5e616/go.mod h1:3xt1FjdF8hUf6vQPIChWIBhFzV8gjjsPE/fR3IyQdNY=
6980
golang.org/x/mod v0.12.0 h1:rmsUpXtvNzj340zd98LZ4KntptpfRHwpFOHG188oHXc=
7081
golang.org/x/mod v0.12.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
82+
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
83+
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
7184
golang.org/x/sys v0.15.0 h1:h48lPFYpsTvQJZF4EKyI4aLHaev3CxivZmv7yZig9pc=
7285
golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
86+
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
7387
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
7488
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
89+
golang.org/x/tools v0.0.0-20190328211700-ab21143f2384/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
7590
golang.org/x/tools v0.12.0 h1:YW6HUoUmYBpwSgyaGaZq1fHjrBjX1rlpZ54T6mu2kss=
7691
golang.org/x/tools v0.12.0/go.mod h1:Sc0INKfu04TlqNoRA1hgpFZbhYXHPr4V5DzpSBTPqQM=
7792
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=

goodreads/book.go

Lines changed: 32 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@ package goodreads
33
import (
44
"encoding/xml"
55
"math"
6+
"regexp"
67
"strings"
8+
9+
"github.com/k3a/html2text"
710
)
811

912
type Book struct {
@@ -14,21 +17,29 @@ type Book struct {
1417
Genres Genres `xml:"popular_shelves"` // The (max) first 5 "genre" shelves
1518
}
1619

20+
func (b *Book) Sanitise() {
21+
b.BestEdition.Sanitise()
22+
for _, series := range b.Series {
23+
series.Sanitise()
24+
}
25+
}
26+
1727
func (b *Book) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
1828
type alias Book
19-
type unmarshaller struct {
29+
var unmarshaller struct {
2030
alias
2131
Edition
2232
}
23-
24-
var book unmarshaller
25-
err := d.DecodeElement(&book, &start)
33+
err := d.DecodeElement(&unmarshaller, &start)
2634
if err != nil {
2735
return err
2836
}
2937

30-
*b = Book(book.alias)
31-
b.BestEdition = book.Edition
38+
*b = Book(unmarshaller.alias)
39+
b.BestEdition = unmarshaller.Edition
40+
41+
b.Sanitise()
42+
3243
return nil
3344
}
3445

@@ -72,26 +83,27 @@ type Edition struct {
7283
LanguageCode string `xml:"language_code"`
7384
}
7485

75-
// func (e *Edition) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
76-
// type alias Edition
77-
// var unmarshaller alias
78-
// err := d.DecodeElement(&unmarshaller, &start)
79-
// if err != nil {
80-
// return err
81-
// }
82-
// *e = Edition(unmarshaller)
86+
func (e *Edition) Sanitise() {
87+
// Description can sometimes be html, so convert to plain text
88+
e.Description = html2text.HTML2Text(e.Description)
8389

84-
// // Cleanup some fields
85-
// e.Description = html2text.HTML2Text(e.Description)
86-
87-
// return nil
88-
// }
90+
// Get largest image by removing anything between the last number and the extensions
91+
// For Example:
92+
// https://i.gr-assets.com/images/S/compressed.photo.goodreads.com/books/1546071216l/5907._SX98_.jpg"
93+
// Should be:
94+
// "https://i.gr-assets.com/images/S/compressed.photo.goodreads.com/books/1546071216l/5907.jpg"
95+
e.ImageURL = (regexp.MustCompile(`(\d+)\..*?\.(jpe?g)`).ReplaceAllString(e.ImageURL, "$1.$2"))
96+
}
8997

9098
type SeriesBook struct {
9199
Series Series `xml:"series"`
92100
BookPosition *string `xml:"user_position"`
93101
}
94102

103+
func (s *SeriesBook) Sanitise() {
104+
s.Series.Sanitise()
105+
}
106+
95107
type Series struct {
96108
Id string `xml:"id"`
97109
Title string `xml:"title"`
@@ -101,18 +113,7 @@ type Series struct {
101113
Numbered bool `xml:"numbered"`
102114
}
103115

104-
func (s *Series) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
105-
type alias Series
106-
var unmarshaller alias
107-
err := d.DecodeElement(&unmarshaller, &start)
108-
if err != nil {
109-
return err
110-
}
111-
*s = Series(unmarshaller)
112-
113-
// Cleanup some fields
116+
func (s *Series) Sanitise() {
114117
s.Title = strings.TrimSpace(s.Title)
115118
s.Description = strings.TrimSpace(s.Description)
116-
117-
return nil
118119
}

server/router.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,8 @@ func NewRouter() (http.Handler, error) {
5050
&oapimiddleware.Options{
5151
SilenceServersWarning: true,
5252
Options: openapi3filter.Options{
53-
AuthenticationFunc: func(ctx context.Context, authInput *openapi3filter.AuthenticationInput) error {
54-
// Skip auth
55-
return nil
53+
AuthenticationFunc: func(_ context.Context, _ *openapi3filter.AuthenticationInput) error {
54+
return nil // Do nothing
5655
},
5756
},
5857
},

0 commit comments

Comments
 (0)