This commit is contained in:
Brian Buller 2024-12-01 08:24:34 -06:00
parent 6c7742cdf2
commit 64c7e6bebf
2 changed files with 26 additions and 41 deletions

25
aoc.go
View File

@ -19,10 +19,10 @@ type AoC struct {
func NewAoC(boardId, session string) (*AoC, error) { func NewAoC(boardId, session string) (*AoC, error) {
if boardId == "" { if boardId == "" {
return nil, new(BoardIdRequiredError) return nil, BoardIdRequiredError
} }
if session == "" { if session == "" {
return nil, new(SessionRequiredError) return nil, SessionRequiredError
} }
return &AoC{ return &AoC{
session: session, session: session,
@ -31,6 +31,7 @@ func NewAoC(boardId, session string) (*AoC, error) {
}, nil }, nil
} }
func (a *AoC) GetSession() string { return a.session }
func (a *AoC) SetSession(session string) { func (a *AoC) SetSession(session string) {
a.session = session a.session = session
a.sessionErr = false a.sessionErr = false
@ -40,7 +41,7 @@ func (a *AoC) NeedsNewSession() bool { return a.sessionErr }
func (a *AoC) SetCachedLeaderboard(l *Leaderboard) error { func (a *AoC) SetCachedLeaderboard(l *Leaderboard) error {
yr, err := strconv.Atoi(l.Event) yr, err := strconv.Atoi(l.Event)
if err != nil { if err != nil {
return new(InvalidYearError) return InvalidYearError
} }
a.boards[yr] = l a.boards[yr] = l
@ -49,20 +50,20 @@ func (a *AoC) SetCachedLeaderboard(l *Leaderboard) error {
func (a *AoC) GetCachedLeaderboard(year int) (*Leaderboard, error) { func (a *AoC) GetCachedLeaderboard(year int) (*Leaderboard, error) {
if year < 2015 || year > time.Now().Year() { if year < 2015 || year > time.Now().Year() {
return nil, new(InvalidYearError) return nil, InvalidYearError
} }
if board, ok := a.boards[year]; ok { if board, ok := a.boards[year]; ok {
return board, nil return board, nil
} }
return nil, LeaderboardNotCachedError{year: year} return nil, LeaderboardNotCachedError
} }
func (a *AoC) GetLeaderboard(year int) (*Leaderboard, error) { func (a *AoC) GetLeaderboard(year int) (*Leaderboard, error) {
if year < 2015 || year > time.Now().Year() { if year < 2015 || year > time.Now().Year() {
return nil, new(InvalidYearError) return nil, InvalidYearError
} }
if a.sessionErr { if a.sessionErr {
return nil, new(SessionError) return nil, SessionError
} }
if board, ok := a.boards[year]; ok { if board, ok := a.boards[year]; ok {
if time.Since(board.LastFetch) < (time.Minute * 15) { 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 != nil {
if err.Error() == "invalid character '<' looking for beginning of value" { if err.Error() == "invalid character '<' looking for beginning of value" {
a.sessionErr = true a.sessionErr = true
return nil, new(SessionError) return nil, SessionError
} }
return nil, err return nil, err
} }
@ -92,6 +93,9 @@ func (a *AoC) fetchLeaderboard(year int) (*Leaderboard, error) {
client := &http.Client{} client := &http.Client{}
boardString := fmt.Sprintf("https://adventofcode.com/%d/leaderboard/private/view/%s.json", year, a.boardId) boardString := fmt.Sprintf("https://adventofcode.com/%d/leaderboard/private/view/%s.json", year, a.boardId)
req, err = http.NewRequest("GET", boardString, nil) req, err = http.NewRequest("GET", boardString, nil)
if err != nil {
return nil, err
}
req.Header.Add("Cookie", "session="+a.session) req.Header.Add("Cookie", "session="+a.session)
resp, err = client.Do(req) resp, err = client.Do(req)
if err != nil { if err != nil {
@ -103,9 +107,12 @@ func (a *AoC) fetchLeaderboard(year int) (*Leaderboard, error) {
return nil, err return nil, err
} }
strBody := string(body) strBody := string(body)
// strBody = strings.ReplaceAll(strBody, "\"last_star_ts\":\"0\"", "\"last_star_ts\":0")
err = json.Unmarshal([]byte(strBody), &leaderboard) err = json.Unmarshal([]byte(strBody), &leaderboard)
if err != nil { 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) return nil, fmt.Errorf("error parsing board: %w", err)
} }
for k, mbr := range leaderboard.Members { for k, mbr := range leaderboard.Members {

View File

@ -1,35 +1,13 @@
package aoc package aoc
import "fmt" import (
"errors"
)
type SessionRequiredError struct{} var (
SessionRequiredError = errors.New("session key is required")
func (e SessionRequiredError) Error() string { SessionError = errors.New("invalid session")
return "session key is required" BoardIdRequiredError = errors.New("board id is required")
} InvalidYearError = errors.New("invalid year")
LeaderboardNotCachedError = errors.New("leaderboard is not cached")
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)
}