-
Notifications
You must be signed in to change notification settings - Fork 7
/
nhentai.go
229 lines (194 loc) · 4.28 KB
/
nhentai.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
package nhentai
import (
"encoding/json"
"errors"
"fmt"
"net/http"
"strconv"
"time"
)
type API struct {
client *http.Client
}
type nhentaiResult struct {
ID interface{} `json:"id,omitempty"`
MediaID string `json:"media_id,omitempty"`
Titles *Titles `json:"title,omitempty"`
UploadDate int64 `json:"upload_date"`
Tags []struct {
ID int `json:"id,omitempty"`
Type string `json:"type,omitempty"`
Name string `json:"name,omitempty"`
URL string `json:"url,omitempty"`
Count int `json:"count,omitempty"`
} `json:"tags,omitempty"`
Pages interface{} `json:"num_pages,omitempty"`
Favorites interface{} `json:"num_favorites,omitempty"`
}
type Hentai struct {
ID int
URL string
Cover string
MediaID string
Titles *Titles
Tags []*Tag
Pages int
Favorites int
UploadedAt time.Time
}
type Titles struct {
Japanese string `json:"japanese,omitempty"`
English string `json:"english,omitempty"`
Pretty string `json:"pretty,omitempty"`
}
type Tag struct {
ID int
Type TagType
Name string
URL string
}
type TagType int
const (
GenreTag TagType = iota
ArtistTag
GroupTag
LanguageTag
CharacterTag
ParodyTag
)
var (
ErrNotFound = errors.New("doujin not found")
ErrCloudflareProtection = errors.New("failed to bypass cloudflare")
)
func New() (*API, error) {
client := &http.Client{}
return &API{
client: client,
}, nil
}
func (n *API) FindHentai(id string) (*Hentai, error) {
res, err := n.get(id)
if err != nil {
return nil, err
}
tags := make([]*Tag, 0, len(res.Tags))
for _, tag := range res.Tags {
prettyTag := &Tag{
ID: tag.ID,
Name: tag.Name,
URL: "https://nhentai.net" + tag.URL,
}
switch tag.Type {
case "artist":
prettyTag.Type = ArtistTag
case "group":
prettyTag.Type = GroupTag
case "language":
prettyTag.Type = LanguageTag
case "parody":
prettyTag.Type = ParodyTag
case "character":
prettyTag.Type = CharacterTag
default:
prettyTag.Type = GenreTag
}
tags = append(tags, prettyTag)
}
return &Hentai{
Titles: res.Titles,
MediaID: res.MediaID,
URL: fmt.Sprintf("https://nhentai.net/g/%v", res.ID),
Tags: tags,
Cover: fmt.Sprintf("https://t.nhentai.net/galleries/%v/cover.jpg", res.MediaID),
ID: interfaceToInt(res.ID),
Pages: interfaceToInt(res.Pages),
Favorites: interfaceToInt(res.Favorites),
UploadedAt: time.Unix(res.UploadDate, 0),
}, nil
}
func (h *Hentai) Genres() []*Tag {
genres := make([]*Tag, 0)
for _, tag := range h.Tags {
if tag.Type == GenreTag {
genres = append(genres, tag)
}
}
return genres
}
func (h *Hentai) Language() (*Tag, bool) {
for _, tag := range h.Tags {
if tag.Type == LanguageTag {
return tag, true
}
}
return nil, false
}
func (h *Hentai) Parodies() []*Tag {
parodies := make([]*Tag, 0)
for _, tag := range h.Tags {
if tag.Type == ParodyTag {
parodies = append(parodies, tag)
}
}
return parodies
}
func (h *Hentai) Characters() []*Tag {
characters := make([]*Tag, 0)
for _, tag := range h.Tags {
if tag.Type == CharacterTag {
characters = append(characters, tag)
}
}
return characters
}
func (h *Hentai) Artists() []*Tag {
artists := make([]*Tag, 0)
for _, tag := range h.Tags {
if tag.Type == GroupTag || tag.Type == ArtistTag {
artists = append(artists, tag)
}
}
return artists
}
func (t *Tag) String() string {
return t.Name
}
func (n *API) get(id string) (*nhentaiResult, error) {
req, err := http.NewRequest("GET", "https://nhentai.net/api/gallery/"+id, nil)
if err != nil {
return nil, fmt.Errorf("failed to create a request to nhentai: %w", err)
}
resp, err := n.client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
switch resp.StatusCode {
case 200:
var res nhentaiResult
err = json.NewDecoder(resp.Body).Decode(&res)
if err != nil {
return nil, err
}
return &res, nil
case 503:
return nil, ErrCloudflareProtection
case 404:
return nil, ErrNotFound
}
return nil, fmt.Errorf("unexpected api response: %v", resp.Status)
}
func interfaceToInt(i interface{}) int {
var res int
switch v := i.(type) {
case int:
res = v
case int64:
res = int(v)
case float64:
res = int(v)
case string:
res, _ = strconv.Atoi(v)
}
return res
}