class Tag

Public Class Methods

listing(keyword, sort = 1, page = 1) click to toggle source

List all doujinshi of the page of a given tag

@param [String] keyword of the research @param [Integer] sort optional, 1 is sorting by time, 2 is by popularity @param [Integer] page each page can return 25 doujinshi @return [Array] array of Info @since 0.2.0

# File lib/nhentai-api.rb, line 428
def self.listing(keyword, sort = 1, page = 1)
  keyword.tr!(' ', '-')
  sort = sort == 1 ? '' : 'popular'
  client = Net::HTTP.get_response(URI("https://nhentai.net/tag/#{keyword}/#{sort}?page=#{page}"))
  res = client.body.split(%r{<div class="gallery".+?>(.+)</div>}).select { |line| line.include?('<a href="/g/') }

  parse_tags(res)
end
parse_tags(res) click to toggle source

@private

# File lib/nhentai-api.rb, line 440
def self.parse_tags(res)
  res.map do |line|
    id    = line.match(%r{/g/(\d+)/})[1]
    name  = line.match(%r{<div class="caption">(.+)</div>})[1].strip
    count = 1
    url   = "/g/#{id}"

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