-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnhl.go
149 lines (129 loc) · 3.03 KB
/
nhl.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
package main
import (
"bytes"
"encoding/json"
"flag"
"fmt"
"io"
"net/http"
"os"
"strings"
"time"
"github.com/markbates/pkger"
"github.com/thoas/go-funk"
)
const author = "Steven Black (https://github.com/StevenBlack/nhl)"
const appVersion = "Version 0.1.4 (Jan 18 2019)"
const description = "NHL plaintext standings and stats"
var urls = map[string]string{
"standings": "https://statsapi.web.nhl.com/api/v1/standings?expand=standings.record",
"schedule": "https://statsapi.web.nhl.com/api/v1/schedule",
"scoring": "https://statsapi.web.nhl.com/api/v1/statTypes",
}
var client = http.Client{
Timeout: time.Second * 4, // Maximum of 4 secs
}
func main() {
// Process flags
version := flag.Bool("v", false, "prints current version")
description := flag.Bool("d", false, "prints a description of this utility")
author := flag.Bool("a", false, "prints the author information")
flag.Parse()
if *version {
fmt.Println(appVersion)
os.Exit(0)
}
if *description {
fmt.Println(description)
os.Exit(0)
}
if *author {
fmt.Println(author)
os.Exit(0)
}
// Sanitize options
options := os.Args[1:]
if len(options) > 0 {
// lowercase the options
for n := range options {
options[n] = strings.ToLower(options[n])
}
}
// unique options, please
options = funk.UniqString(options)
buf := bytes.NewBuffer(nil)
f, err := pkger.Open("/metadata/teamdata.json")
if err != nil {
fmt.Print(err)
return
}
io.Copy(buf, f)
f.Close()
var tm teams
err = json.Unmarshal(buf.Bytes(), &tm)
if err != nil {
fmt.Println("error:", err)
return
}
var mode = reckonMode(options)
switch mode {
case "standings":
standings()
case "scoring":
var teamIds = reckonTeams(options, tm)
scoring(teamIds)
default:
var teamIds = reckonTeams(options, tm)
schedule(teamIds)
}
}
func reckonTeams(opt []string, tm teams) []int {
// analyze options string and return a slice of team ids
// first, Selected set to false
for _, t := range tm {
t.Selected = false
}
// scour for teams
for _, o := range opt {
for ti, t := range tm {
if !t.Selected {
for _, a := range t.Aliases {
if o == a {
tm[ti].Selected = true
}
}
}
}
}
// prepare to return selected team ids
var teamIds []int
for _, t := range tm {
if t.Selected {
teamIds = append(teamIds, t.ID)
}
}
return teamIds
}
func reckonMode(opt []string) string {
scoringAliases := [...]string{"assists", "scoring", "goals", "points", "rookies", "forwards", "f", "defencemen", "defence", "d"}
if any(opt, scoringAliases) {
return "scoring"
}
scoresAliases := [...]string{"score", "scores"}
if any(opt, scoresAliases) {
return "scores"
}
scheduleAliases := [...]string{"sched", "schedule", "sked", "games"}
if any(opt, scheduleAliases) {
return "schedule"
}
return "standings"
}
type teams []struct {
ID int `json:"id"`
Abbreviation string `json:"abbreviation"`
City string `json:"city"`
Alias string `json:"alias"`
Selected bool `json:"selected"`
Aliases []string `json:"aliases"`
}