This repository has been archived by the owner on Dec 19, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
183 lines (145 loc) · 4.26 KB
/
main.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package main
import (
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"log"
"net/http"
"time"
)
type Summoner struct {
AccountID string `json:"accountId"`
}
type Matches struct {
Matches []*Match `json:"matches"`
}
type MatchDuration struct {
Duration uint64 `json:"gameDuration"`
}
type Match struct {
GameID int `json:"gameId"`
}
var (
secondRateLimit = NewLimiter(19, 1*time.Second)
twoMinuteRateLimit = NewLimiter(99, 2*time.Minute)
server string
apiKey string
summonerName string
verbose bool
)
func init() {
flag.StringVar(&server, "server", "euw1", "specifies which server region should be queried")
flag.StringVar(&summonerName, "summonername", "", "specifies the summoner which to query for")
flag.StringVar(&apiKey, "apikey", "", "specifies the api key")
flag.BoolVar(&verbose, "verbose", false, "specifies whether additional information should be given out.")
flag.Parse()
}
func main() {
log.Printf("Querying summoner '%s' in server region '%s'\n", summonerName, server)
if apiKey == "" {
log.Fatalln("Please supply an API key by setting the parameter 'apikey'.")
}
if summonerName == "" {
log.Fatalln("Please specify a summoner name by setting the parameter 'summonername'.")
}
summoner, err := getSummoner()
if err != nil {
log.Fatalf("Error retrieving summoner information: %s", err)
}
if verbose {
log.Printf("Successfully received Account ID: %s\n", summoner.AccountID)
}
matches, err := getMatches(summoner)
if err != nil {
log.Fatalf("Error retrieving match history: %s\n", err)
}
if verbose {
log.Printf("Retrieved %d matches\n", len(matches))
}
duration, sumError := sumDurationAsHours(matches)
if sumError != nil {
log.Fatalf("Error summing up game durations: %s", sumError)
}
log.Printf("You have played %d matches, which cost you %d hours of your life.", len(matches), duration)
}
func ratelimitCheck() {
secondRateLimit.Wait()
twoMinuteRateLimit.Wait()
}
func sumDurationAsHours(matches []*Match) (uint64, error) {
var duration uint64
for index, match := range matches {
matchDuration, err := getMatchDuration(match.GameID)
if err != nil {
return 0, err
}
if verbose && index%19 == 0 {
log.Printf("So far %d matches have been retrieved, accounting for %d hours of "+
"playtime.\n%d matches left to query", index+1, duration/60/60, len(matches)-index+1)
}
duration += matchDuration
}
return duration / 60 / 60, nil
}
func getMatchDuration(matchID int) (uint64, error) {
ratelimitCheck()
response, readError := http.Get(fmt.Sprintf("https://%s.api.riotgames.com/lol/match/v4/matches/%d?api_key=%s", server, matchID, apiKey))
if readError != nil {
return 0, readError
}
rawResponse, readError := ioutil.ReadAll(response.Body)
if readError != nil {
return 0, readError
}
matchDuration := &MatchDuration{}
unmarshalError := json.Unmarshal(rawResponse, matchDuration)
if unmarshalError != nil {
return 0, unmarshalError
}
return matchDuration.Duration, nil
}
func getMatches(summoner *Summoner) ([]*Match, error) {
var matches []*Match
nextIndex := 0
for {
ratelimitCheck()
response, readError := http.Get(fmt.Sprintf("https://%s.api.riotgames.com/lol/match/v4/matchlists/by-account/%s?beginIndex=%d&api_key=%s", server, summoner.AccountID, nextIndex, apiKey))
if readError != nil {
return nil, readError
}
rawResponse, readError := ioutil.ReadAll(response.Body)
if readError != nil {
return nil, readError
}
responseMatches := &Matches{}
unmarshalError := json.Unmarshal(rawResponse, responseMatches)
if unmarshalError != nil {
return nil, readError
}
matches = append(matches, responseMatches.Matches...)
if len(responseMatches.Matches) != 100 {
break
} else {
nextIndex += 100
}
}
return matches, nil
}
func getSummoner() (*Summoner, error) {
ratelimitCheck()
response, readError := http.Get(fmt.Sprintf("https://%s.api.riotgames.com/lol/summoner/v4/summoners/by-name/%s?api_key=%s", server, summonerName, apiKey))
if readError != nil {
return nil, readError
}
rawResponse, readError := ioutil.ReadAll(response.Body)
if readError != nil {
return nil, readError
}
summoner := &Summoner{}
unmarshalError := json.Unmarshal(rawResponse, summoner)
if unmarshalError != nil {
return nil, unmarshalError
}
return summoner, nil
}