Guest User

Untitled

a guest
Jan 4th, 2015
258
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 0.90 KB | None | 0 0
  1. require 'net/http'
  2. require 'open-uri'
  3. require 'fastimage'
  4. require 'json'
  5.  
  6. downloaded_urls = []
  7. while(true)
  8.   uri = URI('http://radikal.ru/Img/GetGalleryPage')
  9.   params = { AnchorId: "", PageSize: 40 }
  10.   uri.query = URI.encode_www_form(params)
  11.   begin
  12.     page = Net::HTTP.get(uri)
  13.   rescue Exception => e
  14.     next
  15.   end
  16.   JSON.parse(page)["Imgs"].map { |e| e["Url"] }.each do |url|
  17.     next if downloaded_urls.include?(url)
  18.     image_path = "images/#{Time.now.to_i}_#{File.basename(url)}"
  19.     size = FastImage.size(url)
  20.     next if size.nil? || (size.first < 350 && size.last < 350)
  21.     File.open(image_path, 'wb') do |output|
  22.       puts "Загружаю #{url}..."
  23.       begin
  24.         output.write(open(url, "rb").read)
  25.         downloaded_urls << url
  26.       rescue Exception => e
  27.         puts "[!] Не удалось загрузить #{url}"
  28.       end
  29.     end
  30.   end
  31.   sleep(10.0)
  32. end
Advertisement
Add Comment
Please, Sign In to add comment