Skip to content
This repository has been archived by the owner on Mar 9, 2021. It is now read-only.

Update api #9

Merged
merged 6 commits into from
Oct 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 26 additions & 39 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,72 +11,59 @@ import (
"io/ioutil"
"net/http"
"strings"
"time"

"github.com/briandowns/spinner"
"github.com/grahamplata/sixers/config"
"github.com/logrusorgru/aurora"
)

func BuildURL(val1 string, val2 string) string {
url := fmt.Sprintf("%s/?seasons[]=%s,%s&postseason=False&team_ids[]=23&per_page=100", config.APIURL, val1, val2)
// BuildURL takes the query parameters and returns a string to be used to retrieve data from the api
func BuildURL(seasonYearStart string, seasonYearEnd string) string {
// TODO currently paging is hard coded address this later
url := fmt.Sprintf("%s/?seasons[]=%s,%s&postseason=False&team_ids[]=%v&per_page=100", config.APIURL, seasonYearStart, seasonYearEnd, config.TeamID)
return url
}

func NextResponse(response *http.Response) bool {
spin := spinner.New(spinner.CharSets[21], 100*time.Millisecond)
gameFound := false
t := time.Now().Format("2006-01-02")
spin.Start()
responseData, _ := ioutil.ReadAll(response.Body)
// NextResponse ...
func NextResponse(response *http.Response) string {
var responseObject Response

responseData, _ := ioutil.ReadAll(response.Body)
json.Unmarshal(responseData, &responseObject)
for i := 0; i < len(responseObject.Data); i++ {
cleanTime := fmt.Sprintf("%sT00:00:00.000Z", t)
if responseObject.Data[i].Date == cleanTime {
status := responseObject.Data[i].Status
gameTime := strings.TrimRight(responseObject.Data[i].Time, " ")
fmt.Printf("10,9 8 %s! There is a game currently @ %s %+s\n", config.SixersLogo, status, gameTime)
gameFound = true
}
}
spin.Stop()

if gameFound == true {
return true
for _, v := range responseObject.Data {
if v.Status != "Final" {
gameTime := strings.TrimRight(v.Time, " ")
resp := fmt.Sprintf("10,9 8 %s! There is a game currently @ %s %+s\n", config.SixersLogo, v.Status, gameTime)
return resp
}
}
return false
return "Sorry, there are not any available games."
}

// RecordResponse
// RecordResponse ...
func RecordResponse(response *http.Response) string {
spin := spinner.New(spinner.CharSets[21], 100*time.Millisecond)
spin.Start()
responseData, _ := ioutil.ReadAll(response.Body)
var gameCount, winRecord int
var responseObject Response

responseData, _ := ioutil.ReadAll(response.Body)
json.Unmarshal(responseData, &responseObject)
var gameCount int
var winRecord int
for i := 0; i < len(responseObject.Data); i++ {
if responseObject.Data[i].VisitorTeamScore != 0 || responseObject.Data[i].HomeTeamScore != 0 {
var visitorScore int = responseObject.Data[i].VisitorTeamScore
var homeScore int = responseObject.Data[i].HomeTeamScore
var homeID int = responseObject.Data[i].HomeTeam.ID
if homeID == 23 {
if homeScore > visitorScore {

for _, v := range responseObject.Data {
if v.VisitorTeamScore != 0 || v.HomeTeamScore != 0 {
if v.HomeTeam.ID == config.TeamID {
if v.HomeTeamScore > v.VisitorTeamScore {
winRecord++
}
} else {
if homeScore < visitorScore {
if v.HomeTeamScore < v.VisitorTeamScore {
winRecord++
}
}
gameCount++
}
}
spin.Stop()
wins := fmt.Sprintf("%s %d", aurora.Green("Wins:"), winRecord)
losses := fmt.Sprintf("%s %d", aurora.Red("Losses:"), (gameCount - winRecord))
pct := fmt.Sprintf("%s %.3f", aurora.Yellow("Win pct:"), (float64(winRecord) / float64(gameCount)))
return fmt.Sprintf("%s\n%s\n%s", wins, losses, pct)
return fmt.Sprintf("%s %s %s", wins, losses, pct)
}
124 changes: 58 additions & 66 deletions api/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,13 @@ Copyright © 2019 Graham Plata <graham.plata@gmail.com>
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"strings"
"testing"

"github.com/grahamplata/sixers/config"
)

var urlTests = []struct {
Expand All @@ -32,40 +36,60 @@ func TestBuildURL(t *testing.T) {
}
}

func TestNextResponse(t *testing.T) {
dummy := Response{
Data: []Game{
{
ID: 1,
Date: "2018-10-16T00:00:00.000Z",
HomeTeamScore: 123,
VisitorTeamScore: 122,
Season: 2020,
Period: 1,
Status: "Final",
Time: " ",
PostSeason: false,
HomeTeam: Team{
ID: 1,
Abbreviation: "BOS",
City: "Boston",
Conference: "East",
Division: "Atlantic",
FullName: "Boston Celtics",
Name: "Celtics",
},
VisitorTeam: Team{
ID: 23,
Abbreviation: "PHI",
City: "Philadelphia",
Conference: "East",
Division: "Atlantic",
FullName: "Philadelphia 76ers",
Name: "76ers",
},
var dummy = Response{
Data: []Game{
{
ID: 1,
Date: "2018-10-16T00:00:00.000Z",
HomeTeamScore: 123,
VisitorTeamScore: 122,
Season: 2020,
Period: 1,
Status: "Final",
Time: " ",
PostSeason: false,
HomeTeam: Team{
ID: 1,
Abbreviation: "BOS",
City: "Boston",
Conference: "East",
Division: "Atlantic",
FullName: "Boston Celtics",
Name: "Celtics",
},
VisitorTeam: Team{
ID: 23,
Abbreviation: "PHI",
City: "Philadelphia",
Conference: "East",
Division: "Atlantic",
FullName: "Philadelphia 76ers",
Name: "76ers",
},
},
},
}

func TestNextResponseNoGameTonight(t *testing.T) {
body, _ := json.Marshal(dummy)
q := &http.Response{
Status: "200 OK",
StatusCode: 200,
Proto: "HTTP/1.1",
ProtoMajor: 1,
ProtoMinor: 1,
Body: ioutil.NopCloser(bytes.NewReader(body)),
ContentLength: int64(len(body)),
Header: make(http.Header, 0),
}
actual := NextResponse(q)
if actual != "Sorry, there are not any available games." {
t.Errorf("NextResponse(stub): expected false, actual %s", actual)
}
}

func TestNextResponseGameTonight(t *testing.T) {
dummy.Data[0].Status = "8PM"
body, _ := json.Marshal(dummy)
q := &http.Response{
Status: "200 OK",
Expand All @@ -78,45 +102,13 @@ func TestNextResponse(t *testing.T) {
Header: make(http.Header, 0),
}
actual := NextResponse(q)
if actual != false {
t.Errorf("NextResponse(stub): expected false, actual %t", actual)
gameTime := strings.TrimRight(dummy.Data[0].Time, " ")
if actual != fmt.Sprintf("10,9 8 %s! There is a game currently @ %s %+s\n", config.SixersLogo, dummy.Data[0].Status, gameTime) {
t.Errorf("NextResponse(stub): expected false, actual %s", actual)
}
}

func TestRecordResponse(t *testing.T) {
dummy := Response{
Data: []Game{
{
ID: 1,
Date: "2018-10-16T00:00:00.000Z",
HomeTeamScore: 122,
VisitorTeamScore: 123,
Season: 2020,
Period: 1,
Status: "Final",
Time: " ",
PostSeason: false,
HomeTeam: Team{
ID: 1,
Abbreviation: "BOS",
City: "Boston",
Conference: "East",
Division: "Atlantic",
FullName: "Boston Celtics",
Name: "Celtics",
},
VisitorTeam: Team{
ID: 23,
Abbreviation: "PHI",
City: "Philadelphia",
Conference: "East",
Division: "Atlantic",
FullName: "Philadelphia 76ers",
Name: "76ers",
},
},
},
}
body, _ := json.Marshal(dummy)
q := &http.Response{
Status: "200 OK",
Expand Down
22 changes: 20 additions & 2 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,26 @@ import (
Copyright © 2019 Graham Plata <graham.plata@gmail.com>
*/

// APIURL ...
// API Configuration
////////////////////////////////////////////////////////////

// APIURL is the base endpoint string
const APIURL string = "https://www.balldontlie.io/api/v1/games"

// SixersLogo ...
// Team Configuration
////////////////////////////////////////////////////////////

// TeamID is the sixers team id from the api
const TeamID int = 23

// SixersLogo is a colored string representing the sixers logo
var SixersLogo string = fmt.Sprintf("%d%ders", aurora.Bold(aurora.Red(7)), aurora.Bold(aurora.Blue(6)))

// Date Time Configuration
////////////////////////////////////////////////////////////

// YearFormat year format
const YearFormat = "2006"

// DateFormat year format
const DateFormat = "2006-01-02"
9 changes: 2 additions & 7 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,16 @@ module github.com/grahamplata/sixers
go 1.13

require (
github.com/briandowns/spinner v1.11.1
github.com/fatih/color v1.9.0 // indirect
github.com/fsnotify/fsnotify v1.4.9 // indirect
github.com/logrusorgru/aurora v2.0.3+incompatible
github.com/mattn/go-colorable v0.1.8 // indirect
github.com/mitchellh/go-homedir v1.1.0
github.com/mitchellh/mapstructure v1.3.3 // indirect
github.com/pelletier/go-toml v1.8.1 // indirect
github.com/spf13/afero v1.4.1 // indirect
github.com/spf13/cast v1.3.1 // indirect
github.com/spf13/cobra v1.0.0
github.com/spf13/cobra v1.1.0
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/spf13/viper v1.7.1
golang.org/x/sys v0.0.0-20201009025420-dfb3f7c4e634 // indirect
golang.org/x/sys v0.0.0-20201014080544-cc95f250f6bc // indirect
gopkg.in/ini.v1 v1.62.0 // indirect
gopkg.in/yaml.v2 v2.3.0 // indirect
)
Loading