This repository has been archived by the owner on Sep 22, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
user.go
193 lines (164 loc) · 5.31 KB
/
user.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
package parser
import (
"encoding/json"
"io"
"io/ioutil"
"net/http"
"github.com/rl404/go-malscraper/errors"
"github.com/rl404/go-malscraper/model"
"github.com/rl404/go-malscraper/pkg/utils"
)
// GetUser to get user details.
func (p *Parser) GetUser(user string) (*model.User, int, error) {
doc, code, err := p.getDoc(utils.BuildURL(malURL, "profile", user), "#content")
if err != nil {
return nil, code, err
}
return p.user.GetDetails(doc), http.StatusOK, nil
}
// GetUserStats to get user stats details.
func (p *Parser) GetUserStats(user string) (*model.UserStats, int, error) {
doc, code, err := p.getDoc(utils.BuildURL(malURL, "profile", user), "#content")
if err != nil {
return nil, code, err
}
return p.user.GetStats(doc), http.StatusOK, nil
}
// GetUserFavorite to get user favorite list.
func (p *Parser) GetUserFavorite(user string) (*model.UserFavorite, int, error) {
doc, code, err := p.getDoc(utils.BuildURL(malURL, "profile", user), "#content")
if err != nil {
return nil, code, err
}
return p.user.GetFavorites(doc), http.StatusOK, nil
}
// GetUserFriend to get user friend list.
func (p *Parser) GetUserFriend(user string, page int) ([]model.UserFriend, int, error) {
q := map[string]interface{}{"offset": 100 * (page - 1)}
doc, code, err := p.getDoc(utils.BuildURLWithQuery(q, malURL, "profile", user, "friends"), "#content")
if err != nil {
return nil, code, err
}
return p.user.GetFriends(doc), http.StatusOK, nil
}
// GetUserHistory to get user history list.
func (p *Parser) GetUserHistory(user string, t string) ([]model.UserHistory, int, error) {
dir := []interface{}{"history", user}
if t != "" {
dir = append(dir, t)
}
doc, code, err := p.getDoc(utils.BuildURL(malURL, dir...), "#content")
if err != nil {
return nil, code, err
}
return p.user.GetHistory(doc), http.StatusOK, nil
}
// GetUserReview to get user review list.
func (p *Parser) GetUserReview(user string, page int) ([]model.Review, int, error) {
q := map[string]interface{}{"p": page}
doc, code, err := p.getDoc(utils.BuildURLWithQuery(q, malURL, "profile", user, "reviews"), "#content")
if err != nil {
return nil, code, err
}
return p.user.GetReviews(doc), http.StatusOK, nil
}
// GetUserRecommendation to get user recommendation list.
func (p *Parser) GetUserRecommendation(user string, page int) ([]model.Recommendation, int, error) {
q := map[string]interface{}{"p": page}
doc, code, err := p.getDoc(utils.BuildURLWithQuery(q, malURL, "profile", user, "recommendations"), ".container-right")
if err != nil {
return nil, code, err
}
return p.user.GetRecommendations(doc), http.StatusOK, nil
}
// GetUserClub to get user club list.
func (p *Parser) GetUserClub(user string) ([]model.Item, int, error) {
doc, code, err := p.getDoc(utils.BuildURL(malURL, "profile", user, "clubs"), "#content")
if err != nil {
return nil, code, err
}
return p.user.GetClubs(doc), http.StatusOK, nil
}
// Testable function.
var readBody func(io.Reader) ([]byte, error) = ioutil.ReadAll
var decodeJSON func([]byte, interface{}) error = json.Unmarshal
// GetUserAnime to get user anime list.
func (p *Parser) GetUserAnime(query model.UserListQuery) ([]model.UserAnime, int, error) {
data := []model.UserAnime{}
q := map[string]interface{}{
"status": query.Status,
"order": query.Order,
"tag": query.Tag,
}
offset := 300 * (query.Page - 1)
if query.Page == -1 {
offset = 0
}
for {
// Get body response.
q["offset"] = offset
body, code, err := p.getBody(utils.BuildURLWithQuery(q, malURL, "animelist", query.Username, "load.json"))
if err != nil {
return nil, code, err
}
// Read body.
resp, err := readBody(body)
if err != nil {
p.logger.Error("failed reading body: %s", err.Error())
return nil, http.StatusInternalServerError, errors.ErrParseBody
}
// Decode to arrays.
var raw []model.UserRawAnime
if err = decodeJSON(resp, &raw); err != nil {
p.logger.Error("failed decode JSON: %s", err.Error())
return nil, http.StatusInternalServerError, errors.ErrDecodeJSON
}
tmp := p.user.GetAnime(raw)
data = append(data, tmp...)
if len(tmp) == 0 || len(tmp) < 300 || query.Page != -1 {
break
}
offset += 300
}
return data, http.StatusOK, nil
}
// GetUserManga to get user manga list.
func (p *Parser) GetUserManga(query model.UserListQuery) ([]model.UserManga, int, error) {
data := []model.UserManga{}
q := map[string]interface{}{
"status": query.Status,
"order": query.Order,
"tag": query.Tag,
}
offset := 300 * (query.Page - 1)
if query.Page == -1 {
offset = 0
}
for {
// Get body response.
q["offset"] = offset
body, code, err := p.getBody(utils.BuildURLWithQuery(q, malURL, "mangalist", query.Username, "load.json"))
if err != nil {
return nil, code, err
}
// Read body.
resp, err := readBody(body)
if err != nil {
p.logger.Error("failed reading body: %s", err.Error())
return nil, http.StatusInternalServerError, errors.ErrParseBody
}
// Decode to arrays.
var raw []model.UserRawManga
if err = decodeJSON(resp, &raw); err != nil {
p.logger.Error("failed decode JSON: %s", err.Error())
return nil, http.StatusInternalServerError, errors.ErrDecodeJSON
}
tmp := p.user.GetManga(raw)
data = append(data, tmp...)
if len(tmp) == 0 || len(tmp) < 300 || query.Page != -1 {
break
}
offset += 300
}
return data, http.StatusOK, nil
}