-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdetails.go
133 lines (122 loc) · 4.85 KB
/
details.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
package tohru
import (
"encoding/json"
"io"
"net/http"
"net/url"
"strconv"
)
type animeDetailsEndRes struct {
Response AnimeDetails `json:"response"`
}
type moreInfoResult struct {
Score string `json:"score"`
ScoredBy string `json:"scored_by"`
TrailerURL string `json:"trailer_url"`
Source string `json:"source"`
Episodes interface{} `json:"episodes"`
Duration string `json:"duration"`
AiredFrom string `json:"aired_from"`
AiredTo interface{} `json:"aired_to"`
AnimeStudioIds string `json:"anime_studio_ids"`
AnimeStudios string `json:"anime_studios"`
}
type commentFlagReasons struct {
CommentFlagReasonID string `json:"comment_flag_reason_id"`
FlagReason string `json:"flag_reason"`
FlagReasonOrder string `json:"flag_reason_order"`
}
type contentRating struct {
ContentType string `json:"content_type"`
Level string `json:"level"`
VoteCount string `json:"vote_count"`
}
type News struct {
NewsID string `json:"news_id"`
AnimeID string `json:"anime_id"`
AnimeName string `json:"anime_name"`
NewsTitle string `json:"news_title"`
NewsDescription string `json:"news_description"`
NewsDescriptionHTML interface{} `json:"news_description_html"`
NewsImage string `json:"news_image"`
NewsSourceLink string `json:"news_source_link"`
NewsVideoLink string `json:"news_video_link"`
NewsPublished string `json:"news_published"`
NewsCreatedAt string `json:"news_created_at"`
NewsUpdatedAt interface{} `json:"news_updated_at"`
NewsImageURL string `json:"news_image_url"`
NewsUpdatedAtFormat string `json:"news_updated_at_format"`
NewsCreatedAtFormat string `json:"news_created_at_format"`
}
type RelatedNews struct {
Data []News `json:"data"`
}
type AnimeDetails struct {
AnimeID string `json:"anime_id"`
AnimeName string `json:"anime_name"`
AnimeType string `json:"anime_type"`
AnimeStatus string `json:"anime_status"`
AnimeSeason string `json:"anime_season"`
AnimeReleaseYear string `json:"anime_release_year"`
AnimeAgeRating string `json:"anime_age_rating"`
AnimeRating string `json:"anime_rating"`
AnimeRatingUserCount string `json:"anime_rating_user_count"`
AnimeDescription string `json:"anime_description"`
AnimeCoverImage string `json:"anime_cover_image"`
AnimeTrailerURL string `json:"anime_trailer_url"`
AnimeEnglishTitle string `json:"anime_english_title"`
AnimeKeywords string `json:"anime_keywords"`
AnimeUpdatedAt string `json:"anime_updated_at"`
AnimeCreatedAt string `json:"anime_created_at"`
AnimeGenreIds string `json:"anime_genre_ids"`
AnimeGenres string `json:"anime_genres"`
AnimeReleaseDay string `json:"anime_release_day"`
AnimeCoverImageURL string `json:"anime_cover_image_url"`
AnimeUpdatedAtFormat string `json:"anime_updated_at_format"`
AnimeCreatedAtFormat string `json:"anime_created_at_format"`
MoreInfoResult moreInfoResult `json:"more_info_result"`
RelatedAnimes interface{} `json:"related_animes"`
RelatedNews interface{} `json:"related_news"`
CommentFlagReasons []commentFlagReasons `json:"comment_flag_reasons"`
ContentRating []contentRating `json:"content_rating"`
Role string `json:"role"`
}
type RelatedAnimes struct {
Animes []Anime `json:"data"`
}
// UnmarshalJSON Implements a custom marsheler
// Because API have bad design and need to check edge cases
func (ad *AnimeDetails) UnmarshalJSON(b []byte) error {
type details AnimeDetails
var a details
if err := json.Unmarshal(b, &a); err != nil {
return err
}
switch a.RelatedAnimes.(type) {
case []interface{}:
a.RelatedAnimes = RelatedAnimes{}
}
switch a.RelatedNews.(type) {
case []interface{}:
a.RelatedNews = RelatedNews{}
}
*ad = AnimeDetails(a)
return nil
}
func (s *AnimeService) GetAnimeDetails(animeID int) (AnimeDetails, error) {
params := url.Values{}
id := strconv.Itoa(animeID)
params.Set("anime_id", id)
params.Set("fetch_episodes", "No")
params.Set("more_info", "Yes")
res, err := s.getAnime(params, GetAnimeDetailsPath, http.MethodGet)
if err != nil {
return AnimeDetails{}, err
}
var details animeDetailsEndRes
err = json.NewDecoder(res.Body).Decode(&details)
defer func(Body io.ReadCloser) {
_ = Body.Close()
}(res.Body)
return details.Response, err
}