-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.go
85 lines (77 loc) · 1.89 KB
/
utils.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
package main
import (
"io/ioutil"
"log"
"net/http"
"os"
"strconv"
"strings"
"time"
"github.com/sahilm/fuzzy"
)
func loadConfigs() {
p := os.Getenv("PORT")
if p != "" {
log.Printf("Using port %s\n", p)
port = p
} else {
log.Println("$PORT not provided, using default port 8080")
}
t := os.Getenv("TTL")
if t != "" {
log.Printf("Using cache ttl of %s seconds\n", t)
if val, err := strconv.Atoi(t); err != nil {
ttl = time.Second * time.Duration(val)
}
} else {
log.Println("$TTL not provided, using default cache ttl of 5 seconds")
}
}
func loadTeams() {
for k, v := range getCfbTeams() {
allTeams[k] = v
allTeamNames = append(allTeamNames, k)
}
for k, v := range getNflTeams() {
allTeams[k] = v
allTeamNames = append(allTeamNames, k)
}
}
func getRankedTeams(query string) []string {
matchedTeamNames := []string{}
matches := fuzzy.Find(query, allTeamNames)
for i := range matches {
matchedTeamNames = append(matchedTeamNames, matches[i].Str)
}
return matchedTeamNames
}
func getHTML(url string) string {
resp, err := http.Get(url)
if err != nil {
return ""
}
defer resp.Body.Close()
rawText, err := ioutil.ReadAll(resp.Body)
if err != nil {
return ""
}
return string(rawText[:])
}
func getDate(body string) string {
if strings.Contains(body, ">Preseason<") {
regularSeason := body[0:strings.Index(body, ">Preseason<")]
preSeason := body[strings.Index(body, ">Preseason<"):]
if strings.Contains(regularSeason, "clr-positive") {
return getDateFromSeason(regularSeason)
}
return getDateFromSeason(preSeason)
}
return getDateFromSeason(body)
}
func getDateFromSeason(body string) string {
lastWin := strings.LastIndex(body, "clr-positive")
rowIdx := strings.LastIndex(body[:lastWin], "tr")
dateStart := rowIdx + strings.Index(body[rowIdx:], ", ") + 2
dateEnd := dateStart + strings.Index(body[dateStart:], "<")
return body[dateStart:dateEnd]
}