-
Notifications
You must be signed in to change notification settings - Fork 0
/
seahawks.go
95 lines (77 loc) · 1.93 KB
/
seahawks.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
// This is my first Golang program!!
// It's a CLI that gives you seahawks scores
// if you have any feedback on this code, please create a an issue at https://github.com/pranavnt/seahawks-cli
// it wouold be greatly appreciated :)
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"os"
"strconv"
"github.com/joho/godotenv"
)
func main() {
if len(os.Args) < 2 {
fmt.Println("You have to provide a command :)")
os.Exit(1)
}
switch os.Args[1] {
case "score":
parseScore()
default:
fmt.Println("command not recognized")
os.Exit(1)
}
}
func parseScore() {
url := constructURL(2020, "REG", 8)
req, _ := http.NewRequest("GET", url, nil)
res, _ := http.DefaultClient.Do(req)
body, _ := ioutil.ReadAll(res.Body)
scoreJSON := string(body)
var score []GameStats
json.Unmarshal([]byte(scoreJSON), &score)
for i := 0; i < len(score); i++ {
homeTeam := score[i].Team
awayTeam := score[i].Opponent
homeTeamScore := score[i].Score
awayTeamScore := score[i].OpponentScore
if homeTeam == "SEA" {
fmt.Println("Seahawks Score:")
fmt.Println(homeTeam + ": " + str(homeTeamScore))
fmt.Println(awayTeam + ": " + str(awayTeamScore))
break
} else if awayTeam == "SEA" {
fmt.Println("Seahawks Score:")
fmt.Println(awayTeam + ": " + str(awayTeamScore))
fmt.Println(homeTeam + ": " + str(homeTeamScore))
break
} else {
}
}
}
func constructURL(season int, seasonType string, week int) (url string) {
url = "https://api.sportsdata.io/v3/nfl/scores/json/TeamGameStats/" + str(season) + seasonType + "/" + str(week) + "?key=" + loadAPI()
return
}
func str(intStr int) (str string) {
str = strconv.Itoa(intStr)
return
}
func loadAPI() (key string) {
err := godotenv.Load()
if err != nil {
fmt.Println("Error loading .env file")
os.Exit(1)
}
key = os.Getenv("API_KEY")
return
}
type GameStats struct {
Team string
Opponent string
Score int
OpponentScore int
}