This repository has been archived by the owner on Jan 8, 2019. It is now read-only.
/
drama.go
94 lines (88 loc) · 2.71 KB
/
drama.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
package kitsu
// https://kitsu.io/api/edge/drama?filter[text]=
import (
"encoding/json"
"fmt"
"net/url"
"github.com/Jeffail/gabs"
)
/* There are currently no dramas on kitsu so this will return with nothing until they add dramas to the website. */
// Drama struct with all the drama data from kitsu
type Drama 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"`
Slug string `json:"slug"`
Synopsis string `json:"synopsis"`
Titles struct {
En string `json:"en"`
EnJp string `json:"en_jp"`
JaJp string `json:"ja_jp"`
} `json:"titles"`
CanonicalTitle string `json:"canonicalTitle"`
AbbreviatedTitles []string `json:"abbreviatedTitles"`
AverageRating string `json:"averageRating"`
UserCount int `json:"userCount"`
FavoritesCount int `json:"favoritesCount"`
StartDate string `json:"startDate"`
EndDate string `json:"endDate"`
PopularityRank int `json:"popularityRank"`
RatingRank int `json:"ratingRank"`
AgeRating string `json:"ageRating"`
AgeRatingGuide string `json:"ageRatingGuide"`
SubType string `json:"subType"`
Status string `json:"status"`
PosterImage struct {
Tiny string `json:"tiny"`
Small string `json:"small"`
Medium string `json:"medium"`
Large string `json:"large"`
Original string `json:"original"`
} `json:"posterImage"`
CoverImage struct {
Tiny string `json:"tiny"`
Small string `json:"small"`
Large string `json:"large"`
Original string `json:"original"`
} `json:"coverImage"`
EpisodeCount int `json:"episodeCount"`
EpisodeLength int `json:"episodeLength"`
YoutubeVideoID string `json:"youtubeVideoId"`
NSFW bool `json:"nsfw"`
} `json:"attributes"`
Relationships struct{} `json:"relationships"`
}
// SearchDrama search for a drama on kitsu.io
// query being the producer to search for
func SearchDrama(query string) (*Drama, error) {
newQuery := url.QueryEscape(query)
uri := fmt.Sprintf("%s/drama?filter[text]=%s", baseURL, newQuery)
byt, er := get(uri)
if er != nil {
return nil, er
}
parJSON, e := gabs.ParseJSON(byt)
if e != nil {
return nil, e
}
drama := parJSON.Path("data").Data().([]interface{})
if len(drama) <= 0 {
err := fmt.Errorf("Could not find any dramas with the query: %s", query)
return nil, err
}
resJSON, er := json.Marshal(drama[0])
if er != nil {
return nil, er
}
dra := new(Drama)
err := json.Unmarshal(resJSON, &dra)
if err != nil {
return nil, err
}
return dra, nil
}