extract code in fn
@@ -1139,23 +1139,29 @@ func extractPlanets(pageHTML string, b *OGame) []Planet {
return res
}
-func extractPlanet(pageHTML string, planetID PlanetID, b *OGame) (Planet, error) {
- planetIDStr := planetID.String()
- doc, _ := goquery.NewDocumentFromReader(strings.NewReader(pageHTML))
- s := doc.Find("div#planet-" + planetIDStr)
- if len(s.Nodes) > 0 { // planet
+func extractPlanetFromSelection(s *goquery.Selection, b *OGame) (Planet, error) {
+ el, _ := s.Attr("id")
+ id, err := strconv.Atoi(strings.TrimPrefix(el, "planet-"))
+ if err != nil {
+ return Planet{}, err
+ }
+
title, _ := s.Find("a").Attr("title")
root, err := html.Parse(strings.NewReader(title))
if err != nil {
return Planet{}, err
}
+
txt := goquery.NewDocumentFromNode(root).Text()
m := planetInfosRgx.FindStringSubmatch(txt)
+ if len(m) < 10 {
+ return Planet{}, errors.New("failed to parse planet infos: " + txt)
+ }
res := Planet{}
res.ogame = b
- res.Img = s.Find("img").AttrOr("src", "")
- res.ID = planetID
+ res.Img = s.Find("img.planetPic").AttrOr("src", "")
+ res.ID = PlanetID(id)
res.Name = m[1]
res.Coordinate.Galaxy, _ = strconv.Atoi(m[2])
res.Coordinate.System, _ = strconv.Atoi(m[3])
@@ -1167,6 +1173,14 @@ func extractPlanet(pageHTML string, planetID PlanetID, b *OGame) (Planet, error)
res.Temperature.Max, _ = strconv.Atoi(m[9])
return res, nil
}
+
+func extractPlanet(pageHTML string, planetID PlanetID, b *OGame) (Planet, error) {
+ planetIDStr := planetID.String()
+ doc, _ := goquery.NewDocumentFromReader(strings.NewReader(pageHTML))
+ s := doc.Find("div#planet-" + planetIDStr)
+ if len(s.Nodes) > 0 { // planet
+ return extractPlanetFromSelection(s, b)
+ }
return Planet{}, errors.New("failed to find planetID")
}
