Skip to content

Instantly share code, notes, and snippets.

@rwjblue
Created August 18, 2012 01:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rwjblue/3383770 to your computer and use it in GitHub Desktop.
Save rwjblue/3383770 to your computer and use it in GitHub Desktop.
Copy missing receipt images back to PMNAS02
require 'csv'
require 'pry'
require 'mysql2'
require 'fileutils'
class TrackerCopy
PMNAS_PATH = '/media/pmnas02'
ORB_RUNNER_PROCESSED_PATH = '/home/public/processed'
def initialize(options)
@error_files = CSV.table(options.delete(:csv_file))
end
def connection
Mysql2::Client.new(:host => '192.168.100.203', :port => 3306, :database => "smarttcom", :username => "", :password => '')
end
def process_bad_files
@error_files.each do |row|
source_path = get_source(row[:image_path])
next if source_path.nil?
source_path = sanitize_source_path(source_path)
File.read(get_ini(source_path)) =~ /fullpath = ([\/\w]+\.pdf)[^\[]*#{File.basename(source_path,'.png')}\.png/m
pdf_path = ORB_RUNNER_PROCESSED_PATH + "/" + $1
pdf_path = pdf_path.gsub(/pending/i, 'Pending').gsub(/#{File.basename(pdf_path)}/i,File.basename(pdf_path).downcase)
final_dest = row[:image_path].gsub("\\","/").gsub("//PMNAS02/IMAGE", PMNAS_PATH).gsub(/pending/i, 'Pending').gsub(/#{File.basename(source_path)}/i,File.basename(source_path).downcase)
FileUtils.cp(pdf_path,final_dest)
puts final_dest
end
end
def get_ini(source_path)
File.dirname(source_path).gsub("compressed_images", "info.ini")
end
def get_source(image_path)
sql = """
SELECT DISTINCT source
FROM smarttcom.image_pages
WHERE UCASE(destination) = '#{connection.escape(image_path).upcase}'
"""
sql.gsub("\n",'')
results = connection.query(sql, :symbolize_keys => true)
row = results.first
row[:source] if row
end
def sanitize_source_path(source_path)
source_path.gsub("\\","/").gsub("//192.168.100.83/public/processed", ORB_RUNNER_PROCESSED_PATH)
end
end
if __FILE__ == $0
tc = TrackerCopy.new(csv_file:'missing_receipts.csv')
tc.process_bad_files
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment