Skip to content

Commit

Permalink
* Added Extractor for LfBuildings
Browse files Browse the repository at this point in the history
- ExtractLfBuildings
- ExtractLfBuildingsFromDoc
- extractLfBuildingsFromDoc
  • Loading branch information
faunX authored and faunX committed Sep 15, 2022
1 parent 5537c50 commit 6269d60
Show file tree
Hide file tree
Showing 7 changed files with 1,654 additions and 4 deletions.
11 changes: 9 additions & 2 deletions pkg/extractor/extractor.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package extractor

import (
"net/url"
"time"

"github.com/PuerkitoBio/goquery"
v6 "github.com/alaingilbert/ogame/pkg/extractor/v6"
v7 "github.com/alaingilbert/ogame/pkg/extractor/v7"
v9 "github.com/alaingilbert/ogame/pkg/extractor/v9"
"github.com/alaingilbert/ogame/pkg/ogame"
"net/url"
"time"
)

type FullPageExtractorBytes interface {
Expand Down Expand Up @@ -410,10 +411,16 @@ type MessagesMarketplaceExtractorBytes interface {

type LfBuildingsExtractorBytes interface {
ExtractUpgradeToken(pageHTML []byte) (string, error)
ExtractLfBuildings(pageHTML []byte) (ogame.LfBuildings, error)
}

type LfBuildingsExtractorDoc interface {
ExtractLfBuildingsFromDoc(doc *goquery.Document) (ogame.LfBuildings, error)
}

type LfBuildingsExtractorBytesDoc interface {
LfBuildingsExtractorBytes
LfBuildingsExtractorDoc
}

// ResourcesBuildingsExtractorBytes supplies page
Expand Down
13 changes: 12 additions & 1 deletion pkg/extractor/v6/extractor.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ package v6
import (
"bytes"
"errors"
"github.com/alaingilbert/ogame/pkg/ogame"
"net/url"
"time"

"github.com/alaingilbert/ogame/pkg/ogame"

"github.com/PuerkitoBio/goquery"
"github.com/alaingilbert/clockwork"
)
Expand Down Expand Up @@ -882,3 +883,13 @@ func (e *Extractor) ExtractIsMobile(pageHTML []byte) bool {
func (e *Extractor) ExtractIsMobileFromDoc(doc *goquery.Document) bool {
panic("not implemented")
}

// ExtractLfBuildings ...
func (e *Extractor) ExtractLfBuildings(pageHTML []byte) (ogame.LfBuildings, error) {
panic("not implemented")
}

// ExtractLfBuildingsFromDoc ...
func (e *Extractor) ExtractLfBuildingsFromDoc(doc *goquery.Document) (ogame.LfBuildings, error) {
panic("not implemented")
}
13 changes: 13 additions & 0 deletions pkg/extractor/v9/extractor.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,24 @@ func (e *Extractor) ExtractConstructions(pageHTML []byte) (buildingID ogame.ID,
return ExtractConstructions(pageHTML, clockwork.NewRealClock())
}

// ExtractResourceSettings ...
func (e *Extractor) ExtractResourceSettings(pageHTML []byte) (ogame.ResourceSettings, string, error) {
doc, _ := goquery.NewDocumentFromReader(bytes.NewReader(pageHTML))
return e.ExtractResourceSettingsFromDoc(doc)
}

// ExtractResourceSettingsFromDoc ...
func (e *Extractor) ExtractResourceSettingsFromDoc(doc *goquery.Document) (ogame.ResourceSettings, string, error) {
return extractResourceSettingsFromDoc(doc)
}

// ExtractLfBuildings ...
func (e *Extractor) ExtractLfBuildings(pageHTML []byte) (ogame.LfBuildings, error) {
doc, _ := goquery.NewDocumentFromReader(bytes.NewReader(pageHTML))
return e.ExtractLfBuildingsFromDoc(doc)
}

// ExtractLfBuildingsFromDoc ...
func (e *Extractor) ExtractLfBuildingsFromDoc(doc *goquery.Document) (ogame.LfBuildings, error) {
return extractLfBuildingsFromDoc(doc)
}
17 changes: 17 additions & 0 deletions pkg/extractor/v9/extractor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,3 +132,20 @@ func TestExtractUserInfos(t *testing.T) {
assert.Equal(t, int64(1102), info.Rank)
assert.Equal(t, int64(2931), info.Total)
}

func TestExtractLfBuildings(t *testing.T) {
pageHTMLBytes, _ := ioutil.ReadFile("../../../samples/v9.0.4/en/lfbuildings.html")
res, _ := NewExtractor().ExtractLfBuildings(pageHTMLBytes)
assert.Equal(t, int64(2), res.ResidentialSector)
assert.Equal(t, int64(1), res.BiosphereFarm)
assert.Equal(t, int64(0), res.ResearchCentre)
assert.Equal(t, int64(0), res.AcademyOfSciences)
assert.Equal(t, int64(0), res.NeuroCalibrationCentre)
assert.Equal(t, int64(0), res.HighEnergySmelting)
assert.Equal(t, int64(0), res.FoodSilo)
assert.Equal(t, int64(0), res.FusionPoweredProduction)
assert.Equal(t, int64(0), res.Skyscraper)
assert.Equal(t, int64(0), res.BiotechLab)
assert.Equal(t, int64(0), res.Metropolis)
assert.Equal(t, int64(0), res.PlanetaryShield)
}
34 changes: 34 additions & 0 deletions pkg/extractor/v9/extracts.go
Original file line number Diff line number Diff line change
Expand Up @@ -570,3 +570,37 @@ func extractResourceSettingsFromDoc(doc *goquery.Document) (ogame.ResourceSettin

return res, token, nil
}

func GetNbr(doc *goquery.Document, name string) int64 {
val := utils.DoParseI64(doc.Find("span."+name+" span.level").First().AttrOr("data-value", "0"))
return val
}

func extractLfBuildingsFromDoc(doc *goquery.Document) (ogame.LfBuildings, error) {
res := ogame.LfBuildings{}
// res.ResidentialSector = GetNbr(doc, "residentialSector")
// res.BiosphereFarm = GetNbr(doc, "biosphereFarm")
// res.ResearchCentre = GetNbr(doc, "researchCentre")
// res.AcademyOfSciences = GetNbr(doc, "academyOfSciences")
// res.NeuroCalibrationCentre = GetNbr(doc, "neuroCalibrationCentre")
// res.HighEnergySmelting = GetNbr(doc, "highEnergySmelting")
// res.FoodSilo = GetNbr(doc, "foodSilo")
// res.FusionPoweredProduction = GetNbr(doc, "fusionPoweredProduction")
// res.Skyscraper = GetNbr(doc, "skyscraper")
// res.BiotechLab = GetNbr(doc, "biotechLab")
// res.Metropolis = GetNbr(doc, "metropolis")
// res.PlanetaryShield = GetNbr(doc, "planetaryShield")
res.ResidentialSector = GetNbr(doc, "lifeformTech11101")
res.BiosphereFarm = GetNbr(doc, "lifeformTech11102")
res.ResearchCentre = GetNbr(doc, "lifeformTech11103")
res.AcademyOfSciences = GetNbr(doc, "lifeformTech11104")
res.NeuroCalibrationCentre = GetNbr(doc, "lifeformTech11105")
res.HighEnergySmelting = GetNbr(doc, "lifeformTech11106")
res.FoodSilo = GetNbr(doc, "lifeformTech11107")
res.FusionPoweredProduction = GetNbr(doc, "lifeformTech11108")
res.Skyscraper = GetNbr(doc, "lifeformTech11109")
res.BiotechLab = GetNbr(doc, "lifeformTech11110")
res.Metropolis = GetNbr(doc, "lifeformTech11111")
res.PlanetaryShield = GetNbr(doc, "lifeformTech11112")
return res, nil
}
7 changes: 6 additions & 1 deletion pkg/parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ package parser
import (
"bytes"
"errors"
"time"

"github.com/PuerkitoBio/goquery"
"github.com/alaingilbert/ogame/pkg/extractor"
v6 "github.com/alaingilbert/ogame/pkg/extractor/v6"
"github.com/alaingilbert/ogame/pkg/ogame"
"time"
)

var ErrParsePageType = errors.New("failed to parse requested page type")
Expand Down Expand Up @@ -44,13 +45,15 @@ type FacilitiesPage struct{ FullPage }
type ShipyardPage struct{ FullPage }
type DefensesPage struct{ FullPage }
type MovementPage struct{ FullPage }
type LfBuildingsPage struct{ FullPage }

type FullPagePages interface {
OverviewPage |
PreferencesPage |
SuppliesPage |
ResourcesSettingsPage |
FacilitiesPage |
LfBuildingsPage |
//TraderOverviewPageContent |
//TraderResourcesPageContent |
ResearchPage |
Expand Down Expand Up @@ -127,6 +130,8 @@ func ParsePage[T FullPagePages](e extractor.Extractor, pageHTML []byte) (T, erro
return T(ResearchPage{fullPage}), nil
case FacilitiesPage:
return T(FacilitiesPage{fullPage}), nil
case LfBuildingsPage:
return T(LfBuildingsPage{fullPage}), nil
case SuppliesPage:
return T(SuppliesPage{fullPage}), nil
case ResourcesSettingsPage:
Expand Down
Loading

0 comments on commit 6269d60

Please sign in to comment.