2024-11-10 21:18:37 -05:00

111 lines
3.6 KiB
Go

package cbb
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"strings"
"time"
"unicode"
)
type Game struct {
Game struct {
GameID string `json:"gameID"`
StartDate string `json:"startDate"`
StartTime string `json:"startTime"`
GameState string `json:"gameState"`
CurrentPeriod string `json:"currentPeriod"`
ContestClock string `json:"contestClock"`
Home Team `json:"home"`
Away Team `json:"away"`
FinalMessage string `json:"finalMessage"`
} `json:"game"`
}
type Team struct {
Score string `json:"score"`
Names struct {
Short string `json:"short"`
Full string `json:"full"`
} `json:"names"`
}
func GetGameInfo(teamName, date string) string {
// If no date is provided, use today's date
if date == "" {
date = time.Now().Format("2006/01/02") // Format as YYYY/MM/DD
} else {
// Check if the last 5 characters are in MM/DD format for date input
if len(date) == 5 && unicode.IsDigit(rune(date[0])) && unicode.IsDigit(rune(date[1])) && date[2] == '/' &&
unicode.IsDigit(rune(date[3])) && unicode.IsDigit(rune(date[4])) {
date = fmt.Sprintf("2024/%s", strings.ReplaceAll(date, "/", "/")) // Add the year and reformat
} else {
return "Invalid date format. Please use MM/DD."
}
}
apiURL := fmt.Sprintf("https://ncaa.ewnix.net/scoreboard/basketball-men/d1/%s", date)
return fetchAndParseGames(apiURL, teamName)
}
func fetchAndParseGames(apiURL, teamName string) string {
teamNameLower := strings.ToLower(teamName)
resp, err := http.Get(apiURL)
if err != nil {
return fmt.Sprintf("Failed to reach the scoreboard API: %v", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return fmt.Sprintf("Failed to reach the scoreboard API. Status code: %d", resp.StatusCode)
}
body, _ := ioutil.ReadAll(resp.Body)
var apiResponse struct {
Games []Game `json:"games"`
}
if err := json.Unmarshal(body, &apiResponse); err != nil {
return "Error parsing the API response."
}
var results []string
for _, game := range apiResponse.Games {
if strings.ToLower(game.Game.Home.Names.Short) == teamNameLower ||
strings.ToLower(game.Game.Away.Names.Short) == teamNameLower {
var gameInfo string
if game.Game.GameState == "live" {
currentPeriod := game.Game.CurrentPeriod
if currentPeriod == "" {
currentPeriod = "HALFTIME"
}
gameInfo = fmt.Sprintf("Live: %s: **%s** %s: **%s** | Half: %s | Time Remaining: %s",
game.Game.Away.Names.Short, game.Game.Away.Score,
game.Game.Home.Names.Short, game.Game.Home.Score,
currentPeriod, game.Game.ContestClock)
} else if game.Game.GameState == "pre" {
gameInfo = fmt.Sprintf("Upcoming: %s @ %s on %s at %s ET",
game.Game.Away.Names.Short, game.Game.Home.Names.Short, game.Game.StartDate, game.Game.StartTime)
} else if game.Game.GameState == "final" {
gameInfo = fmt.Sprintf("Final: %s: **%s** %s: **%s**",
game.Game.Away.Names.Short, game.Game.Away.Score,
game.Game.Home.Names.Short, game.Game.Home.Score)
}
results = append(results, gameInfo)
}
}
if len(results) > 0 {
return strings.Join(results, "\n")
}
return "No game found for the specified team."
}