You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
box-o-sand/rails/map-mash/lib/google_map_location_fetcher.rb

62 lines
1.4 KiB

require 'base64'
require 'logger'
require 'uri'
require 'nokogiri'
require 'typhoeus'
class GoogleMapLocationFetcher
attr_accessor :base_map_url, :log
def initialize
@base_map_url = [
'http://maps.googleapis.com/maps/api/staticmap',
'?zoom=12',
'&sensor=false',
'&size=512x512',
'&maptype=satellite',
].join('')
@log = Logger.new(
File.expand_path('../log/map-crawler.log', File.dirname(__FILE__))
)
@log.level = Logger::INFO
@log.formatter = lambda do |severity, time, prog, message|
"#{time} - #{severity} - #{message}\n"
end
end
def self.mapdump_callback(location, image)
puts "Map '#{location}':"
puts Base64.encode64(image)
end
def fetch(locations, &callback)
callback ||= self.class.method(:mapdump_callback)
hydra = Typhoeus::Hydra.new(:initial_pool_size => 26)
locations.each do |location|
request = Typhoeus::Request.new(
"#{@base_map_url}&center=#{URI.encode(location)}"
)
request.on_complete do |response|
handle_response(response, location, &callback)
end
hydra.queue(request)
end
hydra.run
end
def handle_response(response, location, &callback)
@log.info("Handling request at url #{response.effective_url}")
if response.success? and response.headers_hash[:content_type] =~ /image\/.*/
callback.call(location, response.body)
else
callback.call(location, '')
end
end
end