Last active
May 9, 2017 00:43
-
-
Save ismasan/0cceda2dab01d0eab3016360b6fb10c6 to your computer and use it in GitHub Desktop.
Backup all product images in a Bootic shop
This file contains 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
# gem install thread | |
# | |
# run with: | |
# SUBDOMAIN=foobar btc runner image_backup.rb | |
# | |
require 'open-uri' | |
require 'fileutils' | |
require "thread/pool" | |
require "logger" | |
subdomain = ENV.fetch("SUBDOMAIN") | |
concurrency = ENV.fetch("CONCURRENCY", 10).to_i | |
# Busca la tienda por su subdominio | |
s = root.all_shops(subdomains: subdomain).first | |
raise "no shop for subdomain #{subdomain}" unless s | |
puts "downloading images for shop #{subdomain}, #{concurrency} workers" | |
# Crea un directorio con el nombre del subdominio | |
# para guardar las imágenes | |
dir = "./#{s.subdomain}" | |
FileUtils.mkdir_p dir | |
now = Time.now | |
pool = Thread.pool(concurrency) | |
logger = Logger.new("./errors.log") | |
s.products(status: "all").full_set.each do |pr| | |
# navega hasta el detalle de cada producto | |
# para tener acceso a todas las imágenes | |
pr = pr.self | |
# Itera las imágenes del producto y descarga cada una | |
# descarga hasta 10 imágenes concurrentemente | |
if pr.has?(:images) | |
pr.images.each do |img| | |
pool.process { | |
begin | |
path = File.join(dir, img.file_name) | |
if File.exists?(path) | |
puts "exists: #{path}" | |
else | |
File.open(path, "wb") do |f| | |
# descarga los datos de cada imagen desde su URL | |
img_data = open(img.rels[:original].href) | |
# escribe datos de imagen a archivo local | |
f.write img_data.read | |
end | |
puts path | |
end | |
rescue StandardError => e | |
logger.error "#{pr.slug} (#{img.file_name}): #{e.class.name} #{e.message}" | |
end | |
} | |
end | |
end | |
end | |
pool.shutdown | |
puts "done in #{Time.now - now}" | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment