require "nicoapi/parser/version" require "nori" require "nokogiri" require "pry" module NicoAPI module Parser class TagSearch def initialize @parser = Nori.new end def setXml(xml) @xml = xml end def parse @object = (@parser.parse @xml)["rss"]["channel"] @items = @object["item"].map do |item_object| Item.new item_object end end def items @items end def tag @object["title"].scan(/(?<=タグ\s).+(?=\‐ニコニコ動画)/)[0].split(' ') end def publish_date Time.parse @object["pubDate"] end def last_build_date Time.parse @object["lastBuildDate"] end class Item def initialize(object) @object = object end def title @object["title"] end def video_id url.scan(/(sm|nm)\d{1,}\Z/) $& end def url @object["link"] end def publish_date Time.parse @object["pubDate"] end def thumbnail_url description.raw_text.scan(/(?<=src\=\").{1,}(?=\"\swidth)/) $& end def description @_description ||= Description.new @object["description"] end def length description.raw_text.scan(/(?<=class\=\"nico\-info\-length\"\>)\d{1,}\:\d{1,2}(?=\<\/strong\>)/) length_string = $&.to_s.split(':') length_string[0].to_i * 60 + length_string[1].to_i end class Description attr_reader :raw_text def initialize(raw_text) @raw_text = raw_text.to_s end def text @raw_text.scan(/(?<=class\=\"nico\-description\").{1,}(?=\<\/p\>)/) $& end end end end end end