OSDN Git Service

load whole thumbs
authornomeu <nomeu@users.sourceforge.jp>
Thu, 18 Oct 2012 06:15:12 +0000 (15:15 +0900)
committernomeu <nomeu@users.sourceforge.jp>
Thu, 18 Oct 2012 06:15:12 +0000 (15:15 +0900)
bin/load-whole-thumbs.rb [new file with mode: 0644]
lib/nicovideo.rb

diff --git a/bin/load-whole-thumbs.rb b/bin/load-whole-thumbs.rb
new file mode 100644 (file)
index 0000000..404e530
--- /dev/null
@@ -0,0 +1,38 @@
+#!ruby
+# encoding: utf-8
+$LOAD_PATH.unshift File.expand_path(File.dirname(__FILE__) + '/../lib')
+$LOAD_PATH.unshift File.expand_path(File.dirname(__FILE__) + '/../app/models')
+require 'connection'
+require 'site'
+require 'arc'
+require 'thumb'
+require 'nicovideo'
+
+nv = Nicovideo.new
+
+class << nv
+  PAGE_SIZE = 30
+
+  def tag_search_and_get_thumbinfo_0(tag_name, options = {})
+    video_ids = tag_search(tag_name, options)
+    video_ids.each do |video_id|
+      puts video_id
+      td = get_thumbinfo(video_id)
+      thumb = Thumb.find(td.video_id) || Thumb.new(:key => td.video_id)
+      thumb.title = td.title
+      thumb.description = td.description
+      thumb.save
+    end
+  end
+
+  def tag_search_and_get_thumbinfo(tag_name, size)
+    max_page = (size - 1) / PAGE_SIZE + 1
+    for page in 1 .. max_page
+      tag_search_and_get_thumbinfo_0(tag_name, :page => page, :order => 'a')
+    end
+  end
+end
+
+nv.tag_search_and_get_thumbinfo("MMDデータ配布あり",      264)
+nv.tag_search_and_get_thumbinfo("MMDモデル配布あり",     3610)
+nv.tag_search_and_get_thumbinfo("MMDモーション配布あり", 1458)
index 7d84acf..270ef83 100644 (file)
@@ -5,9 +5,11 @@ require 'rexml/document'
 
 class Nicovideo
 
-  def tag_search(tag_name)
+  def tag_search(tag_name, options = {})
     video_ids = []
-    uri = "http://www.nicovideo.jp/tag/#{ERB::Util.u tag_name}?sort=f&rss=atom"
+    params = { :sort => 'f', :rss => 'atom' }
+    params.update(options)
+    uri = "http://www.nicovideo.jp/tag/#{ERB::Util.u tag_name}?" + params.map { |name, value| "#{name}=#{value}" }.join('&')
     open(uri) do |f|
       rss = RSS::Parser.parse(f)
       rss.entries.each do |ent|