Skip to content

Commit

Permalink
extract upgrade token
Browse files Browse the repository at this point in the history
  • Loading branch information
alaingilbert committed Aug 22, 2022
1 parent 779ee4b commit 2599fb5
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 8 deletions.
17 changes: 15 additions & 2 deletions pkg/extractor/extractor.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ type ShipyardExtractorBytes interface {
ExtractOverviewShipSumCountdownFromBytes(pageHTML []byte) int64
ExtractProduction(pageHTML []byte) ([]ogame.Quantifiable, int64, error)
ExtractShips(pageHTML []byte) (ogame.ShipsInfos, error)
ExtractUpgradeToken(pageHTML []byte) (string, error)
}

type ShipyardExtractorDoc interface {
Expand All @@ -150,6 +151,7 @@ type ShipyardExtractorBytesDoc interface {

type ResearchExtractorBytes interface {
ExtractResearch(pageHTML []byte) ogame.Researches
ExtractUpgradeToken(pageHTML []byte) (string, error)
}

type ResearchExtractorDoc interface {
Expand Down Expand Up @@ -224,6 +226,7 @@ type PreferencesExtractorBytesDoc interface {

type DefensesExtractorBytes interface {
ExtractDefense(pageHTML []byte) (ogame.DefensesInfos, error)
ExtractUpgradeToken(pageHTML []byte) (string, error)
}

type DefensesExtractorDoc interface {
Expand Down Expand Up @@ -405,10 +408,19 @@ type MessagesMarketplaceExtractorBytes interface {
ExtractMarketplaceMessages(pageHTML []byte) ([]ogame.MarketplaceMessage, int64, error)
}

type LfBuildingsExtractorBytes interface {
ExtractUpgradeToken(pageHTML []byte) (string, error)
}

type LfBuildingsExtractorBytesDoc interface {
LfBuildingsExtractorBytes
}

// ResourcesBuildingsExtractorBytes supplies page
type ResourcesBuildingsExtractorBytes interface {
ExtractResourcesBuildings(pageHTML []byte) (ogame.ResourcesBuildings, error)
ExtractTearDownToken(pageHTML []byte) (string, error)
ExtractUpgradeToken(pageHTML []byte) (string, error)
}

type ResourcesBuildingsExtractorDoc interface {
Expand Down Expand Up @@ -451,19 +463,20 @@ type Extractor interface {
GetLifeformEnabled() bool
SetLifeformEnabled(lifeformEnabled bool)

FullPageExtractorBytesDoc
OverviewExtractorBytesDoc
DefensesExtractorBytesDoc
EspionageReportExtractorBytesDoc
EventListExtractorBytesDoc
FacilitiesExtractorBytesDoc
FleetDispatchExtractorBytesDoc
FullPageExtractorBytesDoc
HighscoreExtractorBytesDoc
LfBuildingsExtractorBytesDoc
MessagesCombatReportExtractorBytesDoc
MessagesEspionageReportExtractorBytesDoc
MessagesExpeditionExtractorBytesDoc
MissileAttackLayerExtractorBytesDoc
MovementExtractorBytesDoc
OverviewExtractorBytesDoc
PreferencesExtractorBytesDoc
ResearchExtractorBytesDoc
ResourcesBuildingsExtractorBytesDoc
Expand Down
5 changes: 5 additions & 0 deletions pkg/extractor/v6/extractor.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,11 @@ func (e *Extractor) ExtractTearDownButtonEnabled(pageHTML []byte) bool {
return e.ExtractTearDownButtonEnabledFromDoc(doc)
}

// ExtractUpgradeToken ...
func (e *Extractor) ExtractUpgradeToken(pageHTML []byte) (string, error) {
return extractUpgradeToken(pageHTML)
}

// ExtractLifeformEnabled ...
func (e *Extractor) ExtractLifeformEnabled(pageHTML []byte) bool {
return bytes.Contains(pageHTML, []byte(`lifeformEnabled":true`))
Expand Down
9 changes: 9 additions & 0 deletions pkg/extractor/v6/extracts.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,15 @@ import (
"golang.org/x/net/html"
)

func extractUpgradeToken(pageHTML []byte) (string, error) {
rgx := regexp.MustCompile(`var upgradeEndpoint = ".+&token=([^&]+)&`)
m := rgx.FindSubmatch(pageHTML)
if len(m) != 2 {
return "", errors.New("unable to find form token")
}
return string(m[1]), nil
}

func extractTearDownButtonEnabledFromDoc(doc *goquery.Document) bool {
return !doc.Find("a.demolish_link div").HasClass("demolish_img_disabled")
}
Expand Down
7 changes: 1 addition & 6 deletions pkg/wrapper/ogame.go
Original file line number Diff line number Diff line change
Expand Up @@ -2879,12 +2879,7 @@ func (b *OGame) IsV9() bool {

func getToken(b *OGame, page string, celestialID ogame.CelestialID) (string, error) {
pageHTML, _ := b.getPage(page, ChangePlanet(celestialID))
rgx := regexp.MustCompile(`var upgradeEndpoint = ".+&token=([^&]+)&`)
m := rgx.FindSubmatch(pageHTML)
if len(m) != 2 {
return "", errors.New("unable to find form token")
}
return string(m[1]), nil
return b.extractor.ExtractUpgradeToken(pageHTML)
}

func (b *OGame) tearDown(celestialID ogame.CelestialID, id ogame.ID) error {
Expand Down

0 comments on commit 2599fb5

Please sign in to comment.