This repository has been archived by the owner on Mar 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaddon.go
159 lines (140 loc) · 4.15 KB
/
addon.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
package cav2
import (
"encoding/json"
"fmt"
"io/ioutil"
"strconv"
)
const (
//APIEndpoint URL of API
APIEndpoint = "https://addons-ecs.forgesvc.net/api/v2/"
)
var (
//EMPTY Empty req data
EMPTY []byte
)
//GetAddon gets Addon with addonID
func GetAddon(addonID string) (*Addon, error) {
response, err := GetHTTPResponse("GET", getAPI()+"addon/"+addonID, EMPTY)
if err != nil {
return nil, err
}
var addonResponse *Addon
err = json.NewDecoder(response.Body).Decode(&addonResponse)
if err != nil {
return nil, err
}
return addonResponse, nil
}
//GetAddons Gets addons with []AddonID
func GetAddons(addons []int) ([]*Addon, error) {
jsonPayload, _ := json.Marshal(addons)
response, err := GetHTTPResponse("POST", getAPI()+"addon", jsonPayload)
if err != nil {
return nil, err
}
var addonResponse []*Addon
err = json.NewDecoder(response.Body).Decode(&addonResponse)
if err != nil {
return nil, err
}
return addonResponse, nil
}
//GetAddonFile Gets an addons file with AddonID and fileId
func GetAddonFile(addon int, fileID int) (*File, error) {
response, err := GetHTTPResponse("GET", getAPI()+"addon/"+strconv.Itoa(addon)+"/file/"+strconv.Itoa(fileID), EMPTY)
if err != nil {
return nil, err
}
var addonResponse *File
err = json.NewDecoder(response.Body).Decode(&addonResponse)
if err != nil {
return nil, err
}
return addonResponse, nil
}
//GetAddonFiles Gets files for addonID
func GetAddonFiles(addonID string) ([]*File, error) {
response, err := GetHTTPResponse("GET", getAPI()+"addon/"+addonID+"/files", EMPTY)
if err != nil {
return nil, err
}
var addonResponse []*File
err = json.NewDecoder(response.Body).Decode(&addonResponse)
if err != nil {
return nil, err
}
return addonResponse, nil
}
//GetHashMatches Gets addon Fingerprints
func GetHashMatches(addons []int) (*FingerprintList, error) {
jsonPayload, _ := json.Marshal(addons)
response, err := GetHTTPResponse("POST", APIEndpoint+"fingerprint", jsonPayload)
if err != nil {
return nil, err
}
var addonResponse *FingerprintList
err = json.NewDecoder(response.Body).Decode(&addonResponse)
if err != nil {
return nil, err
}
return addonResponse, nil
}
//GetAddonDatabaseTimestamp Gets last update of addon DB
func GetAddonDatabaseTimestamp() (string, error) {
response, err := GetHTTPResponse("GET", getAPI()+"addon/timestamp", EMPTY)
if err != nil {
return "", err
}
bodyBytes, _ := ioutil.ReadAll(response.Body)
bodyString := string(bodyBytes)
return bodyString, nil
}
//Search searches for addons with query
func Search(query string) ([]*Addon, error) {
//TODO expand this to allow more things to be searched for
//"api/addon/search?gameId={0}§ionId={1}&categoryId={2}&gameVersion={3}&index={4}&pageSize={5}&searchFilter={6}&sort={7}&sortDescending={8}"
seachPayload := "?gameId=432§ionId=-1&categoryId=-1&index=0&pageSize=1000&sort=TotalDownloads&sortDescending=true&searchFilter=" + query
response, err := GetHTTPResponse("GET", getAPI()+"addon/search"+seachPayload, EMPTY)
if err != nil {
return nil, err
}
var addonResponse []*Addon
err = json.NewDecoder(response.Body).Decode(&addonResponse)
if err != nil {
return nil, err
}
return addonResponse, nil
}
//GetAllAddons Uses the search to find as many addons as possible, not perfect but should get most of them.
//This is a very expensive call, takes a while and uses a lot of api requests
func GetAllAddons() ([]*Addon, error) {
allAddons := make([]*Addon, 0)
lastFind := 1000
index := 0
for lastFind == 1000 {
if index > 100 {
fmt.Println("Breaking out index too big")
break
}
seachPayload := "?gameId=432§ionId=6&categoryId=0&index=" + strconv.Itoa(len(allAddons)) + "&searchFilter=&pageSize=1000&sort=5"
response, err := GetHTTPResponse("GET", APIEndpoint+"addon/search"+seachPayload, EMPTY)
if err != nil {
fmt.Println(err.Error())
return nil, err
}
var addonResponse []*Addon
err = json.NewDecoder(response.Body).Decode(&addonResponse)
if err != nil {
fmt.Println(err.Error())
return nil, err
}
lastFind = len(addonResponse)
index++
allAddons = append(allAddons, addonResponse...)
}
return allAddons, nil
}
func getAPI() string {
return APIEndpoint
}