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.



11
12
13
14
15
16
17
18
# File 'lib/nhentai-api.rb', line 11

def initialize(id)
  @id           = id
  @client       = Net::HTTP.get_response(URI("https://nhentai.net/g/#{@id}/"))
  return unless exists?

  @media_id     = client.body.match(%r{\/([0-9]+)\/cover})[1]
  @count_pages  = client.body.match(/Pages:\s*.*>([0-9]+)</)[1].to_i
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



9
10
11
# File 'lib/nhentai-api.rb', line 9

def client
  @client
end

#count_pagesObject (readonly)

Returns the value of attribute count_pages.



9
10
11
# File 'lib/nhentai-api.rb', line 9

def count_pages
  @count_pages
end

#idObject (readonly)

Returns the value of attribute id.



9
10
11
# File 'lib/nhentai-api.rb', line 9

def id
  @id
end

#media_idObject (readonly)

Returns the value of attribute media_id.



9
10
11
# File 'lib/nhentai-api.rb', line 9

def media_id
  @media_id
end

#responseObject (readonly)

Returns the value of attribute response.



9
10
11
# File 'lib/nhentai-api.rb', line 9

def response
  @response
end

Instance Method Details

#count_favoritesObject



54
55
56
57
58
# File 'lib/nhentai-api.rb', line 54

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

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

#coverObject



28
29
30
31
32
# File 'lib/nhentai-api.rb', line 28

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?Boolean

Returns:

  • (Boolean)


20
21
22
# File 'lib/nhentai-api.rb', line 20

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

#page(page = 1) ⇒ Object



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

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

#pagesObject



40
41
42
# File 'lib/nhentai-api.rb', line 40

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

#thumbnail(page = 1) ⇒ Object



44
45
46
47
48
# File 'lib/nhentai-api.rb', line 44

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

#thumbnailsObject



50
51
52
# File 'lib/nhentai-api.rb', line 50

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

#titleObject



24
25
26
# File 'lib/nhentai-api.rb', line 24

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

#upload_dateObject



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

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