2019-11-21 16:28:14 +00:00
|
|
|
package aoc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2024-10-24 14:03:49 +00:00
|
|
|
"io"
|
2019-11-21 16:28:14 +00:00
|
|
|
"net/http"
|
2020-12-18 22:36:56 +00:00
|
|
|
"strconv"
|
2019-11-21 16:28:14 +00:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
type AoC struct {
|
|
|
|
session string
|
|
|
|
boardId string
|
|
|
|
boards map[int]*Leaderboard
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewAoC(boardId, session string) (*AoC, error) {
|
|
|
|
if boardId == "" {
|
2024-10-24 14:03:49 +00:00
|
|
|
return nil, new(BoardIdRequiredError)
|
2019-11-21 16:28:14 +00:00
|
|
|
}
|
|
|
|
if session == "" {
|
2024-10-24 14:03:49 +00:00
|
|
|
return nil, new(SessionRequiredError)
|
2019-11-21 16:28:14 +00:00
|
|
|
}
|
|
|
|
return &AoC{
|
|
|
|
session: session,
|
|
|
|
boardId: boardId,
|
|
|
|
boards: make(map[int]*Leaderboard),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2024-10-24 14:03:49 +00:00
|
|
|
func (a *AoC) SetCachedLeaderboard(l *Leaderboard) error {
|
|
|
|
yr, err := strconv.Atoi(l.Event)
|
|
|
|
if err != nil {
|
|
|
|
return new(InvalidYearError)
|
|
|
|
}
|
|
|
|
|
|
|
|
a.boards[yr] = l
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-11-21 16:28:14 +00:00
|
|
|
func (a *AoC) GetCachedLeaderboard(year int) (*Leaderboard, error) {
|
|
|
|
if year < 2015 || year > time.Now().Year() {
|
2024-10-24 14:03:49 +00:00
|
|
|
return nil, new(InvalidYearError)
|
2019-11-21 16:28:14 +00:00
|
|
|
}
|
|
|
|
if board, ok := a.boards[year]; ok {
|
|
|
|
return board, nil
|
|
|
|
}
|
2024-10-24 14:03:49 +00:00
|
|
|
return nil, LeaderboardNotCachedError{year: year}
|
2019-11-21 16:28:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (a *AoC) GetLeaderboard(year int) (*Leaderboard, error) {
|
|
|
|
if year < 2015 || year > time.Now().Year() {
|
2024-10-24 14:03:49 +00:00
|
|
|
return nil, new(InvalidYearError)
|
2019-11-21 16:28:14 +00:00
|
|
|
}
|
|
|
|
if board, ok := a.boards[year]; ok {
|
2021-12-02 17:44:57 +00:00
|
|
|
if time.Since(board.LastFetch) < (time.Minute * 15) {
|
2019-11-21 16:28:14 +00:00
|
|
|
return board, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
var err error
|
|
|
|
a.boards[year], err = a.fetchLeaderboard(year)
|
|
|
|
if err != nil {
|
|
|
|
if err.Error() == "invalid character '<' looking for beginning of value" {
|
2024-10-24 14:03:49 +00:00
|
|
|
return nil, new(SessionError)
|
2019-11-21 16:28:14 +00:00
|
|
|
}
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return a.boards[year], nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *AoC) fetchLeaderboard(year int) (*Leaderboard, error) {
|
|
|
|
var err error
|
|
|
|
var req *http.Request
|
|
|
|
var resp *http.Response
|
|
|
|
var body []byte
|
|
|
|
leaderboard := new(Leaderboard)
|
|
|
|
leaderboard.Members = make(map[string]Member)
|
|
|
|
|
|
|
|
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)
|
|
|
|
req.Header.Add("Cookie", "session="+a.session)
|
|
|
|
resp, err = client.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
2024-10-24 14:03:49 +00:00
|
|
|
body, err = io.ReadAll(resp.Body)
|
2019-11-21 16:28:14 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
strBody := string(body)
|
2024-10-24 14:03:49 +00:00
|
|
|
// strBody = strings.ReplaceAll(strBody, "\"last_star_ts\":\"0\"", "\"last_star_ts\":0")
|
2019-11-21 16:28:14 +00:00
|
|
|
err = json.Unmarshal([]byte(strBody), &leaderboard)
|
|
|
|
if err != nil {
|
2024-10-24 14:03:49 +00:00
|
|
|
return nil, fmt.Errorf("error parsing board: %w", err)
|
2019-11-21 16:28:14 +00:00
|
|
|
}
|
|
|
|
for k, mbr := range leaderboard.Members {
|
2020-12-18 22:36:56 +00:00
|
|
|
starTs, err := strconv.ParseInt(mbr.RawStarTs, 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
mbr.LastStarTs = time.Unix(starTs, 0)
|
2019-11-21 16:28:14 +00:00
|
|
|
leaderboard.Members[k] = mbr
|
|
|
|
}
|
|
|
|
leaderboard.LastFetch = time.Now()
|
|
|
|
a.boards[year] = leaderboard
|
|
|
|
return leaderboard, err
|
|
|
|
}
|
|
|
|
|
|
|
|
type Leaderboard struct {
|
2022-12-01 13:02:27 +00:00
|
|
|
OwnerID int `json:"owner_id"`
|
2019-11-21 16:28:14 +00:00
|
|
|
Event string `json:"event"`
|
|
|
|
Members map[string]Member `json:"members"`
|
|
|
|
LastFetch time.Time
|
|
|
|
}
|
|
|
|
|
|
|
|
type Member struct {
|
2022-12-01 13:02:27 +00:00
|
|
|
ID int `json:"id"`
|
2019-11-21 16:28:14 +00:00
|
|
|
Name string `json:"name"`
|
|
|
|
Stars int `json:"stars"`
|
2020-12-18 22:36:56 +00:00
|
|
|
RawStarTs string `json:"last_star_ts"`
|
2019-11-21 16:28:14 +00:00
|
|
|
LocalScore int `json:"local_score"`
|
|
|
|
GlobalScore int `json:"global_score"`
|
|
|
|
LastStarTs time.Time
|
|
|
|
SlackID string
|
|
|
|
}
|
|
|
|
|
|
|
|
type ByLocalScore []Member
|
|
|
|
|
|
|
|
func (a ByLocalScore) Len() int { return len(a) }
|
|
|
|
func (a ByLocalScore) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
|
|
|
|
func (a ByLocalScore) Less(i, j int) bool { return a[i].LocalScore < a[j].LocalScore }
|
|
|
|
|
|
|
|
type ByStars []Member
|
|
|
|
|
|
|
|
func (a ByStars) Len() int { return len(a) }
|
|
|
|
func (a ByStars) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
|
|
|
|
func (a ByStars) Less(i, j int) bool { return a[i].Stars < a[j].Stars }
|
|
|
|
|
|
|
|
type ByStarsThenScore []Member
|
|
|
|
|
|
|
|
func (a ByStarsThenScore) Len() int { return len(a) }
|
|
|
|
func (a ByStarsThenScore) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
|
|
|
|
func (a ByStarsThenScore) Less(i, j int) bool {
|
|
|
|
if a[i].Stars == a[j].Stars {
|
|
|
|
return a[i].LocalScore < a[j].LocalScore
|
|
|
|
}
|
|
|
|
return a[i].Stars < a[j].Stars
|
|
|
|
}
|
2021-12-02 17:44:57 +00:00
|
|
|
|
|
|
|
func (m *Member) UnmarshalJSON(data []byte) error {
|
|
|
|
var v map[string]interface{}
|
|
|
|
var err error
|
|
|
|
|
|
|
|
if err = json.Unmarshal(data, &v); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-12-01 13:02:27 +00:00
|
|
|
m.ID = int(v["id"].(float64))
|
2021-12-02 17:44:57 +00:00
|
|
|
m.Name = v["name"].(string)
|
|
|
|
m.Stars = int(v["stars"].(float64))
|
|
|
|
|
|
|
|
switch v["last_star_ts"].(type) {
|
|
|
|
case float64:
|
|
|
|
m.RawStarTs = fmt.Sprintf("%d", int(v["last_star_ts"].(float64)))
|
|
|
|
case int:
|
|
|
|
m.RawStarTs = strconv.Itoa(v["last_star_ts"].(int))
|
|
|
|
case string:
|
|
|
|
m.RawStarTs = v["last_star_ts"].(string)
|
|
|
|
default:
|
|
|
|
m.RawStarTs = "0"
|
|
|
|
}
|
|
|
|
starTs, err := strconv.ParseInt(m.RawStarTs, 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
starTs = 0
|
|
|
|
}
|
|
|
|
m.LastStarTs = time.Unix(starTs, 0)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|