From 9b0fcdf50f86f482f50b0af09311f89c29577fe1 Mon Sep 17 00:00:00 2001 From: Tobias Lindberg Date: Wed, 5 Jan 2022 01:04:34 +0100 Subject: [PATCH 01/22] adding houses endpoints --- README.md | 2 + src/TibiaHousesHouseV3.go | 29 ++++++ src/TibiaHousesOverviewV3.go | 174 +++++++++++++++++++++++++++++++++++ src/webserver.go | 4 + 4 files changed, 209 insertions(+) create mode 100644 src/TibiaHousesHouseV3.go create mode 100644 src/TibiaHousesOverviewV3.go diff --git a/README.md b/README.md index c3967c99..1e498e56 100644 --- a/README.md +++ b/README.md @@ -106,6 +106,8 @@ Those are the current existing endpoints. - GET `/v3/highscores/world/:world` - GET `/v3/highscores/world/:world/:category` - GET `/v3/highscores/world/:world/:category/:vocation` +- GET `/v3/houses/world/:world/house/:houseid` +- GET `/v3/houses/world/:world/town/:town` - GET `/v3/killstatistics/world/:world` - GET `/v3/news/archive` - GET `/v3/news/archive/:days` diff --git a/src/TibiaHousesHouseV3.go b/src/TibiaHousesHouseV3.go new file mode 100644 index 00000000..c391cad1 --- /dev/null +++ b/src/TibiaHousesHouseV3.go @@ -0,0 +1,29 @@ +package main + +import "github.com/gin-gonic/gin" + +// TibiaHousesHouseV3 func +func TibiaHousesHouseV3(c *gin.Context) { + + /* + // getting params from URL + world := c.Param("world") + houseid := c.Param("houseid") + */ + + // + // The base + type JSONData struct{} + + /* + // Adding fix for First letter to be upper and rest lower + world = TibiadataStringWorldFormatToTitleV3(world) + */ + + // + // Build the data-blob + jsonData := JSONData{} + + // return jsonData + TibiaDataAPIHandleSuccessResponse(c, "TibiaHousesHouseV3", jsonData) +} diff --git a/src/TibiaHousesOverviewV3.go b/src/TibiaHousesOverviewV3.go new file mode 100644 index 00000000..1814890b --- /dev/null +++ b/src/TibiaHousesOverviewV3.go @@ -0,0 +1,174 @@ +package main + +import ( + "log" + "regexp" + "strings" + + "github.com/PuerkitoBio/goquery" + "github.com/gin-gonic/gin" +) + +// TibiaHousesOverviewV3 func +func TibiaHousesOverviewV3(c *gin.Context) { + + // getting params from URL + world := c.Param("world") + town := c.Param("town") + + // Default value: + // town - Ab'Dendriel + // housetype - houses + + // Child of House + type Auction struct { + AuctionBid int `json:"current_bid"` + AuctionLeft string `json:"time_left"` + } + + // Child of Houses + type House struct { + Name string `json:"name"` + HouseID int `json:"house_id"` + Size int `json:"size"` + Rent int `json:"rent"` + IsRented bool `json:"rented"` + IsAuctioned bool `json:"auctioned"` + Auction Auction `json:"auction"` + } + + // Child of JSONData + type Houses struct { + World string `json:"world"` + Town string `json:"town"` + HouseList []House `json:"house_list"` + GuildhallList []House `json:"guildhall_list"` + } + + // + // The base includes two levels: Houses and Information + type JSONData struct { + Houses Houses `json:"houses"` + Information Information `json:"information"` + } + + // Creating empty vars + var HouseData, GuildhallData []House + HouseData = []House{} + GuildhallData = []House{} + + // Adding fix for First letter to be upper and rest lower + world = TibiadataStringWorldFormatToTitleV3(world) + town = TibiadataStringWorldFormatToTitleV3(town) + + // list of different house types + HouseTypes := []string{"houses", "guildhalls"} + // running over the HouseTypes array + for _, HouseType := range HouseTypes { + + // Getting data with TibiadataHTMLDataCollectorV3 + BoxContentHTML := TibiadataHTMLDataCollectorV3("https://www.tibia.com/community/?subtopic=houses&world=" + TibiadataQueryEscapeStringV3(world) + "&town=" + TibiadataQueryEscapeStringV3(town) + "&type=" + TibiadataQueryEscapeStringV3(HouseType)) + + // Loading HTML data into ReaderHTML for goquery with NewReader + ReaderHTML, err := goquery.NewDocumentFromReader(strings.NewReader(BoxContentHTML)) + if err != nil { + log.Fatal(err) + } + + // Running query over each div + ReaderHTML.Find(".BoxContent table tr").Each(func(index int, s *goquery.Selection) { + + // Storing HTML into HousesDivHTML + HousesDivHTML, err := s.Html() + if err != nil { + log.Fatal(err) + } + + // Removing linebreaks from HTML + HousesDivHTML = TibiadataHTMLRemoveLinebreaksV3(HousesDivHTML) + + // Regex to get data for record values + regex1 := regexp.MustCompile(`(.*)<\/nobr><\/td>([0-9]+).sqm<\/nobr><\/td>([0-9]+)(k+).gold<\/nobr><\/td>(.*)<\/nobr><\/td>.*houseid" value="([0-9]+)"\/> 0 { + + // Default of vars + IsRented := false + IsAuctioned := false + AuctionBid := 0 + AuctionLeft := "" + + if subma1[0][5] == "rented" { + IsRented = true + } else if strings.Contains(subma1[0][5], "no bid yet") { + // nothing to set? + } else if strings.Contains(subma1[0][5], "auctioned") { + IsAuctioned = true + + regex1b := regexp.MustCompile(`auctioned..([0-9]+).gold..(.*).`) + subma1b := regex1b.FindAllStringSubmatch(subma1[0][5], -1) + AuctionBid = TibiadataStringToIntegerV3(subma1b[0][1]) + AuctionLeft = subma1b[0][2] + } + + // Name + Name := TibiaDataSanitizeEscapedString(subma1[0][1]) + // HouseID + HouseID := TibiadataStringToIntegerV3(subma1[0][6]) + // Size + Size := TibiadataStringToIntegerV3(subma1[0][2]) + // Rent + Rent := TibiadataStringToIntegerV3(subma1[0][3] + strings.Repeat("000", strings.Count(subma1[0][4], "k"))) + + switch HouseType { + case "houses": + HouseData = append(HouseData, House{ + Name: Name, + HouseID: HouseID, + Size: Size, + Rent: Rent, + IsRented: IsRented, + IsAuctioned: IsAuctioned, + Auction: Auction{ + AuctionBid: AuctionBid, + AuctionLeft: AuctionLeft, + }, + }) + case "guildhalls": + GuildhallData = append(GuildhallData, House{ + Name: Name, + HouseID: HouseID, + Size: Size, + Rent: Rent, + IsRented: IsRented, + IsAuctioned: IsAuctioned, + Auction: Auction{ + AuctionBid: AuctionBid, + AuctionLeft: AuctionLeft, + }, + }) + } + } + }) + + } + + // + // Build the data-blob + jsonData := JSONData{ + Houses{ + World: world, + Town: town, + HouseList: HouseData, + GuildhallList: GuildhallData, + }, + Information{ + APIVersion: TibiadataAPIversion, + Timestamp: TibiadataDatetimeV3(""), + }, + } + + // return jsonData + TibiaDataAPIHandleSuccessResponse(c, "TibiaHousesOverviewV3", jsonData) +} diff --git a/src/webserver.go b/src/webserver.go index e47fe710..6228c99d 100644 --- a/src/webserver.go +++ b/src/webserver.go @@ -101,6 +101,10 @@ func runWebServer() { }) v3.GET("/highscores/world/:world/:category/:vocation", TibiaHighscoresV3) + // Tibia houses + v3.GET("/houses/world/:world/house/:houseid", TibiaHousesHouseV3) + v3.GET("/houses/world/:world/town/:town", TibiaHousesOverviewV3) + // Tibia killstatistics v3.GET("/killstatistics/world/:world", TibiaKillstatisticsV3) From 9fc29b130f361ba620975f91ccd8784c5a404b2e Mon Sep 17 00:00:00 2001 From: Tobias Lindberg Date: Wed, 5 Jan 2022 10:25:04 +0100 Subject: [PATCH 02/22] switching to switch for overview status column --- src/TibiaHousesOverviewV3.go | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/src/TibiaHousesOverviewV3.go b/src/TibiaHousesOverviewV3.go index 1814890b..879c79fa 100644 --- a/src/TibiaHousesOverviewV3.go +++ b/src/TibiaHousesOverviewV3.go @@ -16,10 +16,6 @@ func TibiaHousesOverviewV3(c *gin.Context) { world := c.Param("world") town := c.Param("town") - // Default value: - // town - Ab'Dendriel - // housetype - houses - // Child of House type Auction struct { AuctionBid int `json:"current_bid"` @@ -99,13 +95,13 @@ func TibiaHousesOverviewV3(c *gin.Context) { AuctionBid := 0 AuctionLeft := "" - if subma1[0][5] == "rented" { + switch { + case strings.Contains(subma1[0][5], "rented"): IsRented = true - } else if strings.Contains(subma1[0][5], "no bid yet") { + case strings.Contains(subma1[0][5], "no bid yet"): // nothing to set? - } else if strings.Contains(subma1[0][5], "auctioned") { + case strings.Contains(subma1[0][5], "auctioned"): IsAuctioned = true - regex1b := regexp.MustCompile(`auctioned..([0-9]+).gold..(.*).`) subma1b := regex1b.FindAllStringSubmatch(subma1[0][5], -1) AuctionBid = TibiadataStringToIntegerV3(subma1b[0][1]) From c382d840952df4da7bb6e0c37dab71117ae69179 Mon Sep 17 00:00:00 2001 From: Tobias Lindberg Date: Wed, 5 Jan 2022 16:38:48 +0100 Subject: [PATCH 03/22] adding support for house details view --- src/TibiaHousesHouseV3.go | 181 +++++++++++++++++++++++++++++++++++--- 1 file changed, 168 insertions(+), 13 deletions(-) diff --git a/src/TibiaHousesHouseV3.go b/src/TibiaHousesHouseV3.go index c391cad1..6464dc56 100644 --- a/src/TibiaHousesHouseV3.go +++ b/src/TibiaHousesHouseV3.go @@ -1,28 +1,183 @@ package main -import "github.com/gin-gonic/gin" +import ( + "log" + "regexp" + "strings" + + "github.com/PuerkitoBio/goquery" + "github.com/gin-gonic/gin" +) // TibiaHousesHouseV3 func func TibiaHousesHouseV3(c *gin.Context) { - /* - // getting params from URL - world := c.Param("world") - houseid := c.Param("houseid") - */ + // getting params from URL + world := c.Param("world") + houseid := c.Param("houseid") + + // Child of Status + type Rental struct { + Owner string `json:"owner"` + OwnerSex string `json:"owner_sex"` + PaidUntil string `json:"paid_until"` + MovingDate string `json:"moving_date"` + TransferReceiver string `json:"transfer_receiver"` + TransferPrice int `json:"transfer_price"` + TransferAccept bool `json:"transfer_accept"` + } + + // Child of Status + type Auction struct { + CurrentBid int `json:"current_bid"` + CurrentBidder string `json:"current_bidder"` + AuctionOngoing bool `json:"auction_ongoing"` + AuctionEnd string `json:"auction_end"` + } + + // Child of House + type Status struct { + IsRented bool `json:"is_rented"` + IsAuctioned bool `json:"is_auctioned"` + IsTransfering bool `json:"is_transfering"` + IsMoving bool `json:"is_moving"` + TheRental Rental `json:"rental"` + TheAuction Auction `json:"auction"` + Original string `json:"original"` + } + + // Child of JSONData + type House struct { + Houseid int `json:"houseid"` + World string `json:"world"` + Town string `json:"town"` + Name string `json:"name"` + Type string `json:"type"` + Beds int `json:"beds"` + Size int `json:"size"` + Rent int `json:"rent"` + Img string `json:"img"` + Status Status `json:"status"` + } // - // The base - type JSONData struct{} + // The base includes two levels: Houses and Information + type JSONData struct { + House House `json:"house"` + Information Information `json:"information"` + } + + // Creating empty vars + var HouseData House + + // Adding fix for First letter to be upper and rest lower + world = TibiadataStringWorldFormatToTitleV3(world) + + // Getting data with TibiadataHTMLDataCollectorV3 + BoxContentHTML := TibiadataHTMLDataCollectorV3("https://www.tibia.com/community/?subtopic=houses&page=view&world=" + TibiadataQueryEscapeStringV3(world) + "&houseid=" + TibiadataQueryEscapeStringV3(houseid)) + + // Loading HTML data into ReaderHTML for goquery with NewReader + ReaderHTML, err := goquery.NewDocumentFromReader(strings.NewReader(BoxContentHTML)) + if err != nil { + log.Fatal(err) + } + + // Running query over each div + HouseHTML, err := ReaderHTML.Find(".BoxContent table tr").First().Html() + + if err != nil { + log.Fatal(err) + } + + // Regex to get data for house + regex1 := regexp.MustCompile(`(.*)<\/b>.*This ([A-Za-z]+) can.*to ([0-9]+) beds..*([0-9]+) square.*([0-9]+)([k]+).gold<\/b>.*on ([A-Za-z]+)<\/b>.(.*)<\/td>`) + subma1 := regex1.FindAllStringSubmatch(HouseHTML, -1) + + if len(subma1) > 0 { + HouseData.Houseid = TibiadataStringToIntegerV3(houseid) + HouseData.World = subma1[0][8] + + HouseData.Name = TibiaDataSanitizeEscapedString(subma1[0][2]) + HouseData.Img = subma1[0][1] + HouseData.Type = subma1[0][3] + HouseData.Beds = TibiadataStringToIntegerV3(subma1[0][4]) + HouseData.Size = TibiadataStringToIntegerV3(subma1[0][5]) + HouseData.Rent = TibiadataStringToIntegerV3(subma1[0][6] + strings.Repeat("000", strings.Count(subma1[0][7], "k"))) + + HouseData.Status.Original = TibiaDataSanitizeEscapedString(RemoveHtmlTag(subma1[0][9])) + + switch { + case strings.Contains(HouseData.Status.Original, "has been rented by"): + // rented + + switch { + case strings.Contains(HouseData.Status.Original, " pass the "+HouseData.Type+" to "): + HouseData.Status.IsTransfering = true + // matching for this: and pass the to for gold + regex2 := regexp.MustCompile(`and (wants to|will) pass the (house|guildhall) to (.*) for ([0-9]+) gold`) + subma2 := regex2.FindAllStringSubmatch(HouseData.Status.Original, -1) + // storing values from regex + if subma2[0][1] == "will" { + HouseData.Status.TheRental.TransferAccept = true + } + HouseData.Status.TheRental.TransferReceiver = subma2[0][3] + HouseData.Status.TheRental.TransferPrice = TibiadataStringToIntegerV3(subma2[0][4]) + fallthrough + + case strings.Contains(HouseData.Status.Original, " will move out on "): + HouseData.Status.IsMoving = true + // matching for this: will move out on ( + regex2 := regexp.MustCompile(`(He|She) will move out on (.*?) \(`) + subma2 := regex2.FindAllStringSubmatch(HouseData.Status.Original, -1) + // storing values from regex + HouseData.Status.TheRental.MovingDate = TibiadataDatetimeV3(subma2[0][2]) + fallthrough + + default: + HouseData.Status.IsRented = true + // matching for this: The has been rented by . has paid the rent until . + regex2 := regexp.MustCompile(`The (house|guildhall) has been rented by (.*). (He|She) has paid.*until (.*?)\.`) + subma2 := regex2.FindAllStringSubmatch(HouseData.Status.Original, -1) + // storing values from regex + HouseData.Status.TheRental.Owner = subma2[0][2] + HouseData.Status.TheRental.PaidUntil = TibiadataDatetimeV3(subma2[0][4]) + switch subma2[0][3] { + case "She": + HouseData.Status.TheRental.OwnerSex = "female" + case "He": + HouseData.Status.TheRental.OwnerSex = "male" + } + } + + case strings.Contains(HouseData.Status.Original, "is currently being auctioned"): + // auctioned + HouseData.Status.IsAuctioned = true + + // check if bid is going on + if !strings.Contains(HouseData.Status.Original, "No bid has been submitted so far.") { + regex2 := regexp.MustCompile(`The (house|guildhall) is currently.*The auction (will end|has ended) at (.*)\. The.*is ([0-9]+) gold.*submitted by (.*)\.`) + subma2 := regex2.FindAllStringSubmatch(HouseData.Status.Original, -1) + // storing values from regex + HouseData.Status.TheAuction.AuctionEnd = TibiadataDatetimeV3(subma2[0][3]) + HouseData.Status.TheAuction.CurrentBid = TibiadataStringToIntegerV3(subma2[0][4]) + HouseData.Status.TheAuction.CurrentBidder = subma2[0][5] + if subma2[0][2] == "will end" { + HouseData.Status.TheAuction.AuctionOngoing = true + } + } + } - /* - // Adding fix for First letter to be upper and rest lower - world = TibiadataStringWorldFormatToTitleV3(world) - */ + } // // Build the data-blob - jsonData := JSONData{} + jsonData := JSONData{ + HouseData, + Information{ + APIVersion: TibiadataAPIversion, + Timestamp: TibiadataDatetimeV3(""), + }, + } // return jsonData TibiaDataAPIHandleSuccessResponse(c, "TibiaHousesHouseV3", jsonData) From e3f96daf34469414aaa8547c3d9a5eda8c16a7c4 Mon Sep 17 00:00:00 2001 From: Tobias Lindberg Date: Wed, 5 Jan 2022 16:46:54 +0100 Subject: [PATCH 04/22] renaming and reordering --- src/TibiaHousesHouseV3.go | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/src/TibiaHousesHouseV3.go b/src/TibiaHousesHouseV3.go index 6464dc56..c0cf55cb 100644 --- a/src/TibiaHousesHouseV3.go +++ b/src/TibiaHousesHouseV3.go @@ -37,12 +37,12 @@ func TibiaHousesHouseV3(c *gin.Context) { // Child of House type Status struct { - IsRented bool `json:"is_rented"` IsAuctioned bool `json:"is_auctioned"` - IsTransfering bool `json:"is_transfering"` + IsRented bool `json:"is_rented"` IsMoving bool `json:"is_moving"` - TheRental Rental `json:"rental"` - TheAuction Auction `json:"auction"` + IsTransfering bool `json:"is_transfering"` + Auction Auction `json:"auction"` + Rental Rental `json:"rental"` Original string `json:"original"` } @@ -118,10 +118,10 @@ func TibiaHousesHouseV3(c *gin.Context) { subma2 := regex2.FindAllStringSubmatch(HouseData.Status.Original, -1) // storing values from regex if subma2[0][1] == "will" { - HouseData.Status.TheRental.TransferAccept = true + HouseData.Status.Rental.TransferAccept = true } - HouseData.Status.TheRental.TransferReceiver = subma2[0][3] - HouseData.Status.TheRental.TransferPrice = TibiadataStringToIntegerV3(subma2[0][4]) + HouseData.Status.Rental.TransferReceiver = subma2[0][3] + HouseData.Status.Rental.TransferPrice = TibiadataStringToIntegerV3(subma2[0][4]) fallthrough case strings.Contains(HouseData.Status.Original, " will move out on "): @@ -130,7 +130,7 @@ func TibiaHousesHouseV3(c *gin.Context) { regex2 := regexp.MustCompile(`(He|She) will move out on (.*?) \(`) subma2 := regex2.FindAllStringSubmatch(HouseData.Status.Original, -1) // storing values from regex - HouseData.Status.TheRental.MovingDate = TibiadataDatetimeV3(subma2[0][2]) + HouseData.Status.Rental.MovingDate = TibiadataDatetimeV3(subma2[0][2]) fallthrough default: @@ -139,13 +139,13 @@ func TibiaHousesHouseV3(c *gin.Context) { regex2 := regexp.MustCompile(`The (house|guildhall) has been rented by (.*). (He|She) has paid.*until (.*?)\.`) subma2 := regex2.FindAllStringSubmatch(HouseData.Status.Original, -1) // storing values from regex - HouseData.Status.TheRental.Owner = subma2[0][2] - HouseData.Status.TheRental.PaidUntil = TibiadataDatetimeV3(subma2[0][4]) + HouseData.Status.Rental.Owner = subma2[0][2] + HouseData.Status.Rental.PaidUntil = TibiadataDatetimeV3(subma2[0][4]) switch subma2[0][3] { case "She": - HouseData.Status.TheRental.OwnerSex = "female" + HouseData.Status.Rental.OwnerSex = "female" case "He": - HouseData.Status.TheRental.OwnerSex = "male" + HouseData.Status.Rental.OwnerSex = "male" } } @@ -158,11 +158,11 @@ func TibiaHousesHouseV3(c *gin.Context) { regex2 := regexp.MustCompile(`The (house|guildhall) is currently.*The auction (will end|has ended) at (.*)\. The.*is ([0-9]+) gold.*submitted by (.*)\.`) subma2 := regex2.FindAllStringSubmatch(HouseData.Status.Original, -1) // storing values from regex - HouseData.Status.TheAuction.AuctionEnd = TibiadataDatetimeV3(subma2[0][3]) - HouseData.Status.TheAuction.CurrentBid = TibiadataStringToIntegerV3(subma2[0][4]) - HouseData.Status.TheAuction.CurrentBidder = subma2[0][5] + HouseData.Status.Auction.AuctionEnd = TibiadataDatetimeV3(subma2[0][3]) + HouseData.Status.Auction.CurrentBid = TibiadataStringToIntegerV3(subma2[0][4]) + HouseData.Status.Auction.CurrentBidder = subma2[0][5] if subma2[0][2] == "will end" { - HouseData.Status.TheAuction.AuctionOngoing = true + HouseData.Status.Auction.AuctionOngoing = true } } } From 541613cde195ed93ff54b4cf8b0d0f69b69f2edb Mon Sep 17 00:00:00 2001 From: Tobias Lindberg Date: Wed, 5 Jan 2022 17:03:15 +0100 Subject: [PATCH 05/22] TibiaDataConvertValuesWithK for pricing with k+ --- src/TibiaHousesHouseV3.go | 6 ++++-- src/TibiaHousesOverviewV3.go | 2 +- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/src/TibiaHousesHouseV3.go b/src/TibiaHousesHouseV3.go index c0cf55cb..3a24b9a7 100644 --- a/src/TibiaHousesHouseV3.go +++ b/src/TibiaHousesHouseV3.go @@ -90,19 +90,21 @@ func TibiaHousesHouseV3(c *gin.Context) { } // Regex to get data for house - regex1 := regexp.MustCompile(`(.*)<\/b>.*This ([A-Za-z]+) can.*to ([0-9]+) beds..*([0-9]+) square.*([0-9]+)([k]+).gold<\/b>.*on ([A-Za-z]+)<\/b>.(.*)<\/td>`) + regex1 := regexp.MustCompile(`(.*)<\/b>.*This (house|guildhall) can.*to ([0-9]+) beds..*([0-9]+) square.*([0-9]+)([k]+).gold<\/b>.*on ([A-Za-z]+)<\/b>.(.*)<\/td>`) subma1 := regex1.FindAllStringSubmatch(HouseHTML, -1) if len(subma1) > 0 { HouseData.Houseid = TibiadataStringToIntegerV3(houseid) HouseData.World = subma1[0][8] + log.Println(HouseHTML) + HouseData.Name = TibiaDataSanitizeEscapedString(subma1[0][2]) HouseData.Img = subma1[0][1] HouseData.Type = subma1[0][3] HouseData.Beds = TibiadataStringToIntegerV3(subma1[0][4]) HouseData.Size = TibiadataStringToIntegerV3(subma1[0][5]) - HouseData.Rent = TibiadataStringToIntegerV3(subma1[0][6] + strings.Repeat("000", strings.Count(subma1[0][7], "k"))) + HouseData.Rent = TibiaDataConvertValuesWithK(subma1[0][6] + subma1[0][7]) HouseData.Status.Original = TibiaDataSanitizeEscapedString(RemoveHtmlTag(subma1[0][9])) diff --git a/src/TibiaHousesOverviewV3.go b/src/TibiaHousesOverviewV3.go index 879c79fa..9fd96dab 100644 --- a/src/TibiaHousesOverviewV3.go +++ b/src/TibiaHousesOverviewV3.go @@ -115,7 +115,7 @@ func TibiaHousesOverviewV3(c *gin.Context) { // Size Size := TibiadataStringToIntegerV3(subma1[0][2]) // Rent - Rent := TibiadataStringToIntegerV3(subma1[0][3] + strings.Repeat("000", strings.Count(subma1[0][4], "k"))) + Rent := TibiaDataConvertValuesWithK(subma1[0][3] + subma1[0][4]) switch HouseType { case "houses": From aceffabf4f82a13ee8f054d1b528963e9903b9e6 Mon Sep 17 00:00:00 2001 From: Pedro Pessoa Date: Thu, 6 Jan 2022 18:33:31 -0300 Subject: [PATCH 06/22] fetch houses and guildhalls concurrently (#30) --- src/TibiaHousesOverviewV3.go | 253 ++++++++++++++++++----------------- 1 file changed, 128 insertions(+), 125 deletions(-) diff --git a/src/TibiaHousesOverviewV3.go b/src/TibiaHousesOverviewV3.go index 9fd96dab..910e6084 100644 --- a/src/TibiaHousesOverviewV3.go +++ b/src/TibiaHousesOverviewV3.go @@ -9,147 +9,77 @@ import ( "github.com/gin-gonic/gin" ) +// Child of House +type Auction struct { + AuctionBid int `json:"current_bid"` + AuctionLeft string `json:"time_left"` +} + +// Child of Houses +type House struct { + Name string `json:"name"` + HouseID int `json:"house_id"` + Size int `json:"size"` + Rent int `json:"rent"` + IsRented bool `json:"rented"` + IsAuctioned bool `json:"auctioned"` + Auction Auction `json:"auction"` +} + +// Child of JSONData +type Houses struct { + World string `json:"world"` + Town string `json:"town"` + HouseList []House `json:"house_list"` + GuildhallList []House `json:"guildhall_list"` +} + // TibiaHousesOverviewV3 func func TibiaHousesOverviewV3(c *gin.Context) { - // getting params from URL world := c.Param("world") town := c.Param("town") - // Child of House - type Auction struct { - AuctionBid int `json:"current_bid"` - AuctionLeft string `json:"time_left"` - } - - // Child of Houses - type House struct { - Name string `json:"name"` - HouseID int `json:"house_id"` - Size int `json:"size"` - Rent int `json:"rent"` - IsRented bool `json:"rented"` - IsAuctioned bool `json:"auctioned"` - Auction Auction `json:"auction"` - } - - // Child of JSONData - type Houses struct { - World string `json:"world"` - Town string `json:"town"` - HouseList []House `json:"house_list"` - GuildhallList []House `json:"guildhall_list"` - } + // Adding fix for First letter to be upper and rest lower + world = TibiadataStringWorldFormatToTitleV3(world) + town = TibiadataStringWorldFormatToTitleV3(town) - // // The base includes two levels: Houses and Information type JSONData struct { Houses Houses `json:"houses"` Information Information `json:"information"` } - // Creating empty vars - var HouseData, GuildhallData []House - HouseData = []House{} - GuildhallData = []House{} - - // Adding fix for First letter to be upper and rest lower - world = TibiadataStringWorldFormatToTitleV3(world) - town = TibiadataStringWorldFormatToTitleV3(town) - - // list of different house types - HouseTypes := []string{"houses", "guildhalls"} - // running over the HouseTypes array - for _, HouseType := range HouseTypes { - - // Getting data with TibiadataHTMLDataCollectorV3 - BoxContentHTML := TibiadataHTMLDataCollectorV3("https://www.tibia.com/community/?subtopic=houses&world=" + TibiadataQueryEscapeStringV3(world) + "&town=" + TibiadataQueryEscapeStringV3(town) + "&type=" + TibiadataQueryEscapeStringV3(HouseType)) - - // Loading HTML data into ReaderHTML for goquery with NewReader - ReaderHTML, err := goquery.NewDocumentFromReader(strings.NewReader(BoxContentHTML)) - if err != nil { - log.Fatal(err) + var ( + // Creating empty vars + HouseData, GuildhallData []House + + // Channels + done = make(chan struct{}) + housesChan = make(chan House) + guildhallsChan = make(chan House) + + baseURL = "https://www.tibia.com/community/?subtopic=houses&world=" + TibiadataQueryEscapeStringV3(world) + "&town=" + TibiadataQueryEscapeStringV3(town) + "&type=" + ) + + go houseFetcher(baseURL, "houses", done, housesChan) + go houseFetcher(baseURL, "guildhalls", done, guildhallsChan) + + for n := 2; n > 0; { + select { + case h := <-housesChan: + HouseData = append(HouseData, h) + case gh := <-guildhallsChan: + GuildhallData = append(GuildhallData, gh) + case <-done: + n-- } - - // Running query over each div - ReaderHTML.Find(".BoxContent table tr").Each(func(index int, s *goquery.Selection) { - - // Storing HTML into HousesDivHTML - HousesDivHTML, err := s.Html() - if err != nil { - log.Fatal(err) - } - - // Removing linebreaks from HTML - HousesDivHTML = TibiadataHTMLRemoveLinebreaksV3(HousesDivHTML) - - // Regex to get data for record values - regex1 := regexp.MustCompile(`(.*)<\/nobr><\/td>([0-9]+).sqm<\/nobr><\/td>([0-9]+)(k+).gold<\/nobr><\/td>(.*)<\/nobr><\/td>.*houseid" value="([0-9]+)"\/> 0 { - - // Default of vars - IsRented := false - IsAuctioned := false - AuctionBid := 0 - AuctionLeft := "" - - switch { - case strings.Contains(subma1[0][5], "rented"): - IsRented = true - case strings.Contains(subma1[0][5], "no bid yet"): - // nothing to set? - case strings.Contains(subma1[0][5], "auctioned"): - IsAuctioned = true - regex1b := regexp.MustCompile(`auctioned..([0-9]+).gold..(.*).`) - subma1b := regex1b.FindAllStringSubmatch(subma1[0][5], -1) - AuctionBid = TibiadataStringToIntegerV3(subma1b[0][1]) - AuctionLeft = subma1b[0][2] - } - - // Name - Name := TibiaDataSanitizeEscapedString(subma1[0][1]) - // HouseID - HouseID := TibiadataStringToIntegerV3(subma1[0][6]) - // Size - Size := TibiadataStringToIntegerV3(subma1[0][2]) - // Rent - Rent := TibiaDataConvertValuesWithK(subma1[0][3] + subma1[0][4]) - - switch HouseType { - case "houses": - HouseData = append(HouseData, House{ - Name: Name, - HouseID: HouseID, - Size: Size, - Rent: Rent, - IsRented: IsRented, - IsAuctioned: IsAuctioned, - Auction: Auction{ - AuctionBid: AuctionBid, - AuctionLeft: AuctionLeft, - }, - }) - case "guildhalls": - GuildhallData = append(GuildhallData, House{ - Name: Name, - HouseID: HouseID, - Size: Size, - Rent: Rent, - IsRented: IsRented, - IsAuctioned: IsAuctioned, - Auction: Auction{ - AuctionBid: AuctionBid, - AuctionLeft: AuctionLeft, - }, - }) - } - } - }) - } + close(done) + close(guildhallsChan) + close(housesChan) + // // Build the data-blob jsonData := JSONData{ @@ -168,3 +98,76 @@ func TibiaHousesOverviewV3(c *gin.Context) { // return jsonData TibiaDataAPIHandleSuccessResponse(c, "TibiaHousesOverviewV3", jsonData) } + +func houseFetcher(baseURL, houseType string, done chan struct{}, outputChan chan House) { + // Getting data with TibiadataHTMLDataCollectorV3 + BoxContentHTML := TibiadataHTMLDataCollectorV3(baseURL + TibiadataQueryEscapeStringV3(houseType)) + + // Loading HTML data into ReaderHTML for goquery with NewReader + ReaderHTML, err := goquery.NewDocumentFromReader(strings.NewReader(BoxContentHTML)) + if err != nil { + log.Fatal(err) + } + + ReaderHTML.Find(".BoxContent table tr").Each(func(index int, s *goquery.Selection) { + // Storing HTML into HousesDivHTML + HousesDivHTML, err := s.Html() + if err != nil { + log.Fatal(err) + } + + // Removing linebreaks from HTML + HousesDivHTML = TibiadataHTMLRemoveLinebreaksV3(HousesDivHTML) + + // Regex to get data for record values + regex1 := regexp.MustCompile(`(.*)<\/nobr><\/td>([0-9]+).sqm<\/nobr><\/td>([0-9]+)(k+).gold<\/nobr><\/td>(.*)<\/nobr><\/td>.*houseid" value="([0-9]+)"\/> 0 { + // Default vars + var ( + IsRented, IsAuctioned bool + AuctionBid int + AuctionLeft string + ) + + s := subma1[0][5] + switch { + case strings.Contains(s, "rented"): + IsRented = true + // case strings.Contains(s, "no bid yet"): + // nothing to set? + case strings.Contains(s, "auctioned"): + IsAuctioned = true + regex1b := regexp.MustCompile(`auctioned..([0-9]+).gold..(.*).`) + subma1b := regex1b.FindAllStringSubmatch(s, -1) + AuctionBid = TibiadataStringToIntegerV3(subma1b[0][1]) + AuctionLeft = subma1b[0][2] + } + + // Name + Name := TibiaDataSanitizeEscapedString(subma1[0][1]) + // HouseID + HouseID := TibiadataStringToIntegerV3(subma1[0][6]) + // Size + Size := TibiadataStringToIntegerV3(subma1[0][2]) + // Rent + Rent := TibiaDataConvertValuesWithK(subma1[0][3] + subma1[0][4]) + + outputChan <- House{ + Name: Name, + HouseID: HouseID, + Size: Size, + Rent: Rent, + IsRented: IsRented, + IsAuctioned: IsAuctioned, + Auction: Auction{ + AuctionBid: AuctionBid, + AuctionLeft: AuctionLeft, + }, + } + } + }) + + done <- struct{}{} +} From 01100880eaad43fc29cb43bd99848eb6d6cdd2da Mon Sep 17 00:00:00 2001 From: Tobias Lindberg Date: Thu, 6 Jan 2022 22:46:59 +0100 Subject: [PATCH 07/22] updating TibiadataHTMLDataCollectorV3 due to #31 --- src/TibiaHousesHouseV3.go | 5 ++--- src/TibiaHousesOverviewV3.go | 3 ++- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/TibiaHousesHouseV3.go b/src/TibiaHousesHouseV3.go index 3a24b9a7..cb220ee3 100644 --- a/src/TibiaHousesHouseV3.go +++ b/src/TibiaHousesHouseV3.go @@ -74,7 +74,8 @@ func TibiaHousesHouseV3(c *gin.Context) { world = TibiadataStringWorldFormatToTitleV3(world) // Getting data with TibiadataHTMLDataCollectorV3 - BoxContentHTML := TibiadataHTMLDataCollectorV3("https://www.tibia.com/community/?subtopic=houses&page=view&world=" + TibiadataQueryEscapeStringV3(world) + "&houseid=" + TibiadataQueryEscapeStringV3(houseid)) + TibiadataRequest.URL = "https://www.tibia.com/community/?subtopic=houses&page=view&world=" + TibiadataQueryEscapeStringV3(world) + "&houseid=" + TibiadataQueryEscapeStringV3(houseid) + BoxContentHTML := TibiadataHTMLDataCollectorV3(TibiadataRequest) // Loading HTML data into ReaderHTML for goquery with NewReader ReaderHTML, err := goquery.NewDocumentFromReader(strings.NewReader(BoxContentHTML)) @@ -97,8 +98,6 @@ func TibiaHousesHouseV3(c *gin.Context) { HouseData.Houseid = TibiadataStringToIntegerV3(houseid) HouseData.World = subma1[0][8] - log.Println(HouseHTML) - HouseData.Name = TibiaDataSanitizeEscapedString(subma1[0][2]) HouseData.Img = subma1[0][1] HouseData.Type = subma1[0][3] diff --git a/src/TibiaHousesOverviewV3.go b/src/TibiaHousesOverviewV3.go index 910e6084..1a0d364b 100644 --- a/src/TibiaHousesOverviewV3.go +++ b/src/TibiaHousesOverviewV3.go @@ -101,7 +101,8 @@ func TibiaHousesOverviewV3(c *gin.Context) { func houseFetcher(baseURL, houseType string, done chan struct{}, outputChan chan House) { // Getting data with TibiadataHTMLDataCollectorV3 - BoxContentHTML := TibiadataHTMLDataCollectorV3(baseURL + TibiadataQueryEscapeStringV3(houseType)) + TibiadataRequest.URL = baseURL + TibiadataQueryEscapeStringV3(houseType) + BoxContentHTML := TibiadataHTMLDataCollectorV3(TibiadataRequest) // Loading HTML data into ReaderHTML for goquery with NewReader ReaderHTML, err := goquery.NewDocumentFromReader(strings.NewReader(BoxContentHTML)) From 14cf8c05a5ea4c33a2ca723fabe0a1d3f4dbc1f3 Mon Sep 17 00:00:00 2001 From: Tobias Lindberg Date: Fri, 7 Jan 2022 13:41:51 +0100 Subject: [PATCH 08/22] updating due to #36 --- src/TibiaHousesHouseV3.go | 9 ++++++++- src/TibiaHousesOverviewV3.go | 9 ++++++++- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/src/TibiaHousesHouseV3.go b/src/TibiaHousesHouseV3.go index cb220ee3..670b7d01 100644 --- a/src/TibiaHousesHouseV3.go +++ b/src/TibiaHousesHouseV3.go @@ -2,6 +2,7 @@ package main import ( "log" + "net/http" "regexp" "strings" @@ -75,7 +76,13 @@ func TibiaHousesHouseV3(c *gin.Context) { // Getting data with TibiadataHTMLDataCollectorV3 TibiadataRequest.URL = "https://www.tibia.com/community/?subtopic=houses&page=view&world=" + TibiadataQueryEscapeStringV3(world) + "&houseid=" + TibiadataQueryEscapeStringV3(houseid) - BoxContentHTML := TibiadataHTMLDataCollectorV3(TibiadataRequest) + BoxContentHTML, err := TibiadataHTMLDataCollectorV3(TibiadataRequest) + + // return error (e.g. for maintenance mode) + if err != nil { + TibiaDataAPIHandleOtherResponse(c, http.StatusBadGateway, "TibiaHousesHouseV3", gin.H{"error": err.Error()}) + return + } // Loading HTML data into ReaderHTML for goquery with NewReader ReaderHTML, err := goquery.NewDocumentFromReader(strings.NewReader(BoxContentHTML)) diff --git a/src/TibiaHousesOverviewV3.go b/src/TibiaHousesOverviewV3.go index 1a0d364b..0f87a8da 100644 --- a/src/TibiaHousesOverviewV3.go +++ b/src/TibiaHousesOverviewV3.go @@ -2,6 +2,7 @@ package main import ( "log" + "net/http" "regexp" "strings" @@ -102,7 +103,13 @@ func TibiaHousesOverviewV3(c *gin.Context) { func houseFetcher(baseURL, houseType string, done chan struct{}, outputChan chan House) { // Getting data with TibiadataHTMLDataCollectorV3 TibiadataRequest.URL = baseURL + TibiadataQueryEscapeStringV3(houseType) - BoxContentHTML := TibiadataHTMLDataCollectorV3(TibiadataRequest) + BoxContentHTML, err := TibiadataHTMLDataCollectorV3(TibiadataRequest) + + // return error (e.g. for maintenance mode) + if err != nil { + TibiaDataAPIHandleOtherResponse(c, http.StatusBadGateway, "TibiaHousesOverviewV3", gin.H{"error": err.Error()}) + return + } // Loading HTML data into ReaderHTML for goquery with NewReader ReaderHTML, err := goquery.NewDocumentFromReader(strings.NewReader(BoxContentHTML)) From 4ed769c40c94ce4a112d2540ba65221a012d8579 Mon Sep 17 00:00:00 2001 From: Tobias Lindberg Date: Fri, 7 Jan 2022 21:24:12 +0100 Subject: [PATCH 09/22] passing gin.Context to houseFetcher --- src/TibiaHousesOverviewV3.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/TibiaHousesOverviewV3.go b/src/TibiaHousesOverviewV3.go index 0f87a8da..6a85085c 100644 --- a/src/TibiaHousesOverviewV3.go +++ b/src/TibiaHousesOverviewV3.go @@ -63,8 +63,8 @@ func TibiaHousesOverviewV3(c *gin.Context) { baseURL = "https://www.tibia.com/community/?subtopic=houses&world=" + TibiadataQueryEscapeStringV3(world) + "&town=" + TibiadataQueryEscapeStringV3(town) + "&type=" ) - go houseFetcher(baseURL, "houses", done, housesChan) - go houseFetcher(baseURL, "guildhalls", done, guildhallsChan) + go houseFetcher(c, baseURL, "houses", done, housesChan) + go houseFetcher(c, baseURL, "guildhalls", done, guildhallsChan) for n := 2; n > 0; { select { @@ -100,7 +100,7 @@ func TibiaHousesOverviewV3(c *gin.Context) { TibiaDataAPIHandleSuccessResponse(c, "TibiaHousesOverviewV3", jsonData) } -func houseFetcher(baseURL, houseType string, done chan struct{}, outputChan chan House) { +func houseFetcher(c *gin.Context, baseURL, houseType string, done chan struct{}, outputChan chan House) { // Getting data with TibiadataHTMLDataCollectorV3 TibiadataRequest.URL = baseURL + TibiadataQueryEscapeStringV3(houseType) BoxContentHTML, err := TibiadataHTMLDataCollectorV3(TibiadataRequest) From 6fafd08677687290c5a6aea73aede5c4b5465dc5 Mon Sep 17 00:00:00 2001 From: Pedro Pessoa Date: Fri, 7 Jan 2022 21:28:49 -0300 Subject: [PATCH 10/22] properly handle errors on houseFetcher (#38) --- src/TibiaHousesOverviewV3.go | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/src/TibiaHousesOverviewV3.go b/src/TibiaHousesOverviewV3.go index 6a85085c..a238e3b1 100644 --- a/src/TibiaHousesOverviewV3.go +++ b/src/TibiaHousesOverviewV3.go @@ -54,17 +54,19 @@ func TibiaHousesOverviewV3(c *gin.Context) { var ( // Creating empty vars HouseData, GuildhallData []House + Errors []error // Channels done = make(chan struct{}) housesChan = make(chan House) guildhallsChan = make(chan House) + errorsChan = make(chan error) baseURL = "https://www.tibia.com/community/?subtopic=houses&world=" + TibiadataQueryEscapeStringV3(world) + "&town=" + TibiadataQueryEscapeStringV3(town) + "&type=" ) - go houseFetcher(c, baseURL, "houses", done, housesChan) - go houseFetcher(c, baseURL, "guildhalls", done, guildhallsChan) + go houseFetcher(baseURL, "houses", done, housesChan, errorsChan) + go houseFetcher(baseURL, "guildhalls", done, guildhallsChan, errorsChan) for n := 2; n > 0; { select { @@ -72,6 +74,8 @@ func TibiaHousesOverviewV3(c *gin.Context) { HouseData = append(HouseData, h) case gh := <-guildhallsChan: GuildhallData = append(GuildhallData, gh) + case err := <-errorsChan: + Errors = append(Errors, err) case <-done: n-- } @@ -80,8 +84,13 @@ func TibiaHousesOverviewV3(c *gin.Context) { close(done) close(guildhallsChan) close(housesChan) + close(errorsChan) + + if len(Errors) > 0 { + TibiaDataAPIHandleOtherResponse(c, http.StatusBadGateway, "TibiaHousesOverviewV3", gin.H{"error": Errors[0].Error()}) + return + } - // // Build the data-blob jsonData := JSONData{ Houses{ @@ -100,14 +109,17 @@ func TibiaHousesOverviewV3(c *gin.Context) { TibiaDataAPIHandleSuccessResponse(c, "TibiaHousesOverviewV3", jsonData) } -func houseFetcher(c *gin.Context, baseURL, houseType string, done chan struct{}, outputChan chan House) { +func houseFetcher(baseURL, houseType string, done chan struct{}, outputChan chan House, errorsChan chan error) { + defer func() { + done <- struct{}{} + }() + // Getting data with TibiadataHTMLDataCollectorV3 TibiadataRequest.URL = baseURL + TibiadataQueryEscapeStringV3(houseType) BoxContentHTML, err := TibiadataHTMLDataCollectorV3(TibiadataRequest) - - // return error (e.g. for maintenance mode) if err != nil { - TibiaDataAPIHandleOtherResponse(c, http.StatusBadGateway, "TibiaHousesOverviewV3", gin.H{"error": err.Error()}) + // return error (e.g. for maintenance mode) + errorsChan <- err return } @@ -176,6 +188,4 @@ func houseFetcher(c *gin.Context, baseURL, houseType string, done chan struct{}, } } }) - - done <- struct{}{} } From 40500c0e968a38adfc860fe664bd7c98541ac876 Mon Sep 17 00:00:00 2001 From: Tobias Lindberg Date: Tue, 11 Jan 2022 14:26:06 +0100 Subject: [PATCH 11/22] fixing TibiaDataConvertValuesWithK function --- src/TibiaDataUtils.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/TibiaDataUtils.go b/src/TibiaDataUtils.go index dda9e3d7..663ee0b0 100644 --- a/src/TibiaDataUtils.go +++ b/src/TibiaDataUtils.go @@ -222,6 +222,11 @@ func getEnvAsInt(name string, defaultVal int) int { } */ +// TibiaDataConvertValuesWithK func - convert price strings that contain k, kk or more to 3x0 +func TibiaDataConvertValuesWithK(data string) int { + return TibiadataStringToIntegerV3(strings.ReplaceAll(data, "k", "") + strings.Repeat("000", strings.Count(data, "k"))) +} + // TibiaDataVocationValidator func - return valid vocation string and vocation id func TibiaDataVocationValidator(vocation string) (string, string) { // defining return vars From 771292371050ba0c9d4a7d0dc0ead4c331bddb14 Mon Sep 17 00:00:00 2001 From: Tobias Lindberg Date: Tue, 11 Jan 2022 23:46:42 +0100 Subject: [PATCH 12/22] renaming Houses to OverviewHouses --- src/TibiaHousesOverviewV3.go | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/TibiaHousesOverviewV3.go b/src/TibiaHousesOverviewV3.go index a238e3b1..bb94b1e3 100644 --- a/src/TibiaHousesOverviewV3.go +++ b/src/TibiaHousesOverviewV3.go @@ -16,7 +16,7 @@ type Auction struct { AuctionLeft string `json:"time_left"` } -// Child of Houses +// Child of OverviewHouses type House struct { Name string `json:"name"` HouseID int `json:"house_id"` @@ -28,7 +28,7 @@ type House struct { } // Child of JSONData -type Houses struct { +type OverviewHouses struct { World string `json:"world"` Town string `json:"town"` HouseList []House `json:"house_list"` @@ -45,10 +45,10 @@ func TibiaHousesOverviewV3(c *gin.Context) { world = TibiadataStringWorldFormatToTitleV3(world) town = TibiadataStringWorldFormatToTitleV3(town) - // The base includes two levels: Houses and Information + // The base includes two levels: OverviewHouses and Information type JSONData struct { - Houses Houses `json:"houses"` - Information Information `json:"information"` + Houses OverviewHouses `json:"houses"` + Information Information `json:"information"` } var ( @@ -93,7 +93,7 @@ func TibiaHousesOverviewV3(c *gin.Context) { // Build the data-blob jsonData := JSONData{ - Houses{ + OverviewHouses{ World: world, Town: town, HouseList: HouseData, From 811833afcc15ab937509c147e23d2f7b1e72790c Mon Sep 17 00:00:00 2001 From: Tobias Lindberg Date: Thu, 13 Jan 2022 00:35:41 +0100 Subject: [PATCH 13/22] switching from goroutine to for --- src/TibiaHousesOverviewV3.go | 178 +++++++++++++---------------------- 1 file changed, 68 insertions(+), 110 deletions(-) diff --git a/src/TibiaHousesOverviewV3.go b/src/TibiaHousesOverviewV3.go index bb94b1e3..d8a10967 100644 --- a/src/TibiaHousesOverviewV3.go +++ b/src/TibiaHousesOverviewV3.go @@ -54,41 +54,80 @@ func TibiaHousesOverviewV3(c *gin.Context) { var ( // Creating empty vars HouseData, GuildhallData []House - Errors []error + ) - // Channels - done = make(chan struct{}) - housesChan = make(chan House) - guildhallsChan = make(chan House) - errorsChan = make(chan error) + // list of different fansite types + HouseTypes := []string{"houses", "guildhalls"} + // running over the FansiteTypes array + for _, HouseType := range HouseTypes { - baseURL = "https://www.tibia.com/community/?subtopic=houses&world=" + TibiadataQueryEscapeStringV3(world) + "&town=" + TibiadataQueryEscapeStringV3(town) + "&type=" - ) + // Getting data with TibiadataHTMLDataCollectorV3 + TibiadataRequest.URL = "https://www.tibia.com/community/?subtopic=houses&world=" + TibiadataQueryEscapeStringV3(world) + "&town=" + TibiadataQueryEscapeStringV3(town) + "&type=" + TibiadataQueryEscapeStringV3(HouseType) + BoxContentHTML, err := TibiadataHTMLDataCollectorV3(TibiadataRequest) - go houseFetcher(baseURL, "houses", done, housesChan, errorsChan) - go houseFetcher(baseURL, "guildhalls", done, guildhallsChan, errorsChan) - - for n := 2; n > 0; { - select { - case h := <-housesChan: - HouseData = append(HouseData, h) - case gh := <-guildhallsChan: - GuildhallData = append(GuildhallData, gh) - case err := <-errorsChan: - Errors = append(Errors, err) - case <-done: - n-- + // return error (e.g. for maintenance mode) + if err != nil { + TibiaDataAPIHandleOtherResponse(c, http.StatusBadGateway, "TibiaHousesOverviewV3", gin.H{"error": err.Error()}) + return } - } - close(done) - close(guildhallsChan) - close(housesChan) - close(errorsChan) + // Loading HTML data into ReaderHTML for goquery with NewReader + ReaderHTML, err := goquery.NewDocumentFromReader(strings.NewReader(BoxContentHTML)) + if err != nil { + log.Fatal(err) + } + + ReaderHTML.Find(".TableContentContainer .TableContent tr").Each(func(index int, s *goquery.Selection) { + house := House{} + + // Storing HTML into HousesDivHTML + HousesDivHTML, err := s.Html() + if err != nil { + log.Fatal(err) + } + + // Removing linebreaks from HTML + HousesDivHTML = TibiadataHTMLRemoveLinebreaksV3(HousesDivHTML) + HousesDivHTML = TibiaDataSanitizeNbspSpaceString(HousesDivHTML) + + // Regex to get data for record values + regex1 := regexp.MustCompile(`(.*)<\/nobr><\/td>([0-9]+).sqm<\/nobr><\/td>([0-9]+)(k+).gold<\/nobr><\/td>(.*)<\/nobr><\/td>.*houseid" value="([0-9]+)"\/> 0 { + // House details + house.Name = TibiaDataSanitizeEscapedString(subma1[0][1]) + house.HouseID = TibiadataStringToIntegerV3(subma1[0][6]) + house.Size = TibiadataStringToIntegerV3(subma1[0][2]) + house.Rent = TibiaDataConvertValuesWithK(subma1[0][3] + subma1[0][4]) + + // Auction details + s := subma1[0][5] + switch { + case strings.Contains(s, "rented"): + house.IsRented = true + case strings.Contains(s, "auctioned (no bid yet)"): + house.IsAuctioned = true + case strings.Contains(s, "auctioned"): + house.IsAuctioned = true + regex1b := regexp.MustCompile(`auctioned.\(([0-9]+).gold;.(.*).left\)`) + subma1b := regex1b.FindAllStringSubmatch(s, -1) + house.Auction.AuctionBid = TibiadataStringToIntegerV3(subma1b[0][1]) + house.Auction.AuctionLeft = subma1b[0][2] + } + + // append house to list houses/guildhalls + switch HouseType { + case "houses": + HouseData = append(HouseData, house) + case "guildhalls": + GuildhallData = append(GuildhallData, house) + } + + } + + }) - if len(Errors) > 0 { - TibiaDataAPIHandleOtherResponse(c, http.StatusBadGateway, "TibiaHousesOverviewV3", gin.H{"error": Errors[0].Error()}) - return } // Build the data-blob @@ -108,84 +147,3 @@ func TibiaHousesOverviewV3(c *gin.Context) { // return jsonData TibiaDataAPIHandleSuccessResponse(c, "TibiaHousesOverviewV3", jsonData) } - -func houseFetcher(baseURL, houseType string, done chan struct{}, outputChan chan House, errorsChan chan error) { - defer func() { - done <- struct{}{} - }() - - // Getting data with TibiadataHTMLDataCollectorV3 - TibiadataRequest.URL = baseURL + TibiadataQueryEscapeStringV3(houseType) - BoxContentHTML, err := TibiadataHTMLDataCollectorV3(TibiadataRequest) - if err != nil { - // return error (e.g. for maintenance mode) - errorsChan <- err - return - } - - // Loading HTML data into ReaderHTML for goquery with NewReader - ReaderHTML, err := goquery.NewDocumentFromReader(strings.NewReader(BoxContentHTML)) - if err != nil { - log.Fatal(err) - } - - ReaderHTML.Find(".BoxContent table tr").Each(func(index int, s *goquery.Selection) { - // Storing HTML into HousesDivHTML - HousesDivHTML, err := s.Html() - if err != nil { - log.Fatal(err) - } - - // Removing linebreaks from HTML - HousesDivHTML = TibiadataHTMLRemoveLinebreaksV3(HousesDivHTML) - - // Regex to get data for record values - regex1 := regexp.MustCompile(`(.*)<\/nobr><\/td>([0-9]+).sqm<\/nobr><\/td>([0-9]+)(k+).gold<\/nobr><\/td>(.*)<\/nobr><\/td>.*houseid" value="([0-9]+)"\/> 0 { - // Default vars - var ( - IsRented, IsAuctioned bool - AuctionBid int - AuctionLeft string - ) - - s := subma1[0][5] - switch { - case strings.Contains(s, "rented"): - IsRented = true - // case strings.Contains(s, "no bid yet"): - // nothing to set? - case strings.Contains(s, "auctioned"): - IsAuctioned = true - regex1b := regexp.MustCompile(`auctioned..([0-9]+).gold..(.*).`) - subma1b := regex1b.FindAllStringSubmatch(s, -1) - AuctionBid = TibiadataStringToIntegerV3(subma1b[0][1]) - AuctionLeft = subma1b[0][2] - } - - // Name - Name := TibiaDataSanitizeEscapedString(subma1[0][1]) - // HouseID - HouseID := TibiadataStringToIntegerV3(subma1[0][6]) - // Size - Size := TibiadataStringToIntegerV3(subma1[0][2]) - // Rent - Rent := TibiaDataConvertValuesWithK(subma1[0][3] + subma1[0][4]) - - outputChan <- House{ - Name: Name, - HouseID: HouseID, - Size: Size, - Rent: Rent, - IsRented: IsRented, - IsAuctioned: IsAuctioned, - Auction: Auction{ - AuctionBid: AuctionBid, - AuctionLeft: AuctionLeft, - }, - } - } - }) -} From 4bb076f5d27950a4734b88d77470621c7ca7e4a9 Mon Sep 17 00:00:00 2001 From: Tobias Lindberg Date: Thu, 13 Jan 2022 00:37:06 +0100 Subject: [PATCH 14/22] moving structs outside function for overview --- src/TibiaHousesOverviewV3.go | 52 ++++++++++++++++++------------------ 1 file changed, 26 insertions(+), 26 deletions(-) diff --git a/src/TibiaHousesOverviewV3.go b/src/TibiaHousesOverviewV3.go index d8a10967..5553aa27 100644 --- a/src/TibiaHousesOverviewV3.go +++ b/src/TibiaHousesOverviewV3.go @@ -11,28 +11,34 @@ import ( ) // Child of House -type Auction struct { +type HousesAction struct { AuctionBid int `json:"current_bid"` AuctionLeft string `json:"time_left"` } -// Child of OverviewHouses -type House struct { - Name string `json:"name"` - HouseID int `json:"house_id"` - Size int `json:"size"` - Rent int `json:"rent"` - IsRented bool `json:"rented"` - IsAuctioned bool `json:"auctioned"` - Auction Auction `json:"auction"` +// Child of HousesHouses +type HousesHouse struct { + Name string `json:"name"` + HouseID int `json:"house_id"` + Size int `json:"size"` + Rent int `json:"rent"` + IsRented bool `json:"rented"` + IsAuctioned bool `json:"auctioned"` + Auction HousesAction `json:"auction"` } // Child of JSONData -type OverviewHouses struct { - World string `json:"world"` - Town string `json:"town"` - HouseList []House `json:"house_list"` - GuildhallList []House `json:"guildhall_list"` +type HousesHouses struct { + World string `json:"world"` + Town string `json:"town"` + HouseList []HousesHouse `json:"house_list"` + GuildhallList []HousesHouse `json:"guildhall_list"` +} + +// The base includes two levels: HousesHouses and Information +type HousesOverviewResponse struct { + Houses HousesHouses `json:"houses"` + Information Information `json:"information"` } // TibiaHousesOverviewV3 func @@ -45,15 +51,9 @@ func TibiaHousesOverviewV3(c *gin.Context) { world = TibiadataStringWorldFormatToTitleV3(world) town = TibiadataStringWorldFormatToTitleV3(town) - // The base includes two levels: OverviewHouses and Information - type JSONData struct { - Houses OverviewHouses `json:"houses"` - Information Information `json:"information"` - } - var ( // Creating empty vars - HouseData, GuildhallData []House + HouseData, GuildhallData []HousesHouse ) // list of different fansite types @@ -78,7 +78,7 @@ func TibiaHousesOverviewV3(c *gin.Context) { } ReaderHTML.Find(".TableContentContainer .TableContent tr").Each(func(index int, s *goquery.Selection) { - house := House{} + house := HousesHouse{} // Storing HTML into HousesDivHTML HousesDivHTML, err := s.Html() @@ -101,7 +101,7 @@ func TibiaHousesOverviewV3(c *gin.Context) { house.Size = TibiadataStringToIntegerV3(subma1[0][2]) house.Rent = TibiaDataConvertValuesWithK(subma1[0][3] + subma1[0][4]) - // Auction details + // HousesAction details s := subma1[0][5] switch { case strings.Contains(s, "rented"): @@ -131,8 +131,8 @@ func TibiaHousesOverviewV3(c *gin.Context) { } // Build the data-blob - jsonData := JSONData{ - OverviewHouses{ + jsonData := HousesOverviewResponse{ + HousesHouses{ World: world, Town: town, HouseList: HouseData, From 46fc5361680bf4678c592b349da1a6402b6b26ca Mon Sep 17 00:00:00 2001 From: Tobias Lindberg Date: Thu, 13 Jan 2022 01:03:44 +0100 Subject: [PATCH 15/22] moving structs outside function for house --- src/TibiaHousesHouseV3.go | 104 +++++++++++++++++++------------------- 1 file changed, 52 insertions(+), 52 deletions(-) diff --git a/src/TibiaHousesHouseV3.go b/src/TibiaHousesHouseV3.go index 670b7d01..7589ae1f 100644 --- a/src/TibiaHousesHouseV3.go +++ b/src/TibiaHousesHouseV3.go @@ -10,63 +10,63 @@ import ( "github.com/gin-gonic/gin" ) -// TibiaHousesHouseV3 func -func TibiaHousesHouseV3(c *gin.Context) { +// Child of Status +type HouseRental struct { + Owner string `json:"owner"` + OwnerSex string `json:"owner_sex"` + PaidUntil string `json:"paid_until"` + MovingDate string `json:"moving_date"` + TransferReceiver string `json:"transfer_receiver"` + TransferPrice int `json:"transfer_price"` + TransferAccept bool `json:"transfer_accept"` +} - // getting params from URL - world := c.Param("world") - houseid := c.Param("houseid") +// Child of Status +type HouseAuction struct { + CurrentBid int `json:"current_bid"` + CurrentBidder string `json:"current_bidder"` + AuctionOngoing bool `json:"auction_ongoing"` + AuctionEnd string `json:"auction_end"` +} - // Child of Status - type Rental struct { - Owner string `json:"owner"` - OwnerSex string `json:"owner_sex"` - PaidUntil string `json:"paid_until"` - MovingDate string `json:"moving_date"` - TransferReceiver string `json:"transfer_receiver"` - TransferPrice int `json:"transfer_price"` - TransferAccept bool `json:"transfer_accept"` - } +// Child of House +type HouseStatus struct { + IsAuctioned bool `json:"is_auctioned"` + IsRented bool `json:"is_rented"` + IsMoving bool `json:"is_moving"` + IsTransfering bool `json:"is_transfering"` + Auction HouseAuction `json:"auction"` + Rental HouseRental `json:"rental"` + Original string `json:"original"` +} - // Child of Status - type Auction struct { - CurrentBid int `json:"current_bid"` - CurrentBidder string `json:"current_bidder"` - AuctionOngoing bool `json:"auction_ongoing"` - AuctionEnd string `json:"auction_end"` - } +// Child of JSONData +type House struct { + Houseid int `json:"houseid"` + World string `json:"world"` + Town string `json:"town"` + Name string `json:"name"` + Type string `json:"type"` + Beds int `json:"beds"` + Size int `json:"size"` + Rent int `json:"rent"` + Img string `json:"img"` + Status HouseStatus `json:"status"` +} - // Child of House - type Status struct { - IsAuctioned bool `json:"is_auctioned"` - IsRented bool `json:"is_rented"` - IsMoving bool `json:"is_moving"` - IsTransfering bool `json:"is_transfering"` - Auction Auction `json:"auction"` - Rental Rental `json:"rental"` - Original string `json:"original"` - } +// +// The base includes two levels: Houses and Information +type HouseResponse struct { + House House `json:"house"` + Information Information `json:"information"` +} - // Child of JSONData - type House struct { - Houseid int `json:"houseid"` - World string `json:"world"` - Town string `json:"town"` - Name string `json:"name"` - Type string `json:"type"` - Beds int `json:"beds"` - Size int `json:"size"` - Rent int `json:"rent"` - Img string `json:"img"` - Status Status `json:"status"` - } +// TibiaHousesHouseV3 func +func TibiaHousesHouseV3(c *gin.Context) { - // - // The base includes two levels: Houses and Information - type JSONData struct { - House House `json:"house"` - Information Information `json:"information"` - } + // getting params from URL + world := c.Param("world") + houseid := c.Param("houseid") // Creating empty vars var HouseData House @@ -179,7 +179,7 @@ func TibiaHousesHouseV3(c *gin.Context) { // // Build the data-blob - jsonData := JSONData{ + jsonData := HouseResponse{ HouseData, Information{ APIVersion: TibiadataAPIversion, From 653bf50e2a8b74e086f440c414a5923bd20c6d39 Mon Sep 17 00:00:00 2001 From: Tobias Lindberg Date: Thu, 13 Jan 2022 23:03:39 +0100 Subject: [PATCH 16/22] adding ignore for mapping json because it's supposed to be included in build process --- .gitignore | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.gitignore b/.gitignore index 66fd13c9..ba5057f3 100644 --- a/.gitignore +++ b/.gitignore @@ -13,3 +13,6 @@ # Dependency directories (remove the comment below to include it) # vendor/ + +# TibiaData related files that should be ignored +houses_mapping.json From bba472295ccd4c05ddab23a00ceb6f68a2c26ec8 Mon Sep 17 00:00:00 2001 From: Tobias Lindberg Date: Thu, 13 Jan 2022 23:18:47 +0100 Subject: [PATCH 17/22] adding HouseMapping file adding run in TibiaDataInitializer --- src/HousesMapping.go | 42 ++++++++++++++++++++++++++++++++++++++++++ src/main.go | 3 +++ 2 files changed, 45 insertions(+) create mode 100644 src/HousesMapping.go diff --git a/src/HousesMapping.go b/src/HousesMapping.go new file mode 100644 index 00000000..e8426908 --- /dev/null +++ b/src/HousesMapping.go @@ -0,0 +1,42 @@ +package main + +import ( + "encoding/json" + "io/ioutil" +) + +var ( + TibiadataHousesMapping HousesMapping +) + +type AssetsHouse struct { + HouseID int `json:"house_id"` + Town string `json:"town"` + HouseType string `json:"type"` +} +type HousesMapping struct { + Houses []AssetsHouse `json:"houses"` +} + +// TibiaDataHousesMappingInitiator func - used to load data from local JSON file +func TibiaDataHousesMappingInitiator() { + // load content from file into variable file + file, _ := ioutil.ReadFile("houses_mapping.json") + + // loading json and mapping it into the data variable + data := HousesMapping{} + _ = json.Unmarshal([]byte(file), &data) + + // storing data so it's accessible from other places + TibiadataHousesMapping = data +} + +// TibiaDataHousesMapResolver func - used to return both town and type +func TibiaDataHousesMapResolver(houseid int) (town string, housetype string) { + for _, value := range TibiadataHousesMapping.Houses { + if houseid == value.HouseID { + return value.Town, value.HouseType + } + } + return "", "" +} diff --git a/src/main.go b/src/main.go index bbd9335e..42bde83b 100644 --- a/src/main.go +++ b/src/main.go @@ -70,4 +70,7 @@ func TibiaDataInitializer() { } log.Printf("[info] TibiaData API proxy: %s", TibiadataProxyDomain) + + // initializing houses mappings + TibiaDataHousesMappingInitiator() } From c98ccc10b698f6ab579911247e1d1425a418c637 Mon Sep 17 00:00:00 2001 From: Tobias Lindberg Date: Thu, 13 Jan 2022 23:19:31 +0100 Subject: [PATCH 18/22] adding houseid resolver in houseshouse file --- src/TibiaHousesHouseV3.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/TibiaHousesHouseV3.go b/src/TibiaHousesHouseV3.go index 7589ae1f..70e6c570 100644 --- a/src/TibiaHousesHouseV3.go +++ b/src/TibiaHousesHouseV3.go @@ -105,9 +105,10 @@ func TibiaHousesHouseV3(c *gin.Context) { HouseData.Houseid = TibiadataStringToIntegerV3(houseid) HouseData.World = subma1[0][8] + HouseData.Town, HouseData.Type = TibiaDataHousesMapResolver(HouseData.Houseid) + HouseData.Name = TibiaDataSanitizeEscapedString(subma1[0][2]) HouseData.Img = subma1[0][1] - HouseData.Type = subma1[0][3] HouseData.Beds = TibiadataStringToIntegerV3(subma1[0][4]) HouseData.Size = TibiadataStringToIntegerV3(subma1[0][5]) HouseData.Rent = TibiaDataConvertValuesWithK(subma1[0][6] + subma1[0][7]) From c7109abb74546c51770df9ba6b6bd35e54a0fda3 Mon Sep 17 00:00:00 2001 From: Tobias Lindberg Date: Thu, 13 Jan 2022 23:35:36 +0100 Subject: [PATCH 19/22] moving init before start of webserver --- src/main.go | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/main.go b/src/main.go index 42bde83b..c820579c 100644 --- a/src/main.go +++ b/src/main.go @@ -45,6 +45,10 @@ func main() { log.Printf("[debug] TIbiaData API User-Agent: %s", TibiadataUserAgent) } + // initializing houses mappings + TibiaDataHousesMappingInitiator() + + // starting webserver.go stuff runWebServer() } @@ -71,6 +75,4 @@ func TibiaDataInitializer() { log.Printf("[info] TibiaData API proxy: %s", TibiadataProxyDomain) - // initializing houses mappings - TibiaDataHousesMappingInitiator() } From 180836e77ebde57836a2c76d2fdb3683a6873e05 Mon Sep 17 00:00:00 2001 From: Tobias Lindberg Date: Thu, 13 Jan 2022 23:36:20 +0100 Subject: [PATCH 20/22] adding more logging to mapping init func so the log tells if it sails collecting mappings --- src/HousesMapping.go | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/src/HousesMapping.go b/src/HousesMapping.go index e8426908..63583725 100644 --- a/src/HousesMapping.go +++ b/src/HousesMapping.go @@ -3,6 +3,7 @@ package main import ( "encoding/json" "io/ioutil" + "log" ) var ( @@ -21,14 +22,22 @@ type HousesMapping struct { // TibiaDataHousesMappingInitiator func - used to load data from local JSON file func TibiaDataHousesMappingInitiator() { // load content from file into variable file - file, _ := ioutil.ReadFile("houses_mapping.json") + file, err := ioutil.ReadFile("houses_mapping.json") - // loading json and mapping it into the data variable - data := HousesMapping{} - _ = json.Unmarshal([]byte(file), &data) + if err != nil { + log.Println("[error] TibiaData API failed to load content from houses_mapping.json") + } else { + // loading json and mapping it into the data variable + data := HousesMapping{} + err = json.Unmarshal([]byte(file), &data) - // storing data so it's accessible from other places - TibiadataHousesMapping = data + if err != nil { + log.Println("[error] TibiaData API failed to parse content from houses_mapping.json") + } else { + // storing data so it's accessible from other places + TibiadataHousesMapping = data + } + } } // TibiaDataHousesMapResolver func - used to return both town and type From 5f8470f045eaf7cd8c2f98669d9bf08546bbdce5 Mon Sep 17 00:00:00 2001 From: Tobias Lindberg Date: Thu, 13 Jan 2022 23:36:40 +0100 Subject: [PATCH 21/22] adding omitempty to town and type so if mapping fails, they are now displayed --- src/TibiaHousesHouseV3.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/TibiaHousesHouseV3.go b/src/TibiaHousesHouseV3.go index 70e6c570..d6e1d9d5 100644 --- a/src/TibiaHousesHouseV3.go +++ b/src/TibiaHousesHouseV3.go @@ -44,9 +44,9 @@ type HouseStatus struct { type House struct { Houseid int `json:"houseid"` World string `json:"world"` - Town string `json:"town"` + Town string `json:"town,omitempty"` Name string `json:"name"` - Type string `json:"type"` + Type string `json:"type,omitempty"` Beds int `json:"beds"` Size int `json:"size"` Rent int `json:"rent"` From 3819e3b88eefe3d800c621b9d5490ec18d246f0c Mon Sep 17 00:00:00 2001 From: Tobias Lindberg Date: Fri, 14 Jan 2022 00:09:06 +0100 Subject: [PATCH 22/22] switching from local file to dwnload from github in startup --- .gitignore | 3 --- src/HousesMapping.go | 41 +++++++++++++++++++++++++++++++++-------- src/main.go | 6 +++--- 3 files changed, 36 insertions(+), 14 deletions(-) diff --git a/.gitignore b/.gitignore index ba5057f3..66fd13c9 100644 --- a/.gitignore +++ b/.gitignore @@ -13,6 +13,3 @@ # Dependency directories (remove the comment below to include it) # vendor/ - -# TibiaData related files that should be ignored -houses_mapping.json diff --git a/src/HousesMapping.go b/src/HousesMapping.go index 63583725..8cf741d8 100644 --- a/src/HousesMapping.go +++ b/src/HousesMapping.go @@ -2,8 +2,11 @@ package main import ( "encoding/json" - "io/ioutil" "log" + "net/http" + "time" + + "github.com/go-resty/resty/v2" ) var ( @@ -21,15 +24,34 @@ type HousesMapping struct { // TibiaDataHousesMappingInitiator func - used to load data from local JSON file func TibiaDataHousesMappingInitiator() { - // load content from file into variable file - file, err := ioutil.ReadFile("houses_mapping.json") - if err != nil { - log.Println("[error] TibiaData API failed to load content from houses_mapping.json") - } else { - // loading json and mapping it into the data variable + // Setting up resty client + client := resty.New() + + // Set client timeout and retry + client.SetTimeout(5 * time.Second) + client.SetRetryCount(2) + + // Set headers for all requests + client.SetHeaders(map[string]string{ + "Content-Type": "application/json", + "User-Agent": TibiadataUserAgent, + }) + + // Enabling Content length value for all request + client.SetContentLength(true) + + // Disable redirection of client (so we skip parsing maintenance page) + client.SetRedirectPolicy(resty.NoRedirectPolicy()) + + TibiadataAssetsURL := "https://raw.githubusercontent.com/TibiaData/tibiadata-api-assets/main/data/houses_mapping.json" + res, err := client.R().Get(TibiadataAssetsURL) + + switch res.StatusCode() { + case http.StatusOK: + // adding response into the data field data := HousesMapping{} - err = json.Unmarshal([]byte(file), &data) + err = json.Unmarshal([]byte(res.Body()), &data) if err != nil { log.Println("[error] TibiaData API failed to parse content from houses_mapping.json") @@ -37,6 +59,9 @@ func TibiaDataHousesMappingInitiator() { // storing data so it's accessible from other places TibiadataHousesMapping = data } + + default: + log.Printf("[error] TibiaData API failed to load houses mapping. %s", err) } } diff --git a/src/main.go b/src/main.go index c820579c..24374c1b 100644 --- a/src/main.go +++ b/src/main.go @@ -45,9 +45,6 @@ func main() { log.Printf("[debug] TIbiaData API User-Agent: %s", TibiadataUserAgent) } - // initializing houses mappings - TibiaDataHousesMappingInitiator() - // starting webserver.go stuff runWebServer() } @@ -75,4 +72,7 @@ func TibiaDataInitializer() { log.Printf("[info] TibiaData API proxy: %s", TibiadataProxyDomain) + // initializing houses mappings + TibiaDataHousesMappingInitiator() + }