nhentai-api 1.0 → 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 4d63124d136363e121f01218322464cde8ea037e567b73246ffdc56fde309dd0
4
- data.tar.gz: 34796062c118c4db00bac99648af7a7be3f23d2fc5e85751e82929486235fbae
3
+ metadata.gz: 962d39c7b5641737d50b8aae5f6d66b94b330642abe758beb50ada6d1f10008e
4
+ data.tar.gz: e5ce54e4cb8e87e9aac4c8339df0073a20ec33a95ea467691eb75d3b4b77f59b
5
5
  SHA512:
6
- metadata.gz: b690c41f13b19bf979c3d21ef001a4ed6d26975fe214b22c1eac0364130d9d1ba006cc4a5af6beec518c8e20cb90749bc830de2e965072699cae406c6bb5a77a
7
- data.tar.gz: 687c9c8b048e0d16bc848111d3b6cde52339cad52b8bbcd8f235b5a6e597cdd439b6a06d15dd06f77547dc9adcba5cdb0daaaef708783d06bdbebfd21babbd29
6
+ metadata.gz: 5a463e10bb67f877c7768f59e0da29be55e2df000b7b0cb6032725d9d040c0614e2a3298cd0e8718c008644d3b70659d8634fea53727c632a1df0d2998b2e7d5
7
+ data.tar.gz: 8157e7ed49c0e59bfbca4433d9f2f1534514f6e3ee5d9a4ff2485dc0c9f216d766de7c3e3b410593f0e76f614face9f7995a68d3e8690627c19d178884acb80a
@@ -0,0 +1,112 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Doujinshi
4
+ def initialize(id:)
5
+ case id
6
+ when Integer, String
7
+ @id = id
8
+ @client = Net::HTTP.get_response(URI("https://nhentai.net/api/gallery/#{id}"))
9
+ return unless exists?
10
+
11
+ @response = JSON.parse(client.body)
12
+ when Hash
13
+ @response = id
14
+ end
15
+ end
16
+
17
+ def self.random
18
+ fetch('https://nhentai.net/random')
19
+ end
20
+
21
+ def exists?
22
+ client.code == '200'
23
+ end
24
+
25
+ def media_id
26
+ response['media_id']
27
+ end
28
+
29
+ def num_pages
30
+ response['num_pages']
31
+ end
32
+
33
+ def title(type: :pretty)
34
+ response['title'][type.to_s]
35
+ end
36
+
37
+ def cover
38
+ "https://t.nhentai.net/galleries/#{media_id}/cover.#{IMAGE_EXTENSION[response['images']['cover']['t']]}"
39
+ end
40
+
41
+ def page(page: 1)
42
+ "https://i.nhentai.net/galleries/#{media_id}/#{page}.#{IMAGE_EXTENSION[response['images']['pages'][page - 1]['t']]}"
43
+ end
44
+
45
+ def pages
46
+ (1..num_pages).map { |page| page(page: page) }
47
+ end
48
+
49
+ def thumbnail(page: 1)
50
+ "https://t.nhentai.net/galleries/#{media_id}/#{page}t.#{IMAGE_EXTENSION[response['images']['pages'][page - 1]['t']]}"
51
+ end
52
+
53
+ def thumbnails
54
+ (1..num_pages).map { |page| thumbnail(page: page) }
55
+ end
56
+
57
+ def count_favorites
58
+ response['num_favorites']
59
+ end
60
+
61
+ def upload_date
62
+ Time.at(response['upload_date']).utc
63
+ end
64
+
65
+ %w[tags parodies characters artists groups languages categories].each do |method|
66
+ define_method method do
67
+ return instance_variable_get("@#{method}") if instance_variable_defined?("@#{method}")
68
+
69
+ res = response['tags'].select { |tag| tag['type'] == SINGULAR_TAG[method] }
70
+ instance_variable_set("@#{method}", parsing_informations(res))
71
+ end
72
+
73
+ define_method "count_#{method}" do
74
+ send(method).size
75
+ end
76
+
77
+ define_method "#{method}?" do
78
+ !send(method).empty?
79
+ end
80
+ end
81
+
82
+ def related
83
+ client = Net::HTTP.get_response(URI("https://nhentai.net/api/gallery/#{id}/related"))
84
+ response = JSON.parse(client.body)
85
+
86
+ response['result'].map { |doujin| Doujinshi.new(id: doujin) }
87
+ end
88
+
89
+ private
90
+
91
+ attr_reader :client, :response, :id
92
+
93
+ def parsing_informations(res)
94
+ res.map do |line|
95
+ OpenStruct.new(
96
+ id: line['id'],
97
+ name: line['name'],
98
+ count: line['count'],
99
+ url: line['url']
100
+ )
101
+ end
102
+ end
103
+
104
+ def self.fetch(uri_str)
105
+ client = Net::HTTP.get_response(URI(uri_str))
106
+
107
+ case client
108
+ when Net::HTTPFound then new(id: client['location'][3..-2])
109
+ when Net::HTTPRedirection then fetch("https://nhentai.net#{client['location']}")
110
+ end
111
+ end
112
+ end
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ %w[tag parody character artist group language category].each do |class_name|
4
+ c = Class.new do
5
+ attr_reader :client
6
+
7
+ def initialize(keyword:, sort: :none, page: 1)
8
+ @client = Net::HTTP.get_response(URI("https://nhentai.net/#{class_name}/#{keyword.tr(' ', '-')}/#{SORT[sort]}?page=#{page}"))
9
+ end
10
+
11
+ def count
12
+ res = client.body.match(%r{<a.*class="count">(.*)<\/span><\/a>})
13
+ return 0 if res.nil?
14
+
15
+ count = res[1]
16
+ count[-1] == 'K' ? count.to_i * 1000 : count.to_i
17
+ end
18
+
19
+ def listing
20
+ res = client.body.split(%r{<div class="gallery".+?>(.*?)<\/div>}).select { |line| line.include?('<a href="/g/') }
21
+ parse_tiles(res)
22
+ end
23
+
24
+ def exists?
25
+ @client.code == '200'
26
+ end
27
+
28
+ private
29
+
30
+ def class_name
31
+ self.class.name.split('::').last.downcase
32
+ end
33
+ end
34
+
35
+ Kernel.const_set(class_name.capitalize, c)
36
+ end
@@ -0,0 +1,79 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Search
4
+ def initialize(options:, sort: :none, page: 1)
5
+ @options = options
6
+ @client = Net::HTTP.get_response(URI("https://nhentai.net/api/galleries/search?query=#{string_options}&sort=#{SORT[sort]}&page=#{page}"))
7
+ return unless exists?
8
+
9
+ @response = JSON.parse(client.body)
10
+ end
11
+
12
+ def exists?
13
+ client.code == '200'
14
+ end
15
+
16
+ def count
17
+ response['result'].count
18
+ end
19
+
20
+ def num_pages
21
+ response['num_pages']
22
+ end
23
+
24
+ def per_page
25
+ response['per_page']
26
+ end
27
+
28
+ def listing
29
+ response['result'].map { |doujin| Doujinshi.new(id: doujin) }
30
+ end
31
+
32
+ private
33
+
34
+ attr_reader :options, :client, :response
35
+
36
+ def string_options
37
+ %i[keywords tags pages dates]
38
+ .map { |symbol| send("parse_#{symbol}", options[symbol]) if options[symbol] }
39
+ .join(' ')
40
+ end
41
+
42
+ def parse_pages(pages)
43
+ pages.map { |page| "pages:#{page.tr(' ', '')}" }
44
+ end
45
+
46
+ def parse_dates(dates)
47
+ dates.map { |date| "uploaded:#{date.tr(' ', '')}" }
48
+ end
49
+
50
+ def parse_tags(tags)
51
+ ary = []
52
+
53
+ %i[included excluded].each do |type|
54
+ next if tags[type].nil?
55
+
56
+ %i[tags parodies characters artists groups languages categories].each do |subtype|
57
+ next if tags[type][subtype].empty? || tags[type][subtype].nil?
58
+
59
+ ary <<
60
+ tags[type][subtype]
61
+ .map { |word| word.include?(' ') ? "\"#{word}\"" : word }
62
+ .map { |word| word.prepend("#{subtype}:") }
63
+ .map { |word| type == :excluded ? word.prepend('-') : word }
64
+ end
65
+ end
66
+
67
+ ary
68
+ end
69
+
70
+ def parse_keywords(keywords)
71
+ %i[included excluded].map do |type|
72
+ next if keywords[type].nil?
73
+
74
+ keywords[type]
75
+ .map { |word| type == :excluded ? word.prepend('-') : word }
76
+ .map { |word| word.include?(' ') ? "\"#{word}\"" : word }
77
+ end
78
+ end
79
+ end
data/lib/nhentai-api.rb CHANGED
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  %w[net/http ostruct time json].each { |e| require e }
4
- %w[doujinshi search key].each { |e| require_relative e }
4
+ %w[doujinshi search key].each { |e| require "nhentai-api/#{e}" }
5
5
 
6
6
  SORT = {
7
7
  today: 'popular-today',
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nhentai-api
3
3
  version: !ruby/object:Gem::Version
4
- version: '1.0'
4
+ version: 1.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gael Roussel
@@ -17,6 +17,9 @@ extensions: []
17
17
  extra_rdoc_files: []
18
18
  files:
19
19
  - lib/nhentai-api.rb
20
+ - lib/nhentai-api/doujinshi.rb
21
+ - lib/nhentai-api/key.rb
22
+ - lib/nhentai-api/search.rb
20
23
  homepage: https://rubygems.org/gems/nhentai-api
21
24
  licenses:
22
25
  - MIT
@@ -40,7 +43,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
40
43
  - !ruby/object:Gem::Version
41
44
  version: '0'
42
45
  requirements: []
43
- rubygems_version: 3.3.3
46
+ rubygems_version: 3.1.6
44
47
  signing_key:
45
48
  specification_version: 4
46
49
  summary: nhentai-api is a basic and easy to use API for nhentai.net