-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCharacterID.go
51 lines (37 loc) · 1.09 KB
/
CharacterID.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
package ffxiv
import (
"bytes"
"errors"
"fmt"
"strings"
"github.com/PuerkitoBio/goquery"
"github.com/aerogo/http/client"
)
// GetCharacterID fetches the first found character ID for the given nick and server.
func GetCharacterID(nick string, server string) (string, error) {
// Replace spaces with plus signs
nick = strings.Replace(nick, " ", "+", -1)
// Fetch the page
url := fmt.Sprintf("https://na.finalfantasyxiv.com/lodestone/character/?q=%s&worldname=%s", nick, server)
response, err := client.Get(url).End()
if err != nil {
return "", err
}
page := response.Bytes()
reader := bytes.NewReader(page)
document, err := goquery.NewDocumentFromReader(reader)
if err != nil {
return "", err
}
links := document.Find(".entry__link")
if links.Length() == 0 {
return "", errors.New("Character does not exist")
}
href := links.First().AttrOr("href", "")
if !strings.HasPrefix(href, "/lodestone/character/") {
return "", errors.New("Character does not exist")
}
id := strings.TrimPrefix(href, "/lodestone/character/")
id = strings.TrimSuffix(id, "/")
return id, nil
}