-
Notifications
You must be signed in to change notification settings - Fork 1
/
feature.go
188 lines (175 loc) · 4.63 KB
/
feature.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
186
187
188
package labo
import (
"strings"
"github.com/PuerkitoBio/goquery"
)
// Feature is a snapshot of a unique Nintendo Labo kit mechanic provided by a Nintendo Labo Toycon.
//
// Features are provided from the official Nintendo Labo website.
type Feature struct {
Description string `json:"description"`
Icon *Image `json:"icon"`
Name string `json:"name"`
}
// getFeatureDescription searches the *goquery.Selection for the description required for a labo.Feature.
//
// Description is a short overview about the Nintendo Labo Toycon integration and the
// gameplay mechanics it offers. The description is provided from the official Nintendo Labo website.
//
// getFeatureDescription assigns a default description placeholder string if a description substring
// cannot be found.
func getFeatureDescription(s *goquery.Selection, f *Feature) {
const (
CSS string = ".copy > p"
)
var (
description = defaultFeatureDescription
ok bool
substring string
)
s = s.Find(CSS)
substring = strings.TrimSpace(s.Text())
ok = (len(substring) > 0)
if ok {
description = substring
}
f.Description = description
}
// getFeatureIcon searches the *goquery.Selection for the *labo.Image required for a labo.Feature.
//
// Image is a HTML img reference that is provided from the official Nintendo Labo website. Icons
// are generally SVG image files.
//
// getFeatureIcon does not assign an empty *labo.Image should no HTML img tag can be found.
func getFeatureIcon(s *goquery.Selection, f *Feature) {
const (
CSS string = "picture:nth-child(1) > img:nth-child(1)"
)
var (
ok bool
)
s = s.Find(CSS)
ok = (s.Length() > 0)
if !ok {
return
}
f.Icon = newImage(s)
}
// getFeatureName searches the *goquery.Selection for the name required for a labo.Feature.
//
// Name is the namespace of the Nintendo Labo Toycon feature. Name may be a shorthand caption
// that gives a broad summary of the feature but not as verbose as the description.
//
// getFeatureName assigns a default name placeholder string if a name substring
// cannot be found.
func getFeatureName(s *goquery.Selection, f *Feature) {
const (
CSS string = ".header:nth-child(1) > span:nth-child(1)"
)
var (
name = defaultFeatureName
ok bool
substring string
)
s = s.Find(CSS)
substring = strings.TrimSpace(s.Text())
ok = (len(substring) > 0)
if ok {
name = substring
}
f.Name = name
}
// getFeatureSelectionA searches the *goquery.Selection for the HTML content
// needed to get the Nintendo Labo feature icons.
func getFeatureSelectionA(s *goquery.Selection) *goquery.Selection {
const (
CSS string = ".slider-pagination:nth-child(1) > li"
)
var (
ok bool
)
s = s.Find(CSS)
ok = (s.Length() > 0)
if !ok {
return nil
}
return s
}
// getFeatureSelectionB searches the *goquery.Selection for the HTML content
// needed to get the Nintendo Labo feature main body.
func getFeatureSelectionB(s *goquery.Selection) *goquery.Selection {
const (
CSS string = ".slider-content > div"
)
var (
ok bool
)
s = s.Find(CSS)
ok = (s.Length() > 0)
if !ok {
return nil
}
return s
}
// getFeatureSelectionC searches the *goquery.Selection for the HTML content
// needed to get the Nintendo Labo feature name and description.
func getFeatureSelectionC(s *goquery.Selection) *goquery.Selection {
const (
CSS string = ".caption-content > div"
)
var (
ok bool
)
s = s.Find(CSS)
ok = (s.Length() > 0)
if !ok {
return nil
}
return s
}
// newFeature is a constructor function that instantiates and returns a new Feature struct pointer from
// the Nintendo Labo official website.
//
// newFeature requires a collection of specific HTML fragments that make up the entire contents
// of a Nintendo Labo Toycon feature. These are built from the getFeatureSelection lookups.
func newFeature(a, b, c *goquery.Selection) *Feature {
var (
feature = &Feature{}
)
getFeatureDescription(c, feature)
getFeatureIcon(a, feature)
getFeatureName(c, feature)
return feature
}
// newFeatures is a constructor function that instantiates and returns a slice of Feature struct pointers.
func newFeatures(s *goquery.Selection) []*Feature {
var (
a = getFeatureSelectionA(s)
b = getFeatureSelectionB(s)
c = getFeatureSelectionC(s)
ok bool
feature *Feature
features []*Feature
)
ok = (a != nil && b != nil && c != nil)
if !ok {
return features
}
ok = (a.Length() == b.Length())
if !ok {
return features
}
ok = (b.Length() == c.Length())
if !ok {
return features
}
a.Each(func(i int, _ *goquery.Selection) {
feature = newFeature(a.Eq(i), b.Eq(i), c.Eq(i))
ok = (feature != nil)
if !ok {
return
}
features = append(features, feature)
})
return features
}