Tweaking error handling and reporting

This commit is contained in:
Will Bradley 2014-03-12 15:14:14 -07:00
parent 3807af7ff3
commit 10df4f5494

View File

@ -49,7 +49,8 @@ namespace :wordpress do
Rake::Task["environment"].invoke
# scrape images
::Post.all.each do |post|
@posts = ::Post.all
@posts.each do |post|
doc = Nokogiri::HTML(post.body)
doc.css("img").each do |img|
# find remote file path
@ -57,13 +58,16 @@ namespace :wordpress do
# load uri
begin
remote_uri = URI(remote_file)
rescue => error
puts "Error parsing URL #{remote_file}: #{error.message}"
end
# only download if the image is a LFA-hosted image
if remote_uri.host.match(params[:host_match]) != nil
if remote_uri && remote_uri.host.match(params[:host_match]) != nil
# find a local path for it
local_file = File.expand_path(File.join(Rails.public_path,remote_uri.path))
# only download if not already there
unless File.exists?(local_file)
# only download if not already there or if it's zero bytes
unless File.size?(local_file)
# create local folders if necessary
dirname = File.dirname(local_file)
unless File.directory?(dirname)
@ -71,20 +75,18 @@ namespace :wordpress do
end
# save remote file to local
begin
File.open(local_file,'wb'){ |f| f.write(open(remote_file).read) }
puts "Saved file #{remote_file}: #{local_file}"
remote_file_io = open(remote_file)
File.open(local_file,'wb'){ |f| f.write(remote_file_io.read) }
puts "Saved file: #{File.basename(local_file)}"
rescue OpenURI::HTTPError => error
puts "Error saving file #{remote_file}: #{error.message}"
end
end
end
end
end
puts "Finished downloding images from #{@posts.count} posts"
rescue => error
puts "Error loading #{remote_file}: #{error.message}"
end
end
end
end
# desc "Reset the cms relevant tables for a clean import"