Class: ImageDownloaderParallel

Inherits:
ImageDownloader show all
Defined in:
lib/gsv_downloader/image_downloader_parallel.rb

Overview

Google Street View Image Downloader multi_thread version (get the tiles in a multithread way)

Instance Method Summary collapse

Methods inherited from ImageDownloader

#combine_tiles, #crop_pano, #download, #download_tile, #get_nb_tiles, #get_tiles, #set_tmp_dir

Constructor Details

#initialize(tmp_path = "./tmp") ⇒ ImageDownloaderParallel

Returns a new instance of ImageDownloaderParallel.



8
9
10
# File 'lib/gsv_downloader/image_downloader_parallel.rb', line 8

def initialize(tmp_path = "./tmp")
	set_tmp_dir(tmp_path)
end

Instance Method Details

#download_tiles(panoID, zoom_level) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/gsv_downloader/image_downloader_parallel.rb', line 12

def download_tiles(panoID, zoom_level)

	# prepare the information for each tile
	data = []
	filenames = []
	get_tiles(zoom_level) do |x, y|
		data << {
			url: "http://cbk1.google.com/cbk?output=tile&zoom=#{zoom_level}&x=#{x}&y=#{y}&v=4&panoid=#{panoID}",
			filename: "#{@tmp_path}/tile-#{panoID}-#{x}-#{y}.jpg"
		}
	end

	# process
	hydra = Typhoeus::Hydra.new
	data.each do | datum|
		request = Typhoeus::Request.new(datum[:url])
		request.on_complete do |response|
			process_response(response, datum[:filename])
   	end
   	hydra.queue request
	end
	hydra.run
	data.collect{ |datum| datum[:filename]}
end

#process_response(response, filename) ⇒ Object



37
38
39
40
41
42
43
44
45
# File 'lib/gsv_downloader/image_downloader_parallel.rb', line 37

def process_response(response, filename)
	if response.success?
    open(filename, 'wb') do |file|
    	file.write(response.body)
  	end
  else
    raise Exception.new("tile #{filename} not downloaded")
  end
end