-
Notifications
You must be signed in to change notification settings - Fork 1
/
toycon.go
173 lines (161 loc) · 3.61 KB
/
toycon.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
package labo
import (
"strings"
"github.com/PuerkitoBio/goquery"
)
// Toycon is a snapshot of a Nintendo Labo Toycon.
//
// Toycon's are the cardboard products built from a Nintendo Labo kit. Each Toycon
// contains a series of features that are unique to the Nintendo Labo Kit.
type Toycon struct {
About string `json:"about"`
Description string `json:"description"`
Features []*Feature `json:"features"`
Icon *Image `json:"icon"`
Image *Image `json:"image"`
Name string `json:"name"`
}
var (
toyconFn = [](func(s *goquery.Selection, t *Toycon)){
getToyconAbout,
getToyconDescription,
getToyconFeatures,
getToyconIcon,
getToyconImage,
getToyconName}
)
// getToyconAbout searches the *goquery.Selection for the about string required for a labo.Toycon.
func getToyconAbout(s *goquery.Selection, t *Toycon) {
const (
CSS string = ".toy-con-sub-header p"
)
var (
ok bool
substring string
)
s = s.Find(CSS)
ok = (s.Length() > 0)
if !ok {
return
}
substring = strings.TrimSpace(s.Text())
ok = (len(substring) > 0)
if !ok {
return
}
t.About = substring
}
// getToyconDescription searches the *goquery.Selection for the description required for a labo.Toycon.
func getToyconDescription(s *goquery.Selection, t *Toycon) {
const (
CSS string = ".right-column .toy-con-info .copy p"
)
var (
ok bool
substring string
)
s = s.Find(CSS)
ok = (s.Length() > 0)
if !ok {
return
}
substring = strings.TrimSpace(s.Text())
ok = (len(substring) > 0)
if !ok {
return
}
t.Description = substring
}
// getToyconFeatures searches the *goquery.Selection for the *labo.Feature slice required for a labo.Toycon.
func getToyconFeatures(s *goquery.Selection, t *Toycon) {
const (
CSS string = ".left-column .toy-con-slider"
)
var (
ok bool
)
s = s.Find(CSS)
ok = (s.Length() > 0)
if !ok {
return
}
t.Features = newFeatures(s)
}
// getToyconIcon searches the *goquery.Selection for the icon *labo.Image struct required for a labo.Toycon.
func getToyconIcon(s *goquery.Selection, t *Toycon) {
const (
CSS string = ".right-column .toy-con-info .icon > img:nth-child(1)"
)
var (
ok bool
)
s = s.Find(CSS)
ok = (s.Length() > 0)
if !ok {
return
}
t.Icon = newImage(s)
}
// getToyconImage searches the *goquery.Selection for the *labo.Image struct required for a labo.Toycon.
func getToyconImage(s *goquery.Selection, t *Toycon) {
const (
CSS string = ".right-column .main-image picture:nth-child(1) > img:nth-child(1)"
)
var (
ok bool
)
s = s.Find(CSS)
ok = (s.Length() > 0)
if !ok {
return
}
t.Image = newImage(s)
}
// getToyconName searches the *goquery.Selection for the name of the Toycon required for a labo.Toycon.
func getToyconName(s *goquery.Selection, t *Toycon) {
const (
CSS string = ".toy-con-header h3"
)
var (
ok bool
substring string
)
s = s.Find(CSS)
ok = (s.Length() > 0)
if !ok {
return
}
substring = strings.TrimSpace(s.Text())
ok = (len(substring) > 0)
if !ok {
return
}
t.Name = substring
}
// newToycon is a constructor function that instantiates and returns a new *labo.Toycon.
func newToycon(s *goquery.Selection) *Toycon {
var (
t = &Toycon{}
)
for _, fn := range toyconFn {
fn(s, t)
}
return t
}
// newToycons is a constructor function that instantiates and returns a new slice of *labo.Toycon.
func newToycons(s *goquery.Selection) []*Toycon {
var (
toycon *Toycon
toycons []*Toycon
ok bool
)
s.Each(func(i int, s *goquery.Selection) {
toycon = newToycon(s)
ok = (toycon != nil)
if !ok {
return
}
toycons = append(toycons, toycon)
})
return toycons
}