require 'json' require 'bremen/base' module Bremen class Nicovideo < Bremen::Base BASE_URL = 'http://www.nicovideo.jp/' self.default_options = { keyword: '', page: 1, 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 find_url uid_or_url unless uid_or_url.include?('www.nicovideo.jp') "#{BASE_URL}watch/#{uid_or_url}" else uid_or_url end end def search_url options = {} options = default_options.merge(options) query = { page: options[:page], sort: options[:sort], order: options[:order], f_range: options[:within], l_range: options[:length], opt_md: options[:downloadable], } "#{BASE_URL}search/#{CGI.escape(options[:keyword])}?#{build_query(query)}" end private def convert_singly response uid = response.scan(%r{}).flatten.first created_at = Time.parse(response.scan(%r{}).flatten.first.to_s).utc new({ uid: uid, url: "#{BASE_URL}watch/#{uid}", title: CGI.unescape(response.scan(%r{}).flatten.first.to_s), author: Bremen::Author.new({ name: response.scan(%r{(.+)}).flatten.first, }), length: response.scan(%r{}).flatten.first.to_i, thumbnail_url: response.scan(%r{}).flatten.first, created_at: created_at, updated_at: created_at, }) end def convert_multiply response return [] if response.scan(%r{
}).flatten.first response.scan(%r{
\n\n(.*?)\n\n
}m).flatten.map do |html| uid = html.scan(%r{]+ summary="(.+)">}).flatten.first min, sec = html.scan(%r{

([\d:]+)

}).flatten.first.to_s.split(':') created_at = Time.parse(html.scan(%r{(.+:\d\d)}).flatten.first.to_s.gsub(/\xE5\xB9\xB4|\xE6\x9C\x88|\xE6\x97\xA5/, '')).utc new({ uid: uid, url: "#{BASE_URL}watch/#{uid}", title: CGI.unescape(html.scan(%r{]+ class="watch" [^>]+>(.+)}).flatten.first.to_s), length: min.to_i * 60 + sec.to_i, thumbnail_url: html.scan(%r{]*class="img_std96" ?/?>}).flatten.first, created_at: created_at, updated_at: created_at, }) end end end end end