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) {
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 {

View File

@ -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")
)