Created
November 15, 2014 23:23
-
-
Save ender672/6101d69b56de8be49ccd to your computer and use it in GitHub Desktop.
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 '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 |
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 '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