Skip to content

Commit

Permalink
add full support for using config for vip
Browse files Browse the repository at this point in the history
  • Loading branch information
matt1484 committed Sep 17, 2019
1 parent 3179304 commit 3c95ff0
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 12 deletions.
14 changes: 9 additions & 5 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ func (client *HttpClient) PostJson(url string, data interface{}) (*HttpResponse,
type Bl3Client struct {
HttpClient
Config Bl3Config
SessionId string
}

func NewBl3Client() (*Bl3Client, error) {
Expand All @@ -139,12 +140,14 @@ func NewBl3Client() (*Bl3Client, error) {
config := Bl3Config{}
configJson.Out(&config)

client.SetDefaultHeader("Origin", "https://borderlands.com")
client.SetDefaultHeader("Referer", "https://borderlands.com/en-US/vip/")
for header, value := range config.RequestHeaders {
client.SetDefaultHeader(header, value)
}

return &Bl3Client {
HttpClient: *client,
Config: config,
SessionId: "",
}, nil
}

Expand All @@ -154,7 +157,7 @@ func (client *Bl3Client) Login(username string, password string) error {
"password": password,
}

loginRes, err := client.PostJson("https://api.2k.com/borderlands/users/authenticate", data)
loginRes, err := client.PostJson(client.Config.LoginUrl, data)
if err != nil {
return errors.New("Failed to submit login credentials")
}
Expand All @@ -164,15 +167,16 @@ func (client *Bl3Client) Login(username string, password string) error {
return errors.New("Failed to login")
}

if loginRes.Header.Get("X-CT-REDIRECT") == "" {
if loginRes.Header.Get(client.Config.LoginRedirectHeader) == "" {
return errors.New("Failed to start session")
}

sessionRes, err := client.Get(loginRes.Header.Get("X-CT-REDIRECT"))
sessionRes, err := client.Get(loginRes.Header.Get(client.Config.LoginRedirectHeader))
if err != nil {
return errors.New("Failed to get session")
}
defer sessionRes.Body.Close()

client.SessionId = loginRes.Header.Get(client.Config.SessionIdHeader)
return nil
}
17 changes: 10 additions & 7 deletions vip.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package bl3_auto_vip

import (
// "fmt"
"errors"
"strconv"
"strings"
Expand Down Expand Up @@ -79,7 +78,7 @@ func (client *Bl3Client) GetFullVipCodeMap() (VipCodeMap, error) {
return codeMap, err
}

response, err := httpClient.Get("https://www.reddit.com/r/borderlands3/comments/bxgq5p/borderlands_vip_program_codes/")
response, err := httpClient.Get(client.Config.Vip.CodeListUrl)
if err != nil {
return codeMap, errors.New("Failed to get code list")
}
Expand All @@ -89,22 +88,26 @@ func (client *Bl3Client) GetFullVipCodeMap() (VipCodeMap, error) {
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 {
redditHtml.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 == 2 && strings.Contains(strings.ToLower(col.Text()), "no") {
if i == client.Config.Vip.CodeListCheckIndex &&
strings.Contains(strings.ToLower(col.Text()), client.Config.Vip.CodeListInvalidRegex) {
return false
}
if i == 0 {
if i == client.Config.Vip.CodeListCodeIndex {
code = strings.ToLower(col.Text())
}
if i == 3 {
if i == client.Config.Vip.CodeListTypeIndex {
codeTypes = strings.ToLower(col.Text())
return false
}
Expand Down

0 comments on commit 3c95ff0

Please sign in to comment.