Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improvement for EspionageReportSummary #72

Open
wants to merge 6 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions cmd/ogamed/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,9 +250,9 @@ func start(c *cli.Context) error {
e.GET("/bot/fleets", ogame.GetFleetsHandler)
e.GET("/bot/fleets/slots", ogame.GetSlotsHandler)
e.POST("/bot/fleets/:fleetID/cancel", ogame.CancelFleetHandler)
e.GET("/bot/espionage-report/:msgid", ogame.GetEspionageReportHandler)
e.GET("/bot/espionage-report/:galaxy/:system/:position", ogame.GetEspionageReportForHandler)
e.GET("/bot/espionage-report", ogame.GetEspionageReportMessagesHandler)
e.GET("/bot/espionage-report/:msgid", ogame.GetEspionageReportHandler) // returns EspionageReport
e.GET("/bot/espionage-report/:galaxy/:system/:position", ogame.GetEspionageReportForHandler) // returns EspionageReport
e.GET("/bot/espionage-report", ogame.GetEspionageReportMessagesHandler) // returns []EspionageReportSummary
e.POST("/bot/delete-report/:messageID", ogame.DeleteMessageHandler)
e.POST("/bot/delete-all-espionage-reports", ogame.DeleteEspionageMessagesHandler)
e.POST("/bot/delete-all-reports/:tabIndex", ogame.DeleteMessagesFromTabHandler)
Expand Down
11 changes: 11 additions & 0 deletions extractor_v71.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,3 +160,14 @@ func (e ExtractorV71) ExtractBuffActivation(pageHTML []byte) (string, []Item, er
func (e ExtractorV71) ExtractBuffActivationFromDoc(doc *goquery.Document) (string, []Item, error) {
return extractBuffActivationFromDocV71(doc)
}

// ExtractEspionageReportMessageIDs ...
func (e ExtractorV71) ExtractEspionageReportMessageIDs(pageHTML []byte) ([]EspionageReportSummary, int64) {
doc, _ := goquery.NewDocumentFromReader(bytes.NewReader(pageHTML))
return e.ExtractEspionageReportMessageIDsFromDoc(doc)
}

// ExtractEspionageReportMessageIDsFromDoc ...
func (e ExtractorV71) ExtractEspionageReportMessageIDsFromDoc(doc *goquery.Document) ([]EspionageReportSummary, int64) {
return extractEspionageReportMessageIDsFromDocV71(doc)
}
149 changes: 148 additions & 1 deletion extracts_v6.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
"strings"
"time"

"log"

"github.com/PuerkitoBio/goquery"
"github.com/alaingilbert/clockwork"
lua "github.com/yuin/gopher-lua"
Expand Down Expand Up @@ -644,13 +646,17 @@ func extractEspionageReportMessageIDsFromDocV6(doc *goquery.Document) ([]Espiona
}
report := EspionageReportSummary{ID: id, Type: messageType}
report.From = s.Find("span.msg_sender").Text()

// Find target coordinate
spanLink := s.Find("span.msg_title a")
targetStr := spanLink.Text()
report.Target = extractCoordV6(targetStr)
report.Target.Type = PlanetType
if spanLink.Find("figure").HasClass("moon") {
report.Target.Type = MoonType
}

// Find LootPercentage
if messageType == Report {
s.Find("div.compacting").Each(func(i int, s *goquery.Selection) {
if regexp.MustCompile(`%`).MatchString(s.Text()) {
Expand All @@ -659,14 +665,155 @@ func extractEspionageReportMessageIDsFromDocV6(doc *goquery.Document) ([]Espiona
}
})
}
msgs = append(msgs, report)

// LastActivity
log.SetFlags(log.LstdFlags | log.Lshortfile)
report.LastActivity = 0

activity := s.Find("span.msg_content").First().Find("span.ctn.ctn4.fright").First().Text()
activity = strings.TrimSpace(activity)

m := regexp.MustCompile(`(\d{2})`).FindStringSubmatch(activity)

if len(m) > 0 {
minutes := ParseInt(m[1])

if len(m) == 2 {
report.LastActivity = minutes
}
}

// IsBandit, IsStarlord
report.IsBandit = false
report.IsStarlord = false

banditstarlord := s.Find("span.honorRank")
if banditstarlord.Size() > 0 {
if banditstarlord.HasClass("rank_bandit1") || banditstarlord.HasClass("rank_bandit2") || banditstarlord.HasClass("rank_bandit3") {
report.IsBandit = true
} else if banditstarlord.HasClass("rank_starlord1") || banditstarlord.HasClass("rank_starlord2") || banditstarlord.HasClass("rank_starlord3") {
report.IsStarlord = true
}
}

// IsInactive, IsLongInactive
report.IsInactive = false
report.IsLongInactive = false

if s.Find("span.status_abbr_longinactive").Size() > 0 {
report.IsInactive = true
report.IsLongInactive = true
} else if s.Find("span.status_abbr_inactive").Size() > 0 {
report.IsInactive = true
}

// HasFleet, FleetValue
report.HasFleet = false
report.FleetValue = -1

fleet := s.Find("div.compacting").Last().Find("span").Eq(0).AttrOr("title", "-1")

if strings.Contains(fleet, ":") {
fleet = strings.Split(fleet, ":")[1]
fleet = strings.TrimSpace(fleet)
report.FleetValue = ParseInt(fleet)
}

if report.FleetValue > -1 {
report.HasFleet = true
}

// HasDefenses, DefenseValue
report.HasDefenses = false
report.DefenseValue = -1

defense := s.Find("div.compacting").Last().Find("span").Eq(1).AttrOr("title", "-1")
report.DefenseValue = ParseInt(defense)

if report.DefenseValue > -1 {
report.HasDefenses = true
}

// Metal, Crystal, Deuterium
log.SetFlags(log.LstdFlags | log.Lshortfile)

// res := s.Find("div.compacting").Eq(2).Find("span")
res := s.Find("span.resspan").Parent()

metal := res.Find("span.resspan").Eq(0).Text()
crystal := res.Find("span.resspan").Eq(1).Text()
deuterium := res.Find("span.resspan").Eq(2).Text()

log.Print("preParseres = ", metal, crystal, deuterium)

report.Metal = parseRes(metal)
report.Crystal = parseRes(crystal)
report.Deuterium = parseRes(deuterium)

log.Print(report.Metal, report.Crystal, report.Deuterium)

// APIKey
apikey, _ := s.Find("span.icon_apikey").Attr("title")
blaat, _ := goquery.NewDocumentFromReader(strings.NewReader(apikey))
snarfzonk, _ := blaat.Find("input").First().Attr("value")

report.APIKey = snarfzonk

// CreatedAt
// <span class="msg_date fright">09.02.2019 23:25:56</span>
msgDate, _ := time.Parse("02.01.2006 15:04:05", s.Find("span.msg_date").Text())
report.CreatedAt = msgDate

//log.Fatal("--end--")
msgs = append(msgs, report)
}
}
})
return msgs, nbPage
}

