Created
March 7, 2011 06:24
-
-
Save basicxman/858147 to your computer and use it in GitHub Desktop.
Don't ask.
This file contains hidden or 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 'sinatra' | |
require 'net/http' | |
require 'json/pure' | |
require 'RMagick' | |
require 'open-uri' | |
require 'cgi' | |
include Magick | |
set :public, File.dirname(__FILE__) + '/public' | |
set :environment, :development | |
before do | |
request.env['PATH_INFO'].gsub!(/\/$/, '') | |
end | |
get '/' do | |
<<-eof | |
<html> | |
<head> | |
<title>orlyherpyourderp</title> | |
</head> | |
<body> | |
<div style="width:100%;"><div style="width:500px; margin:0 auto;"><span style="font-size:18px; color:#222;">HERP YOUR </span><input style="font-size:18px;" id="derp" /></div></div> | |
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"></script> | |
<script type="text/javascript"> | |
$(document).ready(function() { | |
$("#derp").keyup(function(event) { | |
if (event.keyCode == '13') { | |
window.location = '/' + $(this).val(); | |
} | |
}); | |
}); | |
</script> | |
</body> | |
</html> | |
eof | |
end | |
get '/:derp' do |derp| | |
url = generate_image_api_url(derp) | |
contents = get_google_images(url) | |
image_url = extract_first_url(contents) | |
request_url = get_request_url(image_url) | |
sub_local_url = derp_to_file_name(derp) | |
local_url = generate_full_local_url(sub_local_url) | |
if not File.exists? local_url | |
Net::HTTP.start(image_url.host) do |http| | |
response = http.get(request_url) | |
open(local_url + ".manipulate", "wb") do |file| | |
file.write(response.body) | |
end | |
end | |
img = Magick::Image.read("JPG:" + local_url + ".manipulate").last | |
txt = Draw.new | |
txt.annotate(img, 0, 0, 0, 30, derp) do | |
self.gravity = Magick::NorthGravity | |
self.pointsize = 40 | |
self.stroke = '#000000' | |
self.fill = '#ffffff' | |
self.font_weight = Magick::BoldWeight | |
end | |
txt.annotate(img, get_width(contents), get_height(contents), 0, 100, generate_annotation_text(derp)) do | |
self.gravity = Magick::SouthGravity | |
self.pointsize = 40 | |
self.stroke = '#000000' | |
self.fill = '#ffffff' | |
self.font_weight = Magick::BoldWeight | |
end | |
img.write(local_url) | |
end | |
<<-eof | |
<title>Herp to the derp</title> | |
<img src="#{sub_local_url}" /> | |
eof | |
end | |
helpers do | |
def derp_to_file_name(derp) | |
temp = derp.gsub(/[^0-9A-Za-z\s]/, '') | |
temp.gsub!(/[\s]+/, '_') | |
temp + '.png' | |
end | |
def generate_image_api_url(query) | |
"http://ajax.googleapis.com/ajax/services/search/images?v=1.0&imgsz=large%7Cxlarge%7Cxxlarge%7Chuge&as_filetype=jpg&q=#{CGI.escape(query)}" | |
end | |
def get_google_images(url) | |
contents = Net::HTTP.get_response(URI.parse(url)).body | |
JSON.parse(contents, :symbolize_names => true) | |
end | |
def extract_first_url(hash) | |
URI.parse(hash[:responseData][:results].first[:url]) | |
end | |
def get_width(hash) | |
hash[:responseData][:results].first[:width].to_i | |
end | |
def get_height(hash) | |
hash[:responseData][:results].first[:height].to_i | |
end | |
def get_request_url(url) | |
index = url.to_s.index(url.host) | |
index += url.host.length | |
url.to_s[index..-1] | |
end | |
def generate_full_local_url(local_url) | |
File.join(File.dirname(__FILE__), '/public/', local_url) | |
end | |
def generate_annotation_text(derp) | |
if derp.index("mom").nil? | |
"you've been herped." | |
else | |
"she's been herped." | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment