getaoc/getaoc.go

105 lines
2.3 KiB
Go
Raw Normal View History

2019-12-01 03:58:20 +00:00
package getaoc
import (
2019-12-01 04:19:23 +00:00
"encoding/json"
2019-12-01 03:58:20 +00:00
"fmt"
"io/ioutil"
"net/http"
)
const base = "https://adventofcode.com"
2019-12-01 04:19:23 +00:00
func GetInput(session string, year, day int) (string, error) {
2019-12-01 03:58:20 +00:00
url := fmt.Sprintf("%s/%d/day/%d/input", base, year, day)
r, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
2019-12-01 04:19:23 +00:00
return "", err
2019-12-01 03:58:20 +00:00
}
r.AddCookie(&http.Cookie{
2023-12-02 05:28:42 +00:00
Name: "session",
Value: session,
Path: "",
Domain: ".adventofcode.com",
Secure: false,
HttpOnly: false,
SameSite: 0,
2019-12-01 03:58:20 +00:00
})
c := http.Client{}
resp, err := c.Do(r)
if err != nil {
2019-12-01 04:19:23 +00:00
return "", err
2019-12-01 03:58:20 +00:00
}
defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body)
2019-12-01 04:19:23 +00:00
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{
2023-12-02 05:28:42 +00:00
Name: "session",
Value: session,
Path: "",
Domain: ".adventofcode.com",
Secure: false,
HttpOnly: false,
SameSite: 0,
2019-12-01 04:19:23 +00:00
})
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 {
2023-12-02 05:28:42 +00:00
GlobalScore int `json:"global_score"`
Stars int `json:"stars"`
LocalScore int `json:"local_score"`
LastStarTs int `json:"last_star_ts"`
ID int `json:"id"`
CompletionDayLevel struct {
Num1 struct {
Num1 struct {
GetStarTs int `json:"get_star_ts"`
StarIndex int `json:"star_index"`
} `json:"1"`
Num2 struct {
GetStarTs int `json:"get_star_ts"`
StarIndex int `json:"star_index"`
} `json:"2"`
} `json:"1"`
Num2 struct {
Num1 struct {
GetStarTs int `json:"get_star_ts"`
StarIndex int `json:"star_index"`
} `json:"1"`
Num2 struct {
GetStarTs int `json:"get_star_ts"`
StarIndex int `json:"star_index"`
} `json:"2"`
} `json:"2"`
} `json:"completion_day_level"`
Name string `json:"name"`
2019-12-01 04:19:23 +00:00
}
type LeaderBoard struct {
2023-12-02 05:28:42 +00:00
Event string `json:"event"`
OwnerID int `json:"owner_id"`
2019-12-01 04:19:23 +00:00
Members map[string]Member `json:"members"`
2019-12-01 03:58:20 +00:00
}