Compare commits

..

No commits in common. "main" and "v1.2.0" have entirely different histories.
main ... v1.2.0

2 changed files with 40 additions and 37 deletions

35
aoc.go
View File

@ -13,16 +13,14 @@ type AoC struct {
session string
boardId string
boards map[int]*Leaderboard
sessionErr bool
}
func NewAoC(boardId, session string) (*AoC, error) {
if boardId == "" {
return nil, BoardIdRequiredError
return nil, new(BoardIdRequiredError)
}
if session == "" {
return nil, SessionRequiredError
return nil, new(SessionRequiredError)
}
return &AoC{
session: session,
@ -31,17 +29,10 @@ 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
}
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 InvalidYearError
return new(InvalidYearError)
}
a.boards[yr] = l
@ -50,20 +41,17 @@ func (a *AoC) SetCachedLeaderboard(l *Leaderboard) error {
func (a *AoC) GetCachedLeaderboard(year int) (*Leaderboard, error) {
if year < 2015 || year > time.Now().Year() {
return nil, InvalidYearError
return nil, new(InvalidYearError)
}
if board, ok := a.boards[year]; ok {
return board, nil
}
return nil, LeaderboardNotCachedError
return nil, LeaderboardNotCachedError{year: year}
}
func (a *AoC) GetLeaderboard(year int) (*Leaderboard, error) {
if year < 2015 || year > time.Now().Year() {
return nil, InvalidYearError
}
if a.sessionErr {
return nil, SessionError
return nil, new(InvalidYearError)
}
if board, ok := a.boards[year]; ok {
if time.Since(board.LastFetch) < (time.Minute * 15) {
@ -74,8 +62,7 @@ func (a *AoC) GetLeaderboard(year int) (*Leaderboard, error) {
a.boards[year], err = a.fetchLeaderboard(year)
if err != nil {
if err.Error() == "invalid character '<' looking for beginning of value" {
a.sessionErr = true
return nil, SessionError
return nil, new(SessionError)
}
return nil, err
}
@ -93,9 +80,6 @@ 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 {
@ -107,12 +91,9 @@ 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,13 +1,35 @@
package aoc
import (
"errors"
)
import "fmt"
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")
)
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)
}