require 'net/http'
multithreading = true
Net::HTTP .start ( "2ch.hk" , :use_ssl => true ) do | http|
thread = http.get ( "/b/res/133429168.html" ) .body
sources = [ ]
thread.scan ( /< a class ="desktop" target="_blank" href=".+" > .+< \/ a>/ ) .each do | a|
source = "/b#{/<a class=" desktop" target=" _blank" href=" \.\.( .+ ) ">.+<\/ a>/.match(a).to_a[1]}"
sources << source
end
i = 0
start = Time .now
if multithreading
threads = [ ]
sources.each do | source|
threads << Thread .new ( i) { | j|
file = http.get ( source) .body #breaks everything
# type = /.+\.(.+)/.match(source)[1]
# open("#{j}.#{type}","wb") { |new_file|
# new_file.write(file)
# }
}
i + = 1
end
threads.each do | thr|
thr.join
end
# until downloade=sources.size
#
# end
else
sources.each do | source|
file = http.get ( source) .body
type = / .+ \.( .+ ) / .match ( source) [ 1 ]
open ( "#{i}.#{type}" ,"wb" ) { | new_file|
new_file.write ( file)
}
i + = 1
print "#{(((i).to_f / sources.size) * 100).round(2)}% "
end
puts
end
puts "Done. #{i} files were downloaded. It took #{Time.now - start} seconds"
end
cmVxdWlyZSAnbmV0L2h0dHAnCm11bHRpdGhyZWFkaW5nID0gdHJ1ZQpOZXQ6OkhUVFAuc3RhcnQoIjJjaC5oayIsIDp1c2Vfc3NsID0+IHRydWUpIGRvIHxodHRwfAogIHRocmVhZCA9IGh0dHAuZ2V0KCIvYi9yZXMvMTMzNDI5MTY4Lmh0bWwiKS5ib2R5CiAgc291cmNlcyA9IFtdCiAgdGhyZWFkLnNjYW4oLzxhIGNsYXNzPSJkZXNrdG9wIiB0YXJnZXQ9Il9ibGFuayIgaHJlZj0iLisiPi4rPFwvYT4vKS5lYWNoIGRvIHxhfAogICAgc291cmNlID0gIi9iI3svPGEgY2xhc3M9ImRlc2t0b3AiIHRhcmdldD0iX2JsYW5rIiBocmVmPSJcLlwuKC4rKSI+Lis8XC9hPi8ubWF0Y2goYSkudG9fYVsxXX0iCiAgICBzb3VyY2VzIDw8IHNvdXJjZQogIGVuZAogIGkgPSAw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