require"open-uri"
require"kconv"
require"mechanize"


#v  = "http://w...content-available-to-author-only...o.jp/gp/product/images/4089081661/"
#v = "http://w...content-available-to-author-only...o.jp/gp/product/images/4757536356/ref=dp_image_z_0?ie=UTF8&n=465392&s=books"
v  = "http://w...content-available-to-author-only...o.jp/gp/product/images/4799202170/"
#v  =~ /images\/(\d*)\//
v  =~ /[product|images]\/(\d*)\//
vi = p $1

agent = Mechanize.new
agent.user_agent_alias = "Windows IE 7"

uri  = URI.parse( v )

agent.get(uri)

a=agent.page.body.each_line.each do |m|
  if m =~ /addZoomViewer/
    puts m
    break m
  end
end

a =~ /(\d*),"amztile"/
ai = p $1

files = []
b=6.times.map do | i |
  6.times.map do | k |
    s = "http://z...content-available-to-author-only...n.com/R/1/a=#{vi}+d=_SCR%283,#{i},#{k}%29_+o=01+s=RMTILE+va=MAIN+ve=#{ai}+e=.jpg"
    file = "_t_#{k}_#{i}.jpg"
    begin
      f = open(s,"rb").read
      open( file ,"wb").print  f
      files << file
    rescue
#      p "file_none_ #{ s }"
    end
  end
end.flatten.compact


#b.each do |m|
# m.chomp!
# puts "<img src=\"#{m}\">"
#end



def func ar , name , append
  ar.inject do | a , b |
    pt = "__pt_#{name}.jpg"
      if append == :+
        `"C:\\Program Files\\ImageMagick-6.5.6-Q8\\convert" +append  #{a}  #{b}  #{pt}`
      elsif append == :-
        `"C:\\Program Files\\ImageMagick-6.5.6-Q8\\convert" -append  #{a}  #{b}  #{pt}`
      else
         p :err
         exit
      end
    pt
  end # inject
end


p files.sort

sl = files.sort.map do |m|
 m =~ /(\d*).jpg/
 $1
end.max.to_i + 1

gg = files.sort.each_slice( sl ).each_with_index.map do |m , i|
  p m
  func m , i , :+
end
func gg , "_kuu_mazo____" , :-

[files+gg].flatten.each do |m|
  File.unlink m
end


