From ee4369abff43fbe340433e9f75101380e60d7499 Mon Sep 17 00:00:00 2001 From: Kichiyaki Date: Sun, 31 May 2020 13:16:24 +0200 Subject: [PATCH] change cron messages content, rename some variables --- cron/cron.go | 52 ++++++++++++++++++++++++++----------------- go.mod | 1 - go.sum | 6 ----- main.go | 5 ++--- scraper/helpers.go | 9 -------- scraper/scraper.go | 32 ++++++++++++++++---------- tribalwars/helpers.go | 10 +++++++++ utils/location.go | 13 +++++++++++ 8 files changed, 76 insertions(+), 52 deletions(-) create mode 100644 utils/location.go diff --git a/cron/cron.go b/cron/cron.go index e9c1929..f69df6e 100644 --- a/cron/cron.go +++ b/cron/cron.go @@ -8,7 +8,9 @@ import ( "twdcbot/discord" "twdcbot/scraper" "twdcbot/server" + "twdcbot/tribalwars" "twdcbot/tribe" + "twdcbot/utils" "github.com/robfig/cron/v3" ) @@ -57,22 +59,22 @@ func (h *handler) checkEnnoblements() { continue } for _, tribe := range server.Tribes { - conquers, ok := data[tribe.World] + conquests, ok := data[tribe.World] if ok { if server.LostVillagesChannelID != "" { - for _, conquer := range conquers.LostVillages(tribe.TribeID) { - if server.Tribes.Contains(tribe.World, conquer.NewOwnerTribeID) { + for _, conquest := range conquests.LostVillages(tribe.TribeID) { + if server.Tribes.Contains(tribe.World, conquest.NewOwnerTribeID) { continue } - h.discord.SendMessage(server.LostVillagesChannelID, formatMsgAboutVillageLost(conquer)) + h.discord.SendMessage(server.LostVillagesChannelID, formatMsgAboutVillageLost(tribe.World, conquest)) } } if server.ConqueredVillagesChannelID != "" { - for _, conquer := range conquers.ConqueredVillages(tribe.TribeID) { - if server.Tribes.Contains(tribe.World, conquer.OldOwnerTribeID) { + for _, conquest := range conquests.ConqueredVillages(tribe.TribeID) { + if server.Tribes.Contains(tribe.World, conquest.OldOwnerTribeID) { continue } - h.discord.SendMessage(server.ConqueredVillagesChannelID, formatMsgAboutVillageConquered(conquer)) + h.discord.SendMessage(server.ConqueredVillagesChannelID, formatMsgAboutVillageConquest(tribe.World, conquest)) } } } @@ -80,20 +82,28 @@ func (h *handler) checkEnnoblements() { } } -func formatMsgAboutVillageLost(conquer *scraper.Conquer) string { - return fmt.Sprintf(`Wioska %s (właściciel: %s [%s]) została stracona na rzecz gracza %s (%s)`, - conquer.Village, - conquer.OldOwnerName, - conquer.OldOwnerTribeName, - conquer.NewOwnerName, - conquer.NewOwnerTribeName) +func formatDateOfConquest(loc *time.Location, t time.Time) string { + return t.In(loc).Format("15:04:05") } -func formatMsgAboutVillageConquered(conquer *scraper.Conquer) string { - return fmt.Sprintf(`Gracz %s (%s) podbił wioskę %s od gracza %s (%s)`, - conquer.NewOwnerName, - conquer.NewOwnerTribeName, - conquer.Village, - conquer.OldOwnerName, - conquer.OldOwnerTribeName) +func formatMsgAboutVillageLost(world string, conquest *scraper.Conquest) string { + return fmt.Sprintf(`**%s** %s: Wioska %s (właściciel: %s [%s]) została stracona na rzecz gracza %s (%s)`, + world, + formatDateOfConquest(utils.GetLocation(tribalwars.LanguageCodeFromWorldName(world)), conquest.ConqueredAt), + conquest.Village, + conquest.OldOwnerName, + conquest.OldOwnerTribeName, + conquest.NewOwnerName, + conquest.NewOwnerTribeName) +} + +func formatMsgAboutVillageConquest(world string, conquest *scraper.Conquest) string { + return fmt.Sprintf(`**%s** %s: Gracz %s (%s) podbił wioskę %s od gracza %s (%s)`, + world, + formatDateOfConquest(utils.GetLocation(tribalwars.LanguageCodeFromWorldName(world)), conquest.ConqueredAt), + conquest.NewOwnerName, + conquest.NewOwnerTribeName, + conquest.Village, + conquest.OldOwnerName, + conquest.OldOwnerTribeName) } diff --git a/go.mod b/go.mod index a58ff51..8c355cb 100644 --- a/go.mod +++ b/go.mod @@ -18,7 +18,6 @@ require ( github.com/pkg/errors v0.9.1 github.com/robfig/cron/v3 v3.0.1 github.com/saintfish/chardet v0.0.0-20120816061221-3af4cd4741ca // indirect - github.com/sirupsen/logrus v1.6.0 github.com/temoto/robotstxt v1.1.1 // indirect golang.org/x/net v0.0.0-20200528225125-3c3fba18258b // indirect google.golang.org/protobuf v1.24.0 // indirect diff --git a/go.sum b/go.sum index f72ac20..532dca3 100644 --- a/go.sum +++ b/go.sum @@ -87,8 +87,6 @@ github.com/joho/godotenv v1.3.0 h1:Zjp+RcGpHhGlrMbJzXTrZZPrWj+1vfm90La1wgB6Bhc= github.com/joho/godotenv v1.3.0/go.mod h1:7hK45KPybAkOC6peb+G5yklZfMxEjkZhHbwpqxOKXbg= github.com/kennygrant/sanitize v1.2.4 h1:gN25/otpP5vAsO2djbMhF/LQX6R7+O1TB4yv8NzpJ3o= github.com/kennygrant/sanitize v1.2.4/go.mod h1:LGsjYYtgxbetdg5owWB2mpgUL6e2nfw2eObZ0u0qvak= -github.com/konsorten/go-windows-terminal-sequences v1.0.3 h1:CE8S1cTafDpPvMhIxNJKvHsGVBgn1xWYf1NbHQhywc8= -github.com/konsorten/go-windows-terminal-sequences v1.0.3/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI= github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= @@ -112,10 +110,7 @@ github.com/saintfish/chardet v0.0.0-20120816061221-3af4cd4741ca/go.mod h1:uugorj github.com/segmentio/encoding v0.1.10/go.mod h1:RWhr02uzMB9gQC1x+MfYxedtmBibb9cZ6Vv9VxRSSbw= github.com/segmentio/encoding v0.1.11 h1:Qy9+DK2pmQnF6KjD5IclHekzfZFN+pZrHWyUbE2bhag= github.com/segmentio/encoding v0.1.11/go.mod h1:RWhr02uzMB9gQC1x+MfYxedtmBibb9cZ6Vv9VxRSSbw= -github.com/sirupsen/logrus v1.6.0 h1:UBcNElsrwanuuMsnGSlYmtmgbb23qDR5dG+6X6Oo89I= -github.com/sirupsen/logrus v1.6.0/go.mod h1:7uNnSEd1DgxDLC74fIahvMZmmYsHGZGEOFrfsX/uA88= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= -github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk= github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= @@ -178,7 +173,6 @@ golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5h golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20190922100055-0a153f010e69/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20191010194322-b09406accb47/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= diff --git a/main.go b/main.go index 26657ad..2d4af13 100644 --- a/main.go +++ b/main.go @@ -14,7 +14,6 @@ import ( "github.com/go-pg/pg/v10" "github.com/joho/godotenv" "github.com/robfig/cron/v3" - "github.com/sirupsen/logrus" ) func init() { @@ -35,7 +34,7 @@ func main() { }) defer func() { if err := db.Close(); err != nil { - logrus.Fatal(err) + log.Fatal(err) } }() serverRepo, err := server_repository.NewPgRepo(db) @@ -77,7 +76,7 @@ func main() { signal.Notify(channel, os.Interrupt, os.Kill, syscall.SIGTERM, syscall.SIGINT) <-channel - logrus.Info("shutting down") + log.Print("shutting down") os.Exit(0) } diff --git a/scraper/helpers.go b/scraper/helpers.go index 41334ee..943d8ae 100644 --- a/scraper/helpers.go +++ b/scraper/helpers.go @@ -1,16 +1,7 @@ package scraper -import "time" - var ( TwstatsURLs = map[string]string{ "pl": "https://pl.twstats.com", } - Locations = loadLocations() ) - -func loadLocations() map[string]*time.Location { - m := make(map[string]*time.Location) - m["pl"], _ = time.LoadLocation("Europe/Warsaw") - return m -} diff --git a/scraper/scraper.go b/scraper/scraper.go index b2a17f8..0bf72e7 100644 --- a/scraper/scraper.go +++ b/scraper/scraper.go @@ -8,6 +8,7 @@ import ( "sync" "time" "twdcbot/tribalwars" + "twdcbot/utils" "github.com/PuerkitoBio/goquery" "github.com/gocolly/colly" @@ -17,7 +18,7 @@ const ( pathEnnoblementsLive = "/%s/index.php?page=ennoblements&live=live" ) -type Conquer struct { +type Conquest struct { Village string VillageID int NewOwnerID int @@ -31,10 +32,10 @@ type Conquer struct { ConqueredAt time.Time } -type Conquers []*Conquer +type Conquests []*Conquest -func (c Conquers) LostVillages(tribeID int) Conquers { - filtered := Conquers{} +func (c Conquests) LostVillages(tribeID int) Conquests { + filtered := Conquests{} for _, conquer := range c { if conquer.OldOwnerTribeID == tribeID && conquer.OldOwnerTribeID != conquer.NewOwnerTribeID { filtered = append(filtered, conquer) @@ -43,8 +44,8 @@ func (c Conquers) LostVillages(tribeID int) Conquers { return filtered } -func (c Conquers) ConqueredVillages(tribeID int) Conquers { - filtered := Conquers{} +func (c Conquests) ConqueredVillages(tribeID int) Conquests { + filtered := Conquests{} for _, conquer := range c { if conquer.NewOwnerTribeID == tribeID && conquer.NewOwnerTribeID != conquer.OldOwnerTribeID { filtered = append(filtered, conquer) @@ -58,7 +59,7 @@ type Scraper struct { since time.Time collector *colly.Collector mutex sync.Mutex - result map[string]Conquers + result map[string]Conquests } func New(worlds []string, since time.Time) *Scraper { @@ -99,16 +100,21 @@ func (s *Scraper) getIDFromNodeHref(node *goquery.Selection) int { func (s *Scraper) handleHTML(row *colly.HTMLElement) { world := strings.Split(row.Request.URL.Path, "/")[1] var err error - c := &Conquer{} + c := &Conquest{} + conqueredAtString := strings.TrimSpace(row.DOM.Find("td:last-child").Text()) - location := Locations[tribalwars.LanguageCodeFromWorldName(world)] - c.ConqueredAt, err = time.ParseInLocation("2006-01-02 - 15:04:05", conqueredAtString, location) + location := utils.GetLocation(tribalwars.LanguageCodeFromWorldName(world)) + c.ConqueredAt, err = time.ParseInLocation("2006-01-02 - 15:04:05", + conqueredAtString, + location) if err != nil || c.ConqueredAt.Before(s.since.In(location)) { return } + villageAnchor := row.DOM.Find("a:first-child").First() c.VillageID = s.getIDFromNodeHref(villageAnchor) c.Village = strings.TrimSpace(villageAnchor.Text()) + oldOwnerNode := row.DOM.Find("td:nth-child(3) a:first-child") if len(oldOwnerNode.Nodes) == 0 { c.OldOwnerName = "-" @@ -124,6 +130,7 @@ func (s *Scraper) handleHTML(row *colly.HTMLElement) { c.OldOwnerTribeName = "-" } } + newOwnerNode := row.DOM.Find("td:nth-child(4) a:first-child") c.NewOwnerID = s.getIDFromNodeHref(newOwnerNode) c.NewOwnerName = strings.TrimSpace(newOwnerNode.Text()) @@ -134,13 +141,14 @@ func (s *Scraper) handleHTML(row *colly.HTMLElement) { } else { c.NewOwnerTribeName = "-" } + s.mutex.Lock() s.result[world] = append(s.result[world], c) s.mutex.Unlock() } -func (s *Scraper) Scrap() map[string]Conquers { - s.result = make(map[string]Conquers) +func (s *Scraper) Scrap() map[string]Conquests { + s.result = make(map[string]Conquests) s.collector.OnHTML(".r1", s.handleHTML) s.collector.OnHTML(".r2", s.handleHTML) diff --git a/tribalwars/helpers.go b/tribalwars/helpers.go index fbf3caa..b0f6ba1 100644 --- a/tribalwars/helpers.go +++ b/tribalwars/helpers.go @@ -1,5 +1,15 @@ package tribalwars +var ( + GuestEndpoints = map[string]map[string]string{ + "pl": map[string]string{ + "tribe_info": "https://%s.plemiona.pl/guest.php?screen=info_ally&id=%d", + "player_info": "https://%s.plemiona.pl/guest.php?screen=info_player&id=%d", + "village_info": "https://%s.plemiona.pl/guest.php?screen=info_village&id=%d", + }, + } +) + func LanguageCodeFromWorldName(world string) string { if len(world) < 2 { return "" diff --git a/utils/location.go b/utils/location.go new file mode 100644 index 0000000..f0b755c --- /dev/null +++ b/utils/location.go @@ -0,0 +1,13 @@ +package utils + +import "time" + +func GetLocation(lang string) *time.Location { + switch lang { + case "pl": + loc, _ := time.LoadLocation("Europe/Warsaw") + return loc + default: + return time.UTC + } +}