From 693b1f2edc991358756c54c912c18725a7615065 Mon Sep 17 00:00:00 2001 From: Scott Carpenter Date: Wed, 20 Nov 2019 13:40:04 -0700 Subject: [PATCH] Use https://shift.orcicorn.com as the source for active SHIFT codes --- config.json | 7 +------ shift.go | 48 ++++++++++++++++-------------------------------- 2 files changed, 17 insertions(+), 38 deletions(-) diff --git a/config.json b/config.json index 0c5f5a3..03c585f 100644 --- a/config.json +++ b/config.json @@ -23,12 +23,7 @@ } }, "shiftConfig": { - "codeListUrl": "http://orcz.com/Borderlands_3:_Shift_Codes", - "codeListRowSelector": ".wikitable tbody tr", - "codeListInvalidRegex": "", - "codeListCheckIndex": 3, - "codeListCodeIndex": 4, - + "codeListUrl": "https://shift.orcicorn.com/tags/borderlands3/index.json", "codeInfoUrl": "https://api.2k.com/borderlands/code/", "userInfoUrl": "https://api.2k.com/borderlands/users/me", "gameCodename": "oak" diff --git a/shift.go b/shift.go index 2647131..51d6656 100644 --- a/shift.go +++ b/shift.go @@ -4,16 +4,10 @@ import ( "errors" "strings" "time" - - "github.com/PuerkitoBio/goquery" ) type ShiftConfig struct { CodeListUrl string `json:"codeListUrl"` - CodeListRowSelector string `json:"codeListRowSelector"` - CodeListInvalidRegex string `json:"codeListInvalidRegex"` - CodeListCheckIndex int `json:"codeListCheckIndex"` - CodeListCodeIndex int `json:"codeListCodeIndex"` CodeInfoUrl string `json:"codeInfoUrl"` UserInfoUrl string `json:"userInfoUrl"` GameCodename string `json:"gameCodename"` @@ -41,6 +35,11 @@ type shiftCode struct { Active bool `json:"is_active"` } +type shiftCodeFromList struct { + Code string `json:"code"` + Platform string `json:"platform"` +} + func (client *Bl3Client) GetCodePlatforms(code string) ([]string, bool) { platforms := make([]string, 0) @@ -154,39 +153,24 @@ func (client *Bl3Client) GetFullShiftCodeList() (ShiftCodeMap, error) { return codeMap, err } - response, err := httpClient.Get(client.Config.Shift.CodeListUrl) + res, err := httpClient.Get(client.Config.Shift.CodeListUrl) if err != nil { - return codeMap, errors.New("Failed to get code list") + return codeMap, errors.New("Failed to get SHIFT code list") } - codeHtml, err := response.BodyAsHtmlDoc() + json, err := res.BodyAsJson() if err != nil { - return codeMap, err + return codeMap, errors.New("Failed to get SHIFT code list body as JSON") } - codeHtml.Find(client.Config.Shift.CodeListRowSelector).Each(func(i int, row *goquery.Selection) { - numColumns := len(row.Find("td").Nodes) - if numColumns < client.Config.Shift.CodeListCheckIndex || - numColumns < client.Config.Shift.CodeListCodeIndex { - return + codes := make([]shiftCodeFromList, 0) + json.From("[0].codes").Select("code", "platform").Out(&codes) + for _, code := range codes { + platforms, valid := client.GetCodePlatforms(code.Code) + if valid { + codeMap[code.Code] = platforms } + } - row.Find("td").EachWithBreak(func(i int, col *goquery.Selection) bool { - // not supported yet - // if i == client.Config.Shift.CodeListCheckIndex && - // strings.Contains(strings.ToLower(col.Text()), client.Config.Shift.CodeListInvalidRegex) { - // return false - // } - if i == client.Config.Shift.CodeListCodeIndex { - code := strings.TrimSpace(strings.ToUpper(col.Text())) - platforms, valid := client.GetCodePlatforms(code) - if valid { - codeMap[code] = platforms - } - } - return true - }) - - }) return codeMap, nil } \ No newline at end of file