-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgoverwatch.go
153 lines (122 loc) · 3.58 KB
/
goverwatch.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
package goverwatch
import (
"github.com/gocolly/colly"
"github.com/mattishere/goverwatch/data"
"github.com/mattishere/goverwatch/ranks"
"github.com/mattishere/goverwatch/url"
)
// Returns a Stats struct containing the profile and ranks of the player
func GetStats(name string, discriminator int) (stats data.Stats, err error) {
c := colly.NewCollector()
link := url.GenerateURL(name, discriminator)
stats.Profile.Name = name
stats.Profile.Tag = discriminator
stats.Profile.URL = link
// Profile data (should be moved to the profile package eventually!)
c.OnHTML(".Profile-player--portrait", func(e *colly.HTMLElement) {
if e.Attr("src") != "" {
stats.Profile.ProfilePicture = e.Attr("src")
stats.Profile.Exists = true
}
})
if !stats.Profile.Exists {
// Is the profile private
c.OnHTML(".Profile-private---msg", func(e *colly.HTMLElement) {
stats.Profile.IsPrivate = true
})
c.OnHTML(".Profile-player--title", func(e *colly.HTMLElement) {
stats.Profile.Title = e.Text
})
c.OnHTML(".Profile-playerSummary--endorsement", func(e *colly.HTMLElement) {
stats.Profile.EndorsementIcon = e.Attr("src")
})
// Ranks (should be moved to the ranks package eventually!)
if !stats.Profile.IsPrivate {
var pcRoles []string
c.OnHTML(".mouseKeyboard-view .Profile-playerSummary--role img", func(e *colly.HTMLElement) {
src := e.Attr("src")
pcRoles = append(pcRoles, url.RoleURLS[src])
stats.PC.HasRanks = true
})
var i int
c.OnHTML(".mouseKeyboard-view .Profile-playerSummary--rankImageWrapper", func(e *colly.HTMLElement) {
if len(pcRoles) == 0 {
return
}
var rank data.Rank
e.ForEach(".Profile-playerSummary--rank", func(i int, e *colly.HTMLElement) {
// Rank
if i == 0 {
rankStr := ranks.GetRoleRank(e.Attr("src"))
rank.Rank = rankStr
rank.Icon = e.Attr("src")
return
}
// Division
division, divisionError := ranks.GetRoleDivision(e.Attr("src"))
if divisionError != nil {
err = divisionError
return
}
rank.Division = division
rank.DivisionIcon = e.Attr("src")
})
switch pcRoles[i] {
case "tank":
stats.PC.Ranks.Tank = rank
case "dps":
stats.PC.Ranks.DPS = rank
case "support":
stats.PC.Ranks.Support = rank
case "open":
stats.PC.Ranks.OpenQueue = rank
}
i++
})
var consoleRoles []string
c.OnHTML(".controller-view .Profile-playerSummary--role img", func(e *colly.HTMLElement) {
src := e.Attr("src")
consoleRoles = append(consoleRoles, url.RoleURLS[src])
stats.Console.HasRanks = true
})
var j int
c.OnHTML(".controller-view .Profile-playerSummary--rankImageWrapper", func(e *colly.HTMLElement) {
if len(consoleRoles) == 0 {
return
}
var rank data.Rank
e.ForEach(".Profile-playerSummary--rank", func(i int, e *colly.HTMLElement) {
// Rank
if i == 0 {
rankStr := ranks.GetRoleRank(e.Attr("src"))
rank.Rank = rankStr
rank.Icon = e.Attr("src")
return
}
// Division
division, divisionError := ranks.GetRoleDivision(e.Attr("src"))
if divisionError != nil {
err = divisionError
return
}
rank.Division = division
rank.DivisionIcon = e.Attr("src")
})
switch consoleRoles[j] {
case "tank":
stats.Console.Ranks.Tank = rank
case "dps":
stats.Console.Ranks.DPS = rank
case "support":
stats.Console.Ranks.Support = rank
case "open":
stats.Console.Ranks.OpenQueue = rank
}
j++
})
}
c.Visit(link)
return stats, err
}
return stats, nil
}