-
Notifications
You must be signed in to change notification settings - Fork 1
/
store_page.go
71 lines (56 loc) · 1.33 KB
/
store_page.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
package steam_integration
import (
"github.com/boggydigital/match_node"
"golang.org/x/net/html"
"golang.org/x/net/html/atom"
"strings"
)
const (
tagsContainerClassVariant1 = "glance_tags popular_tags"
tagsContainerClassVariant2 = "popular_tags glance_tags"
)
type StorePage struct {
Doc *html.Node
}
type SteamTagsGetter interface {
GetSteamTags() []string
}
func (sp *StorePage) GetSteamTags() []string {
if sp.Doc == nil {
return nil
}
if tagsContainer := match_node.Match(sp.Doc, &appTagMatcher{}); tagsContainer != nil {
return extractTagsFromContainer(tagsContainer)
}
return nil
}
type appTagMatcher struct{}
func (atm *appTagMatcher) Match(node *html.Node) bool {
if node.Type != html.ElementNode ||
node.DataAtom != atom.Div ||
len(node.Attr) == 0 {
return false
}
for _, a := range node.Attr {
if a.Key == "class" &&
(strings.Contains(a.Val, tagsContainerClassVariant1) ||
strings.Contains(a.Val, tagsContainerClassVariant2)) {
return true
}
}
return false
}
func extractTagsFromContainer(node *html.Node) []string {
tags := make([]string, 0)
if node == nil ||
node.FirstChild == nil {
return tags
}
for ch := node.FirstChild; ch != nil; ch = ch.NextSibling {
if ch.DataAtom != atom.A {
continue
}
tags = append(tags, strings.Trim(ch.FirstChild.Data, "\n\t"))
}
return tags
}