Class: Doujinshi

Inherits:
Object
  • Object
show all
Defined in:
lib/nhentai-api.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id) ⇒ Doujinshi

Returns a new instance of Doujinshi.



18
19
20
21
22
23
24
25
# File 'lib/nhentai-api.rb', line 18

def initialize(id)
  @id           = id
  @client       = Net::HTTP.get_response(URI("https://nhentai.net/g/#{@id}/"))
  if self.exists?
    @media_id     = @client.body.match(%r{\/([0-9]+)\/cover})[1]
    @count_pages  = @client.body.match(/Pages:\s*.*>([0-9]+)</)[1].to_i
  end
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



16
17
18
# File 'lib/nhentai-api.rb', line 16

def client
  @client
end

#count_pagesObject (readonly)

Returns the value of attribute count_pages.



16
17
18
# File 'lib/nhentai-api.rb', line 16

def count_pages
  @count_pages
end

#idObject (readonly)

Returns the value of attribute id.



16
17
18
# File 'lib/nhentai-api.rb', line 16

def id
  @id
end

#media_idObject (readonly)

Returns the value of attribute media_id.



16
17
18
# File 'lib/nhentai-api.rb', line 16

def media_id
  @media_id
end

Instance Method Details

#artistsObject

Give all artists of a doujinshi

See Also:

Since:

  • 0.1.0



264
265
266
267
268
269
# File 'lib/nhentai-api.rb', line 264

def artists
  res = @client.body.match(%r{Artists:\s+<span class="tags">(.+)<\/span>})
  return [] if res.nil?

  parse_tags(res[1])
end

#artists?Boolean

Check if a particular doujinshi have some artists

Returns:

  • (Boolean)

See Also:

Since:

  • 0.1.0



289
290
291
# File 'lib/nhentai-api.rb', line 289

def artists?
  !@client.body.match(%r{Artists:\s+<span class="tags">(.+)<\/span>}).nil?
end

#categoriesObject

Give all categories of a doujinshi

See Also:

Since:

  • 0.1.0



369
370
371
372
373
374
# File 'lib/nhentai-api.rb', line 369

def categories
  res = @client.body.match(%r{Categories:\s+<span class="tags">(.+)<\/span>})
  return [] if res.nil?

  parse_tags(res[1])
end

#categories?Boolean

Check if a particular doujinshi have some categories

Returns:

  • (Boolean)

See Also:

Since:

  • 0.1.0



394
395
396
# File 'lib/nhentai-api.rb', line 394

def categories?
  !@client.body.match(%r{Categories:\s+<span class="tags">(.+)<\/span>}).nil?
end

#charactersObject

Give all characters of a doujinshi

See Also:

Since:

  • 0.1.0



229
230
231
232
233
234
# File 'lib/nhentai-api.rb', line 229

def characters
  res = @client.body.match(%r{Characters:\s+<span class="tags">(.+)<\/span>})
  return [] if res.nil?

  parse_tags(res[1])
end

#characters?Boolean

Check if a particular doujinshi have some characters

Returns:

  • (Boolean)

See Also:

Since:

  • 0.1.0



254
255
256
# File 'lib/nhentai-api.rb', line 254

def characters?
  !@client.body.match(%r{Characters:\s+<span class="tags">(.+)<\/span>}).nil?
end

#count_artistsObject

Give a counter of artists

See Also:

Since:

  • 0.1.0



277
278
279
280
281
# File 'lib/nhentai-api.rb', line 277

def count_artists
  res = @client.body.match(%r{Artists:\s+<span class="tags">(.+)<\/span>})

  res.nil? ? 0 : parse_tags(res[1]).length
end

#count_categoriesObject

Give a counter of categories

See Also:

Since:

  • 0.1.0



382
383
384
385
386
# File 'lib/nhentai-api.rb', line 382

def count_categories
  res = @client.body.match(%r{Categories:\s+<span class="tags">(.+)<\/span>})

  res.nil? ? 0 : parse_tags(res[1]).length
end

#count_charactersObject

Give a counter of characters

See Also:

Since:

  • 0.1.0



242
243
244
245
246
# File 'lib/nhentai-api.rb', line 242

def count_characters
  res = @client.body.match(%r{Characters:\s+<span class="tags">(.+)<\/span>})

  res.nil? ? 0 : parse_tags(res[1]).length
end

#count_favoritesInteger

Give the number of favorites on a doujinshi

Examples:

doujinshi.num_favorites   #=> 13326

Returns:

  • (Integer)

    a counter of favorites on a given doujinshi

Since:

  • 0.1.0



