-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadvent-of-code.go
96 lines (79 loc) · 2.18 KB
/
advent-of-code.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package main
import (
"fmt"
"log"
"net/http"
"net/url"
"sync"
"time"
)
// User Agent used for requests
const userAgent = "github.com/Alextopher/aocbot"
// AdventOfCode is an Advent of Code API client
type AdventOfCode struct {
sync.RWMutex
id string
sessionCookie string
leaderboards map[string]*Leaderboard
lastUpdated time.Time
}
// NewAdventOfCode creates a new Advent of Code API
func NewAdventOfCode(sessionCookie string, id string) *AdventOfCode {
return &AdventOfCode{
sessionCookie: sessionCookie,
leaderboards: make(map[string]*Leaderboard),
id: id,
}
}
// GetLeaderboard gets the most recent leaderboard data from the API
func (aoc *AdventOfCode) GetLeaderboard(year string) *Leaderboard {
if time.Since(aoc.lastUpdated) > 15*time.Minute {
aoc.UpdateLeaderboard(year)
}
aoc.RLock()
leaderboard, ok := aoc.leaderboards[year]
if !ok {
log.Println("Leaderboard not found for year: ", year)
}
aoc.RUnlock()
return leaderboard
}
// UpdateLeaderboard updates a leaderboard by getting the latest data from the API
func (aoc *AdventOfCode) UpdateLeaderboard(year string) error {
requestURL := "https://adventofcode.com/" + year + "/leaderboard/private/view/" + aoc.id + ".json"
fmt.Println(requestURL)
url, err := url.Parse(requestURL)
if err != nil {
log.Println("Error while parsing URL: ", err)
return err
}
request := http.Request{
Method: "GET",
URL: url,
Header: http.Header{
"Cookie": []string{"session=" + aoc.sessionCookie},
"User-Agent": []string{userAgent},
},
}
response, err := http.DefaultClient.Do(&request)
if err != nil {
log.Println("Error while making request: ", err)
return err
}
// Check the content type of the response, if it's not JSON then we can't parse it
contentType := response.Header.Get("Content-Type")
if contentType != "application/json" {
return ErrInvalidSession
}
leaderboard, err := ParseLeaderboard(response.Body)
if err != nil {
log.Println("Error while parsing response: ", err)
return err
}
aoc.Lock()
aoc.leaderboards[year] = leaderboard
aoc.lastUpdated = time.Now()
aoc.Unlock()
log.Printf("Updated leaderboard for (%s, %s)\n", aoc.id, year)
return nil
}