Compare commits
No commits in common. "main" and "v1.2.0" have entirely different histories.
35
aoc.go
35
aoc.go
@ -13,16 +13,14 @@ type AoC struct {
|
|||||||
session string
|
session string
|
||||||
boardId string
|
boardId string
|
||||||
boards map[int]*Leaderboard
|
boards map[int]*Leaderboard
|
||||||
|
|
||||||
sessionErr bool
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewAoC(boardId, session string) (*AoC, error) {
|
func NewAoC(boardId, session string) (*AoC, error) {
|
||||||
if boardId == "" {
|
if boardId == "" {
|
||||||
return nil, BoardIdRequiredError
|
return nil, new(BoardIdRequiredError)
|
||||||
}
|
}
|
||||||
if session == "" {
|
if session == "" {
|
||||||
return nil, SessionRequiredError
|
return nil, new(SessionRequiredError)
|
||||||
}
|
}
|
||||||
return &AoC{
|
return &AoC{
|
||||||
session: session,
|
session: session,
|
||||||
@ -31,17 +29,10 @@ func NewAoC(boardId, session string) (*AoC, error) {
|
|||||||
}, nil
|
}, 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 {
|
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 InvalidYearError
|
return new(InvalidYearError)
|
||||||
}
|
}
|
||||||
|
|
||||||
a.boards[yr] = l
|
a.boards[yr] = l
|
||||||
@ -50,20 +41,17 @@ 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, InvalidYearError
|
return nil, new(InvalidYearError)
|
||||||
}
|
}
|
||||||
if board, ok := a.boards[year]; ok {
|
if board, ok := a.boards[year]; ok {
|
||||||
return board, nil
|
return board, nil
|
||||||
}
|
}
|
||||||
return nil, LeaderboardNotCachedError
|
return nil, LeaderboardNotCachedError{year: year}
|
||||||
}
|
}
|
||||||
|
|
||||||
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, InvalidYearError
|
return nil, new(InvalidYearError)
|
||||||
}
|
|
||||||
if a.sessionErr {
|
|
||||||
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,8 +62,7 @@ func (a *AoC) GetLeaderboard(year int) (*Leaderboard, error) {
|
|||||||
a.boards[year], err = a.fetchLeaderboard(year)
|
a.boards[year], err = a.fetchLeaderboard(year)
|
||||||
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
|
return nil, new(SessionError)
|
||||||
return nil, SessionError
|
|
||||||
}
|
}
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@ -93,9 +80,6 @@ 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 {
|
||||||
@ -107,12 +91,9 @@ 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 {
|
||||||
|
@ -1,13 +1,35 @@
|
|||||||
package aoc
|
package aoc
|
||||||
|
|
||||||
import (
|
import "fmt"
|
||||||
"errors"
|
|
||||||
)
|
|
||||||
|
|
||||||
var (
|
type SessionRequiredError struct{}
|
||||||
SessionRequiredError = errors.New("session key is required")
|
|
||||||
SessionError = errors.New("invalid session")
|
func (e SessionRequiredError) Error() string {
|
||||||
BoardIdRequiredError = errors.New("board id is required")
|
return "session key 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)
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user