--- /dev/null
+#!ruby
+# vim: fileencoding=utf-8
+$LOAD_PATH.unshift File.expand_path(File.dirname(__FILE__) + '/../lib')
+require 'connection'
+require 'thumb'
+
+require 'open-uri'
+require 'erb'
+require 'rss'
+require 'rexml/document'
+
+class Nicovideo
+
+ def tag_search(tag_name)
+ video_ids = []
+ uri = "http://www.nicovideo.jp/tag/#{ERB::Util.u tag_name}?sort=f&rss=atom"
+ open(uri) do |f|
+ rss = RSS::Parser.parse(f)
+ rss.entries.each do |ent|
+ if md = %r(/watch/(.+?)\z).match(ent.link.href)
+ video_id = md[1]
+ video_ids.push video_id
+ end
+ end
+ end
+ video_ids
+ end
+
+ def get_thumbinfo(video_id)
+ uri = "http://ext.nicovideo.jp/api/getthumbinfo/#{video_id}"
+ open(uri) do |f|
+ doc = REXML::Document.new(f)
+ thumb = doc.root.elements['thumb']
+ video_id = thumb.elements['video_id'].text
+ db_thumb = Thumb.find_or_initialize_by_video_id(video_id)
+ db_thumb.title = thumb.elements['title'].text
+ db_thumb.description = thumb.elements['description'].text
+ db_thumb.save
+ end
+ end
+end
+
+nv = Nicovideo.new
+
+def nv.tag_search_and_get_thumbinfo(tag_name)
+ video_ids = tag_search(tag_name)
+ video_ids.each do |video_id|
+ puts video_id
+ get_thumbinfo(video_id)
+ end
+end
+
+nv.tag_search_and_get_thumbinfo("MMDデータ配布あり")
+nv.tag_search_and_get_thumbinfo("MMDモデル配布あり")