Included Modules

Files

Nicovideo::Search

Public Class Methods

new(agent, keyword, sort=nil, order=nil, pagenum=1) click to toggle source
# File lib/nicovideo/search.rb, line 7
def initialize agent, keyword, sort=nil, order=nil, pagenum=1
  super(agent)
  @search_type = 'search'
  #@keyword = CGI.escape(CGI.escape(keyword))
  @keyword = CGI.escape(keyword)
  @sort    = sort
  @order   = order
  @pagenum = pagenum

  params = ["videos", "total_size", "has_next?", "has_prev?"]
  self.register_getter params

  @url = url()

  puts_info "url = #{@url}"
  puts_info "sort=#{@sort},order=#{@order},pagenum=#{@pagenum}"
end

Public Instance Methods

each() click to toggle source
# File lib/nicovideo/search.rb, line 34
def each
  self.videos.each {|v|
    yield v
  }
end
next() click to toggle source
# File lib/nicovideo/search.rb, line 54
def next
  self.pagenum = @pagenum + 1
  self
end
page=(pagenum) click to toggle source
# File lib/nicovideo/search.rb, line 50
def page=(pagenum)
  self.pagenum = pagenum
end
pagenum=(pagenum) click to toggle source
# File lib/nicovideo/search.rb, line 42
def pagenum=(pagenum)
  if @pagenum != pagenum
    @pagenum = pagenum
    get_page(self.url, true)
  end
  @pagenum
end
prev() click to toggle source
# File lib/nicovideo/search.rb, line 59
def prev
  self.pagenum = @pagenum - 1
  self
end
to_a() click to toggle source
# File lib/nicovideo/search.rb, line 40
def to_a() self.videos end
url() click to toggle source
# File lib/nicovideo/search.rb, line 25
def url
  url = "#{BASE_URL}/#{@search_type}/#{@keyword}"
  url += '?' if (@sort || @order || @pagenum)
  url += '&sort='  + @sort  if @sort
  url += '&order=' + @order if @order
  url += '&page=' + @pagenum.to_s if @pagenum
  url
end

Protected Instance Methods

parse(page) click to toggle source
# File lib/nicovideo/search.rb, line 65
def parse(page)
  if page.body =~ /<\/strong> を含む動画はありません。/
      @not_found = true
      raise NotFound
  end

  @total_size = page.search('strong[@class="search_total"]').first.inner_html.sub(/,/,'').to_i

  @has_next = false
  @has_prev = false
  respages = page/'//div[@class="pagelink"]'
  puts_info respages.size
  respages.each {|r| puts_info r.inner_html }
  if respages.size > 0
    respages.each {|text|
      if text.inner_html =~ /<a class="prevpage".*?>.*?戻る.*?<\/a>/
        @has_prev = true
      end
      if text.inner_html =~ /<a class="nextpage".*?>.*?進む.*?<\/a>/
        @has_next = true
      end
    }
  end

  result_xpath = page/'//div[@class="cmn_thumb_R"]//p[@class="TXT12"]/a[@class="video"]'
  
  puts_info result_xpath.size.to_s
  @videos = result_xpath.inject([]) {|arr, v|
    vp = VideoPage.new(@agent, v.attributes['href'].sub(/watch\/(\w+)$/,'\1'))
    vp.title = v.inner_html
    arr << vp
  }
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.