-
Notifications
You must be signed in to change notification settings - Fork 1
/
rank.go
124 lines (100 loc) · 2.69 KB
/
rank.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
package gobgg
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
)
// Rank API is based on the site API, and not the official api
const (
apiCollectionUrl = "api/collections"
apiCollectionItemUrl = "api/collectionitems/%s"
)
type (
rankResponse struct {
Items []map[string]interface{} `json:"items"`
}
rankRequest struct {
Item map[string]interface{} `json:"item"`
}
)
func (bgg *BGG) myCollections(ctx context.Context, objectID int64) (*rankResponse, error) {
name := bgg.GetActiveUsername()
if len(bgg.GetActiveCookies()) == 0 || name == "" {
return nil, fmt.Errorf("call Login first")
}
usr, err := bgg.GetUser(ctx, name)
if err != nil {
return nil, err
}
params := map[string]string{
"objectid": fmt.Sprint(objectID),
"objecttype": "thing",
"userid": fmt.Sprint(usr.UserID),
}
u := bgg.buildURL(apiCollectionUrl, params)
req, err := http.NewRequestWithContext(ctx, http.MethodGet, u, nil)
if err != nil {
return nil, fmt.Errorf("failed to create the request: %w", err)
}
req.Header.Add("content-type", "application/json")
bgg.requestCookies(req)
resp, err := bgg.do(req)
if err != nil {
return nil, fmt.Errorf("http call failed: %w", err)
}
defer resp.Body.Close()
b, err := io.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("read body failed: %w", err)
}
var items rankResponse
if err := json.Unmarshal(b, &items); err != nil {
return nil, fmt.Errorf("decoding JSON failed: %w", err)
}
return &items, nil
}
// SetRank tries to add rank for an item (experimental)
func (bgg *BGG) SetRank(ctx context.Context, objectID int64, rate float64) error {
if rate <= 0 || rate > 10 {
return fmt.Errorf("invalid rate range: %f", rate)
}
items, err := bgg.myCollections(ctx, objectID)
if err != nil {
return err
}
if len(items.Items) == 0 {
return fmt.Errorf("no item found")
}
item := items.Items[0]
collid, ok := item["collid"].(string)
if !ok {
return fmt.Errorf("the response has no collid")
}
item["rating"] = rate
update := rankRequest{
Item: item,
}
b, err := json.Marshal(update)
if err != nil {
return fmt.Errorf("marshaling json failed: %w", err)
}
url := bgg.buildURL(fmt.Sprintf(apiCollectionItemUrl, collid), nil)
req, err := http.NewRequestWithContext(ctx, http.MethodPut, url, bytes.NewBuffer(b))
if err != nil {
return fmt.Errorf("failed to create the request: %w", err)
}
req.Header.Add("content-type", "application/json")
bgg.requestCookies(req)
resp, err := bgg.do(req)
if err != nil {
return fmt.Errorf("http call failed: %w", err)
}
defer resp.Body.Close()
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
return fmt.Errorf("failed with status code")
}
return nil
}