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(`