Instantly share code, notes, and snippets.

Embed
What would you like to do?
downloads_ny_photos_step_1
# First quick draft
require 'open-uri'
require 'uri'
class PhotoDownloader
RESOURCE = 'http://access.nypl.org/image.php/'
def initialize(id, dimension)
@id = id
@width, @height = dimension
end
def save
0.upto(@height-1) do |height|
0.upto(@width-1) do |width|
download(form_url(height, width), "#{@id}_#{height}_#{width}.png")
end
end
end
private
def download(uri, filepath)
File.write(filepath, uri.read, {mode: 'wb'})
end
def form_url(x,y)
URI.join(RESOURCE, "#{@id}/", fetch_tile(x,y))
end
def fetch_tile(x = 0, y = 0)
"tiles/0/11/#{x}_#{y}.png"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment