forked from ryzhak/go_flashscore_parser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
go_flashscore_parser.go
156 lines (136 loc) · 4.01 KB
/
go_flashscore_parser.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
package go_flashscore_parser
import (
"fmt"
"log"
"github.com/fedesog/webdriver"
"time"
"strings"
)
type matchBlock struct {
countryName string
leagueName string
matches []match
}
type match struct {
homeTeamName string
awayTeamName string
goalsHome string
goalsAway string
startTime string
}
const (
FLASHSCORE_URL = "http://www.flashscore.ru"
FLASHSCORE_LOAD_TIME = 10
)
func GetGames(chromeDriverPath string) []matchBlock {
//adding chromedriver and opening page
chromeDriver := webdriver.NewChromeDriver(chromeDriverPath)
err := chromeDriver.Start()
if err != nil {
log.Println(err)
}
desired := webdriver.Capabilities{"Platform": "Linux"}
required := webdriver.Capabilities{}
session, err := chromeDriver.NewSession(desired, required)
if err != nil {
log.Println(err)
}
err = session.Url(FLASHSCORE_URL)
if err != nil {
log.Println(err)
}
//wait for page to load
time.Sleep(FLASHSCORE_LOAD_TIME * time.Second)
//array game blocks
var blocks []matchBlock
//find all blocks with games
HTMLblocks, _ := session.FindElements(webdriver.CSS_Selector, "table.soccer")
for i := 0; i < len(HTMLblocks); i++ {
var matches []match
//finding country name
element, err := HTMLblocks[i].FindElement(webdriver.ClassName, "country_part")
if err != nil {
fmt.Println("Can not find country")
}
//deleting ":" from country name
textValue, err := element.Text()
if err != nil {
fmt.Println("Can not extract text")
}
countryName := strings.Replace(textValue, ":", "", -1)
//finding league name
element, err = HTMLblocks[i].FindElement(webdriver.ClassName, "tournament_part")
leagueName, err := element.Text()
if err != nil {
fmt.Println("Can not extract league name text")
}
//finding all games in current league
matchTRs, err := HTMLblocks[i].FindElements(webdriver.CSS_Selector, "tbody tr")
if err != nil {
fmt.Println("Can not find matches in league")
}
for j := 0; j < len(matchTRs); j++ {
//finding team names
homeTeamNameEl, err := matchTRs[j].FindElement(webdriver.CSS_Selector, "span.padr")
if err != nil {
fmt.Println("Can not find homeTeamName")
}
awayTeamNameEl, err := matchTRs[j].FindElement(webdriver.CSS_Selector, "span.padl")
if err != nil {
fmt.Println("Can not find awayTeamName")
}
homeTeamName, err := homeTeamNameEl.Text()
if err != nil {
fmt.Println("Can not extract homeTeamName text")
}
awayTeamName, err := awayTeamNameEl.Text()
if err != nil {
fmt.Println("Can not extract awayTeamName text")
}
//finding scores
tdScoreEl, err := matchTRs[j].FindElement(webdriver.ClassName, "score")
if err != nil {
fmt.Println("Can not find score element")
}
tdScoreText, err := tdScoreEl.Text()
if err != nil {
fmt.Println("Can not extract text score")
}
//if game is playing or finished
goalsHome := ""
goalsAway := ""
tdLength := len(tdScoreText)
if(tdLength == 5){
goalsHome = string(tdScoreText[0]);
goalsAway = string(tdScoreText[tdLength - 1]);
}
//finding start time
tdTimeEl, err := matchTRs[j].FindElement(webdriver.ClassName, "time")
if err != nil {
fmt.Println("Can not time element")
}
startTime, err := tdTimeEl.Text()
if err != nil {
fmt.Println("Can not extract text from time")
}
matches = append(matches, match{homeTeamName:homeTeamName, awayTeamName:awayTeamName, goalsHome:goalsHome, goalsAway:goalsAway, startTime:startTime})
}
blocks = append(blocks, matchBlock{countryName:countryName, leagueName: leagueName, matches: matches})
}
session.Delete()
chromeDriver.Stop()
return blocks;
}
func Show(blocks []matchBlock){
for i:=0; i < len(blocks); i++ {
fmt.Printf("%s %s\n", blocks[i].countryName, blocks[i].leagueName)
for j:=0; j < len(blocks[i].matches); j++ {
fmt.Printf("%s %s %s:%s %s\n",
blocks[i].matches[j].startTime,
blocks[i].matches[j].homeTeamName,
blocks[i].matches[j].goalsHome,
blocks[i].matches[j].goalsAway,
blocks[i].matches[j].awayTeamName)
}
}
}