/
search.go
170 lines (149 loc) · 3.42 KB
/
search.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
package manga
import (
"encoding/json"
"log"
"github.com/imroc/req"
"github.com/nokusukun/jikan2go/utils"
)
func Search(q Query) (SearchResult, error) {
if q.Page == 0 {
q.Page = 1
}
request, err := utils.CachedReqGet(utils.Config.AppendAPIf("/search/manga"), q.ToParam())
if err != nil {
return SearchResult{}, err
}
var a SearchResult
err = request.ToJSON(&a)
return a, err
}
type Query struct {
Q string `json:"q,omitempty"`
Page int `json:"page,omitempty"`
Type Type `json:"type,omitempty"`
Rated Rated `json:"rated,omitempty"`
Genre MangaGenre `json:"genre,omitempty"`
Score float64 `json:"score,omitempty"`
GenreExclude bool `json:"genre_exclude,omitempty"`
Limit int `json:"limit,omitempty"`
Order Order `json:"order,omitempty"`
Sort Sort `json:"sort,omitempty"`
Producer int64 `json:"producer,omitempty"`
Magazine int64 `json:"magazine,omitempty"`
Letter string `json:"letter,omitempty"`
}
func (q *Query) ToParam() req.Param {
qbytes, _ := json.Marshal(q)
var reqParam req.Param
err := json.Unmarshal(qbytes, &reqParam)
if err != nil {
log.Printf("failed to paramaterize %+v", q)
}
return reqParam
}
type SearchResult struct {
RequestHash string `json:"request_hash"`
RequestCached bool `json:"request_cached"`
RequestCacheExpiry int64 `json:"request_cache_expiry"`
Results []Result `json:"results"`
LastPage int64 `json:"last_page"`
}
type Result struct {
MalID int64 `json:"mal_id"`
URL string `json:"url"`
ImageURL string `json:"image_url"`
Title string `json:"title"`
Publishing bool `json:"publishing"`
Synopsis string `json:"synopsis"`
Type Type `json:"type"`
Chapters int64 `json:"chapters"`
Volumes int64 `json:"volumes"`
Score float64 `json:"score"`
StartDate *string `json:"start_date"`
EndDate *string `json:"end_date"`
Members int64 `json:"members"`
}
type Type string
const (
TypeManga Type = "Manga"
TypeManhua Type = "Manhua"
TypeManhwa Type = "Manhwa"
TypeNovel Type = "Novel"
TypeOneShot Type = "One-shot"
)
type Rated string
const (
G Rated = "G"
PG Rated = "PG"
PG13 Rated = "PG-13"
R Rated = "R"
RatedR Rated = "R+"
Rx Rated = "Rx"
)
type Sort string
const (
SortAsc = "ascending"
SortDesc = "descending"
)
type Order string
const (
OrderTitle = "title"
OrderStartDate = "start_date"
OrderEndDate = "end_date"
OrderScore = "score"
OrderType = "type"
OrderMemebers = "members"
OrderId = "id"
OrderEpisodes = "episodes"
OrderRating = "rating"
)
type MangaGenre = int
//noinspection ALL,GoUnusedConst
const (
GenreReserved MangaGenre = iota
GenreAction
GenreAdventure
GenreCars
GenreComedy
GenreDementia
GenreDemons
GenreMystery
GenreDrama
GenreEcchi
GenreFantasy
GenreGame
GenreHentai
GenreHistorical
GenreHorror
GenreKids
GenreMagic
GenreMartialArts
GenreMecha
GenreMusic
GenreParody
GenreSamurai
GenreRomance
GenreSchool
GenreSciFi
GenreShoujo
GenreShoujoAi
GenreShounen
GenreShounenAi
GenreSpace
GenreSports
GenreSuperPower
GenreVampire
GenreYaoi
GenreYuri
GenreHarem
GenreSliceOfLife
GenreSupernatural
GenreMilitary
GenrePolice
GenrePsychological
GenreSeinen
GenreJosei
GenreDoujinshi
GenreGenderBender
GenreThriller
)