Skip to content

Instantly share code, notes, and snippets.

@rabidcarrot
Created September 20, 2011 04:01
Show Gist options
  • Save rabidcarrot/1228298 to your computer and use it in GitHub Desktop.
Save rabidcarrot/1228298 to your computer and use it in GitHub Desktop.
moving files to folders for import based on model release mapping
require "rubygems"
require "fastercsv"
require "pp"
require "fileutils"
=begin
Command to be run in rails console post file moving and cleanup: ItemImport::Builder::GenericPhotoFtp.new('disorderly').import_all
=end
class ReleaseCSV
attr_accessor :promos
COLUMN_INDEX = {
:photo => 0,
:release => 1,
}
def initialize(import_path)
@file_move_data = {}
@release_count = {}
@photos = []
@duplicates = []
@photo_source_dir = import_path
@model_release_source_dir = File.join @photo_source_dir, 'model_releases'
end
def model_release_source_dir
@model_release_source_dir
end
def special_import_dir
'special_import'
end
def photo_dest_dir(index)
File.join special_import_dir, "shoot_#{index}"
end
def model_release_dest_dir(index)
File.join photo_dest_dir(index), "model_releases"
end
def mv_file(file, dest_dir)
puts "Moving #{file} to #{dest_dir}"
FileUtils.mv file, dest_dir
end
def cp_file(file, dest_dir)
puts "Copying #{file} to #{dest_dir}"
FileUtils.cp file, dest_dir
end
def add_jpg(file)
"#{file}.jpg" unless file =~ /\.jpg$/
end
def import_csv(filename)
FasterCSV.foreach(filename) do |row|
data = {}
COLUMN_INDEX.each { |key, index| data[key] = row[index] }
@file_move_data[data[:release]] = [] unless @file_move_data[data[:release]]
@file_move_data[data[:release]] << File.join(@photo_source_dir, add_jpg(data[:photo]))
@duplicates << data[:photo] if @photos.include? data[:photo]
@photos << data[:photo]
end
pp @file_move_data
pp @photos.count - @photos.uniq.count
pp @photos.count
pp @duplicates
self
end
def move_files
@file_move_data.each_with_index do |photo_data, index|
FileUtils.mkdir_p photo_dest_dir(index)
FileUtils.mkdir_p model_release_dest_dir(index)
cp_file File.join(model_release_source_dir, photo_data[0].strip), model_release_dest_dir(index)
photo_data[1].each do |photo|
mv_file photo, photo_dest_dir(index) unless @duplicates.include? photo
end
puts "Duplicate count: #{@photos.count - @photos.uniq.count}"
pp @duplicates
end
end
end
csv = ReleaseCSV.new('bulk_import/photos').import_csv("bulk_import/photos/model_releases/Envato-20110915.csv")
csv.move_files
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment