-
Notifications
You must be signed in to change notification settings - Fork 26
/
TibiaHousesOverviewV3.go
141 lines (121 loc) · 4.34 KB
/
TibiaHousesOverviewV3.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package main
import (
"log"
"net/http"
"regexp"
"strings"
"github.com/PuerkitoBio/goquery"
"github.com/gin-gonic/gin"
"github.com/go-resty/resty/v2"
)
// Child of House
type HousesAuction struct {
AuctionBid int `json:"current_bid"`
AuctionLeft string `json:"time_left"`
}
// 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 HousesAuction `json:"auction"`
}
// Child of JSONData
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"`
}
var (
houseOverviewDataRegex = regexp.MustCompile(`<td.*><nobr>(.*)<\/nobr><\/td><td.*><nobr>([0-9]+).sqm<\/nobr><\/td><td.*><nobr>([0-9]+)(k+).gold<\/nobr><\/td><td.*><nobr>(.*)<\/nobr><\/td>.*houseid" value="([0-9]+)"\/><div.*`)
houseOverviewAuctionedRegex = regexp.MustCompile(`auctioned.\(([0-9]+).gold;.(.*).left\)`)
)
// TibiaHousesOverviewV3 func
func TibiaHousesOverviewV3Impl(c *gin.Context, world string, town string, htmlDataCollector func(TibiadataRequestStruct) (string, error)) HousesOverviewResponse {
var (
// Creating empty vars
HouseData, GuildhallData []HousesHouse
)
// list of different fansite types
HouseTypes := []string{"houses", "guildhalls"}
// running over the FansiteTypes array
for _, HouseType := range HouseTypes {
tibiadataRequest := TibiadataRequestStruct{
Method: resty.MethodGet,
URL: "https://www.tibia.com/community/?subtopic=houses&world=" + TibiadataQueryEscapeStringV3(world) + "&town=" + TibiadataQueryEscapeStringV3(town) + "&type=" + TibiadataQueryEscapeStringV3(HouseType),
}
BoxContentHTML, err := htmlDataCollector(tibiadataRequest)
// return error (e.g. for maintenance mode)
if err != nil {
TibiaDataAPIHandleResponse(c, http.StatusBadGateway, "TibiaHousesOverviewV3", gin.H{"error": err.Error()})
//TODO: Need to refactor this properly
return HousesOverviewResponse{}
}
// 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 := HousesHouse{}
// Storing HTML into HousesDivHTML
HousesDivHTML, err := s.Html()
if err != nil {
log.Fatal(err)
}
// Removing linebreaks from HTML
HousesDivHTML = TibiadataHTMLRemoveLinebreaksV3(HousesDivHTML)
HousesDivHTML = TibiaDataSanitizeNbspSpaceString(HousesDivHTML)
subma1 := houseOverviewDataRegex.FindAllStringSubmatch(HousesDivHTML, -1)
if len(subma1) > 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])
// HousesAction 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
subma1b := houseOverviewAuctionedRegex.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)
}
}
})
}
// Build the data-blob
return HousesOverviewResponse{
HousesHouses{
World: world,
Town: town,
HouseList: HouseData,
GuildhallList: GuildhallData,
},
Information{
APIVersion: TibiadataAPIversion,
Timestamp: TibiadataDatetimeV3(""),
},
}
}