forked from matt1484/bl3_auto_vip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvip.go
276 lines (229 loc) · 6.44 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
package bl3_auto_vip
import (
"errors"
"strconv"
"strings"
"github.com/PuerkitoBio/goquery"
"github.com/thedevsaddam/gojsonq"
)
type VipCodeTypeMap map[string]string
type VipCodeMap map[string]StringSet
func (v VipCodeMap) Diff(other VipCodeMap) VipCodeMap {
diff := NewVipCodeMap()
for codeType, codes := range v {
for code := range codes {
if _, found := other[codeType][code]; !found {
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)
}
}
func GetCodeTypes() []string {
return []string{
"pax",
"boost",
"email",
"creator",
"vault",
"diamond",
}
}
func GetCodeTypesInString(s string) []string {
l := strings.ToLower(s)
types := make([]string, 0)
for _, t := range GetCodeTypes() {
if strings.Contains(l, t) {
types = append(types, t)
}
}
return types
}
func NewVipCodeTypeMap() VipCodeTypeMap {
codeTypeMap := make(map[string]string)
for _, codeType := range GetCodeTypes() {
codeTypeMap[codeType] = ""
}
return codeTypeMap
}
func NewVipCodeMap() VipCodeMap {
codeTypeMap := make(map[string]StringSet)
for _, codeType := range GetCodeTypes() {
codeTypeMap[codeType] = StringSet{}
}
return codeTypeMap
}
type Bl3VipClient struct {
HttpClient
}
func NewBl3VipClient() (*Bl3VipClient, error) {
client, err := NewHttpClient()
if err != nil {
return nil, errors.New("Failed to start client")
}
client.SetDefaultHeader("Origin", "https://borderlands.com")
client.SetDefaultHeader("Referer", "https://borderlands.com/en-US/vip/")
return &Bl3VipClient{
*client,
}, nil
}
func (client *Bl3VipClient) Login(username string, password string) error {
data := map[string]string{
"username": username,
"password": password,
}
loginRes, err := client.PostJson("https://api.2k.com/borderlands/users/authenticate", data)
if err != nil {
return errors.New("Failed to submit login credentials")
}
defer loginRes.Body.Close()
if loginRes.StatusCode != 200 {
return errors.New("Failed to login")
}
if loginRes.Header.Get("X-CT-REDIRECT") == "" {
return errors.New("Failed to start session")
}
sessionRes, err := client.Get(loginRes.Header.Get("X-CT-REDIRECT"))
if err != nil {
return errors.New("Failed to get session")
}
defer sessionRes.Body.Close()
return nil
}
func (client *Bl3VipClient) GetFullCodeMap() (VipCodeMap, error) {
codeMap := NewVipCodeMap()
httpClient, err := NewHttpClient()
if err != nil {
return codeMap, err
}
response, err := httpClient.Get("https://www.reddit.com/r/borderlands3/comments/bxgq5p/borderlands_vip_program_codes/")
if err != nil {
return codeMap, errors.New("Failed to get code list")
}
redditHtml, err := response.BodyAsHtmlDoc()
if err != nil {
return codeMap, err
}
redditHtml.Find("[data-test-id='post-content'] tbody tr").Each(func(i int, row *goquery.Selection) {
if len(row.Find("td").Nodes) < 4 {
return
}
codeTypes := ""
code := ""
row.Find("td").EachWithBreak(func(i int, col *goquery.Selection) bool {
if i == 2 && strings.Contains(strings.ToLower(col.Text()), "no") {
return false
}
if i == 0 {
code = strings.ToLower(col.Text())
}
if i == 3 {
codeTypes = strings.ToLower(col.Text())
return false
}
return true
})
for _, codeType := range GetCodeTypesInString(codeTypes) {
codeMap[codeType].Add(code)
}
})
return codeMap, nil
}
func (client *Bl3VipClient) GetRedeemedCodeMap() (VipCodeMap, error) {
codeMap := 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 GetCodeTypesInString(act.CodeTypes) {
codeMap.Add(codeType, act.Code)
}
}
return codeMap, nil
}
func (client *Bl3VipClient) getWidgetConf(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 *Bl3VipClient) GetCodeTypeUrlMap() (VipCodeTypeMap, error) {
codeTypeUrlMap := NewVipCodeTypeMap()
widgetConf := client.getWidgetConf("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 GetCodeTypesInString(wid.WidgetName) {
codeTypeUrlMap[codeType] = "https://2kgames.crowdtwist.com/widgets/t/code-redemption/" + strconv.Itoa(wid.WidgetId)
}
}
return codeTypeUrlMap, nil
}
func (client *Bl3VipClient) GetCodeRedemptionUrl(url string) (string, error) {
widgetConf := client.getWidgetConf(url)
if widgetConf == nil {
return "", errors.New("Failed to get code redemption url")
}
campaignId, ok := widgetConf.Find("campaignId").(float64)
if !ok {
return "", errors.New("Failed to get code redemption id")
}
return "https://2kgames.crowdtwist.com/code-redemption-campaign/redeem?cid=" + strconv.Itoa(int(campaignId)), nil
}