package cfb import ( "encoding/json" "fmt" "io/ioutil" "log" "net/http" "strconv" "strings" "time" "unicode" "discord-scores-bot/config" ) type Game struct { Game struct { GameID string `json:"gameID"` StartDate string `json:"startDate"` StartTime string `json:"startTime"` StartTimeEpoch string `json:"startTimeEpoch"` 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 string) string { teamName = strings.TrimSpace(strings.ToLower(teamName)) week := "" // Check if the teamName matches a custom name and replace if found if customName, exists := config.CustomTeamNames[teamName]; exists { log.Printf("Custom team name detected: %s -> %s", teamName, customName) teamName = customName } // 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 := convertEpochToCentralTime(game.Game.StartTimeEpoch) if err != nil { log.Printf("Time conversion error: %v", err) centralTime = game.Game.StartTime // Fall back to original time 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." } func convertEpochToCentralTime(epochStr string) (string, error) { epoch, err := strconv.ParseInt(epochStr, 10, 64) if err != nil { return "", fmt.Errorf("error parsing epoch: %w", err) } // Convert the epoch time to time.Time and set it to Central Time t := time.Unix(epoch, 0).UTC() locCT, err := time.LoadLocation("America/Chicago") if err != nil { return "", fmt.Errorf("error loading Central Time location: %w", err) } centralTime := t.In(locCT) return centralTime.Format("03:04 PM"), nil }