-
Notifications
You must be signed in to change notification settings - Fork 1
/
Character.go
187 lines (142 loc) · 4.28 KB
/
Character.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
package ffxiv
import (
"bytes"
"errors"
"fmt"
"regexp"
"strconv"
"strings"
"unicode"
"github.com/PuerkitoBio/goquery"
"github.com/aerogo/http/client"
)
var digit = regexp.MustCompile("[0-9]+")
// Character represents a Final Fantasy XIV character.
type Character struct {
Nick string
Server string
DataCenter string
Class string
Level int
ItemLevel int
}
// GetCharacter fetches character data for a given character ID.
func GetCharacter(id string) (*Character, error) {
url := fmt.Sprintf("https://na.finalfantasyxiv.com/lodestone/character/%s", id)
response, err := client.Get(url).End()
if err != nil {
return nil, err
}
page := response.Bytes()
reader := bytes.NewReader(page)
document, err := goquery.NewDocumentFromReader(reader)
if err != nil {
return nil, err
}
characterName := document.Find(".frame__chara__name").Text()
if characterName == "" {
return nil, errors.New("Error parsing character name")
}
// This will look like: "Asura (Mana)"
characterServerAndDataCenter := document.Find(".frame__chara__world").Text()
if characterServerAndDataCenter == "" {
return nil, errors.New("Error parsing character server")
}
// Normalize whitespace characters
characterServerAndDataCenter = strings.Map(func(r rune) rune {
if unicode.IsSpace(r) {
return ' '
}
return r
}, characterServerAndDataCenter)
// Split the server and data center
serverInfo := strings.Split(characterServerAndDataCenter, " ")
if len(serverInfo) < 2 {
return nil, errors.New("Character server info does not seem to include the data center")
}
characterServer := serverInfo[0]
characterDataCenter := serverInfo[1]
characterDataCenter = strings.TrimPrefix(characterDataCenter, "(")
characterDataCenter = strings.TrimSuffix(characterDataCenter, ")")
if characterDataCenter == "" {
return nil, errors.New("Error parsing character data center")
}
// Level
characterLevel := document.Find(".character__class__data").Text()
if characterLevel == "" {
return nil, errors.New("Error parsing character level")
}
// Weapon
characterWeapon := document.Find(".db-tooltip__item__category").Text()
if characterWeapon == "" {
return nil, errors.New("Error parsing character class")
}
level, err := strconv.Atoi(digit.FindStringSubmatch(characterLevel)[0])
if err != nil {
return nil, err
}
itemLevel := calculateItemLevel(document)
className := getJobName(document)
if className == "" {
// Determine class name using the weapon info
className = strings.Split(characterWeapon, "'")[0]
className = strings.Replace(className, "Two-handed", "", -1)
className = strings.Replace(className, "One-handed", "", -1)
className = strings.TrimSpace(className)
}
character := &Character{
Nick: characterName,
Class: className,
Server: characterServer,
DataCenter: characterDataCenter,
Level: level,
ItemLevel: itemLevel,
}
return character, nil
}
// calculateItemLevel will try to return the average of all item levels.
func calculateItemLevel(document *goquery.Document) int {
items := document.Find(".item_detail_box")
itemCount := 0
itemLevelSum := 0
items.Each(func(i int, item *goquery.Selection) {
itemCategory := strings.ToLower(item.Find(".db-tooltip__item__category").Text())
// Ignore soul crystals
if itemCategory == "soul crystal" {
return
}
// Two-handed weapons are counted twice
weight := 1
if strings.Contains(itemCategory, "two-handed") {
weight = 2
}
// Find item level
itemLevelText := item.Find(".db-tooltip__item__level").Text()
itemLevelText = digit.FindStringSubmatch(itemLevelText)[0]
itemLevel, err := strconv.Atoi(itemLevelText)
if err != nil {
return
}
itemLevelSum += itemLevel * weight
itemCount += weight
})
if itemCount == 0 {
return 0
}
return itemLevelSum / itemCount
}
// getJobName finds the job name by looking at the soul crystal text.
func getJobName(document *goquery.Document) string {
const soulCrystalPrefix = "Soul of the "
jobName := ""
itemNames := document.Find(".db-tooltip__item__name")
itemNames.EachWithBreak(func(i int, item *goquery.Selection) bool {
itemName := item.Text()
if strings.HasPrefix(itemName, soulCrystalPrefix) {
jobName = itemName[len(soulCrystalPrefix):]
return false
}
return true
})
return jobName
}