OSDN Git Service

added
authornomeu <nomeu@nomeu.org>
Mon, 14 Jun 2010 05:23:34 +0000 (14:23 +0900)
committernomeu <nomeu@nomeu.org>
Mon, 14 Jun 2010 05:23:34 +0000 (14:23 +0900)
bin/load-thumbs.rb [new file with mode: 0644]
lib/thumb.rb [new file with mode: 0644]

diff --git a/bin/load-thumbs.rb b/bin/load-thumbs.rb
new file mode 100644 (file)
index 0000000..6a42f05
--- /dev/null
@@ -0,0 +1,54 @@
+#!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モデル配布あり")
diff --git a/lib/thumb.rb b/lib/thumb.rb
new file mode 100644 (file)
index 0000000..6fcaff1
--- /dev/null
@@ -0,0 +1,3 @@
+class Thumb < ActiveRecord::Base
+  set_primary_key :video_id
+end