-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
154 lines (124 loc) · 3.46 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
package steamapi
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"strings"
"time"
"appengine"
"appengine/urlfetch"
)
//SteamAccount object containing an array of all returned Steam Accounts
type SteamAccount struct {
Players []SteamAccountDetails
}
//SteamAccountDetails gives the details of the SteamAccount
type SteamAccountDetails struct {
SteamID string
CommunityBanned bool
VACBanned bool
NumberOfVACBans int
DaysSinceLastBan int
NumberOfGameBans int
EconomyBan string
LastUpdated time.Time
Updated bool
}
func init() {
http.HandleFunc("/", root)
}
func root(w http.ResponseWriter, r *http.Request) {
steamIdsString := r.FormValue("steamids")
steamIDArray := strings.Split(steamIdsString, ",")
//do we have steam keys?
if len(steamIDArray) == 0 {
http.Error(w, "No Steam IDs present", http.StatusBadRequest)
return
}
//has the api key been set up correctly?
key, err := readAPIKey()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
//Google specific
c := appengine.NewContext(r)
//check if we already have some of there entries in store and is recent
//uncachedIDs, obtainedRecords, err := callMemcache(&c, steamIDArray)
foundAccounts, missingIDs, err1 := callMemcache(&c, steamIDArray)
if err1 != nil {
http.Error(w, err1.Error(), http.StatusInternalServerError)
return
}
//Steam API only allows 100 steamIds to be sent, group them up into a Map
groupedSteamIDArray := groupSteamIDs(missingIDs)
results, err := makeSteamAPICall(&c, groupedSteamIDArray, &key)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
results = append(results, foundAccounts...)
//save entries if they have been updated
if err := SaveAllToStore(c, results); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
//going to assume there's no issue with return size - hope for gzip over the wire...
marshalled, err := json.Marshal(results)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
fmt.Fprint(w, string(marshalled))
}
func callMemcache(c *appengine.Context, idList []string) ([]SteamAccountDetails, []string, error) {
return RetrieveMultiFromStore(c, idList)
}
func groupSteamIDs(idList []string) map[int][]string {
i := 0
j := 0
groupList := make(map[int][]string)
for i < len(idList) {
var arr []string
if i+100 > len(idList) {
arr = idList[i:len(idList)]
} else {
arr = idList[i : i+100]
}
groupList[j] = arr
i += 100
j++
}
return groupList
}
func makeSteamAPICall(c *appengine.Context, groupedSteamIDs map[int][]string, key *string) ([]SteamAccountDetails, error) {
mainEndpoint := "https://api.steampowered.com/ISteamUser/GetPlayerBans/v1/?key=" + *key + "&steamids="
var steamAccounts []SteamAccountDetails
for _, v := range groupedSteamIDs {
endpoint := mainEndpoint + strings.Join(v, ",")
client := urlfetch.Client(*c)
resp, err := client.Get(endpoint)
if err != nil {
return nil, err
}
var m SteamAccount
result, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
if err := json.Unmarshal(result, &m); err != nil {
return nil, err
}
steamAccounts = append(steamAccounts, m.Players...)
}
return steamAccounts, nil
}
func readAPIKey() (string, error) {
f, err := ioutil.ReadFile("key.txt")
if err != nil {
return "", errors.New("API Key file not found")
}
return string(f), nil
}