Sha256: 72b04339cb1ec10996e49dd5b8b189f35849a6d1231973762165c1e3f57d807b

Contents?: true

Size: 1.68 KB

Versions: 1

Compression:

Stored size: 1.68 KB

Contents

require 'json'
require 'bremen/base'

module Bremen
  class Nicovideo < Bremen::Base
    BASE_URL = 'http://www.nicovideo.jp/search/'
    self.default_options = {
      keyword: '',
      sort: 'f', #n(newer commented)/v(viewed)/r(most commented)/m(listed)/f(uploaded)/l(duration)
      order: 'd', #a(asc)/d(desc)
      within: '', #1(24h)/2(1w)/3(1m)
      length: '', #1(-5min)/2(20min-)
      downloadable: '', #1(music downloadable)
    }

    class << self
      def search_url options = {}
        options = default_options.merge(options)
        query = {
          sort: options[:sort],
          order: options[:order],
          f_range: options[:within],
          l_range: options[:length],
          opt_md: options[:downloadable],
        }
        "#{BASE_URL}#{CGI.escape(options[:keyword])}?#{build_query(query)}"
      end

      private
      def convert_from_response response
        response.scan(%r{<div class="thumb_col_1">\n<!---->\n(.*?)\n<!---->\n</div></div>}m).flatten.map do |html|
          uid = html.scan(%r{<table [^>]+ summary="(.+)">}).flatten.first
          min, sec = html.scan(%r{<p class="vinfo_length"><span>([\d:]+)</span></p>}).flatten.first.to_s.split(':')
          created_at = Time.parse(html.scan(%r{<strong>(.+:\d\d)</strong>}).flatten.first.to_s.gsub(/\xE5\xB9\xB4|\xE6\x9C\x88|\xE6\x97\xA5/, ''))
          new({
            uid: uid,
            url: "http://www.nicovideo.jp/watch/#{uid}",
            title: CGI.unescape(html.scan(%r{<a [^>]+ class="watch" [^>]+>(.+)</a>}).flatten.first.to_s),
            length: min.to_i * 60 + sec.to_i,
            created_at: created_at,
            updated_at: created_at,
          })
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
bremen-0.0.1 lib/bremen/nicovideo.rb