lib/img_dl/parser.rb in img_dl-0.0.3 vs lib/img_dl/parser.rb in img_dl-0.0.4
- old
+ new
@@ -39,10 +39,12 @@
end
def start
Thread.start{parse}
download
+ rescue StandardError => e
+ p e
end
def parse
loop do
break unless next_parse?
@@ -56,10 +58,13 @@
rescue StandardError => e
@error_urls << [url,e]
puts e
next
end
+ unless page.respond_to? :images
+ redo
+ end
parse_images page
if continue?
parse_links page
end
end
@@ -74,12 +79,13 @@
def download
@dl_status = "start"
@_download_image = 0
EM.run do
loop do
- if !running? && (@images.empty? || (image_limit_count && @_download_image >= image_limit_count))
+ if !running? && (@images.empty? || (image_limit_count? && @_download_image >= image_limit_count))
@dl_status = "all done"
+ download_complete? and EM.stop
break
end
if @images.empty?
if running?
@dl_status = "wait parser"
@@ -119,11 +125,11 @@
@downloading = false
end
protected
def download_complete?
- !running? && (@images.empty? || (@downloaded_image_count >= @_download_image))
+ !running? && (@downloaded_image_count >= @_download_image || (image_limit_count? and @downloaded_image_count >= image_limit_count))
end
def random_file_name
SecureRandom.uuid
end
@@ -182,11 +188,11 @@
links.each{|link| enq_urls link}
end
def parse_images page
@status = "parse images"
- images = page.images.map{|img| img.src.present? and URI.join @current_url,URI.escape(img.src)}
- images.select!{|img| img.to_s =~ img_reg} if image_reg?
+ images = page.images.map{|img| img.src.present? && URI.join(@current_url,URI.escape(img.src))}
+ images.select!{|img| img.to_s =~ image_reg} if image_reg?
images.each{|img| enq_images img}
end
def continue?
recursive? && (image_limit_count? ? @image_count < image_limit_count : true) && (url_limit_count? ? @url_count < url_limit_count : true)