@@ -10,6 +10,70 @@ import (
1010 "github.com/gin-gonic/gin"
1111)
1212
13+ // Child of Guild
14+ type Guildhall struct {
15+ Name string `json:"name"`
16+ World string `json:"world"` // Maybe duplicate info? Guild can only be on one world..
17+ /*
18+ Town string `json:"town"` // We can collect that from cached info?
19+ Status string `json:"status"` // rented (but maybe also auctioned)
20+ Owner string `json:"owner"` // We can collect that from cached info?
21+ HouseID int `json:"houseid"` // We can collect that from cached info?
22+ */
23+ PaidUntil string `json:"paid_until"` // Paid until date
24+ }
25+
26+ // Child of Guild
27+ type GuildMember struct {
28+ Name string `json:"name"`
29+ Title string `json:"title"`
30+ Rank string `json:"rank"`
31+ Vocation string `json:"vocation"`
32+ Level int `json:"level"`
33+ Joined string `json:"joined"`
34+ Status string `json:"status"`
35+ }
36+
37+ // Child of Guild
38+ type InvitedGuildMember struct {
39+ Name string `json:"name"`
40+ Date string `json:"date"`
41+ }
42+
43+ // Child of Guilds
44+ type Guild struct {
45+ Name string `json:"name"`
46+ World string `json:"world"`
47+ LogoURL string `json:"logo_url"`
48+ Description string `json:"description"`
49+ Guildhalls []Guildhall `json:"guildhalls"`
50+ Active bool `json:"active"`
51+ Founded string `json:"founded"`
52+ Applications bool `json:"open_applications"`
53+ Homepage string `json:"homepage"`
54+ InWar bool `json:"in_war"`
55+ DisbandedDate string `json:"disband_date"`
56+ DisbandedCondition string `json:"disband_condition"`
57+ PlayersOnline int `json:"players_online"`
58+ PlayersOffline int `json:"players_offline"`
59+ MembersTotal int `json:"members_total"`
60+ MembersInvited int `json:"members_invited"`
61+ Members []GuildMember `json:"members"`
62+ Invited []InvitedGuildMember `json:"invites"`
63+ }
64+
65+ // Child of JSONData
66+ type Guilds struct {
67+ Guild Guild `json:"guild"`
68+ }
69+
70+ //
71+ // The base includes two levels: Guild and Information
72+ type GuildResponse struct {
73+ Guilds Guilds `json:"guilds"`
74+ Information Information `json:"information"`
75+ }
76+
1377var (
1478 GuildLogoRegex = regexp .MustCompile (`.*img src="(.*)" width=.*` )
1579 GuildWorldAndFoundationRegex = regexp .MustCompile (`The guild was founded on (.*) on (.*).<br/>` )
@@ -22,99 +86,44 @@ var (
2286
2387// TibiaGuildsGuildV3 func
2488func TibiaGuildsGuildV3 (c * gin.Context ) {
25-
2689 // getting params from URL
2790 guild := c .Param ("guild" )
2891
29- // Child of Guild
30- type Guildhall struct {
31- Name string `json:"name"`
32- World string `json:"world"` // Maybe duplicate info? Guild can only be on one world..
33- /*
34- Town string `json:"town"` // We can collect that from cached info?
35- Status string `json:"status"` // rented (but maybe also auctioned)
36- Owner string `json:"owner"` // We can collect that from cached info?
37- HouseID int `json:"houseid"` // We can collect that from cached info?
38- */
39- PaidUntil string `json:"paid_until"` // Paid until date
40- }
41-
42- // Child of Guild
43- type Members struct {
44- Name string `json:"name"`
45- Title string `json:"title"`
46- Rank string `json:"rank"`
47- Vocation string `json:"vocation"`
48- Level int `json:"level"`
49- Joined string `json:"joined"`
50- Status string `json:"status"`
51- }
52- // Child of Guild
53- type Invited struct {
54- Name string `json:"name"`
55- Date string `json:"date"`
56- }
92+ // Getting data with TibiadataHTMLDataCollectorV3
93+ TibiadataRequest .URL = "https://www.tibia.com/community/?subtopic=guilds&page=view&GuildName=" + TibiadataQueryEscapeStringV3 (guild )
94+ BoxContentHTML , err := TibiadataHTMLDataCollectorV3 (TibiadataRequest )
5795
58- // Child of Guilds
59- type Guild struct {
60- Name string `json:"name"`
61- World string `json:"world"`
62- LogoURL string `json:"logo_url"`
63- Description string `json:"description"`
64- Guildhalls []Guildhall `json:"guildhalls"`
65- Active bool `json:"active"`
66- Founded string `json:"founded"`
67- Applications bool `json:"open_applications"`
68- Homepage string `json:"homepage"`
69- InWar bool `json:"in_war"`
70- DisbandedDate string `json:"disband_date"`
71- DisbandedCondition string `json:"disband_condition"`
72- PlayersOnline int `json:"players_online"`
73- PlayersOffline int `json:"players_offline"`
74- MembersTotal int `json:"members_total"`
75- MembersInvited int `json:"members_invited"`
76- Members []Members `json:"members"`
77- Invited []Invited `json:"invites"`
96+ // return error (e.g. for maintenance mode)
97+ if err != nil {
98+ TibiaDataAPIHandleOtherResponse (c , http .StatusBadGateway , "TibiaGuildsGuildV3" , gin.H {"error" : err .Error ()})
99+ return
78100 }
79101
80- // Child of JSONData
81- type Guilds struct {
82- Guild Guild `json:"guild"`
83- }
102+ jsonData := TibiaGuildsGuildV3Impl (guild , BoxContentHTML )
84103
85- //
86- // The base includes two levels: Guild and Information
87- type JSONData struct {
88- Guilds Guilds `json:"guilds"`
89- Information Information `json:"information"`
90- }
104+ // return jsonData
105+ TibiaDataAPIHandleSuccessResponse (c , "TibiaGuildsGuildV3" , jsonData )
106+ }
91107
108+ func TibiaGuildsGuildV3Impl (guild string , BoxContentHTML string ) GuildResponse {
92109 // Creating empty vars
93110 var (
94- MembersData []Members
95- InvitedData []Invited
111+ MembersData []GuildMember
112+ InvitedData []InvitedGuildMember
96113 GuildGuildhallData []Guildhall
97114 MembersRank , MembersTitle , MembersStatus , GuildDescription , GuildDisbandedDate , GuildDisbandedCondition , GuildHomepage , GuildWorld , GuildLogoURL , GuildFounded string
98- GuildActive , GuildApplications , GuildInWar , GuildNameDetected , GuildDescriptionFinished bool
115+ GuildActive , GuildApplications , GuildInWar , GuildDescriptionFinished bool
99116 MembersCountOnline , MembersCountOffline , MembersCountInvited int
100117 )
101118
102- // Getting data with TibiadataHTMLDataCollectorV3
103- TibiadataRequest .URL = "https://www.tibia.com/community/?subtopic=guilds&page=view&GuildName=" + TibiadataQueryEscapeStringV3 (guild )
104- BoxContentHTML , err := TibiadataHTMLDataCollectorV3 (TibiadataRequest )
105-
106- // return error (e.g. for maintenance mode)
107- if err != nil {
108- TibiaDataAPIHandleOtherResponse (c , http .StatusBadGateway , "TibiaGuildsGuildV3" , gin.H {"error" : err .Error ()})
109- return
110- }
111-
112119 // Loading HTML data into ReaderHTML for goquery with NewReader
113120 ReaderHTML , err := goquery .NewDocumentFromReader (strings .NewReader (BoxContentHTML ))
114121 if err != nil {
115122 log .Fatal (err )
116123 }
117124
125+ guildName , _ := ReaderHTML .Find ("h1" ).First ().Html ()
126+
118127 // Getting data from div.InnerTableContainer and then first p
119128 InnerTableContainerTMPA , err := ReaderHTML .Find (".BoxContent table" ).Html ()
120129 if err != nil {
@@ -130,13 +139,6 @@ func TibiaGuildsGuildV3(c *gin.Context) {
130139 }
131140
132141 for _ , line := range strings .Split (strings .TrimSuffix (InnerTableContainerTMPB , "\n " ), "\n " ) {
133-
134- // setting guild name based on html
135- if ! GuildNameDetected {
136- guild = strings .TrimSpace (RemoveHtmlTag (line ))
137- GuildNameDetected = true
138- }
139-
140142 // Guild information
141143 if ! GuildDescriptionFinished {
142144 // First line is the description..
@@ -199,7 +201,6 @@ func TibiaGuildsGuildV3(c *gin.Context) {
199201
200202 // Running query over each div
201203 ReaderHTML .Find (".TableContentContainer .TableContent tbody tr" ).Each (func (index int , s * goquery.Selection ) {
202-
203204 // Storing HTML into GuildsDivHTML
204205 GuildsDivHTML , err := s .Html ()
205206 if err != nil {
@@ -230,24 +231,22 @@ func TibiaGuildsGuildV3(c *gin.Context) {
230231 MembersCountOffline ++
231232 }
232233
233- MembersData = append (MembersData , Members {
234- Name : TibiaDataSanitizeEscapedString (subma1 [0 ][2 ]),
234+ MembersData = append (MembersData , GuildMember {
235+ Name : TibiaDataSanitizeNbspSpaceString (subma1 [0 ][2 ]),
235236 Title : MembersTitle ,
236237 Rank : MembersRank ,
237238 Vocation : subma1 [0 ][4 ],
238239 Level : TibiadataStringToIntegerV3 (subma1 [0 ][5 ]),
239240 Joined : TibiadataDateV3 (subma1 [0 ][6 ]),
240241 Status : MembersStatus ,
241242 })
242-
243243 } else {
244-
245244 // Regex to get data for record values
246245 subma2 := GuildMemberInvitesInformationRegex .FindAllStringSubmatch (GuildsDivHTML , - 1 )
247246
248247 if len (subma2 ) > 0 {
249248 MembersCountInvited ++
250- InvitedData = append (InvitedData , Invited {
249+ InvitedData = append (InvitedData , InvitedGuildMember {
251250 Name : subma2 [0 ][1 ],
252251 Date : subma2 [0 ][2 ],
253252 })
@@ -257,10 +256,10 @@ func TibiaGuildsGuildV3(c *gin.Context) {
257256
258257 //
259258 // Build the data-blob
260- jsonData := JSONData {
259+ return GuildResponse {
261260 Guilds {
262261 Guild {
263- Name : guild ,
262+ Name : guildName ,
264263 World : GuildWorld ,
265264 LogoURL : GuildLogoURL ,
266265 Description : GuildDescription ,
@@ -286,7 +285,4 @@ func TibiaGuildsGuildV3(c *gin.Context) {
286285 Timestamp : TibiadataDatetimeV3 ("" ),
287286 },
288287 }
289-
290- // return jsonData
291- TibiaDataAPIHandleSuccessResponse (c , "TibiaGuildsGuildV3" , jsonData )
292288}
0 commit comments