Sha256: da84aa1cb03060e431c9b8fd4d5f1a482d897c84451a5f0a64ab534abc040c8a

Contents?: true

Size: 1.35 KB

Versions: 1

Compression:

Stored size: 1.35 KB

Contents

# -*- coding: utf-8 -*-
module Kindai
  class Searcher
    include Enumerable
    attr_accessor :keyword
    def self.search keyword
      Kindai::Util.logger.debug "keyword: #{keyword}"
      me = self.new
      me.keyword = keyword
      me
    end

    def length
      @length ||= total_of(@keyword)
    end

    def each
      (1..(1/0.0)).each{ |page|
        Kindai::Util.logger.debug "page #{page}"
        uris = result_for(@keyword, page)
        return if uris.empty?
        uris.each{ |uri|
          yield Kindai::Book.new_from_permalink(uri)
        }
      }
    end

    protected
    def total_of(keyword)
      page = Nokogiri(Kindai::Util.fetch_uri(uri_for(keyword)))
      total = page.at('.tableheadercontent-left p').content.scan(/\d+/).first.to_i

      Kindai::Util.logger.debug "total: #{total}"
      total
    end

    def result_for keyword, page = 1
      page = Nokogiri Kindai::Util.fetch_uri(uri_for(keyword, page))
      page.search('a.item-link').map{ |item|
        'http://dl.ndl.go.jp' + item.attr('href')
      }
    end

    def uri_for keyword, page = 1
      rows = 100
      params = { :SID => 'kindai', :viewRestricted => 0, :searchWord => keyword, :pageNo => page, :rows => rows }
      root = URI.parse("http://dl.ndl.go.jp/search/searchResult")
      query = '?' + Kindai::Util.expand_params(params)
      root + query
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
kindai-2.8.0 lib/kindai/searcher.rb