getaoc/getaoc.go

82 lines
1.7 KiB
Go

package getaoc
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
)
const base = "https://adventofcode.com"
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 {
return "", 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 "", err
}
defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.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"`
LocalScore int `json:"local_score"`
}
type LeaderBoard struct {
Event string `json:"event"`
OwnerID string `json:"owner_id"`
Members map[string]Member `json:"members"`
}