-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
172 lines (131 loc) · 4.62 KB
/
main.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
package animechan
import (
"encoding/json"
"fmt"
"net/http"
"github.com/joao-1/animechan-go/helpers"
)
const CharacterPath = "/character"
const AnimePath = "/anime"
const QuotesOnlyPath = "/quotes"
const RandomPath = "/random"
type IRandom interface {
Anime(anime string) (Quote, error)
Character(character string) (Quote, error)
Only() (Quote, error)
}
type IQuotes interface {
Anime(anime string, page *int) ([]Quote, error)
Character(character string, page *int) ([]Quote, error)
Only() ([]Quote, error)
}
// Quote struct.
type Quote struct {
Anime string
Character string
Quote string
}
type QuoteAPIResponse struct {
Key int `json:"key"`
Anime string `json:"anime"`
Character string `json:"character"`
Quote string `json:"quote"`
V int `json:"__v"`
}
// Quotes is the struct for the quotes endpoint.
type Random struct {
fetch *helpers.Fetch
client *http.Client
BaseURL string
}
// Searches for a quote from a random anime and character
func (r *Random) Only() (Quote, error) {
path := r.BaseURL + RandomPath
res, err := r.fetch.Get(helpers.GetParams{Client: r.client, Url: path})
if err != nil { return Quote{}, err }
return formatOneQuote(res.Data)
}
// Searches for a random quote from a specific anime
func (r *Random) Anime(anime string) (Quote, error) {
path := r.BaseURL + RandomPath + AnimePath
res, err := r.fetch.Get(helpers.GetParams{Client: r.client, Url: path, Query: map[string]string{"title": anime}})
if err != nil { return Quote{}, err }
return formatOneQuote(res.Data)
}
// Search for a random quote from a specific character
func (r *Random) Character(character string) (Quote, error) {
path := r.BaseURL + RandomPath + CharacterPath
res, err := r.fetch.Get(helpers.GetParams{Client: r.client, Url: path, Query: map[string]string{"name": character}})
if err != nil { return Quote{}, err }
return formatOneQuote(res.Data)
}
// Quotes is the struct for the quotes endpoint.
type Quotes struct {
fetch *helpers.Fetch
client *http.Client
BaseURL string
}
// Searches for 10 quotes from random anime and character.
func (q *Quotes) Only() ([]Quote, error) {
path := q.BaseURL + QuotesOnlyPath
res, err := q.fetch.Get(helpers.GetParams{Client: q.client, Url: path})
if err != nil { return []Quote{}, err }
return formatManyQuote(res.Data)
}
// Searches for quotes from a specific anime. It is possible to specify page.
func (q *Quotes) Anime(anime string, page *int) ([]Quote, error) {
path := q.BaseURL + QuotesOnlyPath + AnimePath
pageToSearch := 10
if page != nil {pageToSearch = *page}
res, err := q.fetch.Get(helpers.GetParams{Client: q.client, Url: path, Query: map[string]string{"title": anime, "page":fmt.Sprint(pageToSearch)}})
if err != nil { return []Quote{}, err }
return formatManyQuote(res.Data)
}
// Searches for quotes from a specific character. It is possible to specify page.
func (q *Quotes) Character(character string, page *int) ([]Quote, error) {
path := q.BaseURL + QuotesOnlyPath + CharacterPath
pageToSearch := 10
if page != nil {pageToSearch = *page}
res, err := q.fetch.Get(helpers.GetParams{Client: q.client, Url: path, Query: map[string]string{"name": character, "page":fmt.Sprint(pageToSearch)}})
if err != nil { return []Quote{}, err }
return formatManyQuote(res.Data)
}
// Animechan is the main struct for the package. It contains the client for the http requests.
type Animechan struct {
Client *http.Client
BaseURL string
}
// Unlike the normal quote endpoint, it searches for a random one. It is possible to specify character or anime.
func (a *Animechan) Random() *Random {
random := new(Random)
fetch := new(helpers.Fetch)
random.fetch = fetch
random.client = a.Client
random.BaseURL = a.BaseURL
return random
}
// Searches for quotes from a specific anime or character. It is possible to specify page.
func (a *Animechan) Quotes() *Quotes {
quotes := new(Quotes)
fetch := new(helpers.Fetch)
quotes.fetch = fetch
quotes.client = a.Client
quotes.BaseURL = a.BaseURL
return quotes
}
func formatOneQuote(data string) (Quote, error) {
var apiQuote QuoteAPIResponse
errParse := json.Unmarshal([]byte(data), &apiQuote)
if errParse != nil { return Quote{}, errParse }
return Quote{Anime: apiQuote.Anime, Character: apiQuote.Character, Quote: apiQuote.Quote}, nil
}
func formatManyQuote(data string) ([]Quote, error) {
var apiQuotes []QuoteAPIResponse
errParse := json.Unmarshal([]byte(data), &apiQuotes)
if errParse != nil { return []Quote{}, errParse }
var quotes []Quote
for _, value := range apiQuotes {
quotes = append(quotes, Quote{Anime: value.Anime, Character: value.Character, Quote: value.Quote})
}
return quotes, nil
}