Skip to content

Instantly share code, notes, and snippets.

@ender672
Created November 15, 2014 23:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ender672/6101d69b56de8be49ccd to your computer and use it in GitHub Desktop.
Save ender672/6101d69b56de8be49ccd to your computer and use it in GitHub Desktop.
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