Skip to content

Commit 21c5356

Browse files
Fix decoding decoding issues (#16)
* Fix decoding issues and save some allocations (#15) * addition to the decoding issue - split sanitize function into two - applying sanitizze functions to requires endpoints - removing comments and minor cleanup - fixing build workflow for docker tagging Co-authored-by: kamilon <kennedybushnell@kamilon.com>
1 parent fa1db0f commit 21c5356

10 files changed

+41
-73
lines changed

.github/workflows/build.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ jobs:
3030
ghcr.io/tibiadata/tibiadata-api-go
3131
tags: |
3232
type=edge
33+
type=ref,event=branch
3334
type=semver,pattern=v{{version}}
3435
type=semver,pattern=v{{major}}.{{minor}}
3536

src/TibiaCharactersCharacterV3.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ func TibiaCharactersCharacterV3(c *gin.Context) {
8888
// Child of Characters
8989
type Deaths struct {
9090
DeathEntries []DeathEntries `json:"death_entries"`
91-
TruncatedDeaths bool `json:"truncated"` // TODO: when are those relevant..
91+
TruncatedDeaths bool `json:"truncated"` // deathlist can be truncated.. but we don't have logic for that atm
9292
}
9393

9494
// Child of Characters
@@ -206,13 +206,14 @@ func TibiaCharactersCharacterV3(c *gin.Context) {
206206

207207
// Removing line breaks
208208
CharacterTrHTML = TibiadataHTMLRemoveLinebreaksV3(CharacterTrHTML)
209+
// Unescape hmtl string
210+
CharacterTrHTML = TibiaDataSanitizeEscapedString(CharacterTrHTML)
209211

210212
// Regex to get data for fansites
211-
regex1 := regexp.MustCompile(`<td.*class="[a-zA-Z0-9_.-]+".*>(.*):<\/.*td><td>(.*)<\/td>`)
213+
regex1 := regexp.MustCompile(`<td.*class=.[a-zA-Z0-9_.-]+..*>(.*):<\/.*td><td>(.*)<\/td>`)
212214
subma1 := regex1.FindAllStringSubmatch(CharacterTrHTML, -1)
213215

214216
if len(subma1) > 0 {
215-
subma1[0][2] = html.UnescapeString(subma1[0][2])
216217

217218
if subma1[0][1] == "Name" {
218219
Tmp := strings.Split(subma1[0][2], "<")
@@ -249,7 +250,6 @@ func TibiaCharactersCharacterV3(c *gin.Context) {
249250
CharacterInformationData.Residence = subma1[0][2]
250251
} else if strings.Contains(subma1[0][1], "Account") && strings.Contains(subma1[0][1], "Status") {
251252
// } else if subma1[0][1] == "Account Status" {
252-
// TODO this does not work.. somehow.. -.-
253253
CharacterInformationData.AccountStatus = subma1[0][2]
254254
} else if subma1[0][1] == "Married To" {
255255
CharacterInformationData.MarriedTo = TibiadataRemoveURLsV3(subma1[0][2])
@@ -264,7 +264,6 @@ func TibiaCharactersCharacterV3(c *gin.Context) {
264264
})
265265
} else if strings.Contains(subma1[0][1], "Guild") && strings.Contains(subma1[0][1], "Membership") {
266266
// } else if subma1[0][1] == "Guild Membership" {
267-
// TODO this does not work.. somehow.. -.-
268267
Tmp := strings.Split(subma1[0][2], " of the <a href=")
269268
CharacterInformationData.Guild.Rank = Tmp[0]
270269
CharacterInformationData.Guild.GuildName = TibiadataRemoveURLsV3("<a href=" + Tmp[1])

src/TibiaCreaturesCreatureV3.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,9 @@ func TibiaCreaturesCreatureV3(c *gin.Context) {
7373
// Preparing data for JSONData
7474
if len(subma1) > 0 {
7575

76-
// Description
76+
// Description (and unescape hmtl string)
7777
CreatureDescription = strings.ReplaceAll(subma1[0][3], "<br/>", "\n")
78+
CreatureDescription = TibiaDataSanitizeEscapedString(CreatureDescription)
7879

7980
// Behaviour
8081
// Regex to get data..
@@ -142,7 +143,7 @@ func TibiaCreaturesCreatureV3(c *gin.Context) {
142143
// Build the data-blob
143144
jsonData := JSONData{
144145
Creature{
145-
Name: subma1[0][1],
146+
Name: TibiaDataSanitizeEscapedString(subma1[0][1]),
146147
Race: race,
147148
ImageURL: subma1[0][2],
148149
Description: CreatureDescription,

src/TibiaCreaturesOverviewV3.go

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,6 @@ func TibiaCreaturesOverviewV3(c *gin.Context) {
8080
// check if regex return length is over 0 and the match of name is over 1
8181
if len(subma1) > 0 && len(subma1[0][3]) > 1 {
8282

83-
// printing the name of the creature
84-
//log.Println(subma1[0][1])
85-
8683
// Adding bool to indicate features in creature_list
8784
FeaturedRace := false
8885
if subma1[0][1] == BoostedCreatureRace {
@@ -91,7 +88,7 @@ func TibiaCreaturesOverviewV3(c *gin.Context) {
9188

9289
// Creating data block to return
9390
CreaturesData = append(CreaturesData, Creature{
94-
Name: subma1[0][3],
91+
Name: TibiaDataSanitizeEscapedString(subma1[0][3]),
9592
Race: subma1[0][1],
9693
ImageURL: subma1[0][2],
9794
Featured: FeaturedRace,
@@ -100,9 +97,6 @@ func TibiaCreaturesOverviewV3(c *gin.Context) {
10097
}
10198
})
10299

103-
// Printing the CreaturesData data to log
104-
// log.Println(CreaturesData)
105-
106100
//
107101
// Build the data-blob
108102
jsonData := JSONData{

src/TibiaHighscoresV3.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ func TibiaHighscoresV3(c *gin.Context) {
222222

223223
HighscoreData = append(HighscoreData, Highscore{
224224
Rank: HighscoreDataRank,
225-
Name: subma1[0][1],
225+
Name: TibiaDataSanitizeEscapedString(subma1[0][1]),
226226
Vocation: HighscoreDataVocation,
227227
World: HighscoreDataWorld,
228228
Level: HighscoreDataLevel,

src/TibiaKillstatisticsV3.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,6 @@ func TibiaKillstatisticsV3(c *gin.Context) {
8181
// we don't want to include the Total row
8282
} else {
8383

84-
// Storing race name
85-
KillStatisticsRace := strings.TrimSpace(subma1[0][1])
86-
8784
// Store the values..
8885
KillStatisticsLastDayKilledPlayers := TibiadataStringToIntegerV3(subma1[0][2])
8986
TotalLastDayKilledPlayers += KillStatisticsLastDayKilledPlayers
@@ -96,7 +93,7 @@ func TibiaKillstatisticsV3(c *gin.Context) {
9693

9794
// Append new Entry item to KillStatisticsData
9895
KillStatisticsData = append(KillStatisticsData, Entry{
99-
Race: KillStatisticsRace,
96+
Race: TibiaDataSanitizeEscapedString(subma1[0][1]),
10097
LastDayKilledPlayers: KillStatisticsLastDayKilledPlayers,
10198
LastDayKilledByPlayers: KillStatisticsLastDayKilledByPlayers,
10299
LastWeekKilledPlayers: KillStatisticsLastWeekKilledPlayers,

src/TibiaSpellsOverviewV3.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ func TibiaSpellsOverviewV3(c *gin.Context) {
127127
SpellsData = append(SpellsData, Spell{
128128
Name: subma1[0][2],
129129
Spell: subma1[0][1],
130-
Formula: TibiaDataSanitizeString(subma1[0][3]),
130+
Formula: TibiaDataSanitizeDoubleQuoteString(TibiadataUnescapeStringV3(subma1[0][3])),
131131
Level: TibiadataStringToIntegerV3(subma1[0][6]),
132132
Mana: TibiadataStringToIntegerV3(subma1[0][7]),
133133
Price: TibiadataStringToIntegerV3(subma1[0][8]),

src/TibiaSpellsSpellV3.go

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ func TibiaSpellsSpellV3(c *gin.Context) {
1717

1818
// Child of SpellInformation
1919
type SpellInformation struct {
20-
// Name string `json:"name"`
2120
Formula string `json:"formula"`
2221
Vocation []string `json:"vocation"`
2322
GroupAttack bool `json:"group_attack"`
@@ -39,7 +38,6 @@ func TibiaSpellsSpellV3(c *gin.Context) {
3938

4039
// Child of RuneInformation
4140
type RuneInformation struct {
42-
// Name string `json:"name"`
4341
Vocation []string `json:"vocation"`
4442
GroupAttack bool `json:"group_attack"`
4543
GroupHealing bool `json:"group_healing"`
@@ -125,23 +123,13 @@ func TibiaSpellsSpellV3(c *gin.Context) {
125123
// check if regex return length is over 0 and the match of name is over 1
126124
if len(subma1) > 0 {
127125

128-
// Creating easy to use vars
126+
// Creating easy to use vars (and unescape hmtl right string)
129127
WorldsInformationLeftColumn := subma1[0][1]
130-
WorldsInformationRightColumn := subma1[0][2]
131-
132-
/*
133-
if WorldsInformationLeftColumn == "Name" {
134-
if SpellInformationSection == "spell" {
135-
SpellsInfoName = WorldsInformationRightColumn
136-
} else if SpellInformationSection == "rune" {
137-
RuneInfoName = WorldsInformationRightColumn
138-
}
139-
}
140-
*/
128+
WorldsInformationRightColumn := TibiaDataSanitizeEscapedString(subma1[0][2])
141129

142130
// Formula
143131
if WorldsInformationLeftColumn == "Formula" {
144-
SpellsInfoFormula = TibiaDataSanitizeString(WorldsInformationRightColumn)
132+
SpellsInfoFormula = TibiaDataSanitizeDoubleQuoteString(WorldsInformationRightColumn)
145133
}
146134

147135
// Vocation
@@ -238,7 +226,6 @@ func TibiaSpellsSpellV3(c *gin.Context) {
238226

239227
// City
240228
if WorldsInformationLeftColumn == "City" {
241-
WorldsInformationRightColumn = TibiaDataSanitizeString(WorldsInformationRightColumn)
242229
SpellsInfoCity = strings.Split(WorldsInformationRightColumn, ", ")
243230
}
244231

@@ -278,7 +265,6 @@ func TibiaSpellsSpellV3(c *gin.Context) {
278265
Description: SpellDescription,
279266
HasSpellInformation: SpellsHasSpellSection,
280267
SpellInformation: SpellInformation{
281-
// Name: SpellsInfoName,
282268
Formula: SpellsInfoFormula,
283269
Vocation: SpellsInfoVocation,
284270
GroupAttack: SpellsInfoGroupAttack,
@@ -299,7 +285,6 @@ func TibiaSpellsSpellV3(c *gin.Context) {
299285
},
300286
HasRuneInformation: SpellsHasRuneSection,
301287
RuneInformation: RuneInformation{
302-
// Name: RuneInfoName,
303288
Vocation: RuneInfoVocation,
304289
GroupAttack: RuneInfoGroupAttack,
305290
GroupHealing: RuneInfoGroupHealing,

src/TibiaWorldsWorldV3.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,9 @@ func TibiaWorldsWorldV3(c *gin.Context) {
8888

8989
if len(subma1) > 0 {
9090

91-
// Creating easy to use vars
91+
// Creating easy to use vars (and unescape hmtl right string)
9292
WorldsInformationLeftColumn := subma1[0][1]
93-
WorldsInformationRightColumn := subma1[0][2]
93+
WorldsInformationRightColumn := TibiaDataSanitizeEscapedString(subma1[0][2])
9494

9595
if WorldsInformationLeftColumn == "Status" {
9696
if strings.Contains(WorldsInformationRightColumn, "</div>Online") {

src/webserver.go

Lines changed: 24 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package main
22

33
import (
4+
"bytes"
45
"encoding/json"
56
"html"
7+
"io"
68
"log"
79
"net/http"
810
"net/url"
@@ -267,11 +269,14 @@ func TibiadataHTMLDataCollectorV3(TibiaURL string) string {
267269
}
268270
}
269271

270-
// Convert string to io.Reader
271-
res_io := strings.NewReader(res.String())
272+
// Convert body to io.Reader
273+
resIo := bytes.NewReader(res.Body())
274+
275+
// wrap reader in a converting reader from ISO 8859-1 to UTF-8
276+
resIo2 := TibiaDataConvertEncodingtoUTF8(resIo)
272277

273278
// Load the HTML document
274-
doc, err := goquery.NewDocumentFromReader(res_io)
279+
doc, err := goquery.NewDocumentFromReader(resIo2)
275280
if err != nil {
276281
log.Printf("[error] TibiadataHTMLDataCollectorV3 (URL: %s) error: %s", TibiaURL, err)
277282
}
@@ -282,30 +287,20 @@ func TibiadataHTMLDataCollectorV3(TibiaURL string) string {
282287
log.Fatal(err)
283288
}
284289

285-
// convert string from eg "&nbsp;" to " "
286-
data = html.UnescapeString(data)
287-
data = strings.ReplaceAll(data, "&nbsp;", " ")
288-
289-
// convert string from ISO 8859-1 to UTF-8
290-
data, _ = TibiaDataConvertEncodingtoUTF8(data)
291-
292290
// Return of extracted html to functions..
293-
return string(data)
291+
return data
294292
}
295293

296294
// TibiadataHTMLRemoveLinebreaksV3 func
297295
func TibiadataHTMLRemoveLinebreaksV3(data string) string {
298-
return string(strings.ReplaceAll(data, "\n", ""))
296+
return strings.ReplaceAll(data, "\n", "")
299297
}
300298

301299
// TibiadataRemoveURLsV3 func
302300
func TibiadataRemoveURLsV3(data string) string {
303301
// prepare return value
304302
var returnData string
305303

306-
// convert string from UTF8 to ISO88591
307-
data, _ = TibiaDataConvertEncodingtoISO88591(data)
308-
309304
// Regex to remove URLs
310305
regex := regexp.MustCompile(`<a.*>(.*)<\/a>`)
311306
result := regex.FindAllStringSubmatch(data, -1)
@@ -315,34 +310,29 @@ func TibiadataRemoveURLsV3(data string) string {
315310
} else {
316311
returnData = ""
317312
}
318-
return string(returnData)
313+
return returnData
319314
}
320315

321316
// TibiadataStringWorldFormatToTitleV3 func
322317
func TibiadataStringWorldFormatToTitleV3(world string) string {
323-
return string(strings.Title(strings.ToLower(world)))
318+
return strings.Title(strings.ToLower(world))
324319
}
325320

326321
// TibiadataUnescapeStringV3 func
327322
func TibiadataUnescapeStringV3(data string) string {
328-
// data, _ = TibiaDataConvertEncodingtoUTF8(data)
329-
return string(html.UnescapeString(data))
323+
return html.UnescapeString(data)
330324
}
331325

332326
// TibiadataQueryEscapeStringV3 func
333327
func TibiadataQueryEscapeStringV3(data string) string {
334328
data, _ = TibiaDataConvertEncodingtoISO88591(data)
335-
return string(url.QueryEscape(data))
329+
return url.QueryEscape(data)
336330
}
337331

338332
// TibiadataDatetimeV3 func
339333
func TibiadataDatetimeV3(date string) string {
340-
341334
var returnDate string
342335

343-
// we need to use TibiaDataConvertEncodingtoISO88591 so that the parser doens't complain
344-
date, _ = TibiaDataConvertEncodingtoISO88591(date)
345-
346336
// If statement to determine if date string is filled or empty
347337
if date == "" {
348338
// The string that should be returned is the current timestamp
@@ -389,9 +379,6 @@ func TibiadataDatetimeV3(date string) string {
389379

390380
// TibiadataDateV3 func
391381
func TibiadataDateV3(date string) string {
392-
// we need to use TibiaDataConvertEncodingtoISO88591 so that the parser doens't complain
393-
date, _ = TibiaDataConvertEncodingtoISO88591(date)
394-
395382
// use regex to skip weird formatting on "spaces"
396383
regex1 := regexp.MustCompile(`([a-zA-Z]{3}).*([0-9]{2}).*([0-9]{4})`)
397384
subma1 := regex1.FindAllStringSubmatch(date, -1)
@@ -416,7 +403,7 @@ func TibiadataStringToIntegerV3(data string) int {
416403
returnData, _ := strconv.Atoi(processedString)
417404

418405
// Return of formatted date and time string to functions..
419-
return int(returnData)
406+
return returnData
420407
}
421408

422409
// match html tag and replace it with ""
@@ -444,9 +431,8 @@ func TibiaDataConvertEncodingtoISO88591(data string) (string, error) {
444431
}
445432

446433
// TibiaDataConvertEncodingtoUTF8 func - convert string from latin1 (ISO 8859-1) to UTF-8
447-
func TibiaDataConvertEncodingtoUTF8(data string) (string, error) {
448-
data, err := charmap.ISO8859_1.NewDecoder().String(data)
449-
return data, err
434+
func TibiaDataConvertEncodingtoUTF8(data io.Reader) io.Reader {
435+
return charmap.ISO8859_1.NewDecoder().Reader(data)
450436
}
451437

452438
// isEnvExist func - check if environment var is set
@@ -457,8 +443,13 @@ func isEnvExist(key string) bool {
457443
return false
458444
}
459445

460-
func TibiaDataSanitizeString(data string) string {
461-
data = html.UnescapeString(data)
446+
// TibiaDataSanitizeEscapedString func - run unescape string on string
447+
func TibiaDataSanitizeEscapedString(data string) string {
448+
return html.UnescapeString(data)
449+
}
450+
451+
// TibiaDataSanitizeDoubleQuoteString func - replaces double quotes to single quotes in strings
452+
func TibiaDataSanitizeDoubleQuoteString(data string) string {
462453
return strings.ReplaceAll(data, "\"", "'")
463454
}
464455

0 commit comments

Comments
 (0)