129
130
131
132
133
# File 'lib/nhentai-api.rb', line 129

def count_favorites
  regex = %r{<span>Favorite <span class="nobold">.(\d+).<\/span><\/span>}

  @client.body.match(regex)[1].to_i
end

#count_groupsObject

Give a counter of groups

See Also:

Since:

  • 0.1.0



312
313
314
315
316
# File 'lib/nhentai-api.rb', line 312

def count_groups
  res = @client.body.match(%r{Groups:\s+<span class="tags">(.+)<\/span>})

  res.nil? ? 0 : parse_tags(res[1]).length
end

#count_languagesObject

Give a counter of languages

See Also:

Since:

  • 0.1.0



347
348
349
350
351
# File 'lib/nhentai-api.rb', line 347

def count_languages
  res = @client.body.match(%r{Languages:\s+<span class="tags">(.+)<\/span>})

  res.nil? ? 0 : parse_tags(res[1]).length
end

#count_parodiesObject

Give a counter of parodies

See Also:

Since:

  • 0.1.0



207
208
209
210
211
# File 'lib/nhentai-api.rb', line 207

def count_parodies
  res = @client.body.match(%r{Parodies:\s+<span class="tags">(.+)<\/span>})

  res.nil? ? 0 : parse_tags(res[1]).length
end

#count_tagsInteger

Give a counter of tags

Examples:

doujinshi.count_tags    #=> 9

Returns:

  • (Integer)

    of tags

Since:

  • 0.1.0



170
171
172
173
174
# File 'lib/nhentai-api.rb', line 170

def count_tags
  res = @client.body.match(%r{Tags:\s*<span class="tags">(.+)<\/span>})

  res.nil? ? 0 : parse_tags(res[1]).length
end

#coverString

Give the cover’s URL of a doujinshi

Examples:

doujinshi.cover   #=> 'https://t.nhentai.net/galleries/1170172/cover.jpg'

Returns:

  • (String)

    the cover’s URL of a given doujinshi

Since:

  • 0.1.0



59
60
61
62
63
# File 'lib/nhentai-api.rb', line 59

def cover
  res = @client.body.match(%r{https://t.nhentai.net/galleries/#{@media_id}/cover\.(.{3})"})

  "https://t.nhentai.net/galleries/#{@media_id}/cover.#{res[1]}"
end

#exists?Bool

Check if a doujinshi with the given id exist

Examples:

doujinshi.exists?   #=> true

Returns:

  • (Bool)

    true if the doujinshi exist, otherwise false

Since:

  • 0.1.0



35
36
37
# File 'lib/nhentai-api.rb', line 35

def exists?
  @client.code == '200'
end

#groupsObject

Give all groups of a doujinshi

See Also:

Since:

  • 0.1.0



299
300
301
302
303
304
# File 'lib/nhentai-api.rb', line 299

def groups
  res = @client.body.match(%r{Groups:\s+<span class="tags">(.+)<\/span>})
  return [] if res.nil?

  parse_tags(res[1])
end

#groups?Boolean

Check if a particular doujinshi have some groups

Returns:

  • (Boolean)

See Also:

Since:

  • 0.1.0



324
325
326
# File 'lib/nhentai-api.rb', line 324

def groups?
  !@client.body.match(%r{Groups:\s+<span class="tags">(.+)<\/span>}).nil?
end

#languagesObject

Give all languages of a doujinshi

See Also:

Since:

  • 0.1.0



334
335
336
337
338
339
# File 'lib/nhentai-api.rb', line 334

def languages
  res = @client.body.match(%r{Languages:\s+<span class="tags">(.+)<\/span>})
  return [] if res.nil?

  parse_tags(res[1])
end

#languages?Boolean

Check if a particular doujinshi have some languages

Returns:

  • (Boolean)

See Also:

Since:

  • 0.1.0



359
360
361
# File 'lib/nhentai-api.rb', line 359

def languages?
  !@client.body.match(%r{Languages:\s+<span class="tags">(.+)<\/span>}).nil?
end

#page(page = 1) ⇒ String

Give the URL of a given page of a doujinshi

Examples:

doujinshi.get_page        #=> 'https://i.nhentai.net/galleries/1170172/1.jpg'
doujinshi.get_page(10)    #=> 'https://i.nhentai.net/galleries/1170172/10.jpg'

Parameters:

  • page (Integer) (defaults to: 1)

    a particular page of a doujinshi

Returns:

  • (String)

    the URL of a given page of a doujinshi

Since:

  • 0.1.0



75
76
77
78
79
# File 'lib/nhentai-api.rb', line 75

def page(page = 1)
  res = @client.body.match(%r{https://t.nhentai.net/galleries/#{@media_id}/#{page}t\.(.{3})"})

  "https://i.nhentai.net/galleries/#{@media_id}/#{page}.#{res[1]}"
end

#pagesArray

Give the URL of a all pages of a doujinshi

Examples:

doujinshi.pages   #=> ['https://i.nhentai.net/galleries/1170172/1.jpg', ..., 'https://i.nhentai.net/galleries/1170172/31.jpg']

Returns:

  • (Array)

    array pages’ URL

Since:

  • 0.1.0



89
90
91
# File 'lib/nhentai-api.rb', line 89

def pages
  (1..@count_pages).map { |page| page(page) }
end

#parodiesObject

Give all parodies of a doujinshi

See Also:

Since:

  • 0.1.0



194
195
196
197
198
199
# File 'lib/nhentai-api.rb', line 194

def parodies
  res = @client.body.match(%r{Parodies:\s+<span class="tags">(.+)<\/span>})
  return [] if res.nil?

  parse_tags(res[1])
end

#parodies?Boolean

Check if a particular doujinshi have some parodies

Returns:

  • (Boolean)

See Also:

Since:

  • 0.1.0



219
220
221
# File 'lib/nhentai-api.rb', line 219

def parodies?
  !@client.body.match(%r{Parodies:\s+<span class="tags">(.+)<\/span>}).nil?
end

#tagsArray

Give all tags of a doujinshi

Examples:

doujinshi.tags

Returns:

  • (Array)

    of Tag class of a given doujinshi

Since:

  • 0.1.0



155
156
157
158
159
160
# File 'lib/nhentai-api.rb', line 155

def tags
  res = @client.body.match(%r{Tags:\s*<span class="tags">(.+)<\/span>})
  return [] if res.nil?

  parse_tags(res[1])
end

#tags?Bool

Check if a particular doujinshi have some tags

Examples:

doujinshi.tags?   #=> true

Returns:

  • (Bool)

    true if the doujinshi have tags, otherwise false

Since:

  • 0.1.0



184
185
186
# File 'lib/nhentai-api.rb', line 184

def tags?
  !@client.body.match(%r{Tags:\s*<span class="tags">(.+)<\/span>}).nil?
end

#thumbnail(page = 1) ⇒ String

Give the thumbnail’s URL of a given page of a doujinshi

Examples:

doujinshi.get_thumbnail       #=> 'https://t.nhentai.net/galleries/1170172/1t.jpg'
doujinshi.get_thumbnail(10)   #=> 'https://t.nhentai.net/galleries/1170172/10t.jpg'

Parameters:

  • page (Integer) (defaults to: 1)

    a particular page of a doujinshi

Returns:

  • (String)

    the thumbnail’s URL of a given page of a doujinshi

Since:

  • 0.1.0



103
104
105
106
107
# File 'lib/nhentai-api.rb', line 103

def thumbnail(page = 1)
  res = @client.body.match(%r{https://t.nhentai.net/galleries/#{@media_id}/(#{page}t\..{3})"})

  "https://t.nhentai.net/galleries/#{@media_id}/#{res[1]}"
end

#thumbnailsArray

Give the URL of a all thumbnails of a doujinshi

Examples:

doujinshi.thumbnails    #=> ['https://t.nhentai.net/galleries/1170172/1t.jpg',..., 'https://t.nhentai.net/galleries/1170172/31t.jpg']

Returns:

  • (Array)

    an array thumbnails’ URL

Since:

  • 0.1.0



117
118
119
# File 'lib/nhentai-api.rb', line 117

def thumbnails
  (1..@count_pages).map { |page| thumbnail(page) }
end

#titleString

Give the title of a doujinshi

Examples:

doujinshi.title   #=> '[Illumination. (Ogadenmon)] Android no Ecchi na Yatsu | Horny Androids (NieR:Automata) [English] =TLL + mrwayne= [Digital]'

Returns:

  • (String)

    the title of a given doujinshi

Since:

  • 0.1.0



47
48
49
# File 'lib/nhentai-api.rb', line 47

def title
  @client.body.match(/"pretty">(.*?)</)[1]
end

#upload_dateInteger

Give the upload date of a doujinshi

Examples:

doujinshi.upload_date   #=> 2018-01-17 15:56:16 +0000

Returns:

  • (Integer)

    the upload date of a given doujinshi

Since:

  • 0.1.0



143
144
145
# File 'lib/nhentai-api.rb', line 143

def upload_date
  Time.iso8601(@client.body.match(/<time .+ datetime="(.*?)"/)[1])
end