-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nicovideo_user_response.go
92 lines (80 loc) · 2.16 KB
/
nicovideo_user_response.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
package nico
import (
"context"
"encoding/xml"
"errors"
"fmt"
"net/http"
"net/url"
"strconv"
)
// NicovideoUserResponse is the response of the user information acquisition API.
type NicovideoUserResponse struct {
Status string `xml:"status,attr"`
UserInfo UserInfo `xml:"user"`
VitaOption VitaOption `xml:"vita_option"`
Additionals string `xml:"additionals"`
Error UserInfoError `xml:"error"`
}
// UserInfo is user information.
type UserInfo struct {
ID int64 `xml:"id"`
Nickname string `xml:"nickname"`
ThumbnailURL string `xml:"thumbnail_url"`
}
// VitaOption is an option of Vita.
type VitaOption struct {
UserSecret int64 `xml:"user_secret"`
}
// UserInfoError is an error when acquiring user information.
type UserInfoError struct {
Status string
Code string `xml:"code"`
Description string `xml:"description"`
}
func (e UserInfoError) Error() string {
return fmt.Sprintf("%s: %s: %s", e.Status, e.Code, e.Description)
}
// GetNicovideoUserResponse gets the response of the user information API.
// No login required.
func (c *Client) GetNicovideoUserResponse(ctx context.Context, userID int64) (*NicovideoUserResponse, error) {
u, err := url.Parse(c.ceBaseRawurl)
if err != nil {
return nil, err
}
u.Path = "api/v1/user.info"
v := url.Values{}
v.Set("user_id", strconv.FormatInt(userID, 10))
u.RawQuery = v.Encode()
req, err := http.NewRequest(http.MethodGet, u.String(), nil)
if err != nil {
return nil, err
}
req = req.WithContext(ctx)
resp, err := c.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, errors.New(resp.Status)
}
nur := NicovideoUserResponse{}
if err := xml.NewDecoder(resp.Body).Decode(&nur); err != nil {
return nil, err
}
if nur.Status != "ok" {
nur.Error.Status = nur.Status
return nil, nur.Error
}
return &nur, nil
}
// GetUserInfo is get user's information.
// No login required.
func (c *Client) GetUserInfo(ctx context.Context, userID int64) (*UserInfo, error) {
nur, err := c.GetNicovideoUserResponse(ctx, userID)
if err != nil {
return nil, err
}
return &nur.UserInfo, nil
}