Add support for live games.

This commit is contained in:
Kevin Thompson 2024-11-08 22:50:18 -06:00
parent ae61dc7b28
commit b5f343e19c

View File

@ -1,16 +1,14 @@
package clients
package cfbd_client
import (
"discord-cfb-bot/config"
"encoding/json"
"fmt"
"net/http"
"net/url"
"time"
"discord-cfb-bot/config"
)
const seasonStartDate = "2024-08-28"
type Game struct {
HomeTeam string `json:"home_team"`
AwayTeam string `json:"away_team"`
@ -19,19 +17,59 @@ type Game struct {
StartDateTime string `json:"start_date"`
}
type ScoreboardGame struct {
HomeTeam string `json:"home_team"`
AwayTeam string `json:"away_team"`
HomePoints *int `json:"home_points"`
AwayPoints *int `json:"away_points"`
StartDateTime string `json:"start_date"`
Status string `json:"status"`
}
func getCurrentWeek() int {
seasonStartDate := "2024-08-28" // Replace with actual season start date
startDate, _ := time.Parse("2006-01-02", seasonStartDate)
weeks := int(time.Since(startDate).Hours() / (24 * 7))
return weeks + 1
}
func GetGameInfo(teamName string) string {
func getLiveGame(teamName string) string {
url := "https://api.collegefootballdata.com/scoreboard"
currentWeek := getCurrentWeek()
encodedTeamName := url.QueryEscape(teamName)
url := fmt.Sprintf("https://api.collegefootballdata.com/games?year=%d&week=%d&team=%s", time.Now().Year(), currentWeek, encodedTeamName)
params := fmt.Sprintf("?year=%d&week=%d", time.Now().Year(), currentWeek)
req, _ := http.NewRequest("GET", url, nil)
req, _ := http.NewRequest("GET", url+params, nil)
req.Header.Set("Authorization", "Bearer "+config.CFBDAPIKey)
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
fmt.Println("Error fetching data from scoreboard API:", err)
return "Error fetching live game data."
}
defer resp.Body.Close()
var games []ScoreboardGame
if err := json.NewDecoder(resp.Body).Decode(&games); err != nil {
fmt.Println("Error decoding scoreboard response:", err)
return "Error parsing live game data."
}
for _, game := range games {
if game.Status == "in_progress" && (game.HomeTeam == teamName || game.AwayTeam == teamName) {
return fmt.Sprintf("Live: %s: %d %s: %d", game.AwayTeam, *game.AwayPoints, game.HomeTeam, *game.HomePoints)
}
}
return "No live games found for the team."
}
func GetGameInfo(teamName string) string {
apiUrl := "https://api.collegefootballdata.com/games"
currentWeek := getCurrentWeek()
queryParams := fmt.Sprintf("?year=%d&week=%d&team=%s", time.Now().Year(), currentWeek, url.QueryEscape(teamName))
req, _ := http.NewRequest("GET", apiUrl+queryParams, nil)
req.Header.Set("Authorization", "Bearer "+config.CFBDAPIKey)
client := &http.Client{}
@ -50,12 +88,15 @@ func GetGameInfo(teamName string) string {
for _, game := range games {
startTime, _ := time.Parse(time.RFC3339, game.StartDateTime)
eastern, err := time.LoadLocation("America/New_York")
if err != nil {
fmt.Println("Error loading Eastern timezone")
return "Error fetching game time"
}
startTimeEastern := startTime.In(eastern)
// Load Eastern Time location
eastern, err := time.LoadLocation("America/New_York")
if err != nil {
fmt.Println("Error loading Eastern timezone:", err)
return "Error fetching game time."
}
startTimeEastern := startTime.In(eastern)
if time.Now().After(startTime) && game.HomePoints != nil && game.AwayPoints != nil {
return fmt.Sprintf("%s: %d %s: %d", game.AwayTeam, *game.AwayPoints, game.HomeTeam, *game.HomePoints)
} else if time.Now().Before(startTime) {
@ -63,5 +104,12 @@ func GetGameInfo(teamName string) string {
}
}
return "No recent or upcoming game data found."
// Call the getLiveGame function if no specific game is found
liveGameResult := getLiveGame(teamName)
if liveGameResult != "No live games found for the team." {
return liveGameResult
}
return "No recent, upcoming, or live games found for the team."
}