Created
July 1, 2011 20:31
-
-
Save betawaffle/1059332 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
def process(filename) | |
File.open filename, 'r' do |f| | |
ln = 0 | |
begin | |
while l = f.gets | |
l.strip! | |
ln += 0 | |
yield l, ln | |
end | |
rescue Interrupt | |
puts "\r\033[0;31mInterrupt\033[0m" | |
return | |
end | |
end | |
end | |
SRC = '/Volumes/Data/I Drive/IDS/24519' | |
DST = '/Volumes/Data/SyncIDS Testing' | |
FILES = Dir.glob("#{SRC}/*.pdf", File::FNM_CASEFOLD) | |
FOUND = {} | |
def find_pdf(cite, npl, auto = true) | |
name = nil | |
year = nil | |
if cite =~ %r{^ ([A-Z]+) .+ (\d{4}[abc]?) \)? \s* [.,]? $}ix | |
name = $1 | |
year = $2 | |
else | |
puts " \033[0;31mError\033[0m" | |
return nil | |
end | |
files = [] | |
other = [] | |
matched = FILES.select do |path| | |
file = File.basename(path) | |
next if FOUND.has_key? file | |
unless file =~ %r{^ #{name} }ix | |
next | |
end | |
if file =~ %r{ #{year} }ix | |
files.push file | |
else | |
other.push file | |
end | |
end | |
if files.length == 0 && other.length == 0 | |
short = name[0..-2] | |
unless short.length == 0 | |
return find_pdf(cite.gsub(/^#{name}/, short), npl, false) | |
end | |
puts " \033[0;31mNo Matches\033[0m" | |
return nil | |
end | |
if files.length == 1 && other.length == 0 | |
file = files.first | |
puts " \033[0;32mFound:\033[0m\t#{file}" | |
if auto == false | |
STDOUT.write('? ') | |
STDOUT.flush | |
y = STDIN.gets | |
if y !~ %r{^ (y|yes) }ix | |
return nil | |
end | |
end | |
FOUND[file] = npl | |
return file | |
end | |
if files.length == 0 | |
puts " \033[0;33mNo Exact Matches\033[0m" | |
end | |
matched.each_index do |i| | |
path = matched[i] | |
file = File.basename(path) | |
if file =~ %r{ #{year} }ix | |
puts "#{i + 1}\t\033[0;32m#{file}\033[0m" | |
else | |
puts "#{i + 1}\t#{file}" | |
end | |
end | |
STDOUT.write('> ') | |
STDOUT.flush | |
i = STDIN.gets.to_i - 1 | |
unless i < 0 | |
path = matched[i] | |
file = File.basename(path) | |
FOUND[file] = npl | |
return file | |
end | |
return nil | |
end | |
npls = {} | |
process 'renames.txt' do |l, ln| | |
if l =~ %r{ (.+?) \t (?:NPL)? (\d+)\.pdf }x | |
npls[$2] = $1 | |
FOUND[$1] = $2 | |
end | |
end | |
File.open 'renames.txt', 'w' do |renames| | |
npls.each do |npl, file| | |
renames.puts "#{file}\tNPL#{npl}.pdf" | |
end | |
process 'npl-list-numbered.txt' do |l, ln| | |
unless l =~ %r{^ (?:...)? \[ NPL (\d+) \] \s+ (.+) $}x | |
puts "\033[0;34m#{l}\033[0m" | |
next | |
end | |
next if npls.has_key? $1 | |
puts "\033[0;34m#{$2}\033[0m" | |
file = find_pdf $2, $1.to_i | |
renames.puts "#{file}\tNPL#{$1.to_i}.pdf" unless file.nil? | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment