From dc696067b57c99d85733a0ed609abd9f750da619 Mon Sep 17 00:00:00 2001 From: Chris Sexton Date: Sat, 30 Nov 2019 23:19:23 -0500 Subject: [PATCH] add leaderboard --- cmd/getaoc/main.go | 6 +++- cmd/getleaderboard/main.go | 41 ++++++++++++++++++++++++++++ getaoc.go | 56 ++++++++++++++++++++++++++++++++++---- go.sum | 0 4 files changed, 97 insertions(+), 6 deletions(-) create mode 100644 cmd/getleaderboard/main.go create mode 100644 go.sum diff --git a/cmd/getaoc/main.go b/cmd/getaoc/main.go index aebede4..ac5fe68 100644 --- a/cmd/getaoc/main.go +++ b/cmd/getaoc/main.go @@ -3,6 +3,7 @@ package main import ( "flag" "fmt" + "log" "os" "strconv" @@ -33,7 +34,10 @@ func main() { sess = *sessId } - body := getaoc.GetInput(sess, *year, day) + body, err := getaoc.GetInput(sess, *year, day) + if err != nil { + log.Fatal(err) + } if *file == "-" { fmt.Println(body) diff --git a/cmd/getleaderboard/main.go b/cmd/getleaderboard/main.go new file mode 100644 index 0000000..1db941a --- /dev/null +++ b/cmd/getleaderboard/main.go @@ -0,0 +1,41 @@ +package main + +import ( + "flag" + "fmt" + "log" + "os" + + "code.chrissexton.org/cws/getaoc" +) + +var year = flag.Int("year", 2019, "event year") +var sessId = flag.String("session", "", "session id") +var boardId = flag.Int("id", 0, "board id") + +const base = "https://adventofcode.com" + +func main() { + flag.Parse() + if *boardId == 0 { + fmt.Fprintln(os.Stderr, "Error: Must provide a leaderboard id.") + return + } + + sess, ok := os.LookupEnv("AOC_SESSION") + if !ok && *sessId == "" { + fmt.Fprintln(os.Stderr, "Error: Must provide a session id.") + return + } + + if *sessId != "" { + sess = *sessId + } + + body, err := getaoc.GetLeaderboard(sess, *year, *boardId) + if err != nil { + log.Fatal(err) + } + + fmt.Println(body) +} diff --git a/getaoc.go b/getaoc.go index ebb5e36..e5c0ce1 100644 --- a/getaoc.go +++ b/getaoc.go @@ -1,20 +1,20 @@ package getaoc import ( + "encoding/json" "fmt" "io/ioutil" - "log" "net/http" ) const base = "https://adventofcode.com" -func GetInput(session string, year, day int) string { +func GetInput(session string, year, day int) (string, error) { url := fmt.Sprintf("%s/%d/day/%d/input", base, year, day) r, err := http.NewRequest(http.MethodGet, url, nil) if err != nil { - panic(err) + return "", err } r.AddCookie(&http.Cookie{ Name: "session", @@ -28,9 +28,55 @@ func GetInput(session string, year, day int) string { c := http.Client{} resp, err := c.Do(r) if err != nil { - log.Fatal(err) + return "", err } defer resp.Body.Close() body, _ := ioutil.ReadAll(resp.Body) - return string(body) + return string(body), nil +} + +func GetLeaderboard(session string, year, id int) (LeaderBoard, error) { + url := fmt.Sprintf("%s/%d/leaderboard/private/view/%d.json", base, year, id) + board := LeaderBoard{} + + r, err := http.NewRequest(http.MethodGet, url, nil) + if err != nil { + return board, err + } + r.AddCookie(&http.Cookie{ + Name: "session", + Value: session, + Path: "", + Domain: ".adventofcode.com", + Secure: false, + HttpOnly: false, + SameSite: 0, + }) + c := http.Client{} + resp, err := c.Do(r) + if err != nil { + return board, err + } + defer resp.Body.Close() + dec := json.NewDecoder(resp.Body) + err = dec.Decode(&board) + if err != nil { + return board, err + } + return board, nil +} + +type Member struct { + ID string `json:"id"` + Stars int `json:"stars"` + GlobalScore int `json:"global_score"` + Name string `json:"name"` + LastStarTs int `json:"last_star_ts"` + LocalScore int `json:"local_score"` +} + +type LeaderBoard struct { + Event string `json:"event"` + OwnerID string `json:"owner_id"` + Members map[string]Member `json:"members"` } diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..e69de29