OSDN Git Service

add archivers
authornomeu <nomeu@users.sourceforge.jp>
Tue, 26 Jul 2011 11:34:49 +0000 (20:34 +0900)
committernomeu <nomeu@users.sourceforge.jp>
Tue, 26 Jul 2011 11:34:49 +0000 (20:34 +0900)
app/models/arc.rb
bin/read-lzh-entries.rb [new file with mode: 0644]
bin/read-rar-entries.rb [new file with mode: 0644]
bin/read-zip-entries.rb [new file with mode: 0644]
bin/uppervolta-3d.net-all.rb [new file with mode: 0644]
lib/archivers.rb [new file with mode: 0644]

index 05a5b96..e781036 100644 (file)
@@ -1,47 +1,27 @@
-gem 'zipruby'
-require 'zipruby'
-require 'nkf'
+require 'archivers'
 
 class Arc < ActiveGroonga::Base
   def code
     key
   end
 
-  def encode(str)
-    str && NKF.nkf('-Sw', str)
-  end
-
   def create_zip_entries(path)
-    Zip::Archive.open(path) do |ar|
-      puts "# #{path}"
-
-      n = ar.num_files # number of entries
-      n.times do |i|
-        # entry_name = ar.get_name(i) # get entry name from archive
-
-        # open entry
-        ar.fopen(i) do |f|
-          path = encode(f.name)
-          case path
-          when /\.pmd\z/
-            puts [ code, path ].join(' ')
-            pmds = Pmd.select { |rec| rec.arc == code && rec.path == path }
-            pmds.count != 0 || Pmd.create(:arc => code, :path => path)
-          when /\.vmd\z/
-            puts [ code, path ].join(' ')
-            vmds = Vmd.select { |rec| rec.arc == code && rec.path == path }
-            vmds.count != 0 || Vmd.create(:arc => code, :path => path)
-          when /\.x\z/
-            puts [ code, path ].join(' ')
-            xes = X.select { |rec| rec.arc == code && rec.path == path }
-            xes.count != 0 || X.create(:arc => code, :path => path)
-          end
-
-          # content = f.read # read entry content
-        end
+    zip = Archivers::Zip.new(path)
+    zip.each_entry do |ent|
+      case ent
+      when /\.pmd\z/
+        puts [ code, ent ].join(' ')
+        pmds = Pmd.select { |rec| rec.arc == code && rec.path == ent }
+        pmds.count != 0 || Pmd.create(:arc => code, :path => ent)
+      when /\.vmd\z/
+        puts [ code, ent ].join(' ')
+        vmds = Vmd.select { |rec| rec.arc == code && rec.path == ent }
+        vmds.count != 0 || Vmd.create(:arc => code, :path => ent)
+      when /\.x\z/
+        puts [ code, ent ].join(' ')
+        xes = X.select { |rec| rec.arc == code && rec.path == ent }
+        xes.count != 0 || X.create(:arc => code, :path => ent)
       end
     end
-  rescue Zip::Error => exc
-    puts "error: " + exc.message
   end
 end
diff --git a/bin/read-lzh-entries.rb b/bin/read-lzh-entries.rb
new file mode 100644 (file)
index 0000000..54428fb
--- /dev/null
@@ -0,0 +1,12 @@
+#!ruby
+# encoding: utf-8
+$LOAD_PATH.unshift File.expand_path(File.dirname(__FILE__) + '/../lib')
+require 'rubygems'
+require 'archivers'
+
+path = ARGV.shift
+
+lzh = Archivers::Lzh.new(path)
+lzh.each_entry do |ent|
+  puts ent
+end
diff --git a/bin/read-rar-entries.rb b/bin/read-rar-entries.rb
new file mode 100644 (file)
index 0000000..e65306f
--- /dev/null
@@ -0,0 +1,12 @@
+#!ruby
+# encoding: utf-8
+$LOAD_PATH.unshift File.expand_path(File.dirname(__FILE__) + '/../lib')
+require 'rubygems'
+require 'archivers'
+
+path = ARGV.shift
+
+rar = Archivers::Rar.new(path)
+rar.each_entry do |ent|
+  puts ent
+end
diff --git a/bin/read-zip-entries.rb b/bin/read-zip-entries.rb
new file mode 100644 (file)
index 0000000..9a54493
--- /dev/null
@@ -0,0 +1,12 @@
+#!ruby
+# encoding: utf-8
+$LOAD_PATH.unshift File.expand_path(File.dirname(__FILE__) + '/../lib')
+require 'rubygems'
+require 'archivers'
+
+path = ARGV.shift
+
+zip = Archivers::Zip.new(path)
+zip.each_entry do |ent|
+  puts ent
+end
diff --git a/bin/uppervolta-3d.net-all.rb b/bin/uppervolta-3d.net-all.rb
new file mode 100644 (file)
index 0000000..fda39c1
--- /dev/null
@@ -0,0 +1,44 @@
+#!/usr/bin/ruby
+# download from uppervolta-3d.net
+#
+$LOAD_PATH.unshift File.expand_path(File.dirname(__FILE__) + '/../lib')
+require 'sn_uploader'
+
+def each_filename(text)
+  file_href = './src/'
+  file_re = Regexp.new(Regexp.escape(file_href) + '(.+?)\.html')
+  text.scan(/href="(.+?)"/) do |href, |
+    if md = file_re.match(href)
+      yield md[1]
+    end
+  end
+end
+
+name = ARGV.shift || 'kiss'
+
+uploader = SnUploader.new
+uploader.host = "uppervolta-3d.net"
+uploader.root_path = "/" + name
+uploader.base_html = "all.html"
+uploader.local_dir = "/Volumes/uploader/arc/uppervolta-3d.net/" + name
+
+src_path = "/Volumes/uploader/src/uppervolta-3d.net/" + name + "/all.html"
+
+if false
+  # read index.html from remote site
+  body = uploader.get_base
+  # save index.html as local cache
+  open(src_path, 'wb') { |f| f.write body }
+end
+
+# read index.html from local cache
+body = IO.read(src_path)
+
+each_filename(body) do |filename|
+  exist = uploader.local_file_exist?(filename)
+  puts [ filename, exist ? 'exist' : 'download' ].join("\t")
+  unless exist
+    sleep(5)
+    uploader.download_file_through_clicker(filename)
+  end
+end
diff --git a/lib/archivers.rb b/lib/archivers.rb
new file mode 100644 (file)
index 0000000..cbdbe9f
--- /dev/null
@@ -0,0 +1,76 @@
+gem 'zipruby'
+require 'zipruby'
+require 'nkf'
+
+module Archivers
+  class Lzh
+    def initialize(path)
+      @path = path
+    end
+
+    def each_entry
+      text = `lha lv #{@path}`
+      mode = :head
+      StringIO.new(text).each_line do |line|
+        case mode
+        when :head
+          if /\A----/.match(line)
+            mode = :flip
+            next
+          end
+        when :flip
+          if /\A----/.match(line)
+            mode = :tail
+            next
+          end
+          yield line
+          mode = :flop
+        when :flop
+          mode = :flip
+        end
+      end
+    end
+  end
+
+  class Rar
+    def initialize(path)
+      @path = path
+    end
+
+    def each_entry
+      text = `unrar vb #{@path}`
+      StringIO.new(text).each_line do |line|
+        yield line
+      end
+    end
+  end
+
+  class Zip
+    def initialize(path)
+      @path = path
+    end
+
+    def encode(str)
+      str && NKF.nkf('-Sw', str)
+    end
+
+    def each_entry
+      ::Zip::Archive.open(@path) do |ar|
+        n = ar.num_files # number of entries
+        n.times do |i|
+          # entry_name = ar.get_name(i) # get entry name from archive
+
+          # open entry
+          ar.fopen(i) do |f|
+            path = encode(f.name)
+            yield path
+
+            # content = f.read # read entry content
+          end
+        end
+      end
+    rescue ::Zip::Error => exc
+      puts "error: " + exc.message
+    end
+  end
+end