Created
November 9, 2013 23:09
-
-
Save Timopheym/7391266 to your computer and use it in GitHub Desktop.
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
namespace :spree do | |
desc "Export Products to CSV File" | |
task :export_products_csv => :environment do | |
require 'fastercsv' | |
products = Product.find(:all) | |
puts "Exporting to #{RAILS_ROOT}/products.csv" | |
FasterCSV.open("#{RAILS_ROOT}/products.csv", "w") do |csv| | |
csv << ["id", "name", "description","sku", "master_price" ] | |
products.each do |p| | |
csv << [p.id, | |
p.name.titleize, | |
p.description, | |
p.sku, | |
p.master_price.to_s] | |
end | |
end | |
puts "Export Complete" | |
end | |
desc "Update / Import products from CSV File, expects file=/path/to/import.csv" | |
task :import_products_csv => :environment do | |
require 'fastercsv' | |
n = 0 | |
u = 0 | |
FasterCSV.foreach(ENV['file']) do |row| | |
if row[0].nil? | |
# Adding new product | |
puts "Adding new product: #{row[1]}" | |
product = Product.new() | |
n += 1 | |
else | |
# Updating existing product | |
next if row[0].downcase == "id" #skip header row | |
puts "Updating product: #{row[1]}" | |
product = Product.find(row[0]) | |
u += 1 | |
end | |
product.name = row[1] | |
product.description = row[2] | |
product.sku = row[3].to_s | |
product.master_price = row[4].to_d | |
product.save! | |
end | |
puts "" | |
puts "Import Completed - Added: #{n} | Updated #{u} Products" | |
end | |
desc "Test SFTP Connection" | |
task :connect => :environment do | |
require 'net/ssh' | |
require 'net/sftp' | |
Net::SFTP.start('89.31.90.118', 'Picture', :password => 'SFoNBv') do |sftp| | |
#1) Взять предпоследний файл и скачать | |
#2) Запустить его в импортере | |
#3) Запустить в кроне | |
#sftp.dir.sort! { |a,b| a.longname.downcase <=> b.name.downcase } | |
dirs = [] | |
sftp.dir.foreach("/") do |entry| | |
dirs.push(entry.name) | |
end | |
sftp.download!("/"+dirs[dirs.length-2], "last_import_file.xml") | |
f = File.open("last_import_file.xml") | |
doc = Nokogiri::XML(f) | |
doc.css("goods").each do |p| | |
#puts p.id | |
end | |
sftp.close! | |
end | |
end | |
desc "Update / Import products from XML File, expects file=/path/to/import.xml" | |
task :import_products_xml => :environment do | |
require 'nokogiri' | |
require 'open-uri' | |
require 'net/ssh' | |
require 'net/sftp' | |
puts '** Connecting' | |
Net::SFTP.start('89.31.90.118', 'Picture', :password => 'SFoNBv') do |sftp| | |
#1) Взять предпоследний файл и скачать | |
dirs = [] | |
puts '** Get last file name' | |
sftp.dir.foreach("/") do |entry| | |
dirs.push(entry.name) | |
end | |
puts '** Download last file name' | |
sftp.download!("/"+dirs[dirs.length-2], "last_import_file.xml") | |
#2) Запустить его в импортере | |
#f = File.open("Export 2013-01-31 05-00.xml") | |
puts '** Starting import' | |
f = File.open("last_import_file.xml") | |
doc = Nokogiri::XML(f) | |
n = 0 | |
u = 0 | |
doc.css("goods").each do |p| | |
#not used fields!!! | |
#puts p.css('code2').text | |
#puts p.css('count').text | |
if !Spree::Product.exists?(p.css('id').text.to_i) | |
# Adding new product | |
puts "Adding new product: #{p.css('id').text.to_i}" | |
product = Spree::Product.new() | |
#Adding image | |
id_length = p.css('id').text.length | |
nulls_count = 5 - id_length | |
null = '' | |
nulls_count.times do | |
null += '0' | |
end | |
uri = URI.parse('http://s3.amazonaws.com/toynet/toys_images/'+null+p.css('id').text+'.jpg') | |
res = Net::HTTP.get_response(uri) | |
if res.code.to_i != 403 | |
puts 'http://s3.amazonaws.com/toynet/toys_images/'+null+p.css('id').text+'.jpg' | |
i = Spree::Image.create!({:attachment => open(URI.parse("http://s3.amazonaws.com/toynet/toys_images/"+null+p.css('id').text+".jpg"))}) | |
product.images << i | |
end | |
n += 1 | |
else | |
# Updating existing product | |
puts "Updating product: #{p.css('id').text.to_i}" | |
product = Spree::Product.find(p.css('id').text.to_i) | |
u += 1 | |
end | |
product.id = p.css('id').text.to_i | |
product.name = p.css('name').text | |
product.description = p.css('count_in_box').text | |
product.sku = p.css('code1').text.to_s | |
product.price = p.css('price').text.to_d | |
product.available_on = '2013/02/14' | |
product.save! | |
end | |
puts "" | |
puts "Import Completed - Added: #{n} | Updated #{u} Products" | |
f.close | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment