From 6269d604c64f57115db3b68e0caf59d2b5eeccbc Mon Sep 17 00:00:00 2001 From: faunX Date: Thu, 15 Sep 2022 09:09:01 +0200 Subject: [PATCH] * Added Extractor for LfBuildings - ExtractLfBuildings - ExtractLfBuildingsFromDoc - extractLfBuildingsFromDoc --- pkg/extractor/extractor.go | 11 +- pkg/extractor/v6/extractor.go | 13 +- pkg/extractor/v9/extractor.go | 13 + pkg/extractor/v9/extractor_test.go | 17 + pkg/extractor/v9/extracts.go | 34 + pkg/parser/parser.go | 7 +- samples/v9.0.4/en/lfbuildings.html | 1563 ++++++++++++++++++++++++++++ 7 files changed, 1654 insertions(+), 4 deletions(-) create mode 100644 samples/v9.0.4/en/lfbuildings.html diff --git a/pkg/extractor/extractor.go b/pkg/extractor/extractor.go index 62bb66e8..38342c84 100644 --- a/pkg/extractor/extractor.go +++ b/pkg/extractor/extractor.go @@ -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 { @@ -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 diff --git a/pkg/extractor/v6/extractor.go b/pkg/extractor/v6/extractor.go index 72f731e8..9a6c2809 100644 --- a/pkg/extractor/v6/extractor.go +++ b/pkg/extractor/v6/extractor.go @@ -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" ) @@ -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") +} diff --git a/pkg/extractor/v9/extractor.go b/pkg/extractor/v9/extractor.go index f76499ef..b8306f96 100644 --- a/pkg/extractor/v9/extractor.go +++ b/pkg/extractor/v9/extractor.go @@ -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) +} diff --git a/pkg/extractor/v9/extractor_test.go b/pkg/extractor/v9/extractor_test.go index 03bbcd1e..18f22b9f 100644 --- a/pkg/extractor/v9/extractor_test.go +++ b/pkg/extractor/v9/extractor_test.go @@ -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) +} diff --git a/pkg/extractor/v9/extracts.go b/pkg/extractor/v9/extracts.go index 5c9f727d..fd1540a9 100644 --- a/pkg/extractor/v9/extracts.go +++ b/pkg/extractor/v9/extracts.go @@ -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 +} diff --git a/pkg/parser/parser.go b/pkg/parser/parser.go index 0112e041..98f44855 100644 --- a/pkg/parser/parser.go +++ b/pkg/parser/parser.go @@ -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") @@ -44,6 +45,7 @@ type FacilitiesPage struct{ FullPage } type ShipyardPage struct{ FullPage } type DefensesPage struct{ FullPage } type MovementPage struct{ FullPage } +type LfBuildingsPage struct{ FullPage } type FullPagePages interface { OverviewPage | @@ -51,6 +53,7 @@ type FullPagePages interface { SuppliesPage | ResourcesSettingsPage | FacilitiesPage | + LfBuildingsPage | //TraderOverviewPageContent | //TraderResourcesPageContent | ResearchPage | @@ -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: diff --git a/samples/v9.0.4/en/lfbuildings.html b/samples/v9.0.4/en/lfbuildings.html new file mode 100644 index 00000000..2734be40 --- /dev/null +++ b/samples/v9.0.4/en/lfbuildings.html @@ -0,0 +1,1563 @@ + + + + Bermuda OGame + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + + + + + + +
+ + +
+ + +
+ + + + + + +
+ +
+ + + + + +
+
+
+
+
+ +
+
+
+ +
+ +
+
+
+ + 6,624 + +
+
+
+
+
+ + 7,270 + +
+
+
+
+
+ + 964 + +
+
+
+
+
+ + 8 + +
+
+
+
+
+ + 210.127 + +
+
+
+
+
+ + 10.032 + +
+
+
+
+ + +
+
+ + 10,000,000 + +
+
+
+ +
+ +
+
+ + + + + 0 + + +
+ +
+ ajax spinner + load... +
+ +
+
+ +
+
+
+ +
+
+
+
+ ? +
+
+
+
+
+
+ +
+ + + + + + + + + +
+
+ +
+
+
+
+
+
+
+ + +

Events

+
+ + + +
+
+
+
+ + + +
+
+
+
+
+
+

Lifeform Buildings - Homeworld

+ +
+
+
+
+
+

+ Lifeform Buildings +

+
    +
  • 2
  • 1
  • 0
  • 0
  • 0
  • 0
  • 0
  • 0
  • 0
  • 0
  • 0
  • 0
+
+
+
+
+
+
+

Lifeform Buildings

+
+ + +
+ +
+
+
+
+ +
+
+
+
+
+ +
+
+ +
+
+ +
+
+
+ + + + + +
+
+ +
+ +
+ + + +