func parseRes(input string) (res int64) {
// Metaal: 2,4M
// Kristal: 3,6M
// Deuterium: 4,8M
// Deuterium: 140.000

if strings.Contains(input, ":") {
input = strings.Split(input, ":")[1]
}

if strings.Contains(input, ",") {
input = strings.Replace(input, ",", ".", 1)
}

if strings.Contains(input, "Miljard") || strings.Contains(input, "B") { // Miljard (NL) = Billion (EN)
if strings.Contains(input, "Miljard") {
input = strings.Split(input, "M")[0]
} else if strings.Contains(input, "B") {
input = strings.Split(input, "B")[0]
}
input = strings.TrimSpace(input)
input, _ := strconv.ParseFloat(input, 64)
inputres := input * 1e9
inputres2 := int64(inputres)
return inputres2
} else if strings.Contains(input, "M") {
input = strings.Split(input, "M")[0]
input = strings.TrimSpace(input)
input, _ := strconv.ParseFloat(input, 64)
inputres := input * 1e6
inputres2 := int64(inputres)
return inputres2
} else {
input = strings.Replace(input, ".", "", 1)
input = strings.TrimSpace(input)
inputint, _ := strconv.ParseInt(input, 10, 64)
return inputint
}

return int64(-1)
}

func extractCombatReportMessagesFromDocV6(doc *goquery.Document) ([]CombatReportSummary, int64) {
msgs := make([]CombatReportSummary, 0)
nbPage, _ := strconv.ParseInt(doc.Find("ul.pagination li").Last().AttrOr("data-page", "1"), 10, 64)
Expand Down
151 changes: 151 additions & 0 deletions extracts_v71.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
"strings"
"time"

"log"

"github.com/PuerkitoBio/goquery"
"github.com/alaingilbert/clockwork"
"golang.org/x/net/html"
Expand Down Expand Up @@ -822,3 +824,152 @@ func extractBuffActivationFromDocV71(doc *goquery.Document) (token string, items
}
return
}

