Skip to content

Instantly share code, notes, and snippets.

@nemo
Last active December 10, 2015 02:58
Show Gist options
  • Save nemo/4371269 to your computer and use it in GitHub Desktop.
Save nemo/4371269 to your computer and use it in GitHub Desktop.
Check which BaseRecipes don't have an image
require 'ruby-progressbar'
base_recipes = BaseRecipe.where(:draft => false)
pbar = ProgressBar.create :total => base_recipes.count
url_but_not_image = []
no_image_and_no_url = []
def image_exists(url)
uri = URI.parse(url.to_s)
exists = false
Net::HTTP.start(uri.host, uri.port) do |http|
header = http.head(uri.request_uri)
exists = (header.code == "200" and (header.content_type =~ /image/) != nil)
end
exists
end
base_recipes.each do |b|
if b.image_url_exists then
if !image_exists(b.image) && b.image.present?
url_but_not_image << b.id
end
else
if b.image.present?
if !image_exists(b.image)
no_image_and_no_url << b.id
end
else
no_image_and_no_url << b.id
end
end
pbar.increment
end
pbar.finish()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment