class Doujinshi

Attributes

client[R]
count_pages[R]
id[R]
media_id[R]

Public Class Methods

new(id) click to toggle source
# 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

Public Instance Methods

artists() click to toggle source

Give all artists of a doujinshi

@since 0.1.0 @see Doujinshi#tags

# 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?() click to toggle source

Check if a particular doujinshi have some artists

@since 0.1.0 @see Doujinshi#tags?

# File lib/nhentai-api.rb, line 289
def artists?
  !@client.body.match(%r{Artists:\s+<span class="tags">(.+)<\/span>}).nil?
end
categories() click to toggle source

Give all categories of a doujinshi

@since 0.1.0 @see Doujinshi#tags

# 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?() click to toggle source

Check if a particular doujinshi have some categories

@since 0.1.0 @see Doujinshi#tags?

# File lib/nhentai-api.rb, line 394
def categories?
  !@client.body.match(%r{Categories:\s+<span class="tags">(.+)<\/span>}).nil?
end
characters() click to toggle source

Give all characters of a doujinshi

@since 0.1.0 @see Doujinshi#tags

# 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?() click to toggle source

Check if a particular doujinshi have some characters

@since 0.1.0 @see Doujinshi#tags?

# File lib/nhentai-api.rb, line 254
def characters?
  !@client.body.match(%r{Characters:\s+<span class="tags">(.+)<\/span>}).nil?
end
count_artists() click to toggle source

Give a counter of artists

@since 0.1.0 @see Doujinshi#count_tags

# 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_categories() click to toggle source

Give a counter of categories

@since 0.1.0 @see Doujinshi#count_tags

# 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_characters() click to toggle source

Give a counter of characters

@since 0.1.0 @see Doujinshi#count_tags

# 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_favorites() click to toggle source

Give the number of favorites on a doujinshi

@return [Integer] a counter of favorites on a given doujinshi @since 0.1.0 @example

doujinshi.num_favorites   #=> 13326
# 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_groups() click to toggle source

Give a counter of groups

@since 0.1.0 @see Doujinshi#count_tags

# 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_languages() click to toggle source

Give a counter of languages

@since 0.1.0 @see Doujinshi#count_tags

# 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_parodies() click to toggle source

Give a counter of parodies

@since 0.1.0 @see Doujinshi#count_tags

# 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_tags() click to toggle source

Give a counter of tags

@return [Integer] of tags @since 0.1.0 @example

doujinshi.count_tags    #=> 9
# 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
cover() click to toggle source

Give the cover's URL of a doujinshi

@return [String] the cover's URL of a given doujinshi @since 0.1.0 @example

doujinshi.cover   #=> 'https://t.nhentai.net/galleries/1170172/cover.jpg'
# 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?() click to toggle source

Check if a doujinshi with the given id exist

@return [Bool] true if the doujinshi exist, otherwise false @since 0.1.0 @example

doujinshi.exists?   #=> true
# File lib/nhentai-api.rb, line 35
def exists?
  @client.code == '200'
end
groups() click to toggle source

Give all groups of a doujinshi

@since 0.1.0 @see Doujinshi#tags

# 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?() click to toggle source

Check if a particular doujinshi have some groups

@since 0.1.0 @see Doujinshi#tags?

# File lib/nhentai-api.rb, line 324
def groups?
  !@client.body.match(%r{Groups:\s+<span class="tags">(.+)<\/span>}).nil?
end
languages() click to toggle source

Give all languages of a doujinshi

@since 0.1.0 @see Doujinshi#tags

# 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?() click to toggle source

Check if a particular doujinshi have some languages

@since 0.1.0 @see Doujinshi#tags?

# File lib/nhentai-api.rb, line 359
def languages?
  !@client.body.match(%r{Languages:\s+<span class="tags">(.+)<\/span>}).nil?
end
page(page = 1) click to toggle source

Give the URL of a given page of a doujinshi

@param [Integer] page a particular page of a doujinshi @return [String] the URL of a given page of a doujinshi @since 0.1.0 @example

doujinshi.get_page        #=> 'https://i.nhentai.net/galleries/1170172/1.jpg'
doujinshi.get_page(10)    #=> 'https://i.nhentai.net/galleries/1170172/10.jpg'
# 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
pages() click to toggle source

Give the URL of a all pages of a doujinshi

@return [Array] array pages' URL @since 0.1.0 @example

doujinshi.pages   #=> ['https://i.nhentai.net/galleries/1170172/1.jpg', ..., 'https://i.nhentai.net/galleries/1170172/31.jpg']
# File lib/nhentai-api.rb, line 89
def pages
  (1..@count_pages).map { |page| page(page) }
end
parodies() click to toggle source

Give all parodies of a doujinshi

@since 0.1.0 @see Doujinshi#tags

# 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?() click to toggle source

Check if a particular doujinshi have some parodies

@since 0.1.0 @see Doujinshi#tags?

# File lib/nhentai-api.rb, line 219
def parodies?
  !@client.body.match(%r{Parodies:\s+<span class="tags">(.+)<\/span>}).nil?
end
tags() click to toggle source

Give all tags of a doujinshi

@return [Array] of Tag class of a given doujinshi @since 0.1.0 @example

doujinshi.tags
# 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?() click to toggle source

Check if a particular doujinshi have some tags

@return [Bool] true if the doujinshi have tags, otherwise false @since 0.1.0 @example

doujinshi.tags?   #=> true
# File lib/nhentai-api.rb, line 184
def tags?
  !@client.body.match(%r{Tags:\s*<span class="tags">(.+)<\/span>}).nil?
end
thumbnail(page = 1) click to toggle source

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

@param [Integer] page a particular page of a doujinshi @return [String] the thumbnail's URL of a given page of a doujinshi @since 0.1.0 @example

doujinshi.get_thumbnail       #=> 'https://t.nhentai.net/galleries/1170172/1t.jpg'
doujinshi.get_thumbnail(10)   #=> 'https://t.nhentai.net/galleries/1170172/10t.jpg'
# 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
thumbnails() click to toggle source

Give the URL of a all thumbnails of a doujinshi

@return [Array] an array thumbnails' URL @since 0.1.0 @example

doujinshi.thumbnails    #=> ['https://t.nhentai.net/galleries/1170172/1t.jpg',..., 'https://t.nhentai.net/galleries/1170172/31t.jpg']
# File lib/nhentai-api.rb, line 117
def thumbnails
  (1..@count_pages).map { |page| thumbnail(page) }
end
title() click to toggle source

Give the title of a doujinshi

@return [String] the title of a given doujinshi @since 0.1.0 @example

doujinshi.title   #=> '[Illumination. (Ogadenmon)] Android no Ecchi na Yatsu | Horny Androids (NieR:Automata) [English] =TLL + mrwayne= [Digital]'
# File lib/nhentai-api.rb, line 47
def title
  @client.body.match(/"pretty">(.*?)</)[1]
end
upload_date() click to toggle source

Give the upload date of a doujinshi

@return [Integer] the upload date of a given doujinshi @since 0.1.0 @example

doujinshi.upload_date   #=> 2018-01-17 15:56:16 +0000
# File lib/nhentai-api.rb, line 143
def upload_date
  Time.iso8601(@client.body.match(/<time .+ datetime="(.*?)"/)[1])
end

Private Instance Methods

parse_tags(res) click to toggle source

@private

# File lib/nhentai-api.rb, line 404
def parse_tags(res)
  res.split(%r{<a(.+?)<\/a>}).reject(&:empty?).map do |line|
    id    = line.match(/tag-(\d+)/)[1]
    name  = line.match(/">(.+?)</)[1].strip
    count = line.match(/class="count">(\d+.)</)[1]
    url   = line.match(/href=\"(.+?)\"/)[1]

    count = count[-1] == 'K' ? count.to_i * 1000 : count.to_i

    Info.new(id, name, count, url)
  end
end