Add support for live games.
This commit is contained in:
@ -1,16 +1,14 @@
|
|||||||
package clients
|
package cfbd_client
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"discord-cfb-bot/config"
|
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
"time"
|
"time"
|
||||||
|
"discord-cfb-bot/config"
|
||||||
)
|
)
|
||||||
|
|
||||||
const seasonStartDate = "2024-08-28"
|
|
||||||
|
|
||||||
type Game struct {
|
type Game struct {
|
||||||
HomeTeam string `json:"home_team"`
|
HomeTeam string `json:"home_team"`
|
||||||
AwayTeam string `json:"away_team"`
|
AwayTeam string `json:"away_team"`
|
||||||
@ -19,19 +17,59 @@ type Game struct {
|
|||||||
StartDateTime string `json:"start_date"`
|
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 {
|
func getCurrentWeek() int {
|
||||||
|
seasonStartDate := "2024-08-28" // Replace with actual season start date
|
||||||
startDate, _ := time.Parse("2006-01-02", seasonStartDate)
|
startDate, _ := time.Parse("2006-01-02", seasonStartDate)
|
||||||
weeks := int(time.Since(startDate).Hours() / (24 * 7))
|
weeks := int(time.Since(startDate).Hours() / (24 * 7))
|
||||||
return weeks + 1
|
return weeks + 1
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetGameInfo(teamName string) string {
|
func getLiveGame(teamName string) string {
|
||||||
|
url := "https://api.collegefootballdata.com/scoreboard"
|
||||||
currentWeek := getCurrentWeek()
|
currentWeek := getCurrentWeek()
|
||||||
|
params := fmt.Sprintf("?year=%d&week=%d", time.Now().Year(), currentWeek)
|
||||||
encodedTeamName := url.QueryEscape(teamName)
|
|
||||||
url := fmt.Sprintf("https://api.collegefootballdata.com/games?year=%d&week=%d&team=%s", time.Now().Year(), currentWeek, encodedTeamName)
|
|
||||||
|
|
||||||
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)
|
req.Header.Set("Authorization", "Bearer "+config.CFBDAPIKey)
|
||||||
|
|
||||||
client := &http.Client{}
|
client := &http.Client{}
|
||||||
@ -50,12 +88,15 @@ func GetGameInfo(teamName string) string {
|
|||||||
|
|
||||||
for _, game := range games {
|
for _, game := range games {
|
||||||
startTime, _ := time.Parse(time.RFC3339, game.StartDateTime)
|
startTime, _ := time.Parse(time.RFC3339, game.StartDateTime)
|
||||||
eastern, err := time.LoadLocation("America/New_York")
|
|
||||||
if err != nil {
|
// Load Eastern Time location
|
||||||
fmt.Println("Error loading Eastern timezone")
|
eastern, err := time.LoadLocation("America/New_York")
|
||||||
return "Error fetching game time"
|
if err != nil {
|
||||||
}
|
fmt.Println("Error loading Eastern timezone:", err)
|
||||||
startTimeEastern := startTime.In(eastern)
|
return "Error fetching game time."
|
||||||
|
}
|
||||||
|
startTimeEastern := startTime.In(eastern)
|
||||||
|
|
||||||
if time.Now().After(startTime) && game.HomePoints != nil && game.AwayPoints != nil {
|
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)
|
return fmt.Sprintf("%s: %d %s: %d", game.AwayTeam, *game.AwayPoints, game.HomeTeam, *game.HomePoints)
|
||||||
} else if time.Now().Before(startTime) {
|
} 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."
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user