Last active
December 10, 2015 18:49
-
-
Save DrkCloudStrife/4477256 to your computer and use it in GitHub Desktop.
Finds missing images in .html files. It assumes that images are stored in the same directory and exports results to a file in /tmp directory.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
script_name = File.basename($0) | |
directory = ARGV[0] || './' | |
regEx = /.*\<[Ii][Mm][Gg].*[Ss][Rr][Cc]=["']([^'"]*)/ | |
files = Dir.glob("#{directory}/**/*.html") | |
broken_images = {} | |
broken_images_count = 0 | |
errors = [] | |
puts "Searching in #{directory}..." | |
files.each do |file| | |
path = file.split('/') | |
filename = path.pop | |
current_path = File.join(path) | |
content = File.open(file).read | |
broken_images[file] ||= [] | |
begin | |
puts "reading #{file}..." | |
content.scan(regEx) do |img_link| | |
image_path = File.join(current_path, img_link) | |
unless File.exists?(image_path) | |
broken_images[file] << image_path | |
broken_images_count += 1 | |
end | |
end | |
broken_images[file].uniq! | |
rescue Exception => e | |
message = "File: #{file}\n" | |
message += "#{e.message}\n" | |
message += e.backtrace.join("\n") | |
errors << message | |
end | |
end | |
puts "#{broken_images_count} images missing..." | |
unless broken_images_count == 0 | |
result_path = "/tmp/#{Time.now.to_i}_#{script_name}_results.txt" | |
File.open(result_path, 'w') do |file| | |
broken_images.each do |filename, images| | |
if images.count > 0 | |
file.write("#{filename}\n") | |
images.each{|image| file.write(" -#{image}\n")} | |
end | |
end | |
end | |
puts "Broken image results in #{result_path}..." | |
end | |
unless errors.empty? | |
error_path = "/tmp/#{Time.now.to_i}_#{script_name}_errors.log" | |
File.open(error_path, 'w') do |file| | |
errors.each do |error| | |
file.write("#{error}\n=====\n") | |
end | |
end | |
puts "Error log in #{error_path}..." | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment