From 64c7e6bebfcb7e6b3d21643c8d454a6df8586373 Mon Sep 17 00:00:00 2001 From: Brian Buller Date: Sun, 1 Dec 2024 08:24:34 -0600 Subject: [PATCH] Updates --- aoc.go | 25 ++++++++++++++++--------- aoc_errors.go | 42 ++++++++++-------------------------------- 2 files changed, 26 insertions(+), 41 deletions(-) diff --git a/aoc.go b/aoc.go index 8a1bee6..106d3c8 100644 --- a/aoc.go +++ b/aoc.go @@ -19,10 +19,10 @@ type AoC struct { func NewAoC(boardId, session string) (*AoC, error) { if boardId == "" { - return nil, new(BoardIdRequiredError) + return nil, BoardIdRequiredError } if session == "" { - return nil, new(SessionRequiredError) + return nil, SessionRequiredError } return &AoC{ session: session, @@ -31,6 +31,7 @@ func NewAoC(boardId, session string) (*AoC, error) { }, nil } +func (a *AoC) GetSession() string { return a.session } func (a *AoC) SetSession(session string) { a.session = session a.sessionErr = false @@ -40,7 +41,7 @@ func (a *AoC) NeedsNewSession() bool { return a.sessionErr } func (a *AoC) SetCachedLeaderboard(l *Leaderboard) error { yr, err := strconv.Atoi(l.Event) if err != nil { - return new(InvalidYearError) + return InvalidYearError } a.boards[yr] = l @@ -49,20 +50,20 @@ func (a *AoC) SetCachedLeaderboard(l *Leaderboard) error { func (a *AoC) GetCachedLeaderboard(year int) (*Leaderboard, error) { if year < 2015 || year > time.Now().Year() { - return nil, new(InvalidYearError) + return nil, InvalidYearError } if board, ok := a.boards[year]; ok { return board, nil } - return nil, LeaderboardNotCachedError{year: year} + return nil, LeaderboardNotCachedError } func (a *AoC) GetLeaderboard(year int) (*Leaderboard, error) { if year < 2015 || year > time.Now().Year() { - return nil, new(InvalidYearError) + return nil, InvalidYearError } if a.sessionErr { - return nil, new(SessionError) + return nil, SessionError } if board, ok := a.boards[year]; ok { if time.Since(board.LastFetch) < (time.Minute * 15) { @@ -74,7 +75,7 @@ func (a *AoC) GetLeaderboard(year int) (*Leaderboard, error) { if err != nil { if err.Error() == "invalid character '<' looking for beginning of value" { a.sessionErr = true - return nil, new(SessionError) + return nil, SessionError } return nil, err } @@ -92,6 +93,9 @@ func (a *AoC) fetchLeaderboard(year int) (*Leaderboard, error) { client := &http.Client{} boardString := fmt.Sprintf("https://adventofcode.com/%d/leaderboard/private/view/%s.json", year, a.boardId) req, err = http.NewRequest("GET", boardString, nil) + if err != nil { + return nil, err + } req.Header.Add("Cookie", "session="+a.session) resp, err = client.Do(req) if err != nil { @@ -103,9 +107,12 @@ func (a *AoC) fetchLeaderboard(year int) (*Leaderboard, error) { return nil, err } strBody := string(body) - // strBody = strings.ReplaceAll(strBody, "\"last_star_ts\":\"0\"", "\"last_star_ts\":0") err = json.Unmarshal([]byte(strBody), &leaderboard) if err != nil { + if err.Error() == "invalid character '<' looking for beginning of value" { + a.sessionErr = true + return nil, SessionError + } return nil, fmt.Errorf("error parsing board: %w", err) } for k, mbr := range leaderboard.Members { diff --git a/aoc_errors.go b/aoc_errors.go index b9cbd6c..c748b10 100644 --- a/aoc_errors.go +++ b/aoc_errors.go @@ -1,35 +1,13 @@ package aoc -import "fmt" +import ( + "errors" +) -type SessionRequiredError struct{} - -func (e SessionRequiredError) Error() string { - return "session key is required" -} - -type SessionError struct{} - -func (e SessionError) Error() string { - return "invalid session" -} - -type BoardIdRequiredError struct{} - -func (e BoardIdRequiredError) Error() string { - return "board id is required" -} - -type InvalidYearError struct{} - -func (e InvalidYearError) Error() string { - return "invalid year" -} - -type LeaderboardNotCachedError struct { - year int -} - -func (e LeaderboardNotCachedError) Error() string { - return fmt.Sprintf("leaderboard (%d) is not cached", e.year) -} +var ( + SessionRequiredError = errors.New("session key is required") + SessionError = errors.New("invalid session") + BoardIdRequiredError = errors.New("board id is required") + InvalidYearError = errors.New("invalid year") + LeaderboardNotCachedError = errors.New("leaderboard is not cached") +)