Skip to content

Commit

Permalink
Fix fields html when full
Browse files Browse the repository at this point in the history
  • Loading branch information
alaingilbert committed Aug 8, 2018
1 parent 356d309 commit 55849f3
Show file tree
Hide file tree
Showing 3 changed files with 1,303 additions and 2 deletions.
6 changes: 4 additions & 2 deletions ogame.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (

"github.com/PuerkitoBio/goquery"
"github.com/Sirupsen/logrus"
"github.com/microcosm-cc/bluemonday"
"github.com/pkg/errors"
"golang.org/x/net/html"
)
Expand Down Expand Up @@ -892,8 +893,9 @@ func extractPlanets(pageHTML string, b *OGame) []Planet {
planetPic, _ := s.Find("img.planetPic").Attr("src")

txt, _ := s.Find("a.planetlink").Attr("title")
r1 := regexp.MustCompile(`<b>([^\[]+) \[(\d+):(\d+):(\d+)]</b><br/>([\d.]+)km \((\d+)/(\d+)\)<(?:BR|br)>([-\d]+).+C (?:bis|to|à) ([-\d]+).+C<br/>`)
m1 := r1.FindStringSubmatch(txt)
p := bluemonday.StrictPolicy()
r1 := regexp.MustCompile(`([^\[]+) \[(\d+):(\d+):(\d+)]([\d.]+)km \((\d+)/(\d+)\)([-\d]+).+C (?:bis|to|à) ([-\d]+).+C`)
m1 := r1.FindStringSubmatch(p.Sanitize(txt))
if len(m1) < 10 {
b.error("failed to parse planet infos: " + txt)
return
Expand Down
15 changes: 15 additions & 0 deletions ogame_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,21 @@ func TestExtractUserInfos_fr(t *testing.T) {
assert.Equal(t, 3348, infos.Total)
}

func TestExtractPlanets_fieldsFilled(t *testing.T) {
pageHTMLBytes, _ := ioutil.ReadFile("samples/overview_fields_filled.html")
planets := extractPlanets(string(pageHTMLBytes), nil)
assert.Equal(t, 5, len(planets))
assert.Equal(t, PlanetID(33698658), planets[0].ID)
assert.Equal(t, Coordinate{Galaxy: 4, System: 116, Position: 12}, planets[0].Coordinate)
assert.Equal(t, "Homeworld", planets[0].Name)
assert.Equal(t, "https://gf3.geo.gfsrv.net/cdnea/7d7ba402d90247ef7d89aa1035e525.png", planets[0].Img)
assert.Equal(t, -23, planets[0].Temperature.Min)
assert.Equal(t, 17, planets[0].Temperature.Max)
assert.Equal(t, 188, planets[0].Fields.Built)
assert.Equal(t, 188, planets[0].Fields.Total)
assert.Equal(t, 12800, planets[0].Diameter)
}

func TestExtractPlanets(t *testing.T) {
pageHTMLBytes, _ := ioutil.ReadFile("samples/overview_inactive.html")
planets := extractPlanets(string(pageHTMLBytes), nil)
Expand Down
Loading

0 comments on commit 55849f3

Please sign in to comment.