class StaticImageDownloader::Images
Constants
- DOWNLOAD_OPTIONS
- EMPTY_FILE_NAME
- IMAGE_EXT
- MAX_FILE_NAME_LENGTH
Attributes
absolute_src[RW]
file_base_name[RW]
file_path_name[RW]
full_path_name[RW]
page_host[RW]
src[RW]
Public Class Methods
default_download_option()
click to toggle source
# File lib/static_image_download/images.rb, line 44 def default_download_option @@DEFAULTDONWLOADOPTION end
default_path()
click to toggle source
# File lib/static_image_download/images.rb, line 48 def default_path @@DEFAULTPATH end
default_timeout()
click to toggle source
# File lib/static_image_download/images.rb, line 52 def default_timeout @@DEFAULTTIMEOUT end
get_successfull_pictures_number()
click to toggle source
# File lib/static_image_download/images.rb, line 56 def get_successfull_pictures_number @@SUCCESSFULLPICTURES.to_s end
new(src, file_path_name=@@DEFAULTPATH, download_option=@@DEFAULTDONWLOADOPTION, page_host="")
click to toggle source
# File lib/static_image_download/images.rb, line 24 def initialize(src, file_path_name=@@DEFAULTPATH, download_option=@@DEFAULTDONWLOADOPTION, page_host="") @src = src @page_host = page_host # Reserved for future @download_option = download_option.nil? ? @@DEFAULTDONWLOADOPTION : download_option @file_path_name = file_path_name.nil? ? @@DEFAULTPATH : file_path_name.gsub(/\/+$/,'') file_base_name = @src.sub(/.*\//,'') file_base_name = EMPTY_FILE_NAME + rand(1000).to_s if !file_base_name || file_base_name.empty? if file_base_name.size > MAX_FILE_NAME_LENGTH file_base_name = file_base_name[-MAX_FILE_NAME_LENGTH..file_base_name.size] end @file_base_name = file_base_name @file_full_name = File.expand_path(File.join(@file_path_name, @file_base_name)) @full_path_name = File.expand_path(File.join(@file_path_name)) Dir::mkdir(@full_path_name) unless FileTest.directory?(@full_path_name) end
Private Class Methods
inc_successfull_pictures_number()
click to toggle source
# File lib/static_image_download/images.rb, line 61 def inc_successfull_pictures_number @@SUCCESSFULLPICTURES += 1 end
Public Instance Methods
download(download_option=@@DEFAULTDONWLOADOPTION, timeout=@@DEFAULTTIMEOUT, h={:dup_file_names => true})
click to toggle source
# File lib/static_image_download/images.rb, line 66 def download(download_option=@@DEFAULTDONWLOADOPTION, timeout=@@DEFAULTTIMEOUT, h={:dup_file_names => true}) #p "download_option=#{download_option}" begin response = nil status = Timeout::timeout(timeout) { h[:start_time] = Time.now response = method_to_value(download_option, h) } rescue => error p "#{error}" nil end end
method_to_value(option, h={})
click to toggle source
# File lib/static_image_download/images.rb, line 84 def method_to_value(option, h={}) #p "option= #{option}" method = option_to_method(option) p "method= #{method}" if $debug_option begin response = send(method, h) || "" @@SUCCESSFULLPICTURES += 1 if response[:path] return response rescue => error p "method_to_value.error = #{error}" nil end end
option_to_method(option)
click to toggle source
# File lib/static_image_download/images.rb, line 80 def option_to_method(option) opt = DOWNLOAD_OPTIONS[option] end
Private Instance Methods
check_file_name(src, h={})
click to toggle source
# File lib/static_image_download/images.rb, line 108 def check_file_name(src, h={}) result = {} response = {} file_full_name = @file_full_name fname_counter = 1 if File.exist?(file_full_name) and !h[:dup_file_names] response[:error] = "Error downloading. File #{file_full_name} already exists" p response[:error] p " src= #{src}" if $debug_option result[:response] = response #return result else while File.exist?(file_full_name) fname_counter += 1; file_full_name = File.dirname(@file_full_name) + '/' + File.basename(@file_full_name, '.*') + '_' + fname_counter.to_s + File.extname(@file_full_name) end result[:file_full_name] = file_full_name #File.new(file_full_name, "wb").close end return result end
curb_simple(h={})
click to toggle source
# File lib/static_image_download/images.rb, line 130 def curb_simple(h={}) response = {} src = @src result = check_file_name(src, h) response = result[:response] if result[:response] return response if response[:error] file_full_name = result[:file_full_name] begin curl = Curl::Easy.download(src, file_full_name) rcode = curl.response_code.to_s #p "response_code=" + rcode if $debug_option unless @@HTTPONSUCCESS =~ rcode File.delete(file_full_name) if File.exist?(file_full_name) end print_download_log(rcode, file_full_name, h) rpath = file_full_name if File.exist?(file_full_name) rescue => error response[:error] = error.message File.delete(file_full_name) if File.exist?(file_full_name) end response[:response_code] = rcode response[:path] = rpath return response end
http_get(h={})
click to toggle source
# File lib/static_image_download/images.rb, line 157 def http_get(h={}) response = {} src = @src result = check_file_name(src, h) response = result[:response] if result[:response] return response if response[:error] file_full_name = result[:file_full_name] begin answer = Net::HTTP.get_response(URI.parse(src)) rcode = answer.code if @@HTTPONSUCCESS =~ rcode open(file_full_name, "wb") { |file| file.write(answer.body) } end #p "response_code=" + answer.code if $debug_option print_download_log(rcode, file_full_name, h) rpath = file_full_name if File.exist?(file_full_name) rescue => error response[:error] = error.message File.delete(file_full_name) if File.exist?(file_full_name) end response[:response_code] = rcode response[:path] = rpath return response end
print_download_log(rcode, file_full_name, h={})
click to toggle source
# File lib/static_image_download/images.rb, line 100 def print_download_log(rcode, file_full_name, h={}) if @@HTTPONSUCCESS !~ rcode p "Error: html_res_code=" + rcode + " " + (Time.now - h[:start_time]).to_s + " sec. for #{File.basename(file_full_name)} File could not be saved!" else p "html_res_code=" + rcode + " " + (Time.now - h[:start_time]).to_s + " sec. for #{File.basename(file_full_name)}" if $debug_option end end