Created
July 26, 2011 04:31
-
-
Save nickel/1105980 to your computer and use it in GitHub Desktop.
Get all the Startup Quotes with this simple ruby script
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
#!/usr/bin/env ruby | |
require 'rubygems' | |
require 'nokogiri' | |
require 'open-uri' | |
url_base = "http://startupquote.com/page/" | |
images_path = File.expand_path(ARGV[0] || "~/Pictures/quotes") | |
if !File.exists?(images_path) | |
puts "Error, Image directory doesn't exist." | |
puts "Usage: ruby #{$0} image_directory_path" | |
exit | |
end | |
page = 1 | |
go = true | |
while(go) | |
document = Nokogiri::HTML(open("#{url_base}#{page.to_s}")) | |
# No more quotes? | |
go = false and break if (images = document.search("//div[@class='photoPost']").search("img")) == [] | |
for image in images | |
src = image.attributes["src"] | |
name = src.content.split("/").last | |
file_path = "#{images_path}/#{name}" | |
unless File.exists?(file_path) | |
File.open(file_path, 'w') do |output| | |
# Download image | |
open(src) do |input| | |
output << input.read | |
end | |
end | |
puts image.attributes["alt"].content.gsub("\n", " ") | |
else | |
# I have all the quotes | |
go = false and break | |
end | |
end | |
page += 1 | |
sleep 2 | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment