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{