Created
January 9, 2014 02:29
-
-
Save maliabadi/8328468 to your computer and use it in GitHub Desktop.
Want a bunch of harmless looking desktop backgrounds?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rubygems' | |
require 'mechanize' | |
class DesktopScraper | |
BASE = "http://simpledesktops.com" | |
def destination_folder | |
File.dirname(__FILE__) + "/desktops" | |
end | |
def initialize | |
@posts = [] | |
end | |
def agent | |
@agent ||= Mechanize.new | |
end | |
def get url | |
agent.get url | |
end | |
def doc | |
Nokogiri::HTML.parse(agent.page.body) | |
end | |
def run | |
# get the profile urls... | |
(1..36).to_a.each do |page| | |
get("#{BASE}/browse/#{page}") | |
links = doc.css('.desktops .edge .desktop a').to_a.map do |node| | |
node.attribute('href').value | |
end.select do |href| | |
href =~ /browse/ | |
end.map do |href| | |
BASE + href | |
end.uniq | |
@posts += links | |
@posts.uniq! | |
end | |
# go to each profile url | |
@posts.each_with_index do |url,index| | |
get url | |
get (BASE + doc.at_css('.desktop a').attribute('href').value) | |
agent.page.save "#{destination_folder}/#{agent.page.filename}" | |
end | |
end | |
end | |
DesktopScraper.new().run |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment