Skip to content

Instantly share code, notes, and snippets.

@gotmayonase
Last active August 29, 2015 14:04
Show Gist options
  • Save gotmayonase/3ae5c2709995cc93da04 to your computer and use it in GitHub Desktop.
Save gotmayonase/3ae5c2709995cc93da04 to your computer and use it in GitHub Desktop.
def process_url(url, download_directory, pages)
doc = Nokogiri::HTML(open(url))
doc.css('.thing.link').each do |entry|
id = entry['data-fullname']
link = entry.css('a.title')[0]
next unless link
title = link.content
if title =~ /(\d{3,5})\s?x\s?(\d{3,5})/
width, height = $1.to_i, $2.to_i
href = link["href"]
extension = File.extname(URI.parse(href).path)
if extension.nil? || extension.empty?
extension = ".jpg"
href = "#{href}.jpg"
end
if width >= 1920
filename = File.join(download_directory,"#{id}#{extension}")
unless File.exists? filename
puts "Downloading #{href} to #{filename}"
img = open(href).read rescue nil
if img
File.open(filename, 'wb') { |f| f.write(img) }
else
`touch #{filename}`
end
end
end
end
end
pages -= 1
if pages > 0
url = doc.css('.nav-buttons .nextprev a').last['href']
process_url(url, download_directory, pages)
end
end
require 'nokogiri'
require 'open-uri'
puts "Starting import @ #{Time.now}"
url = "http://www.reddit.com/r/earthporn+villageporn+cityporn+spaceporn+waterporn+abandonedporn+animalporn+humanporn+botanicalporn+adrenalineporn+destructionporn+movieposterporn+albumartporn+machineporn+newsporn+geekporn+bookporn+mapporn+adporn+designporn+roomporn+militaryporn+historyporn+quotesporn+skyporn+fireporn+infrastructureporn/top/?sort=top&t=all"
process_url(url, "/Users/mmayo/Dropbox/Pictures/Wallpapers/sfwporn/", 20)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment