OSDN Git Service

refactored
authornomeu <nomeu@nomeu.org>
Sat, 12 Jun 2010 18:13:28 +0000 (03:13 +0900)
committernomeu <nomeu@nomeu.org>
Sat, 12 Jun 2010 18:13:28 +0000 (03:13 +0900)
bin/read-upl.rb

index c33370e..6f36dd1 100644 (file)
@@ -2,25 +2,31 @@
 # vim: fileencoding=utf-8
 require 'nkf'
 
-def encode(str)
-  str && NKF.nkf('-Sw', str)
-end
+class Scrap
+  def match(line)
+    @md = @row_re.match(line)
+  end
+
+  def row
+    raise
+  end
 
-def encode_to_sjis(str)
-  str && NKF.nkf('-Ws', str)
+  def encode(str)
+    str && NKF.nkf('-Sw', str)
+  end
+
+  def encode_to_sjis(str)
+    str && NKF.nkf('-Ws', str)
+  end
 end
 
-class Scrap_mmd
+class Scrap_mmd < Scrap
   def initialize
     @row_re = %r(</td><td><a href="(.+?)">(.+?)</a></td><td>(.+?)</td><td>(.+?)</td><td>(.+?)</td><td>(.+?)</td><td>(.+?)</td></tr>)
     @key_re = Regexp.new(Regexp.escape("<font color=\"#FF0000\">[DLKey] </font>"))
     @password_re = Regexp.new(Regexp.escape("<font color=\"#FF0000\">*</font>"))
   end
 
-  def match(line)
-    @md = @row_re.match(line)
-  end
-
   def row
     _, href, name, comment, size, date, mime, orig = @md.to_a
     comment.sub!(@key_re, '')
@@ -29,16 +35,12 @@ class Scrap_mmd
   end
 end
 
-class Scrap_mmdfile
+class Scrap_mmdfile < Scrap
   def initialize
     @row_re = %r(<td>\[<a href="(.+?)" >(.+?)</a>\]</td><td>(.+?) <a href="(?:.+?)">(?:.+?)</a></td><td class="size">(.+?)</td><td class="date">(.+?)</td><td class="orig"><span class="orig">(.+?)</span></td>)
     @key_re = Regexp.new(Regexp.escape("<strong class=\"key\">[P]</strong> "))
   end
 
-  def match(line)
-    @md = @row_re.match(line)
-  end
-
   def row
     _, href, name, comment, size, date, orig = @md.to_a
     comment.sub!(@key_re, '')
@@ -46,31 +48,23 @@ class Scrap_mmdfile
   end
 end
 
-class Scrap_mmdacc1
+class Scrap_mmdacc1 < Scrap
   def initialize
     @row_re = %r(<td align=center>\[<a href='(.+?)' target='_new'>(.+?)</a>\]</td><td>(.+?)</td><td align=right>(.+?)</td><td align=center>(.+?)</td><td>(.+))
   end
 
-  def match(line)
-    @md = @row_re.match(line)
-  end
-
   def row
     _, href, name, comment, size, date, orig = @md.to_a
     [ name, encode(comment), size, date, encode(orig) ]
   end
 end
 
-class Scrap_mmdacc2
+class Scrap_mmdacc2 < Scrap
   def initialize
     @row_re = %r(</td><td><a href="(.+?)">(.+?)</a></td><td>(.+?)</td><td>(.+?)</td><td>(.+?)</td><td class="down_max">(.+?)</td></tr>)
     @key_re = Regexp.new(Regexp.escape(encode_to_sjis("<font color=\"#FF0000\">[パス設定] </font>")))
   end
 
-  def match(line)
-    @md = @row_re.match(line)
-  end
-
   def row
     _, href, name, comment, size, date = @md.to_a
     comment.sub!(@key_re, '')
@@ -95,8 +89,7 @@ ent = "/Volumes/uploader/src/#{location}/index.html"
 open(ent) do |f|
   while line = f.gets
     if scrap.match(line)
-      name, comment, size, date, orig = scrap.row
-      puts [ name, comment, size, date, orig ].join("\t")
+      puts scrap.row.join("\t")
     end
   end
 end