Skip to content

Instantly share code, notes, and snippets.

@rterbush
Created May 3, 2013 21:52
Show Gist options
  • Save rterbush/5514497 to your computer and use it in GitHub Desktop.
Save rterbush/5514497 to your computer and use it in GitHub Desktop.
ftp = Net::FTP.new
ftp.passive = true
ftp.connect(host)
ftp.login(uname,passwd)
lmtime = File.mtime("#{home}/.rsrtimestamp")
File.write("#{home}/.rsrtimestamp", tstart)
CSV.foreach(fd, :col_sep => ';', :quote_char => '|', :encoding => "CP1252") do |row|
variant = Spree::Variant.find_by_sku(row[0].to_s)
unless variant.nil?
product = Spree::Product.find(variant.product_id)
u += 1
# attach images
imagename = row[14]
unless imagename.nil?
if imagename[0] =~ /[0-9]/
imgchar = '#'
else
imgchar = imagename[0].downcase
end
imgpattern = imagename.strip[0..-7]
ftp.chdir("/rsr_number/#{imgchar}")
ftp.nlst("#{imgpattern}_[0-9].*").each do |i|
rmtime = ftp.mtime("#{i}")
next if lmtime > rmtime
product.images.delete_all
img = Spree::Image.new({:attachment => StringIO.open(ftp.getbinaryfile("#{i}", nil)).binmode, :viewable => product.master}, :without_protection => true)
img.save!
puts "added image: #{i}".light_blue
product.images << img
end
product.save!
end
else
puts "#{row[2]} is a new product. Not updating image".cyan
n += 1
end
end
ftp.close
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment