nhentai-api 0.2.1 → 1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/nhentai-api.rb +34 -541
  3. metadata +11 -9
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 109d37e0a068cd29975a5089ba3fb56fca2483a8aee361608d17e12a76f7e6ee
4
- data.tar.gz: 4995763a326ed1d4e68d25857b85563a5000136022af7f9b41b13d91901c8229
3
+ metadata.gz: 4d63124d136363e121f01218322464cde8ea037e567b73246ffdc56fde309dd0
4
+ data.tar.gz: 34796062c118c4db00bac99648af7a7be3f23d2fc5e85751e82929486235fbae
5
5
  SHA512:
6
- metadata.gz: ea468a964c41b3bf7ac264f16a6206ff6c56be7fa98c3b4beeb765d08d6a2a803e56cd12cf453d3f9804dd6e81262058f7c5caf7b2e7bdabb05f9051c8f94abb
7
- data.tar.gz: 7b66422ee77f01ef7f76f8be440a45970b612f4e36c6a405895766354cccea1b107bf171d592e8259c91172c68183e1a8c6f66dc352fab606c086918cdc6432c
6
+ metadata.gz: b690c41f13b19bf979c3d21ef001a4ed6d26975fe214b22c1eac0364130d9d1ba006cc4a5af6beec518c8e20cb90749bc830de2e965072699cae406c6bb5a77a
7
+ data.tar.gz: 687c9c8b048e0d16bc848111d3b6cde52339cad52b8bbcd8f235b5a6e597cdd439b6a06d15dd06f77547dc9adcba5cdb0daaaef708783d06bdbebfd21babbd29
data/lib/nhentai-api.rb CHANGED
@@ -1,543 +1,36 @@
1
- require 'net/http'
2
- require 'time'
3
-
4
- class Info
5
- attr_reader :id, :name, :count, :url
6
-
7
- def initialize(id, name, count, url)
8
- @id = id
9
- @name = name
10
- @count = count
11
- @url = url
12
- end
13
- end
14
-
15
- class Doujinshi
16
- attr_reader :id, :client, :media_id, :count_pages
17
-
18
- def initialize(id)
19
- @id = id
20
- @client = Net::HTTP.get_response(URI("https://nhentai.net/g/#{@id}/"))
21
- if self.exists?
22
- @media_id = @client.body.match(%r{\/([0-9]+)\/cover})[1]
23
- @count_pages = @client.body.match(/([0-9]+) pages/)[1].to_i
24
- end
25
- end
26
-
27
- #
28
- # Check if a doujinshi with the given id exist
29
- #
30
- # @return [Bool] true if the doujinshi exist, otherwise false
31
- # @since 0.1.0
32
- # @example
33
- # doujinshi.exists? #=> true
34
- #
35
- def exists?
36
- @client.code == '200'
37
- end
38
-
39
- #
40
- # Give the title of a doujinshi
41
- #
42
- # @return [String] the title of a given doujinshi
43
- # @since 0.1.0
44
- # @example
45
- # doujinshi.title #=> '[Illumination. (Ogadenmon)] Android no Ecchi na Yatsu | Horny Androids (NieR:Automata) [English] =TLL + mrwayne= [Digital]'
46
- #
47
- def title
48
- @client.body.match(%r{<div id="info">\s+<h1>(.+)<\/h1>})[1]
49
- end
50
-
51
- #
52
- # Give the cover's URL of a doujinshi
53
- #
54
- # @return [String] the cover's URL of a given doujinshi
55
- # @since 0.1.0
56
- # @example
57
- # doujinshi.cover #=> 'https://t.nhentai.net/galleries/1170172/cover.jpg'
58
- #
59
- def cover
60
- res = @client.body.match(%r{https://t.nhentai.net/galleries/#{@media_id}/cover\.(.{3})"})
61
-
62
- "https://t.nhentai.net/galleries/#{@media_id}/cover.#{res[1]}"
63
- end
64
-
65
- #
66
- # Give the URL of a given page of a doujinshi
67
- #
68
- # @param [Integer] page a particular page of a doujinshi
69
- # @return [String] the URL of a given page of a doujinshi
70
- # @since 0.1.0
71
- # @example
72
- # doujinshi.get_page #=> 'https://i.nhentai.net/galleries/1170172/1.jpg'
73
- # doujinshi.get_page(10) #=> 'https://i.nhentai.net/galleries/1170172/10.jpg'
74
- #
75
- def page(page = 1)
76
- res = @client.body.match(%r{https://t.nhentai.net/galleries/#{@media_id}/#{page}t\.(.{3})"})
77
-
78
- "https://i.nhentai.net/galleries/#{@media_id}/#{page}.#{res[1]}"
79
- end
80
-
81
- #
82
- # Give the URL of a all pages of a doujinshi
83
- #
84
- # @return [Array] array pages' URL
85
- # @since 0.1.0
86
- # @example
87
- # doujinshi.pages #=> ['https://i.nhentai.net/galleries/1170172/1.jpg', ..., 'https://i.nhentai.net/galleries/1170172/31.jpg']
88
- #
89
- def pages
90
- (1..@count_pages).map { |page| page(page) }
91
- end
92
-
93
- #
94
- # Give the thumbnail's URL of a given page of a doujinshi
95
- #
96
- # @param [Integer] page a particular page of a doujinshi
97
- # @return [String] the thumbnail's URL of a given page of a doujinshi
98
- # @since 0.1.0
99
- # @example
100
- # doujinshi.get_thumbnail #=> 'https://t.nhentai.net/galleries/1170172/1t.jpg'
101
- # doujinshi.get_thumbnail(10) #=> 'https://t.nhentai.net/galleries/1170172/10t.jpg'
102
- #
103
- def thumbnail(page = 1)
104
- res = @client.body.match(%r{https://t.nhentai.net/galleries/#{@media_id}/(#{page}t\..{3})"})
105
-
106
- "https://t.nhentai.net/galleries/#{@media_id}/#{res[1]}"
107
- end
108
-
109
- #
110
- # Give the URL of a all thumbnails of a doujinshi
111
- #
112
- # @return [Array] an array thumbnails' URL
113
- # @since 0.1.0
114
- # @example
115
- # doujinshi.thumbnails #=> ['https://t.nhentai.net/galleries/1170172/1t.jpg',..., 'https://t.nhentai.net/galleries/1170172/31t.jpg']
116
- #
117
- def thumbnails
118
- (1..@count_pages).map { |page| thumbnail(page) }
119
- end
120
-
121
- #
122
- # Give the number of favorites on a doujinshi
123
- #
124
- # @return [Integer] a counter of favorites on a given doujinshi
125
- # @since 0.1.0
126
- # @example
127
- # doujinshi.num_favorites #=> 13326
128
- #
129
- def count_favorites
130
- regex = %r{<span>Favorite <span class="nobold">.(\d+).<\/span><\/span>}
131
-
132
- @client.body.match(regex)[1].to_i
133
- end
134
-
135
- #
136
- # Give the upload date of a doujinshi
137
- #
138
- # @return [Integer] the upload date of a given doujinshi
139
- # @since 0.1.0
140
- # @example
141
- # doujinshi.upload_date #=> 2018-01-17 15:56:16 +0000
142
- #
143
- def upload_date
144
- Time.iso8601(@client.body.match(/datetime="(.+)"/)[1])
145
- end
146
-
147
- #
148
- # Give all tags of a doujinshi
149
- #
150
- # @return [Array] of Tag class of a given doujinshi
151
- # @since 0.1.0
152
- # @example
153
- # doujinshi.tags
154
- #
155
- def tags
156
- res = @client.body.match(%r{Tags:\s*<span class="tags">(.+)<\/span>})
157
-
158
- parse_tags(res[1])
159
- end
160
-
161
- #
162
- # Give a counter of tags
163
- #
164
- # @return [Integer] of tags
165
- # @since 0.1.0
166
- # @example
167
- # doujinshi.count_tags #=> 9
168
- #
169
- def count_tags
170
- res = @client.body.match(%r{Tags:\s*<span class="tags">(.+)<\/span>})
171
-
172
- res.nil? ? 0 : parse_tags(res[1]).length
173
- end
174
-
175
- #
176
- # Check if a particular doujinshi have some tags
177
- #
178
- # @return [Bool] true if the doujinshi have tags, otherwise false
179
- # @since 0.1.0
180
- # @example
181
- # doujinshi.tags? #=> true
182
- #
183
- def tags?
184
- !@client.body.match(%r{Tags:\s*<span class="tags">(.+)<\/span>}).nil?
185
- end
186
-
187
- #
188
- # Give all parodies of a doujinshi
189
- #
190
- # @since 0.1.0
191
- # @see Doujinshi#tags
192
- #
193
- def parodies
194
- res = @client.body.match(%r{Parodies:\s+<span class="tags">(.+)<\/span>})
195
-
196
- parse_tags(res[1])
197
- end
198
-
199
- #
200
- # Give a counter of parodies
201
- #
202
- # @since 0.1.0
203
- # @see Doujinshi#count_tags
204
- #
205
- def count_parodies
206
- res = @client.body.match(%r{Parodies:\s+<span class="tags">(.+)<\/span>})
207
-
208
- res.nil? ? 0 : parse_tags(res[1]).length
209
- end
210
-
211
- #
212
- # Check if a particular doujinshi have some parodies
213
- #
214
- # @since 0.1.0
215
- # @see Doujinshi#tags?
216
- #
217
- def parodies?
218
- !@client.body.match(%r{Parodies:\s+<span class="tags">(.+)<\/span>}).nil?
219
- end
220
-
221
- #
222
- # Give all characters of a doujinshi
223
- #
224
- # @since 0.1.0
225
- # @see Doujinshi#tags
226
- #
227
- def characters
228
- res = @client.body.match(%r{Characters:\s+<span class="tags">(.+)<\/span>})
229
-
230
- parse_tags(res[1])
231
- end
232
-
233
- #
234
- # Give a counter of characters
235
- #
236
- # @since 0.1.0
237
- # @see Doujinshi#count_tags
238
- #
239
- def count_characters
240
- res = @client.body.match(%r{Characters:\s+<span class="tags">(.+)<\/span>})
241
-
242
- res.nil? ? 0 : parse_tags(res[1]).length
243
- end
244
-
245
- #
246
- # Check if a particular doujinshi have some characters
247
- #
248
- # @since 0.1.0
249
- # @see Doujinshi#tags?
250
- #
251
- def characters?
252
- !@client.body.match(%r{Characters:\s+<span class="tags">(.+)<\/span>}).nil?
253
- end
254
-
255
- #
256
- # Give all artists of a doujinshi
257
- #
258
- # @since 0.1.0
259
- # @see Doujinshi#tags
260
- #
261
- def artists
262
- res = @client.body.match(%r{Artists:\s+<span class="tags">(.+)<\/span>})
263
-
264
- parse_tags(res[1])
265
- end
266
-
267
- #
268
- # Give a counter of artists
269
- #
270
- # @since 0.1.0
271
- # @see Doujinshi#count_tags
272
- #
273
- def count_artists
274
- res = @client.body.match(%r{Artists:\s+<span class="tags">(.+)<\/span>})
275
-
276
- res.nil? ? 0 : parse_tags(res[1]).length
277
- end
278
-
279
- #
280
- # Check if a particular doujinshi have some artists
281
- #
282
- # @since 0.1.0
283
- # @see Doujinshi#tags?
284
- #
285
- def artists?
286
- !@client.body.match(%r{Artists:\s+<span class="tags">(.+)<\/span>}).nil?
287
- end
288
-
289
- #
290
- # Give all groups of a doujinshi
291
- #
292
- # @since 0.1.0
293
- # @see Doujinshi#tags
294
- #
295
- def groups
296
- res = @client.body.match(%r{Groups:\s+<span class="tags">(.+)<\/span>})
297
-
298
- parse_tags(res[1])
299
- end
300
-
301
- #
302
- # Give a counter of groups
303
- #
304
- # @since 0.1.0
305
- # @see Doujinshi#count_tags
306
- #
307
- def count_groups
308
- res = @client.body.match(%r{Groups:\s+<span class="tags">(.+)<\/span>})
309
-
310
- res.nil? ? 0 : parse_tags(res[1]).length
311
- end
312
-
313
- #
314
- # Check if a particular doujinshi have some groups
315
- #
316
- # @since 0.1.0
317
- # @see Doujinshi#tags?
318
- #
319
- def groups?
320
- !@client.body.match(%r{Groups:\s+<span class="tags">(.+)<\/span>}).nil?
321
- end
322
-
323
- #
324
- # Give all languages of a doujinshi
325
- #
326
- # @since 0.1.0
327
- # @see Doujinshi#tags
328
- #
329
- def languages
330
- res = @client.body.match(%r{Languages:\s+<span class="tags">(.+)<\/span>})
331
-
332
- parse_tags(res[1])
333
- end
334
-
335
- #
336
- # Give a counter of languages
337
- #
338
- # @since 0.1.0
339
- # @see Doujinshi#count_tags
340
- #
341
- def count_languages
342
- res = @client.body.match(%r{Languages:\s+<span class="tags">(.+)<\/span>})
343
-
344
- res.nil? ? 0 : parse_tags(res[1]).length
345
- end
346
-
347
- #
348
- # Check if a particular doujinshi have some languages
349
- #
350
- # @since 0.1.0
351
- # @see Doujinshi#tags?
352
- #
353
- def languages?
354
- !@client.body.match(%r{Languages:\s+<span class="tags">(.+)<\/span>}).nil?
355
- end
356
-
357
- #
358
- # Give all categories of a doujinshi
359
- #
360
- # @since 0.1.0
361
- # @see Doujinshi#tags
362
- #
363
- def categories
364
- res = @client.body.match(%r{Categories:\s+<span class="tags">(.+)<\/span>})
365
-
366
- parse_tags(res[1])
367
- end
368
-
369
- #
370
- # Give a counter of categories
371
- #
372
- # @since 0.1.0
373
- # @see Doujinshi#count_tags
374
- #
375
- def count_categories
376
- res = @client.body.match(%r{Categories:\s+<span class="tags">(.+)<\/span>})
377
-
378
- res.nil? ? 0 : parse_tags(res[1]).length
379
- end
380
-
381
- #
382
- # Check if a particular doujinshi have some categories
383
- #
384
- # @since 0.1.0
385
- # @see Doujinshi#tags?
386
- #
387
- def categories?
388
- !@client.body.match(%r{Categories:\s+<span class="tags">(.+)<\/span>}).nil?
389
- end
390
-
391
- #
392
- # @private
393
- #
394
-
395
- private
396
-
397
- def parse_tags(res)
398
- res.split(%r{<a(.+?)<\/a>}).reject(&:empty?).map do |line|
399
- id = line.match(/tag-(\d+)/)[1]
400
- name = line.match(/">(.+?)</)[1].strip
401
- count = line.match(/\((.+?)\)</)[1].tr(',', '').to_i
402
- url = line.match(/href=\"(.+?)\"/)[1]
403
-
404
- Info.new(id, name, count, url)
405
- end
406
- end
407
- end
408
-
409
- class Tag
410
- #
411
- # List all doujinshi of the page of a given tag
412
- #
413
- # @param [String] keyword of the research
414
- # @param [Integer] sort optional, 1 is sorting by time, 2 is by popularity
415
- # @param [Integer] page each page can return 25 doujinshi
416
- # @return [Array] array of Info
417
- # @since 0.2.0
418
- #
419
- def self.listing(keyword, sort = 1, page = 1)
420
- keyword.tr!(' ', '-')
421
- sort = sort == 1 ? '' : 'popular'
422
- client = Net::HTTP.get_response(URI("https://nhentai.net/tag/#{keyword}/#{sort}?page=#{page}"))
423
- res = client.body.split(%r{<div class="gallery".+?>(.+)</div>}).select { |line| line.include?('<a href="/g/') }
424
-
425
- parse_tags(res)
426
- end
427
-
428
- #
429
- # @private
430
- #
431
- def self.parse_tags(res)
432
- res.map do |line|
433
- id = line.match(%r{/g/(\d+)/})[1]
434
- name = line.match(%r{<div class="caption">(.+)</div>})[1].strip
435
- count = 1
436
- url = "/g/#{id}"
437
-
438
- Info.new(id, name, count, url)
439
- end
440
- end
441
- end
442
-
443
- class Parody < Tag
444
- #
445
- # List all doujinshi of the page of a given parody
446
- #
447
- # @since 0.2.0
448
- # @see Tag#listing
449
- #
450
- def self.listing(keyword, sort = 1, page = 1)
451
- keyword.tr!(' ', '-')
452
- sort = sort == 1 ? '' : 'popular'
453
- client = Net::HTTP.get_response(URI("https://nhentai.net/parody/#{keyword}/#{sort}?page=#{page}"))
454
- res = client.body.split(%r{<div class="gallery".+?>(.+)</div>}).select { |line| line.include?('<a href="/g/') }
455
-
456
- parse_tags(res)
457
- end
458
- end
459
-
460
- class Character < Tag
461
- #
462
- # List all doujinshi of the page of a given character
463
- #
464
- # @since 0.2.0
465
- # @see Tag#listing
466
- #
467
- def self.listing(keyword, sort = 1, page = 1)
468
- keyword.tr!(' ', '-')
469
- sort = sort == 1 ? '' : 'popular'
470
- client = Net::HTTP.get_response(URI("https://nhentai.net/character/#{keyword}/#{sort}?page=#{page}"))
471
- res = client.body.split(%r{<div class="gallery".+?>(.+)</div>}).select { |line| line.include?('<a href="/g/') }
472
-
473
- parse_tags(res)
474
- end
475
- end
476
-
477
- class Artist < Tag
478
- #
479
- # List all doujinshi of the page of a given artists
480
- #
481
- # @since 0.2.0
482
- # @see Tag#listing
483
- #
484
- def self.listing(keyword, sort = 1, page = 1)
485
- keyword.tr!(' ', '-')
486
- sort = sort == 1 ? '' : 'popular'
487
- client = Net::HTTP.get_response(URI("https://nhentai.net/artist/#{keyword}/#{sort}?page=#{page}"))
488
- res = client.body.split(%r{<div class="gallery".+?>(.+)</div>}).select { |line| line.include?('<a href="/g/') }
489
-
490
- parse_tags(res)
491
- end
492
- end
493
-
494
- class Group < Tag
495
- #
496
- # List all doujinshi of the page of a given group
497
- #
498
- # @since 0.2.0
499
- # @see Tag#listing
500
- #
501
- def self.listing(keyword, sort = 1, page = 1)
502
- keyword.tr!(' ', '-')
503
- sort = sort == 1 ? '' : 'popular'
504
- client = Net::HTTP.get_response(URI("https://nhentai.net/group/#{keyword}/#{sort}?page=#{page}"))
505
- res = client.body.split(%r{<div class="gallery".+?>(.+)</div>}).select { |line| line.include?('<a href="/g/') }
506
-
507
- parse_tags(res)
508
- end
509
- end
510
-
511
- class Language < Tag
512
- #
513
- # List all doujinshi of the page of a given language
514
- #
515
- # @since 0.2.0
516
- # @see Tag#listing
517
- #
518
- def self.listing(keyword, sort = 1, page = 1)
519
- keyword.tr!(' ', '-')
520
- sort = sort == 1 ? '' : 'popular'
521
- client = Net::HTTP.get_response(URI("https://nhentai.net/language/#{keyword}/#{sort}?page=#{page}"))
522
- res = client.body.split(%r{<div class="gallery".+?>(.+)</div>}).select { |line| line.include?('<a href="/g/') }
523
-
524
- parse_tags(res)
525
- end
526
- end
527
-
528
- class Category < Tag
529
- #
530
- # List all doujinshi of the page of a given category
531
- #
532
- # @since 0.2.0
533
- # @see Tag#listing
534
- #
535
- def self.listing(keyword, sort = 1, page = 1)
536
- keyword.tr!(' ', '-')
537
- sort = sort == 1 ? '' : 'popular'
538
- client = Net::HTTP.get_response(URI("https://nhentai.net/category/#{keyword}/#{sort}?page=#{page}"))
539
- res = client.body.split(%r{<div class="gallery".+?>(.+)</div>}).select { |line| line.include?('<a href="/g/') }
540
-
541
- parse_tags(res)
1
+ # frozen_string_literal: true
2
+
3
+ %w[net/http ostruct time json].each { |e| require e }
4
+ %w[doujinshi search key].each { |e| require_relative e }
5
+
6
+ SORT = {
7
+ today: 'popular-today',
8
+ week: 'popular-week',
9
+ all_time: 'popular'
10
+ }.freeze
11
+
12
+ IMAGE_EXTENSION = {
13
+ 'j' => 'jpg',
14
+ 'p' => 'png',
15
+ 'g' => 'gif'
16
+ }.freeze
17
+
18
+ SINGULAR_TAG = {
19
+ 'tags' => 'tag',
20
+ 'parodies' => 'parody',
21
+ 'characters' => 'character',
22
+ 'artists' => 'artist',
23
+ 'groups' => 'group',
24
+ 'languages' => 'language',
25
+ 'categories' => 'category'
26
+ }.freeze
27
+
28
+ def parse_tiles(res)
29
+ res.map do |line|
30
+ id = line.match(%r{/g/(\d+)/})[1]
31
+ name = line.match(/<div class="caption">(.+)/)[1].strip
32
+ url = "/g/#{id}"
33
+
34
+ OpenStruct.new(id: id, name: name, url: url)
542
35
  end
543
36
  end
metadata CHANGED
@@ -1,17 +1,17 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nhentai-api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: '1.0'
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gael Roussel
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-06-06 00:00:00.000000000 Z
11
+ date: 2022-05-20 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: nhentai-api is a basic and easy to use API for nhentai.net
14
- email: groussel42@gmail.com
14
+ email: gaelroussel@protonmail.com
15
15
  executables: []
16
16
  extensions: []
17
17
  extra_rdoc_files: []
@@ -21,9 +21,11 @@ homepage: https://rubygems.org/gems/nhentai-api
21
21
  licenses:
22
22
  - MIT
23
23
  metadata:
24
- documentation_uri: https://www.rubydoc.info/github/groussel42/nhentai-api
25
- source_code_uri: https://github.com/groussel42/nhentai-api
26
- post_install_message:
24
+ source_code_uri: https://github.com/Mraiih/nhentai-api
25
+ changelog_uri: https://github.com/Mraiih/nhentai-api/blob/master/CHANGELOG.md
26
+ documentation_uri: https://github.com/Mraiih/nhentai-api/wiki/Documentation
27
+ funding_uri: https://ko-fi.com/mraiih
28
+ post_install_message:
27
29
  rdoc_options: []
28
30
  require_paths:
29
31
  - lib
@@ -38,8 +40,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
38
40
  - !ruby/object:Gem::Version
39
41
  version: '0'
40
42
  requirements: []
41
- rubygems_version: 3.0.6
42
- signing_key:
43
+ rubygems_version: 3.3.3
44
+ signing_key:
43
45
  specification_version: 4
44
46
  summary: nhentai-api is a basic and easy to use API for nhentai.net
45
47
  test_files: []