-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_teams.go
97 lines (91 loc) · 2.39 KB
/
get_teams.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
package main
import (
"fmt"
"html"
"strings"
"time"
)
var (
cfbTeamsURL = "http://www.espn.com/college-football/teams"
nflTeamsURL = "http://www.espn.com/nfl/teams"
)
// Team is a struct storing team name and id
type Team struct {
ID string
Division string
}
// CacheEntry stores cache of last time team one
type CacheEntry struct {
lastRefresh time.Time
lastWin int
}
func getCfbTeams() map[string]Team {
body := getHTML(cfbTeamsURL)
searchString := "/college-football/team/_/id/"
skipLength := len(searchString)
teamSet := map[string]bool{}
teamMap := map[string]Team{}
cursor := 0
for cursor < len(body) {
index := strings.Index(body[cursor:], searchString)
if index == -1 {
break
}
cursor += index
index = strings.Index(body[cursor+skipLength:], "/")
if index == -1 {
break
}
idEnd := cursor + skipLength + index
teamID := body[cursor+skipLength : idEnd]
if _, ok := teamSet[teamID]; !ok {
// skip over <a> ending angle bracket
cursor = strings.Index(body[cursor:], "<h2") + cursor
nameStart := strings.Index(body[cursor:], ">") + cursor + 1
nameEnd := strings.Index(body[nameStart:], "<") + nameStart
teamName := html.UnescapeString(body[nameStart:nameEnd])
cursor = nameEnd
teamSet[teamID] = true
teamMap[teamName] = Team{teamID, "cfb"}
} else {
cursor++
}
}
fmt.Printf("%d CFB teams captured\n", len(teamMap))
return teamMap
}
func getNflTeams() map[string]Team {
body := getHTML(nflTeamsURL)
searchString := "/nfl/team/_/name/"
skipLength := len(searchString)
teamSet := map[string]bool{}
teamMap := map[string]Team{}
cursor := 0
for cursor < len(body) {
index := strings.Index(body[cursor:], searchString)
if index == -1 {
break
}
cursor += index
index = strings.Index(body[cursor+skipLength:], "/")
if index == -1 {
break
}
idEnd := cursor + skipLength + index
teamID := body[cursor+skipLength : idEnd]
if _, ok := teamSet[teamID]; !ok {
// skip over <a> ending angle bracket
cursor = strings.Index(body[cursor:], "<h2") + cursor
nameStart := strings.Index(body[cursor:], ">") + cursor + 1
nameEnd := strings.Index(body[nameStart:], "<") + nameStart
teamName := html.UnescapeString(body[nameStart:nameEnd])
cursor = nameEnd
teamSet[teamID] = true
teamMap[teamName] = Team{teamID, "nfl"}
} else {
cursor++
}
}
fmt.Printf("%d NFL teams captured\n", len(teamMap))
return teamMap
}