Class: Tag

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

Direct Known Subclasses

Artist, Category, Character, Group, Language, Parody

Class Method Summary collapse

Class Method Details

.listing(keyword, sort = 1, page = 1) ⇒ Array

List all doujinshi of the page of a given tag

Parameters:

  • keyword (String)

    of the research

  • sort (Integer) (defaults to: 1)

    optional, 1 is sorting by time, 2 is by popularity

  • page (Integer) (defaults to: 1)

    each page can return 25 doujinshi

Returns:

  • (Array)

    array of Info

Since:

  • 0.2.0



428
429
430
431
432
433
434
435
# 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) ⇒ Object



440
441
442
443
444
445
446
447
448
449
# 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