-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
127 lines (121 loc) · 3.28 KB
/
client.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
package uplay
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"net/url"
)
type Client struct {
username string
password string
appID string
ticket string
}
func (c *Client) Authenticate() error {
req, err := http.NewRequest(
"POST",
"https://uplayconnect.ubi.com/ubiservices/v2/profiles/sessions",
bytes.NewBuffer([]byte("{\"rememberMe\":true}")),
)
if err != nil {
panic(err)
}
req.Header.Set("Ubi-AppId", c.appID)
req.Header.Set("Ubi-RequestedPlatformType", "uplay")
req.Header.Set("X-Requested-With", "XMLHttpRequest")
req.Header.Set("Content-Type", "application/json")
req.SetBasicAuth(c.username, c.password)
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
dec := json.NewDecoder(resp.Body)
var data struct {
HTTPCode int `json:"httpCode"`
Message string `json:"message"`
Ticket string `json:"ticket"`
}
if err := dec.Decode(&data); err != nil {
return nil
}
if data.Ticket == "" {
return fmt.Errorf("Ubisoft login returned HTTP code %d: %s", data.HTTPCode, data.Message)
}
c.ticket = data.Ticket
return nil
}
func New(username, password string) *Client {
return &Client{
appID: "314d4fef-e568-454a-ae06-43e3bece12a6",
username: username,
password: password,
}
}
func (c *Client) UserSearch(platform int, username string) ([]Profile, error) {
var data = struct {
Profiles []Profile
}{}
platformString, ok := uplayoverlayPlatforms[platform]
if !ok {
return nil, fmt.Errorf("Invalid platform type specified")
}
url := fmt.Sprintf(
"https://uplayoverlay.ubi.com/ubiservices/v1/profiles?nameOnPlatform=%s&platformType=%s",
url.QueryEscape(username),
url.QueryEscape(platformString),
)
req, err := http.NewRequest("GET", url, nil)
if err != nil {
panic(err)
}
req.Header.Set("Authorization", fmt.Sprintf("Ubi_v1 t=%s", c.ticket))
req.Header.Set("Ubi-AppId", "f35adcb5-1911-440c-b1c9-48fdc1701c68")
client := &http.Client{}
if rsp, err := client.Do(req); err != nil {
return data.Profiles, fmt.Errorf("Error requesting data: %s", err.Error())
} else {
defer rsp.Body.Close()
dec := json.NewDecoder(rsp.Body)
if err := dec.Decode(&data); err != nil {
return data.Profiles, fmt.Errorf("Error decoding data: %s", err.Error())
}
}
return data.Profiles, nil
}
func (c *Client) DivisionStats(platform int, uuid string) ([]DivisionStat, error) {
var data []struct {
Stats []DivisionStat `json:"stats"`
}
platformString, ok := uplaywebcenterPlatforms[platform]
if !ok {
return nil, fmt.Errorf("Invalid platform type specified")
}
req, err := http.NewRequest(
"GET",
fmt.Sprintf("https://uplaywebcenter.ubi.com/v1/stats/playerStats/?game=TCTD&locale=en-GB&platform=%s&userId=%s", url.QueryEscape(platformString), url.QueryEscape(uuid)),
nil,
)
if err != nil {
panic(err)
}
req.Header.Set("Authorization", fmt.Sprintf("Ubi_v1 t=%s", c.ticket))
req.Header.Set("Ubi-AppId", "f35adcb5-1911-440c-b1c9-48fdc1701c68")
client := &http.Client{}
if rsp, err := client.Do(req); err != nil {
return []DivisionStat{}, err
} else {
defer rsp.Body.Close()
dec := json.NewDecoder(rsp.Body)
dec.UseNumber()
if err := dec.Decode(&data); err != nil {
return []DivisionStat{}, err
}
}
if len(data) > 0 {
return data[0].Stats, nil
}
return []DivisionStat{}, nil
}