-
Notifications
You must be signed in to change notification settings - Fork 0
/
livescore.go
91 lines (79 loc) · 1.95 KB
/
livescore.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
package app
import (
"strings"
"github.com/yhat/scrape"
"golang.org/x/net/html"
)
const (
classTag = "class"
classContentTag = "content"
classRowGray = "row-gray"
classMinElmt = "min"
classTrightElmt = "tright"
classPlyElmt = "ply"
classScoreLink = "scorelink"
)
//LivescoreParser parse livescore
func LivescoreParser(root *html.Node) []Match {
var matches []Match
contentElmt, contentOK := scrape.Find(root, scrape.ByClass(classContentTag))
if contentOK {
//find all row-gray
rowGrayMatcher := func(n *html.Node) bool {
classes := strings.Fields(scrape.Attr(n, "class"))
for _, c := range classes {
if c == classRowGray {
parentClasses := strings.Fields(scrape.Attr(n.Parent, "class"))
for _, pc := range parentClasses {
if pc == classContentTag {
return true
}
}
}
}
return false
}
rows := scrape.FindAll(contentElmt, rowGrayMatcher)
matchChann := make(chan Match)
for _, rowElmt := range rows {
go func(rowElmt *html.Node) {
var time string
var homeTeam string
var awayTeam string
var score string
timeElmt, timeElmtOK := scrape.Find(rowElmt, scrape.ByClass(classMinElmt))
if timeElmtOK {
time = scrape.Text(timeElmt)
}
scoreElmt, scoreElmtOK := scrape.Find(rowElmt, scrape.ByClass(classScoreLink))
if scoreElmtOK {
score = scrape.Text(scoreElmt)
}
teamElmts := scrape.FindAll(rowElmt, scrape.ByClass(classPlyElmt))
for i := 0; i < len(teamElmts); i++ {
teamElmt := teamElmts[i]
if i%2 == 0 {
homeTeam = scrape.Text(teamElmt)
} else {
awayTeam = scrape.Text(teamElmt)
}
}
match := Match{
HomeTeam: homeTeam,
AwayTeam: awayTeam,
Score: score,
Time: time,
}
matchChann <- match
}(rowElmt)
}
for i := 0; i < len(rows); i++ {
select {
case m := <-matchChann:
matches = append(matches, m)
}
}
close(matchChann)
}
return matches
}