This repository has been archived by the owner on Jan 8, 2019. It is now read-only.
/
user.go
239 lines (231 loc) · 7.34 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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
package kitsu
import (
"encoding/json"
"fmt"
"net/url"
"github.com/Jeffail/gabs"
)
// User struct with all the user's data from kitsu
type User struct {
ID string `json:"id"`
Type string `json:"type"`
Links struct {
Self string `json:"self"`
} `json:"links"`
Attributes struct {
CreatedAt string `json:"createdAt"`
UpdatedAt string `json:"updatedAt"`
Name string `json:"name"`
PastNames []string `json:"pastNames"`
Slug string `json:"slug"`
About string `json:"about"`
Location string `json:"location"`
WaifuOrHusbando string `json:"waifuOrHusbando"`
FollowersCount int `json:"followersCount"`
FollowingCount int `json:"followingCount"`
LifeSpentOnAnime int `json:"lifeSpentOnAnime"`
Birthday string `json:"birthday"`
Gender string `json:"gender"`
CommentsCount int `json:"commentsCount"`
FavoritesCount int `json:"favoritesCount"`
LikesGivenCount int `json:"likesGivenCount"`
ReviewsCount int `json:"reviewsCount"`
LikesReceivedCount int `json:"likesReceivedCount"`
PostsCount int `json:"postsCount"`
RatingsCount int `json:"ratingsCount"`
MediaReactionsCount int `json:"mediaReactionsCount"`
ProExpiresAt string `json:"proExpiresAt"`
Title string `json:"title"`
ProfileCompleted bool `json:"profileCompleted"`
FeedCompleted bool `json:"feedCompleted"`
Website string `json:"website"`
Avatar struct {
Tiny string `json:"tiny"`
Small string `json:"small"`
Medium string `json:"medium"`
Large string `json:"large"`
Original string `json:"original"`
} `json:"avatar"`
CoverImage struct {
Tiny string `json:"tiny"`
Small string `json:"small"`
Large string `json:"large"`
Original string `json:"original"`
} `json:"coverImage"`
RatingSystem string `json:"ratingSystem"`
Theme string `json:"theme"`
FacebookID string `json:"facebookId"`
} `json:"attributes"`
Relationships struct{} `json:"relationships"` // Relationships can contain different stuff for everyone
}
// SearchUser search for a user on kitsu.io
// query being the user to search for
func SearchUser(query string) (*User, error) {
newQuery := url.QueryEscape(query)
uri := fmt.Sprintf("%s/users?filter[name]=%s", baseURL, newQuery)
byt, er := get(uri)
if er != nil {
return nil, er
}
parJSON, e := gabs.ParseJSON(byt)
if e != nil {
return nil, e
}
user := parJSON.Path("data").Data().([]interface{})
if len(user) <= 0 {
err := fmt.Errorf("Could not find any users with the query: %s", query)
return nil, err
}
resJSON, er := json.Marshal(user[0])
if er != nil {
return nil, er
}
u := new(User)
err := json.Unmarshal(resJSON, &u)
if err != nil {
return nil, err
}
return u, nil
}
// UserByID struct with all the user's data from kitsu
type UserByID struct {
ID string `json:"id"`
Type string `json:"type"`
Links struct {
Self string `json:"self"`
} `json:"links"`
Attributes struct {
CreatedAt string `json:"createdAt"`
UpdatedAt string `json:"updatedAt"`
Name string `json:"name"`
PastNames []string `json:"pastNames"`
Slug string `json:"slug"`
About string `json:"about"`
Location string `json:"location"`
WaifuOrHusbando string `json:"waifuOrHusbando"`
FollowersCount int `json:"followersCount"`
FollowingCount int `json:"followingCount"`
LifeSpentOnAnime int `json:"lifeSpentOnAnime"`
Birthday string `json:"birthday"`
Gender string `json:"gender"`
CommentsCount int `json:"commentsCount"`
FavoritesCount int `json:"favoritesCount"`
LikesGivenCount int `json:"likesGivenCount"`
ReviewsCount int `json:"reviewsCount"`
LikesReceivedCount int `json:"likesReceivedCount"`
PostsCount int `json:"postsCount"`
RatingsCount int `json:"ratingsCount"`
MediaReactionsCount int `json:"mediaReactionsCount"`
ProExpiresAt string `json:"proExpiresAt"`
Title string `json:"title"`
ProfileCompleted bool `json:"profileCompleted"`
FeedCompleted bool `json:"feedCompleted"`
Website string `json:"website"`
Avatar struct {
Tiny string `json:"tiny"`
Small string `json:"small"`
Medium string `json:"medium"`
Large string `json:"large"`
Original string `json:"original"`
} `json:"avatar"`
CoverImage struct {
Tiny string `json:"tiny"`
Small string `json:"small"`
Large string `json:"large"`
Original string `json:"original"`
} `json:"coverImage"`
RatingSystem string `json:"ratingSystem"`
Theme string `json:"theme"`
FacebookID string `json:"facebookId"`
} `json:"attributes"`
Relationships struct{} `json:"relationships"`
}
// GetUser get a user by his/her id from kitsu.io
// id of course being the id
func GetUser(id int) (*UserByID, error) {
uri := fmt.Sprintf("%s/users/%d", baseURL, id)
byt, er := get(uri)
if er != nil {
return nil, er
}
parJSON, e := gabs.ParseJSON(byt)
if e != nil {
return nil, e
}
user := parJSON.Path("data").Data().(map[string]interface{})
resJSON, er := json.Marshal(user)
if er != nil {
return nil, er
}
u := new(UserByID)
err := json.Unmarshal(resJSON, &u)
if err != nil {
return nil, err
}
return u, nil
}
// Stats struct with all the stats from the user
type Stats struct {
ID string `json:"id"`
Type string `json:"type"`
Links struct {
Self string `json:"self"`
} `json:"links"`
Attributes struct {
CreatedAt string `json:"createdAt"`
UpdatedAt string `json:"updatedAt"`
Kind string `json:"kind"`
StatsData struct {
Total int `json:"total"`
TotalMedia int `json:"total_media"`
Activity []string `json:"activity"`
AllCategories struct {
War int `json:"war"`
Asia int `json:"asia"`
Cops int `json:"cops"`
Idol int `json:"idol"`
Mars int `json:"mars"`
Navy int `json:"navy"`
Past int `json:"past"`
Alien int `json:"alien"`
Angel int `json:"angel"`
Angst int `json:"angst"`
China int `json:"china"`
Crime int `json:"crime"`
Deity int `json:"deity"`
Demon int `json:"demon"`
} `json:"all_categories"`
AllTime struct {
TotalTime int `json:"total_time"`
TotalMedia int `json:"total_media"`
TotalProgress int `json:"total_progress"`
} `json:"all_time"`
AllYears struct{} `json:"all_years"`
} `json:"statsData"`
} `json:"attributes"`
Relationships struct{} `json:"relationships"`
}
// GetStats get the stats of a user by his/her id from kitsu.io
// id of course being the id
func GetStats(id int) (*Stats, error) {
uri := fmt.Sprintf("%s/stats/%d", baseURL, id)
byt, er := get(uri)
if er != nil {
return nil, er
}
parJSON, e := gabs.ParseJSON(byt)
if e != nil {
return nil, e
}
stats := parJSON.Path("data").Data().(map[string]interface{})
resJSON, er := json.Marshal(stats)
if er != nil {
return nil, er
}
sts := new(Stats)
err := json.Unmarshal(resJSON, &sts)
if err != nil {
return nil, err
}
return sts, nil
}