Session Stuff

This commit is contained in:
Brian Buller 2024-11-04 16:44:20 -06:00
parent 2c48d9b961
commit 6c7742cdf2

12
aoc.go
View File

@ -13,6 +13,8 @@ type AoC struct {
session string
boardId string
boards map[int]*Leaderboard
sessionErr bool
}
func NewAoC(boardId, session string) (*AoC, error) {
@ -29,6 +31,12 @@ func NewAoC(boardId, session string) (*AoC, error) {
}, nil
}
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 {
@ -53,6 +61,9 @@ func (a *AoC) GetLeaderboard(year int) (*Leaderboard, error) {
if year < 2015 || year > time.Now().Year() {
return nil, new(InvalidYearError)
}
if a.sessionErr {
return nil, new(SessionError)
}
if board, ok := a.boards[year]; ok {
if time.Since(board.LastFetch) < (time.Minute * 15) {
return board, nil
@ -62,6 +73,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, new(SessionError)
}
return nil, err