Now the timestamps are all strings

This commit is contained in:
Brian Buller 2020-12-18 16:36:56 -06:00
parent 495a1b6371
commit cf5d10aaf8
3 changed files with 37 additions and 6 deletions

16
aoc.go
View File

@ -6,6 +6,7 @@ import (
"fmt"
"io/ioutil"
"net/http"
"strconv"
"time"
)
@ -69,6 +70,7 @@ func (a *AoC) fetchLeaderboard(year int) (*Leaderboard, error) {
client := &http.Client{}
boardString := fmt.Sprintf("https://adventofcode.com/%d/leaderboard/private/view/%s.json", year, a.boardId)
fmt.Println(boardString)
req, err = http.NewRequest("GET", boardString, nil)
req.Header.Add("Cookie", "session="+a.session)
@ -85,14 +87,16 @@ func (a *AoC) fetchLeaderboard(year int) (*Leaderboard, error) {
//strBody = strings.ReplaceAll(strBody, "\"last_star_ts\":0", "\"last_star_ts\":\"0\"")
err = json.Unmarshal([]byte(strBody), &leaderboard)
if err != nil {
fmt.Println("Error parsing board")
fmt.Println(err)
return nil, err
}
for k, mbr := range leaderboard.Members {
//starTs, err := strconv.ParseInt(mbr.RawStarTs, 10, 64)
//if err != nil {
// continue
//}
mbr.LastStarTs = time.Unix(mbr.RawStarTs, 0)
starTs, err := strconv.ParseInt(mbr.RawStarTs, 10, 64)
if err != nil {
continue
}
mbr.LastStarTs = time.Unix(starTs, 0)
leaderboard.Members[k] = mbr
}
leaderboard.LastFetch = time.Now()
@ -111,7 +115,7 @@ type Member struct {
ID string `json:"id"`
Name string `json:"name"`
Stars int `json:"stars"`
RawStarTs int64 `json:"last_star_ts"`
RawStarTs string `json:"last_star_ts"`
LocalScore int `json:"local_score"`
GlobalScore int `json:"global_score"`
LastStarTs time.Time

BIN
cmd/cmd Executable file

Binary file not shown.

27
cmd/main.go Normal file
View File

@ -0,0 +1,27 @@
package main
import (
"fmt"
"os"
"time"
aoc "git.bullercodeworks.com/brian/go-adventofcode"
)
func main() {
if len(os.Args) < 3 {
fmt.Println("Usage: cmd <board id> <session id>")
}
a, err := aoc.NewAoC(os.Args[1], os.Args[2])
if err != nil {
panic(err)
}
l, err := a.GetLeaderboard(2020)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
for _, v := range l.Members {
fmt.Println(v.Name, v.LocalScore, v.LastStarTs.Format(time.RFC3339))
}
}