fork(1) download
  1. require 'net/http'
  2. multithreading = true
  3. Net::HTTP.start("2ch.hk", :use_ssl => true) do |http|
  4. thread = http.get("/b/res/133429168.html").body
  5. sources = []
  6. thread.scan(/<a class="desktop" target="_blank" href=".+">.+<\/a>/).each do |a|
  7. source = "/b#{/<a class="desktop" target="_blank" href="\.\.(.+)">.+<\/a>/.match(a).to_a[1]}"
  8. sources << source
  9. end
  10. i = 0
  11. start = Time.now
  12. if multithreading
  13. threads = []
  14. sources.each do |source|
  15. threads << Thread.new(i) { |j|
  16. file = http.get(source).body #breaks everything
  17. # type = /.+\.(.+)/.match(source)[1]
  18. # open("#{j}.#{type}","wb") { |new_file|
  19. # new_file.write(file)
  20. # }
  21. }
  22. i += 1
  23. end
  24. threads.each do |thr|
  25. thr.join
  26. end
  27. # until downloade=sources.size
  28. #
  29. # end
  30. else
  31. sources.each do |source|
  32. file = http.get(source).body
  33. type = /.+\.(.+)/.match(source)[1]
  34. open("#{i}.#{type}","wb") { |new_file|
  35. new_file.write(file)
  36. }
  37. i += 1
  38. print "#{(((i).to_f / sources.size) * 100).round(2)}% "
  39. end
  40. puts
  41. end
  42. puts "Done. #{i} files were downloaded. It took #{Time.now - start} seconds"
  43. end
  44.  
Runtime error #stdin #stdout #stderr 0.13s 15104KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
/usr/lib/ruby/2.1.0/net/http.rb:879:in `initialize': getaddrinfo: Name or service not known (SocketError)
	from /usr/lib/ruby/2.1.0/net/http.rb:879:in `open'
	from /usr/lib/ruby/2.1.0/net/http.rb:879:in `block in connect'
	from /usr/lib/ruby/2.1.0/timeout.rb:76:in `timeout'
	from /usr/lib/ruby/2.1.0/net/http.rb:878:in `connect'
	from /usr/lib/ruby/2.1.0/net/http.rb:863:in `do_start'
	from /usr/lib/ruby/2.1.0/net/http.rb:852:in `start'
	from /usr/lib/ruby/2.1.0/net/http.rb:583:in `start'
	from prog.rb:3:in `<main>'