-
Notifications
You must be signed in to change notification settings - Fork 26
/
TibiaFansitesV3.go
208 lines (183 loc) · 6.31 KB
/
TibiaFansitesV3.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
package main
import (
"log"
"regexp"
"strings"
"github.com/PuerkitoBio/goquery"
)
// Child of Fansite
type ContentType struct {
Statistics bool `json:"statistics"`
Texts bool `json:"texts"`
Tools bool `json:"tools"`
Wiki bool `json:"wiki"`
}
// Child of Fansite
type SocialMedia struct {
Discord bool `json:"discord"`
Facebook bool `json:"facebook"`
Instagram bool `json:"instagram"`
Reddit bool `json:"reddit"`
Twitch bool `json:"twitch"`
Twitter bool `json:"twitter"`
Youtube bool `json:"youtube"`
}
// Child of Fansites
type Fansite struct {
Name string `json:"name"`
LogoURL string `json:"logo_url"`
Homepage string `json:"homepage"`
Contact string `json:"contact"`
ContentType ContentType `json:"content_type"`
SocialMedia SocialMedia `json:"social_media"`
Languages []string `json:"languages"`
Specials []string `json:"specials"`
FansiteItem bool `json:"fansite_item"`
FansiteItemURL string `json:"fansite_item_url"`
}
// Child of JSONData
type Fansites struct {
PromotedFansites []Fansite `json:"promoted"`
SupportedFansites []Fansite `json:"supported"`
}
//
// The base includes two levels: Fansites and Information
type FansitesResponse struct {
Fansites Fansites `json:"fansites"`
Information Information `json:"information"`
}
var (
FansiteInformationRegex = regexp.MustCompile(`<td><a href="(.*)" target.*img .*src="(.*)" alt="(.*)"\/><\/a>.*<a href=".*">(.*)<\/a><\/td><td.*top;">(.*)<\/td><td.*top;">(.*)<\/td><td.*top;">(.*)<\/td><td.*<ul><li>(.*)<\/li><\/ul><\/td><td.*top;">(.*)<\/td>`)
FansiteImgTagRegex = regexp.MustCompile(`<img[^>]+\bsrc="([^"]+)"`)
FansiteLanguagesRegex = regexp.MustCompile("iti__flag.iti__(..)")
FansiteAnchorRegex = regexp.MustCompile(`.*src="(.*)" alt=".*`)
)
func TibiaFansitesV3Impl(BoxContentHTML string) FansitesResponse {
// Loading HTML data into ReaderHTML for goquery with NewReader
ReaderHTML, err := goquery.NewDocumentFromReader(strings.NewReader(BoxContentHTML))
if err != nil {
log.Fatal(err)
}
// Creating empty PromotedFansitesData and SupportedFansitesData var
var PromotedFansitesData, SupportedFansitesData []Fansite
// list of different fansite types
FansiteTypes := []string{"promoted", "supported"}
// running over the FansiteTypes array
for _, FansiteType := range FansiteTypes {
// Running query over each tr in <FansiteType>fansitesinnertable
ReaderHTML.Find("#" + FansiteType + "fansitesinnertable tr").First().NextAll().Each(func(index int, s *goquery.Selection) {
// #promotedfansitesinnertable
// #supportedfansitesinnertable
// Storing HTML into FansiteTrHTML
FansiteTrHTML, err := s.Html()
if err != nil {
log.Fatal(err)
}
// Removing line breaks
FansiteTrHTML = TibiadataHTMLRemoveLinebreaksV3(FansiteTrHTML)
// Regex to get data for fansites
subma1 := FansiteInformationRegex.FindAllStringSubmatch(FansiteTrHTML, -1)
if len(subma1) > 0 {
// ContentType
ContentTypeData := ContentType{}
imgs1 := FansiteImgTagRegex.FindAllStringSubmatch(subma1[0][5], -1)
out := make([]string, len(imgs1))
for i := range out {
s := imgs1[i][1]
switch {
case strings.Contains(s, "Statistics"):
ContentTypeData.Statistics = true
case strings.Contains(s, "ArticlesNews"):
ContentTypeData.Texts = true
case strings.Contains(s, "Tools"):
ContentTypeData.Tools = true
case strings.Contains(s, "Wiki"):
ContentTypeData.Wiki = true
}
}
// SocialMedia
SocialMediaData := SocialMedia{}
imgs2 := FansiteImgTagRegex.FindAllStringSubmatch(subma1[0][6], -1)
out2 := make([]string, len(imgs2))
for i := range out2 {
s := imgs2[i][1]
switch {
case strings.Contains(s, "Discord"):
SocialMediaData.Discord = true
case strings.Contains(s, "Facebook"):
SocialMediaData.Facebook = true
case strings.Contains(s, "Instagram"):
SocialMediaData.Instagram = true
case strings.Contains(s, "Reddit"):
SocialMediaData.Reddit = true
case strings.Contains(s, "Twitch"):
SocialMediaData.Twitch = true
case strings.Contains(s, "Twitter"):
SocialMediaData.Twitter = true
case strings.Contains(s, "Youtube"):
SocialMediaData.Youtube = true
}
}
// Languages
found := FansiteLanguagesRegex.FindAllString(subma1[0][7], -1)
FansiteLanguagesData := make([]string, len(found))
for i := range FansiteLanguagesData {
FansiteLanguagesData[i] = strings.ReplaceAll(found[i], "iti__flag iti__", "")
}
// Specials
subma1[0][8] = TibiaDataSanitizeEscapedString(subma1[0][8])
FansiteSpecialsData := strings.Split(subma1[0][8], "</li><li>")
// FansiteItem & FansiteItemURL
var FansiteItemData bool
var FansiteItemURLData string
subma1item := FansiteAnchorRegex.FindAllStringSubmatch(subma1[0][9], -1)
if len(subma1item) > 0 {
FansiteItemData = true
FansiteItemURLData = subma1item[0][1]
} else {
FansiteItemData = false
FansiteItemURLData = ""
}
switch FansiteType {
case "promoted":
PromotedFansitesData = append(PromotedFansitesData, Fansite{
Name: subma1[0][3],
LogoURL: subma1[0][2],
Homepage: subma1[0][1],
Contact: subma1[0][4],
ContentType: ContentTypeData,
SocialMedia: SocialMediaData,
Languages: FansiteLanguagesData,
Specials: FansiteSpecialsData,
FansiteItem: FansiteItemData,
FansiteItemURL: FansiteItemURLData,
})
case "supported":
SupportedFansitesData = append(SupportedFansitesData, Fansite{
Name: subma1[0][3],
LogoURL: subma1[0][2],
Homepage: subma1[0][1],
Contact: subma1[0][4],
ContentType: ContentTypeData,
SocialMedia: SocialMediaData,
Languages: FansiteLanguagesData,
Specials: FansiteSpecialsData,
FansiteItem: FansiteItemData,
FansiteItemURL: FansiteItemURLData,
})
}
}
})
}
// Build the data-blob
return FansitesResponse{
Fansites{
PromotedFansites: PromotedFansitesData,
SupportedFansites: SupportedFansitesData,
},
Information{
APIVersion: TibiadataAPIversion,
Timestamp: TibiadataDatetimeV3(""),
},
}
}