-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
168 lines (138 loc) · 4.5 KB
/
main.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
package main
import (
"encoding/json"
"fmt"
"log"
"os"
"strconv"
s "strings"
"time"
"github.com/PuerkitoBio/goquery"
"github.com/gocolly/colly"
)
type Player struct {
Name string
IsActive bool
isHoF bool
Position string
Height string
Teams []string
SeasonAverages []SeasonAverage
}
type SeasonAverage struct {
Season string
Age int
Team string
GamesPlayed int
PtsPerGame float64
RebPerGame float64
AstPerGame float64
StlPerGame float64
BlkPerGame float64
TOVPerGame float64
FGP float64
ThreePP float64
FTPerGame float64
MPG float64
FTP float64
}
var LETTERS = []string{"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"}
func main() {
// Instantiate default collector
c := colly.NewCollector()
playerC := colly.NewCollector()
playerC.Limit(&colly.LimitRule{
DomainGlob: "*basketball-reference.com*",
Parallelism: 2,
Delay: 2 * time.Second,
})
// Import players.json
jsonFile, err := os.ReadFile("players.json")
players := map[string]Player{}
err = json.Unmarshal(jsonFile, &players)
if err != nil {
log.Fatal(err)
}
// On every a element which has href attribute call callback
c.OnHTML("tbody tr", func(e *colly.HTMLElement) {
name := e.ChildText("th")
playerProfileLink := e.ChildAttr("a", "href")
position := e.ChildText("td[data-stat='pos']")
height := e.ChildText("td[data-stat='height']")
var isActive bool
if e.ChildText("strong") != "" {
isActive = true
} else {
isActive = false
}
var isHoF bool
if s.Contains(e.ChildText("th"), "*") {
isHoF = true
name = s.Replace(name, "*", "", -1)
} else {
isHoF = false
}
player := Player{name, isActive, isHoF, position, height, []string{}, []SeasonAverage{}}
if _, ok := players[name]; !ok {
players[name] = player
}
if isActive {
playerC.Visit("https://www.basketball-reference.com" + playerProfileLink)
}
fmt.Println(player, playerProfileLink)
})
// Before making a request print "Visiting ..."
c.OnRequest(func(r *colly.Request) {
fmt.Println("Visiting", r.URL.String())
})
playerC.OnRequest(func(r *colly.Request) {
fmt.Println("Visiting", r.URL.String())
})
playerC.OnHTML("body", func(e *colly.HTMLElement) {
name := e.ChildText("h1")
teams := []string{}
teamsMap := map[string]bool{}
table := e.DOM.Find("table#playoffs_per_game tbody")
seasonAverages := []SeasonAverage{}
table.Children().Each(func(i int, s *goquery.Selection) {
team := s.Find("td[data-stat='team_id']").Text()
if team != "TOT" && !teamsMap[team] {
teams = append(teams, team)
teamsMap[team] = true
}
season := s.Find("th[data-stat='season']").Text()
age, _ := strconv.Atoi(s.Find("td[data-stat='age']").Text())
gamesPlayed, _ := strconv.Atoi(s.Find("td[data-stat='g']").Text())
ptsPerGame, _ := strconv.ParseFloat(s.Find("td[data-stat='pts_per_g']").Text(), 64)
rebPerGame, _ := strconv.ParseFloat(s.Find("td[data-stat='trb_per_g']").Text(), 64)
astPerGame, _ := strconv.ParseFloat(s.Find("td[data-stat='ast_per_g']").Text(), 64)
stlPerGame, _ := strconv.ParseFloat(s.Find("td[data-stat='stl_per_g']").Text(), 64)
blkPerGame, _ := strconv.ParseFloat(s.Find("td[data-stat='blk_per_g']").Text(), 64)
tovPerGame, _ := strconv.ParseFloat(s.Find("td[data-stat='tov_per_g']").Text(), 64)
fgp, _ := strconv.ParseFloat(s.Find("td[data-stat='fg_pct']").Text(), 64)
threePP, _ := strconv.ParseFloat(s.Find("td[data-stat='fg3_pct']").Text(), 64)
ftPerGame, _ := strconv.ParseFloat(s.Find("td[data-stat='ft_per_g']").Text(), 64)
mpg, _ := strconv.ParseFloat(s.Find("td[data-stat='mp_per_g']").Text(), 64)
ftP, _ := strconv.ParseFloat(s.Find("td[data-stat='ft_pct']").Text(), 64)
seasonAverage := SeasonAverage{season, age, team, gamesPlayed, ptsPerGame, rebPerGame, astPerGame, stlPerGame, blkPerGame, tovPerGame, fgp, threePP, ftPerGame, mpg, ftP}
seasonAverages = append(seasonAverages, seasonAverage)
})
player := players[name]
player.Teams = teams
player.SeasonAverages = seasonAverages
players[name] = player
})
for _, letter := range LETTERS {
c.Visit("https://www.basketball-reference.com/players/" + letter + "/")
}
// Write players to json file
jsonPlayers, err := json.Marshal(players)
if err != nil {
log.Fatal(err)
}
err = os.WriteFile("players-playoffs.json", jsonPlayers, 0644)
if err != nil {
log.Fatal(err)
}
fmt.Println("Done")
}