Custom Member Parsing

This commit is contained in:
Brian Buller 2021-12-02 11:44:57 -06:00
parent 8174b349bc
commit af7437c238

36
aoc.go
View File

@ -7,7 +7,6 @@ import (
"io/ioutil"
"net/http"
"strconv"
"strings"
"time"
)
@ -46,7 +45,7 @@ func (a *AoC) GetLeaderboard(year int) (*Leaderboard, error) {
return nil, errors.New("Invalid Year")
}
if board, ok := a.boards[year]; ok {
if time.Since(board.LastFetch) < (time.Minute * 10) {
if time.Since(board.LastFetch) < (time.Minute * 15) {
return board, nil
}
}
@ -83,7 +82,7 @@ func (a *AoC) fetchLeaderboard(year int) (*Leaderboard, error) {
return nil, err
}
strBody := string(body)
strBody = strings.ReplaceAll(strBody, "\"last_star_ts\":\"0\"", "\"last_star_ts\":0")
//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")
@ -143,3 +142,34 @@ func (a ByStarsThenScore) Less(i, j int) bool {
}
return a[i].Stars < a[j].Stars
}
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
}
m.ID = v["id"].(string)
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
}