Skip to content

Commit 3ac76ef

Browse files
authored
Cache new regex queries (#66)
1 parent ae6eea1 commit 3ac76ef

File tree

3 files changed

+28
-26
lines changed

3 files changed

+28
-26
lines changed

src/TibiaCharactersCharacterV3.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,8 @@ var (
128128
summonRegex = regexp.MustCompile(`(an? .+) of ([^<]+)`)
129129
accountBadgesRegex = regexp.MustCompile(`\(this\), &#39;(.*)&#39;, &#39;(.*)&#39;,.*\).*src="(.*)" alt=.*`)
130130
accountAchievementsRegex = regexp.MustCompile(`<td class="[a-zA-Z0-9_.-]+">(.*)<\/td><td>(.*)?<?.*<\/td>`)
131+
titleRegex = regexp.MustCompile(`(.*) \(([0-9]+).*`)
132+
characterInfoRegex = regexp.MustCompile(`<td.*<nobr>[0-9]+\..(.*)<\/nobr><\/td><td.*><nobr>(.*)<\/nobr><\/td><td style="width: 70%">(.*)<\/td><td.*`)
131133
)
132134

133135
// TibiaCharactersCharacterV3 func
@@ -189,8 +191,7 @@ func TibiaCharactersCharacterV3Impl(BoxContentHTML string) CharacterResponse {
189191
case "Sex:":
190192
CharacterInformationData.Sex = RowData
191193
case "Title:":
192-
regex1t := regexp.MustCompile(`(.*) \(([0-9]+).*`)
193-
subma1t := regex1t.FindAllStringSubmatch(RowData, -1)
194+
subma1t := titleRegex.FindAllStringSubmatch(RowData, -1)
194195
CharacterInformationData.Title = subma1t[0][1]
195196
CharacterInformationData.UnlockedTitles = TibiadataStringToIntegerV3(subma1t[0][2])
196197
case "Vocation:":
@@ -408,9 +409,7 @@ func TibiaCharactersCharacterV3Impl(BoxContentHTML string) CharacterResponse {
408409
// Removing line breaks
409410
CharacterListHTML = TibiadataHTMLRemoveLinebreaksV3(CharacterListHTML)
410411

411-
// Regex to get data for fansites
412-
regex1 := regexp.MustCompile(`<td.*<nobr>[0-9]+\..(.*)<\/nobr><\/td><td.*><nobr>(.*)<\/nobr><\/td><td style="width: 70%">(.*)<\/td><td.*`)
413-
subma1 := regex1.FindAllStringSubmatch(CharacterListHTML, -1)
412+
subma1 := characterInfoRegex.FindAllStringSubmatch(CharacterListHTML, -1)
414413

415414
if len(subma1) > 0 {
416415
TmpCharacterName := subma1[0][1]

src/TibiaHousesHouseV3.go

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,17 @@ type HouseResponse struct {
5959
Information Information `json:"information"`
6060
}
6161

62+
var (
63+
houseDataRegex = regexp.MustCompile(`<td.*src="(.*)" width.*<b>(.*)<\/b>.*This (house|guildhall) can.*to ([0-9]+) beds..*<b>([0-9]+) square.*<b>([0-9]+)([k]+).gold<\/b>.*on <b>([A-Za-z]+)<\/b>.(.*)<\/td>`)
64+
// matching for this: and <wants to|will> pass the <HouseType> to <TransferReceiver> for <TransferPrice> gold
65+
housePassingRegex = regexp.MustCompile(`and (wants to|will) pass the (house|guildhall) to (.*) for ([0-9]+) gold`)
66+
// matching for this: <OwnerSex> will move out on <MovingDate> (
67+
moveOutRegex = regexp.MustCompile(`(He|She) will move out on (.*?) \(`)
68+
// matching for this: The <HouseType> has been rented by <Owner>. <OwnerSex> has paid the rent until <PaidUntil>.
69+
paidUntilRegex = regexp.MustCompile(`The (house|guildhall) has been rented by (.*). (He|She) has paid.*until (.*?)\.`)
70+
houseAuctionedRegex = regexp.MustCompile(`The (house|guildhall) is currently.*The auction (will end|has ended) at (.*)\. The.*is ([0-9]+) gold.*submitted by (.*)\.`)
71+
)
72+
6273
// TibiaHousesHouseV3 func
6374
func TibiaHousesHouseV3Impl(houseid string, BoxContentHTML string) HouseResponse {
6475
// Creating empty vars
@@ -78,8 +89,7 @@ func TibiaHousesHouseV3Impl(houseid string, BoxContentHTML string) HouseResponse
7889
}
7990

8091
// Regex to get data for house
81-
regex1 := regexp.MustCompile(`<td.*src="(.*)" width.*<b>(.*)<\/b>.*This (house|guildhall) can.*to ([0-9]+) beds..*<b>([0-9]+) square.*<b>([0-9]+)([k]+).gold<\/b>.*on <b>([A-Za-z]+)<\/b>.(.*)<\/td>`)
82-
subma1 := regex1.FindAllStringSubmatch(HouseHTML, -1)
92+
subma1 := houseDataRegex.FindAllStringSubmatch(HouseHTML, -1)
8393

8494
if len(subma1) > 0 {
8595
HouseData.Houseid = TibiadataStringToIntegerV3(houseid)
@@ -102,9 +112,8 @@ func TibiaHousesHouseV3Impl(houseid string, BoxContentHTML string) HouseResponse
102112
switch {
103113
case strings.Contains(HouseData.Status.Original, " pass the "+HouseData.Type+" to "):
104114
HouseData.Status.IsTransfering = true
105-
// matching for this: and <wants to|will> pass the <HouseType> to <TransferReceiver> for <TransferPrice> gold
106-
regex2 := regexp.MustCompile(`and (wants to|will) pass the (house|guildhall) to (.*) for ([0-9]+) gold`)
107-
subma2 := regex2.FindAllStringSubmatch(HouseData.Status.Original, -1)
115+
116+
subma2 := housePassingRegex.FindAllStringSubmatch(HouseData.Status.Original, -1)
108117
// storing values from regex
109118
if subma2[0][1] == "will" {
110119
HouseData.Status.Rental.TransferAccept = true
@@ -115,18 +124,14 @@ func TibiaHousesHouseV3Impl(houseid string, BoxContentHTML string) HouseResponse
115124

116125
case strings.Contains(HouseData.Status.Original, " will move out on "):
117126
HouseData.Status.IsMoving = true
118-
// matching for this: <OwnerSex> will move out on <MovingDate> (
119-
regex2 := regexp.MustCompile(`(He|She) will move out on (.*?) \(`)
120-
subma2 := regex2.FindAllStringSubmatch(HouseData.Status.Original, -1)
127+
subma2 := moveOutRegex.FindAllStringSubmatch(HouseData.Status.Original, -1)
121128
// storing values from regex
122129
HouseData.Status.Rental.MovingDate = TibiadataDatetimeV3(subma2[0][2])
123130
fallthrough
124131

125132
default:
126133
HouseData.Status.IsRented = true
127-
// matching for this: The <HouseType> has been rented by <Owner>. <OwnerSex> has paid the rent until <PaidUntil>.
128-
regex2 := regexp.MustCompile(`The (house|guildhall) has been rented by (.*). (He|She) has paid.*until (.*?)\.`)
129-
subma2 := regex2.FindAllStringSubmatch(HouseData.Status.Original, -1)
134+
subma2 := paidUntilRegex.FindAllStringSubmatch(HouseData.Status.Original, -1)
130135
// storing values from regex
131136
HouseData.Status.Rental.Owner = subma2[0][2]
132137
HouseData.Status.Rental.PaidUntil = TibiadataDatetimeV3(subma2[0][4])
@@ -141,11 +146,9 @@ func TibiaHousesHouseV3Impl(houseid string, BoxContentHTML string) HouseResponse
141146
case strings.Contains(HouseData.Status.Original, "is currently being auctioned"):
142147
// auctioned
143148
HouseData.Status.IsAuctioned = true
144-
145149
// check if bid is going on
146150
if !strings.Contains(HouseData.Status.Original, "No bid has been submitted so far.") {
147-
regex2 := regexp.MustCompile(`The (house|guildhall) is currently.*The auction (will end|has ended) at (.*)\. The.*is ([0-9]+) gold.*submitted by (.*)\.`)
148-
subma2 := regex2.FindAllStringSubmatch(HouseData.Status.Original, -1)
151+
subma2 := houseAuctionedRegex.FindAllStringSubmatch(HouseData.Status.Original, -1)
149152
// storing values from regex
150153
HouseData.Status.Auction.AuctionEnd = TibiadataDatetimeV3(subma2[0][3])
151154
HouseData.Status.Auction.CurrentBid = TibiadataStringToIntegerV3(subma2[0][4])
@@ -155,10 +158,8 @@ func TibiaHousesHouseV3Impl(houseid string, BoxContentHTML string) HouseResponse
155158
}
156159
}
157160
}
158-
159161
}
160162

161-
//
162163
// Build the data-blob
163164
return HouseResponse{
164165
HouseData,

src/TibiaHousesOverviewV3.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ type HousesOverviewResponse struct {
4242
Information Information `json:"information"`
4343
}
4444

45+
var (
46+
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.*`)
47+
houseOverviewAuctionedRegex = regexp.MustCompile(`auctioned.\(([0-9]+).gold;.(.*).left\)`)
48+
)
49+
4550
// TibiaHousesOverviewV3 func
4651
func TibiaHousesOverviewV3Impl(c *gin.Context, world string, town string) HousesOverviewResponse {
4752
var (
@@ -86,9 +91,7 @@ func TibiaHousesOverviewV3Impl(c *gin.Context, world string, town string) Houses
8691
HousesDivHTML = TibiadataHTMLRemoveLinebreaksV3(HousesDivHTML)
8792
HousesDivHTML = TibiaDataSanitizeNbspSpaceString(HousesDivHTML)
8893

89-
// Regex to get data for record values
90-
regex1 := 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.*`)
91-
subma1 := regex1.FindAllStringSubmatch(HousesDivHTML, -1)
94+
subma1 := houseOverviewDataRegex.FindAllStringSubmatch(HousesDivHTML, -1)
9295

9396
if len(subma1) > 0 {
9497
// House details
@@ -106,8 +109,7 @@ func TibiaHousesOverviewV3Impl(c *gin.Context, world string, town string) Houses
106109
house.IsAuctioned = true
107110
case strings.Contains(s, "auctioned"):
108111
house.IsAuctioned = true
109-
regex1b := regexp.MustCompile(`auctioned.\(([0-9]+).gold;.(.*).left\)`)
110-
subma1b := regex1b.FindAllStringSubmatch(s, -1)
112+
subma1b := houseOverviewAuctionedRegex.FindAllStringSubmatch(s, -1)
111113
house.Auction.AuctionBid = TibiadataStringToIntegerV3(subma1b[0][1])
112114
house.Auction.AuctionLeft = subma1b[0][2]
113115
}

0 commit comments

Comments
 (0)