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 Rake::Task["environment"].invoke
# scrape images # scrape images
::Post.all.each do |post| @posts = ::Post.all
@posts.each do |post|
doc = Nokogiri::HTML(post.body) doc = Nokogiri::HTML(post.body)
doc.css("img").each do |img| doc.css("img").each do |img|
# find remote file path # find remote file path
@ -57,34 +58,35 @@ namespace :wordpress do
# load uri # load uri
begin begin
remote_uri = URI(remote_file) remote_uri = URI(remote_file)
# only download if the image is a LFA-hosted image
if 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)
# create local folders if necessary
dirname = File.dirname(local_file)
unless File.directory?(dirname)
FileUtils.mkdir_p(dirname)
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}"
rescue OpenURI::HTTPError => error
puts "Error saving file #{remote_file}: #{error.message}"
end
end
end
rescue => error rescue => error
puts "Error loading #{remote_file}: #{error.message}" puts "Error parsing URL #{remote_file}: #{error.message}"
end end
# only download if the image is a LFA-hosted image
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 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)
FileUtils.mkdir_p(dirname)
end
# save remote file to local
begin
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
end end
puts "Finished downloding images from #{@posts.count} posts"
end end
# desc "Reset the cms relevant tables for a clean import" # desc "Reset the cms relevant tables for a clean import"