133 lines
4.5 KiB
Go

package cfb
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"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"`
}
// Convert time from Eastern to Central
func convertToCentralTime(easternTime string) (string, error) {
parsedTime, err := time.Parse("03:04PM ET", easternTime)
if err != nil {
return "", fmt.Errorf("error parsing time: %w", err)
}
centralLocation, err := time.LoadLocation("America/Chicago")
if err != nil {
return "", fmt.Errorf("error loading Central Time location: %w", err)
}
centralTime := parsedTime.In(centralLocation)
return centralTime.Format("03:04 PM"), nil
}
func GetGameInfo(teamName string) string {
teamName = strings.TrimSpace(teamName)
week := ""
// Check if the last two characters of the input are integers for the week
if len(teamName) >= 2 && unicode.IsDigit(rune(teamName[len(teamName)-1])) && unicode.IsDigit(rune(teamName[len(teamName)-2])) {
week = teamName[len(teamName)-2:]
teamName = strings.TrimSpace(teamName[:len(teamName)-2])
log.Printf("Week detected: %s", week)
}
// Build the API URL based on whether a week is provided
apiURL := "https://ncaa.ewnix.net/scoreboard/football/fbs"
if week != "" {
apiURL = fmt.Sprintf("https://ncaa.ewnix.net/scoreboard/football/fbs/2024/%s/all-conf", week)
}
log.Printf("Fetching data from API: %s", apiURL)
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
teamNameLower := strings.ToLower(teamName)
for _, game := range apiResponse.Games {
homeTeamLowerShort := strings.ToLower(game.Game.Home.Names.Short)
awayTeamLowerShort := strings.ToLower(game.Game.Away.Names.Short)
// Check if the team matches the short name only
if homeTeamLowerShort == teamNameLower || awayTeamLowerShort == teamNameLower {
var gameInfo string
if game.Game.GameState == "live" {
period := game.Game.CurrentPeriod
if period == "" {
period = "HALFTIME"
}
gameInfo = fmt.Sprintf("%s: **%s** %s: **%s** | Quarter: %s | Time Remaining: %s",
game.Game.Away.Names.Short, game.Game.Away.Score,
game.Game.Home.Names.Short, game.Game.Home.Score,
period, game.Game.ContestClock)
} else if game.Game.GameState == "pre" {
centralTime, err := convertToCentralTime(game.Game.StartTime)
if err != nil {
log.Printf("Time conversion error: %v", err)
centralTime = game.Game.StartTime // Fall back to the original if conversion fails
}
gameInfo = fmt.Sprintf("Upcoming: %s @ %s on %s at %s CT",
game.Game.Away.Names.Short, game.Game.Home.Names.Short, game.Game.StartDate, centralTime)
} 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."
}