-
Notifications
You must be signed in to change notification settings - Fork 0
/
search.go
65 lines (52 loc) · 1.34 KB
/
search.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
package ffxivapi
import (
"github.com/PuerkitoBio/goquery"
"strconv"
"strings"
)
type SearchResult struct {
ID int
Level int
Avatar string
Lang string
Name string
World string
}
func (api *FFXIVAPI) Search(characterName string, world string) ([]SearchResult, error) {
doc, err := api.lodestone("/lodestone/character/", map[string]string{
"q": characterName,
"worldname": strings.Title(strings.ToLower(world)),
})
if err != nil {
return nil, err
}
results := make([]SearchResult, 0, 1)
doc.Find("a.entry__link").Each(func(i int, sel *goquery.Selection) {
result := SearchResult{}
idlink, found := sel.Attr("href")
if !found {
return
}
parts := strings.Split(strings.Trim(idlink, "/"), "/")
if len(parts) == 0 {
return
}
result.ID, _ = strconv.Atoi(parts[len(parts)-1])
image := sel.Find("img").First()
imagesrc, found := image.Attr("src")
if !found {
return
}
result.Avatar = imagesrc
result.Name = sel.Find("p.entry__name").First().Text()
result.World = sel.Find("p.entry__world").First().Text()
result.Lang = sel.Find(".entry__chara__lang").First().Text()
levelText := sel.Find(".entry__chara_info").First().Find("span").First().Text()
if levelText == "" {
return
}
result.Level, _ = strconv.Atoi(levelText)
results = append(results, result)
})
return results, nil
}