Class: Panchira::Narou::Novel18Resolver

Inherits:
Resolver
  • Object
show all
Defined in:
lib/panchira/resolvers/narou_resolver.rb

Constant Summary collapse

URL_REGEXP =
/novel18\.syosetu\.com\//.freeze
ID_REGEXP =
%{novel18\.syosetu\.com/(?<id>[^/]+)}

Instance Method Summary collapse

Methods inherited from Resolver

applicable?, #fetch

Constructor Details

#initialize(url, options = nil) ⇒ Novel18Resolver

Returns a new instance of Novel18Resolver.



11
12
13
14
15
16
17
# File 'lib/panchira/resolvers/narou_resolver.rb', line 11

def initialize(url, options = nil)
  super(url, options)

  if id = @url.match(ID_REGEXP)[:id]
    @desc = fetch_page("https://novel18.syosetu.com/novelview/infotop/ncode/#{id}/")
  end
end

Instance Method Details

#fetch_page(uri) ⇒ Object



19
20
21
22
23
24
25
26
# File 'lib/panchira/resolvers/narou_resolver.rb', line 19

def fetch_page(uri)
  u = URI.parse(uri)
  http = Net::HTTP.new(u.host, u.port)
  http.use_ssl = u.port == 443
  res = http.get u.request_uri, {'cookie' => 'over18=yes;'}

  Nokogiri::HTML.parse(res.body, uri)
end

#parse_authorObject



32
33
34
# File 'lib/panchira/resolvers/narou_resolver.rb', line 32

def parse_author
  @desc&.xpath('//*[@id="noveltable1"]/tr[2]/td')&.text&.strip
end

#parse_canonical_urlObject

og:urlで指定されたncode.syosetu.com/~~~にアクセスすると301で戻されるので何もしない



42
43
44
# File 'lib/panchira/resolvers/narou_resolver.rb', line 42

def parse_canonical_url
  @url
end

#parse_descriptionObject



28
29
30
# File 'lib/panchira/resolvers/narou_resolver.rb', line 28

def parse_description
  @desc&.xpath('//*[@id="noveltable1"]/tr/td')&.first&.text&.strip
end

#parse_tagsObject



36
37
38
39
# File 'lib/panchira/resolvers/narou_resolver.rb', line 36

def parse_tags
  # つらい。
  @desc&.xpath('//*[@id="noveltable1"]/tr[3]')&.text&.split("\n\n\n")&.dig(1)&.split(' ')
end