-
Notifications
You must be signed in to change notification settings - Fork 26
/
TibiaGuildsOverviewV3.go
102 lines (86 loc) · 2.73 KB
/
TibiaGuildsOverviewV3.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
package main
import (
"log"
"strings"
"github.com/PuerkitoBio/goquery"
)
// Child of Guilds
type OverviewGuild struct {
Name string `json:"name"`
LogoURL string `json:"logo_url"`
Description string `json:"description"`
}
// Child of JSONData
type OverviewGuilds struct {
World string `json:"world"`
Active []OverviewGuild `json:"active"`
Formation []OverviewGuild `json:"formation"`
}
//
// The base includes two levels: Guilds and Information
type GuildsOverviewResponse struct {
Guilds OverviewGuilds `json:"guilds"`
Information Information `json:"information"`
}
func TibiaGuildsOverviewV3Impl(world string, BoxContentHTML string) GuildsOverviewResponse {
// Creating empty vars
var (
ActiveGuilds, FormationGuilds []OverviewGuild
GuildCategory string
)
// Loading HTML data into ReaderHTML for goquery with NewReader
ReaderHTML, err := goquery.NewDocumentFromReader(strings.NewReader(BoxContentHTML))
if err != nil {
log.Fatal(err)
}
// Running query over each div
ReaderHTML.Find(".TableContainer").Each(func(index int, s *goquery.Selection) {
// Figure out the guild category
s.Find(".Text").Each(func(index int, s *goquery.Selection) {
tableName := s.Nodes[0].FirstChild.Data
if strings.Contains(tableName, "Active Guilds") {
GuildCategory = "active"
} else if strings.Contains(tableName, "Guilds in Course of Formation") {
GuildCategory = "formation"
}
})
if GuildCategory != "" {
// Extract guilds
s.Find(".TableContent tbody").Children().NextAll().Each(func(index int, s *goquery.Selection) {
tableRow := s.Nodes[0]
nameAndDescriptionNode := tableRow.FirstChild.NextSibling.NextSibling
name := nameAndDescriptionNode.FirstChild.FirstChild.Data
logoURL := tableRow.FirstChild.FirstChild.Attr[0].Val
description := ""
// Check if there's a description to fetch.
if nameAndDescriptionNode.FirstChild.NextSibling != nil && nameAndDescriptionNode.FirstChild.NextSibling.NextSibling != nil {
description = strings.TrimSpace(nameAndDescriptionNode.FirstChild.NextSibling.NextSibling.Data)
}
OneGuild := OverviewGuild{
Name: name,
LogoURL: logoURL,
Description: description,
}
// Adding OneGuild to correct category
if GuildCategory == "active" {
ActiveGuilds = append(ActiveGuilds, OneGuild)
} else if GuildCategory == "formation" {
FormationGuilds = append(FormationGuilds, OneGuild)
}
})
}
})
//
// Build the data-blob
return GuildsOverviewResponse{
OverviewGuilds{
World: world,
Active: ActiveGuilds,
Formation: FormationGuilds,
},
Information{
APIVersion: TibiadataAPIversion,
Timestamp: TibiadataDatetimeV3(""),
},
}
}