-
Notifications
You must be signed in to change notification settings - Fork 26
/
TibiaHighscoresV3.go
161 lines (139 loc) · 5.21 KB
/
TibiaHighscoresV3.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package main
import (
"log"
"regexp"
"strings"
"github.com/PuerkitoBio/goquery"
)
// Child of Highscores
type Highscore struct {
Rank int `json:"rank"` // Rank column
Name string `json:"name"` // Name column
Vocation string `json:"vocation"` // Vocation column
World string `json:"world"` // World column
Level int `json:"level"` // Level column
Value int `json:"value"` // Points/SkillLevel column
Title string `json:"title,omitempty"` // Title column (when category: loyalty)
}
// Child of JSONData
type Highscores struct {
World string `json:"world"`
Category string `json:"category"`
Vocation string `json:"vocation"`
HighscoreAge int `json:"highscore_age"`
HighscoreList []Highscore `json:"highscore_list"`
}
//
// The base includes two levels: Highscores and Information
type HighscoresResponse struct {
Highscores Highscores `json:"highscores"`
Information Information `json:"information"`
}
var (
HighscoresAgeRegex = regexp.MustCompile(`.*<div class="Text">Highscores.*Last Update: ([0-9]+) minutes ago.*`)
SevenColumnRegex = regexp.MustCompile(`<td>.*<\/td><td.*">(.*)<\/a><\/td><td.*>(.*)<\/td><td.*>(.*)<\/td><td>(.*)<\/td><td.*>(.*)<\/td><td.*>(.*)<\/td>`)
SixColumnRegex = regexp.MustCompile(`<td>.*<\/td><td.*">(.*)<\/a><\/td><td.*">(.*)<\/td><td>(.*)<\/td><td.*>(.*)<\/td><td.*>(.*)<\/td>`)
)
func TibiaHighscoresV3Impl(world string, category HighscoreCategory, vocationName string, BoxContentHTML string) HighscoresResponse {
// Loading HTML data into ReaderHTML for goquery with NewReader
ReaderHTML, err := goquery.NewDocumentFromReader(strings.NewReader(BoxContentHTML))
if err != nil {
log.Fatal(err)
}
// Creating empty HighscoreData var
var (
HighscoreData []Highscore
HighscoreDataVocation, HighscoreDataWorld, HighscoreDataTitle string
HighscoreDataRank, HighscoreDataLevel, HighscoreDataValue, HighscoreAge int
)
// getting age of data
subma1 := HighscoresAgeRegex.FindAllStringSubmatch(string(BoxContentHTML), 1)
HighscoreAge = TibiadataStringToIntegerV3(subma1[0][1])
// Running query over each div
ReaderHTML.Find(".TableContent tr").First().NextAll().Each(func(index int, s *goquery.Selection) {
// Storing HTML into CreatureDivHTML
HighscoreDivHTML, err := s.Html()
if err != nil {
log.Fatal(err)
}
// Regex the data table..
var subma1 [][]string
/*
Tibia highscore table columns
Achievment => Rank Name Vocation World Level Points
Axe => Rank Name Vocation World Level Skill Level
Charm => Rank Name Vocation World Level Points
Club => Rank Name Vocation World Level Skill Level
Distance => Rank Name Vocation World Level Skill Level
Drome => Rank Name Vocation World Level Score
Exp => Rank Name Vocation World Level Points
Fishing => Rank Name Vocation World Level Skill Level
Fist => Rank Name Vocation World Level Skill Level
Goshnar => Rank Name Vocation World Level Points
Loyalty => Rank Name Title Vocation World Level Points
Magic lvl => Rank Name Vocation World Level Skill Level
Shield => Rank Name Vocation World Level Skill Level
Sword => Rank Name Vocation World Level Skill Level
*/
if category == loyaltypoints {
subma1 = SevenColumnRegex.FindAllStringSubmatch(HighscoreDivHTML, -1)
} else {
subma1 = SixColumnRegex.FindAllStringSubmatch(HighscoreDivHTML, -1)
}
if len(subma1) > 0 {
// Debugging of what is in which column
if TibiadataDebug {
log.Println("1 -> " + subma1[0][1])
log.Println("2 -> " + subma1[0][2])
log.Println("3 -> " + subma1[0][3])
log.Println("4 -> " + subma1[0][4])
log.Println("5 -> " + subma1[0][5])
if category == loyaltypoints {
log.Println("6 -> " + subma1[0][6])
}
}
HighscoreDataRank++
if category == loyaltypoints {
HighscoreDataTitle = subma1[0][2]
HighscoreDataVocation = subma1[0][3]
HighscoreDataWorld = subma1[0][4]
HighscoreDataLevel = TibiadataStringToIntegerV3(subma1[0][5])
HighscoreDataValue = TibiadataStringToIntegerV3(subma1[0][6])
} else {
HighscoreDataVocation = subma1[0][2]
HighscoreDataWorld = subma1[0][3]
HighscoreDataLevel = TibiadataStringToIntegerV3(subma1[0][4])
HighscoreDataValue = TibiadataStringToIntegerV3(subma1[0][5])
}
HighscoreData = append(HighscoreData, Highscore{
Rank: HighscoreDataRank,
Name: TibiaDataSanitizeEscapedString(subma1[0][1]),
Vocation: HighscoreDataVocation,
World: HighscoreDataWorld,
Level: HighscoreDataLevel,
Value: HighscoreDataValue,
Title: HighscoreDataTitle,
})
}
})
// Printing the HighscoreData data to log
if TibiadataDebug {
log.Println(HighscoreData)
}
categoryString, _ := category.String()
//
// Build the data-blob
return HighscoresResponse{
Highscores{
World: strings.Title(strings.ToLower(world)),
Category: categoryString,
Vocation: vocationName,
HighscoreAge: HighscoreAge,
HighscoreList: HighscoreData,
},
Information{
APIVersion: TibiadataAPIversion,
Timestamp: TibiadataDatetimeV3(""),
},
}
}