// This function was initially created to extract MessageIDs from EspionageReportSummary messages. It has since been extended to include all fields, but kept it original name.
func extractEspionageReportMessageIDsFromDocV71(doc *goquery.Document) ([]EspionageReportSummary, int64) {
msgs := make([]EspionageReportSummary, 0)
nbPage, _ := strconv.ParseInt(doc.Find("ul.pagination li").Last().AttrOr("data-page", "1"), 10, 64)
doc.Find("li.msg").Each(func(i int, s *goquery.Selection) {
if idStr, exists := s.Attr("data-msg-id"); exists {
if id, err := strconv.ParseInt(idStr, 10, 64); err == nil {
messageType := Report
if s.Find("span.espionageDefText").Size() > 0 {
messageType = Action
}
report := EspionageReportSummary{ID: id, Type: messageType}
report.From = s.Find("span.msg_sender").Text()

// Added extra logging information (timestamp + linenumbers)
log.SetFlags(log.LstdFlags | log.Lshortfile)

// Username
report.Username = strings.TrimSpace(s.Find("div.compacting").First().Find("span").Eq(1).Text())

// Find PlayerClass
playerClass := s.Find("div.compacting").Eq(1).Text()
if len(playerClass) > 0 && strings.Contains(playerClass, ":") {
report.PlayerClass = strings.TrimSpace(strings.Split(playerClass, ":")[1])
}

// Find target coordinate
spanLink := s.Find("span.msg_title a")
targetStr := spanLink.Text()
report.Target = extractCoordV6(targetStr)
report.Target.Type = PlanetType
if spanLink.Find("figure").HasClass("moon") {
report.Target.Type = MoonType
}

// Find LootPercentage
if messageType == Report {
s.Find("div.compacting").Each(func(i int, s *goquery.Selection) {
if regexp.MustCompile(`%`).MatchString(s.Text()) {
report.LootPercentage, _ = strconv.ParseFloat(regexp.MustCompile(`: (\d+)%`).FindStringSubmatch(s.Text())[1], 64)
report.LootPercentage /= 100
}
})
}

// LastActivity
report.LastActivity = 0
activity := s.Find("span.msg_content").First().Find("span.ctn.ctn4.fright").First().Text()
activity = strings.TrimSpace(activity)
m := regexp.MustCompile(`(\d{2})`).FindStringSubmatch(activity)
if len(m) > 0 {
minutes := ParseInt(m[1])

if len(m) == 2 {
report.LastActivity = minutes
}
}

// CounterEspionage
ceTxt := s.Find("span.msg_content div.compacting").Eq(3).Find("span.fright").Text()
ceTxtRegexp := regexp.MustCompile(`: (\d+)%`).FindStringSubmatch(ceTxt)
if len(ceTxtRegexp) == 2 {
report.CounterEspionage, _ = strconv.ParseInt(ceTxtRegexp[1], 10, 64)
}

// IsBandit, IsStarlord
report.IsBandit = false
report.IsStarlord = false

banditstarlord := s.Find("span.honorRank")
if banditstarlord.Size() > 0 {
if banditstarlord.HasClass("rank_bandit1") || banditstarlord.HasClass("rank_bandit2") || banditstarlord.HasClass("rank_bandit3") {
report.IsBandit = true
} else if banditstarlord.HasClass("rank_starlord1") || banditstarlord.HasClass("rank_starlord2") || banditstarlord.HasClass("rank_starlord3") {
report.IsStarlord = true
}
}

// IsInactive, IsLongInactive
report.IsInactive = false
report.IsLongInactive = false

if s.Find("span.status_abbr_longinactive").Size() > 0 {
report.IsInactive = true
report.IsLongInactive = true
} else if s.Find("span.status_abbr_inactive").Size() > 0 {
report.IsInactive = true
}

// HasFleet, FleetValue
report.HasFleet = false
report.FleetValue = -1

fleet := s.Find("div.compacting").Last().Find("span").Eq(0).AttrOr("title", "-1")

if strings.Contains(fleet, ":") {
fleet = strings.Split(fleet, ":")[1]
fleet = strings.TrimSpace(fleet)
report.FleetValue = ParseInt(fleet)
}

if report.FleetValue > -1 {
report.HasFleet = true
}

// HasDefenses, DefenseValue
report.HasDefenses = false
report.DefenseValue = -1

defense := s.Find("div.compacting").Last().Find("span").Eq(1).AttrOr("title", "-1")
report.DefenseValue = ParseInt(defense)

if report.DefenseValue > -1 {
report.HasDefenses = true
}

// Metal, Crystal, Deuterium
res := s.Find("span.resspan").Parent()

metal := res.Find("span.resspan").Eq(0).Text()
crystal := res.Find("span.resspan").Eq(1).Text()
deuterium := res.Find("span.resspan").Eq(2).Text()

//log.Print("preParseres = ", metal, crystal, deuterium)

report.Metal = parseRes(metal)
report.Crystal = parseRes(crystal)
report.Deuterium = parseRes(deuterium)

//log.Print(report.Metal, report.Crystal, report.Deuterium)

// APIKey
apikey, _ := s.Find("span.icon_apikey").Attr("title")
blaat, _ := goquery.NewDocumentFromReader(strings.NewReader(apikey))
snarfzonk := blaat.Find("input").First().AttrOr("value", "")
report.APIKey = snarfzonk

// CreatedAt
// <span class="msg_date fright">09.02.2019 23:25:56</span>
msgDate, _ := time.Parse("02.01.2006 15:04:05", s.Find("span.msg_date").Text())
report.CreatedAt = msgDate

msgs = append(msgs, report)
}
}
})
return msgs, nbPage
}
Loading