This repository has been archived by the owner on Jan 8, 2019. It is now read-only.
/
anime.go
164 lines (158 loc) · 4.89 KB
/
anime.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
package kitsu
import (
"encoding/json"
"fmt"
"net/url"
"github.com/Jeffail/gabs"
)
// Anime struct with all the anime data from kitsu
type Anime struct {
ID string `json:"id"`
Type string `json:"type"`
Links struct {
Self string `json:"self"`
} `json:"links"`
Attributes struct {
TBA string `json:"tba"`
AbbreviatedTitles []string `json:"abbreviatedTitles"`
AverageRating string `json:"averageRating"`
Status string `json:"status"`
AgeRating string `json:"ageRating"`
Subtype string `json:"subtype"`
CanonicalTitle string `json:"canonicalTitle"`
EpisodeLength int `json:"episodeLength"`
CoverImage struct {
Original string `json:"original"`
Tiny string `json:"tiny"`
Small string `json:"small"`
Large string `json:"large"`
} `json:"coverImage"`
Slug string `json:"slug"`
Titles struct {
EnJp string `json:"en_jp"`
JaJp string `json:"ja_jp"`
} `json:"titles"`
AgeRatingGuide string `json:"ageRatingGuide"`
StartDate string `json:"startDate"`
EpisodeCount int `json:"episodeCount"`
FavoritesCount int `json:"favoritesCount"`
NSFW bool `json:"nsfw"`
EndDate string `json:"endDate"`
RatingRank int `json:"ratingRank"`
PosterImage struct {
Tiny string `json:"tiny"`
Small string `json:"small"`
Medium string `json:"medium"`
Large string `json:"large"`
Original string `json:"original"`
} `json:"posterImage"`
Synopsis string `json:"synopsis"`
ShowType string `json:"showType"`
UserCount int `json:"userCount"`
PopularityRank int `json:"popularityRank"`
} `json:"attributes"`
YoutubeVideoID string `json:"youtubeVideoId"`
}
// SearchAnime search for an anime on kitsu.io
// query being the anime to search for
// offset being the page offset
func SearchAnime(query string, offset int) (*Anime, error) {
newQuery := url.QueryEscape(query)
uri := fmt.Sprintf("%s/anime?filter[text]=%s&page[offset]=%v", baseURL, newQuery, offset)
byt, er := get(uri)
if er != nil {
return nil, er
}
parJSON, e := gabs.ParseJSON(byt)
if e != nil {
return nil, e
}
anime := parJSON.Path("data").Data().([]interface{})
if len(anime) <= 0 {
err := fmt.Errorf("Could not find any anime with the query: %s", query)
return nil, err
}
resJSON, er := json.Marshal(anime[0]) // Right now I'm doing anime[0] because I have no idea how to handle it when it would return more than 1 result.
if er != nil {
return nil, er
}
ani := new(Anime)
err := json.Unmarshal(resJSON, &ani)
if err != nil {
return nil, err
}
return ani, nil
}
// AnimeByID holds the data from searching an anime by the id
type AnimeByID struct {
ID string `json:"id"`
Type string `json:"type"`
Links struct {
Self string `json:"self"`
} `json:"links"`
Attributes struct {
TBA string `json:"tba"`
AbbreviatedTitles []string `json:"abbreviatedTitles"`
AverageRating string `json:"averageRating"`
Status string `json:"status"`
AgeRating string `json:"ageRating"`
Subtype string `json:"subtype"`
CanonicalTitle string `json:"canonicalTitle"`
EpisodeLength int `json:"episodeLength"`
CoverImage struct {
Original string `json:"original"`
Tiny string `json:"tiny"`
Small string `json:"small"`
Large string `json:"large"`
} `json:"coverImage"`
Slug string `json:"slug"`
Titles struct {
En string `json:"en"`
EnJp string `json:"en_jp"`
JaJp string `json:"ja_jp"`
} `json:"titles"`
AgeRatingGuide string `json:"ageRatingGuide"`
StartDate string `json:"startDate"`
EpisodeCount int `json:"episodeCount"`
FavoritesCount int `json:"favoritesCount"`
NSFW bool `json:"nsfw"`
EndDate string `json:"endDate"`
RatingRank int `json:"ratingRank"`
PosterImage struct {
Tiny string `json:"tiny"`
Small string `json:"small"`
Medium string `json:"medium"`
Large string `json:"large"`
Original string `json:"original"`
} `json:"posterImage"`
Synopsis string `json:"synopsis"`
ShowType string `json:"showType"`
UserCount int `json:"userCount"`
PopularityRank int `json:"popularityRank"`
} `json:"attributes"`
YoutubeVideoID string `json:"youtubeVideoId"`
}
// GetAnime will fetch an anime with the given id from kitsu.io
// id of course being the id
func GetAnime(id int) (*AnimeByID, error) {
uri := fmt.Sprintf("%s/anime/%d", baseURL, id)
byt, er := get(uri)
if er != nil {
return nil, er
}
parJSON, e := gabs.ParseJSON(byt)
if e != nil {
return nil, e
}
anime := parJSON.Path("data").Data().(map[string]interface{})
resJSON, er := json.Marshal(anime)
if er != nil {
return nil, er
}
ani := new(AnimeByID)
err := json.Unmarshal(resJSON, &ani)
if err != nil {
return nil, err
}
return ani, nil
}