-
Notifications
You must be signed in to change notification settings - Fork 19
/
vip.go
319 lines (274 loc) · 8.48 KB
/
vip.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
package bl3_auto_vip
import (
"errors"
"strconv"
"strings"
"github.com/PuerkitoBio/goquery"
"github.com/thedevsaddam/gojsonq"
)
type VipCodeMap map[string]StringSet
func (v VipCodeMap) Diff(other VipCodeMap) VipCodeMap {
diff := VipCodeMap{}
for codeType, codes := range v {
for code := range codes {
if _, found := other[codeType][code]; !found {
if _, found := diff[codeType]; !found {
diff[codeType] = StringSet{}
}
diff[codeType].Add(code)
}
}
}
return diff
}
func (v VipCodeMap) Add(codeType, code string) {
codeType = strings.ToLower(codeType)
code = strings.ToLower(code)
if _, found := v[codeType]; found {
v[codeType].Add(code)
}
}
type VipActivity struct {
Title string `json:"title"`
Link string `json:"link_href"`
}
type VipConfig struct {
CodeListUrl string `json:"codeListUrl"`
CodeListRowSelector string `json:"codeListRowSelector"`
CodeListInvalidRegex string `json:"codeListInvalidRegex"`
CodeListCheckIndex int `json:"codeListCheckIndex"`
CodeListCodeIndex int `json:"codeListCodeIndex"`
CodeListTypeIndex int `json:"codeListTypeIndex"`
CodeTypeUrlMap map[string]string `json:"codeTypeUrlMap"`
}
func (conf *VipConfig) GetCodeTypes() []string {
codeTypes := make([]string, 0)
for codeType, _ := range conf.CodeTypeUrlMap {
codeTypes = append(codeTypes, codeType)
}
return codeTypes
}
func (conf *VipConfig) DetectCodeTypes(s string) []string {
l := strings.ToLower(s)
types := make([]string, 0)
for codeType, _ := range conf.CodeTypeUrlMap {
if strings.Contains(l, codeType) {
types = append(types, codeType)
}
}
return types
}
func (conf *Bl3Config) NewVipCodeMap() VipCodeMap {
codeTypeMap := make(map[string]StringSet)
for codeType, _ := range conf.Vip.CodeTypeUrlMap {
codeTypeMap[codeType] = StringSet{}
}
return codeTypeMap
}
func (client *Bl3Client) GetFullVipCodeMap() (VipCodeMap, error) {
codeMap := client.Config.NewVipCodeMap()
httpClient, err := NewHttpClient()
if err != nil {
return codeMap, err
}
response, err := httpClient.Get(client.Config.Vip.CodeListUrl)
if err != nil {
return codeMap, errors.New("Failed to get code list")
}
codeHtml, err := response.BodyAsHtmlDoc()
if err != nil {
return codeMap, err
}
codeHtml.Find(client.Config.Vip.CodeListRowSelector).Each(func(i int, row *goquery.Selection) {
numColumns := len(row.Find("td").Nodes)
if numColumns < client.Config.Vip.CodeListCheckIndex ||
numColumns < client.Config.Vip.CodeListCodeIndex ||
numColumns < client.Config.Vip.CodeListTypeIndex {
return
}
codeTypes := ""
code := ""
row.Find("td").EachWithBreak(func(i int, col *goquery.Selection) bool {
if i == client.Config.Vip.CodeListCheckIndex &&
strings.Contains(strings.ToLower(col.Text()), client.Config.Vip.CodeListInvalidRegex) {
return false
}
if i == client.Config.Vip.CodeListCodeIndex {
code = strings.TrimSpace(strings.ToLower(col.Text()))
}
if i == client.Config.Vip.CodeListTypeIndex {
codeTypes = strings.ToLower(col.Text())
return false
}
return true
})
for _, codeType := range client.Config.Vip.DetectCodeTypes(codeTypes) {
codeMap[codeType].Add(code)
}
})
return codeMap, nil
}
func (client *Bl3Client) GetRedeemedVipCodeMap() (VipCodeMap, error) {
codeMap := client.Config.NewVipCodeMap()
url := "https://2kgames.crowdtwist.com/request?widgetId=9470"
data := map[string]interface{}{
"model_data": map[string]interface{}{
"activity": map[string]interface{}{
"newest_activities": map[string]interface{}{
"properties": []string{"notes", "title"},
"query": map[string]interface{}{
"type": "user_activities_me",
"args": map[string]int{
"row_start": 1,
"row_end": 1000000,
},
},
},
},
},
}
res, err := client.PostJson(url, data)
if err != nil {
return codeMap, errors.New("Failed to get redeemed code list")
}
type activity struct {
CodeTypes string `json:"title"`
Code string `json:"notes"`
}
activities := make([]activity, 0)
resJson, err := res.BodyAsJson()
if err != nil {
return codeMap, err
}
resJson.From("model_data.activity.newest_activities").Out(&activities)
for _, act := range activities {
for _, codeType := range client.Config.Vip.DetectCodeTypes(act.CodeTypes) {
codeMap.Add(codeType, act.Code)
}
}
return codeMap, nil
}
func (client *Bl3Client) getVipWidgetConf(url string) *gojsonq.JSONQ {
response, err := client.Get(url)
if err != nil {
return nil
}
widgetHtml, err := response.BodyAsHtmlDoc()
if err != nil {
return nil
}
script := ""
widgetHtml.Find("script").EachWithBreak(func(i int, scriptTag *goquery.Selection) bool {
if strings.Contains(scriptTag.Text(), "widgetConf") {
script = scriptTag.Text()
return false
}
return true
})
script = strings.TrimSpace(strings.Split(strings.Join(strings.Split(strings.Join(strings.Split(script, "widgetConf")[1:], ""), "=")[1:], ""), ";")[0])
json := JsonFromString(script)
return json
}
func (client *Bl3Client) GenerateVipCodeUrlMap() (map[string]string, error) {
codeTypeUrlMap := make(map[string]string)
widgetConf := client.getVipWidgetConf("https://2kgames.crowdtwist.com/widgets/t/activity-list/9904/?__locale__=en#2")
if widgetConf == nil {
return codeTypeUrlMap, errors.New("Failed to get code redemption types")
}
type widget struct {
WidgetId int `json:"widgetId"`
WidgetName string `json:"widgetName"`
}
widgets := make([]widget, 0)
widgetConf.From("entries").Select("link.widgetId", "link.widgetName").Out(&widgets)
for _, wid := range widgets {
for _, codeType := range client.Config.Vip.DetectCodeTypes(wid.WidgetName) {
widgetConf := client.getVipWidgetConf("https://2kgames.crowdtwist.com/widgets/t/code-redemption/" + strconv.Itoa(wid.WidgetId))
if widgetConf == nil {
codeTypeUrlMap[codeType] = ""
continue
}
campaignId, ok := widgetConf.Find("campaignId").(float64)
if !ok {
codeTypeUrlMap[codeType] = ""
continue
}
codeTypeUrlMap[codeType] = "https://2kgames.crowdtwist.com/code-redemption-campaign/redeem?cid=" + strconv.Itoa(int(campaignId))
}
}
return codeTypeUrlMap, nil
}
func (client *Bl3Client) GetVipActivities() ([]VipActivity, error) {
activities := make([]VipActivity, 0)
widgetConf := client.getVipWidgetConf("https://2kgames.crowdtwist.com/widgets/t/activity-list/9446?__locale__=en")
if widgetConf == nil {
return activities, errors.New("failed to get activity names")
}
type activity struct {
Name string `json:"name"`
}
activityNames := make([]activity, 0)
widgetConf.From("entries").Select("activity.name").Out(&activityNames)
names := make([]string, len(activityNames))
for i, activity := range activityNames {
names[i] = activity.Name
}
url := "https://2kgames.crowdtwist.com/request?widgetId=9446"
data := map[string]interface{}{
"model_data": map[string]interface{}{
"activity": map[string]interface{}{
"activities": map[string]interface{}{
"properties": []string{"title", "link_href", "user_activity_status"},
"query": map[string]interface{}{
"type": "activities_by_name",
"args": map[string]interface{}{
"names": names,
},
},
},
},
},
}
response, err := client.PostJson(url, data)
if err != nil {
return activities, errors.New("failed to get activities")
}
responseJson, err := response.BodyAsJson()
if err != nil {
return activities, errors.New("failed to get activities")
}
responseJson.From("model_data.activity.activities").Where("user_activity_status.has_reached_freq_cap", "=", false).Select("title", "link_href").Out(&activities)
return activities, nil
}
func (client *Bl3Client) RedeemVipActivity(activity VipActivity) bool {
response, err := client.Get(activity.Link)
if err != nil {
return false
}
defer response.Body.Close()
return true
}
func (client *Bl3Client) RedeemVipCode(codeType, code string) (string, bool) {
res, err := client.PostJson(client.Config.Vip.CodeTypeUrlMap[codeType], map[string]string {
"code": code,
})
if err != nil {
return "bad request", false
}
resJson, err := res.BodyAsJson()
if err != nil {
return "invalid response", false
}
exception := ""
resJson.From("exception.model").Out(&exception)
success := ""
resJson.Reset().From("message").Out(&success)
if exception != "" {
// technically the code may be valid but just unredeemable by this account (limits/already redeemed)
return exception, !strings.Contains(strings.ToLower(exception), "invalid")
}
if success != "" {
return success, true
}
return "wrong response format", false
}