LunaeStellsr

Ruby Image Downloader Example

Nov 20th, 2019
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 0.88 KB | None | 0 0
  1. if Gem.win_platform?
  2.   module Process
  3.     RLIMIT_NOFILE = 7
  4.     def self.getrlimit(*args)
  5.       [1024]
  6.     end
  7.   end
  8. end
  9.  
  10. require 'mechanize'
  11.  
  12. $agent = Mechanize.new
  13.  
  14. SaveFolder = "downloads"
  15. Dir.mkdir SaveFolder unless File.exist? SaveFolder
  16.  
  17. def save_image(link, filename)
  18.   path = "#{SaveFolder}/#{filename}"
  19.   puts "Saving #{link} to #{path}"
  20.   $agent.get(link).save(path)
  21. end
  22.  
  23. doc = $agent.get 'https://www.deviantart.com/'
  24. doc.links.each do |link|
  25.   link = link.uri.to_s
  26.   next if link[-1] == 's'
  27.   next unless link.include?("/art/")
  28.   puts link
  29.   image_page = $agent.get(link)
  30.   title = image_page.search("._2p6cd").first.inner_text rescue nil
  31.   if title.nil?
  32.     puts "No title found in #{link}, skipping"
  33.     next
  34.   end
  35.   title.tr!(":\\\/?!*", '')
  36.   img_link = image_page.search("._1izoQ").first.get_attribute("src")
  37.   save_image(img_link, "#{title}.png")
  38. end
Add Comment
Please, Sign In to add comment