Skip to content

Instantly share code, notes, and snippets.

@ender672 ender672/fetch.rb
Created Nov 15, 2014

Embed
What would you like to do?
require 'google-search'
require 'uri'
require 'open-uri'
require 'fileutils'
FileUtils.mkdir("images")
Google::Search::Image.new(query: 'High Quality').each do |image|
name = File.basename(URI.parse(image.uri).path)
path = File.join("images", name)
puts name
begin
open(path, 'wb') do |file|
file << open(image.uri, read_timeout: 5).read
end
rescue
puts "failed."
FileUtils.rm(path)
end
end
require 'oil'
require 'sinatra'
set :server, 'puma'
get '/' do
Dir.glob('images/*.jpg').map do |i|
%Q{<p>#{i}</p><img src="#{i}?w=700&h=500" />}
end
end
get '/images/*.jpg' do
content_type :jpeg
io = File.new(".#{request.path_info}", 'rb')
cache_control :none
[200, Oil.new(io, params[:w].to_i, params[:h].to_i)]
end
helpers do
def img_url(path, width, height)
"/images/#{path}?w=#{width}&h=#{height}"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.