#!/usr/bin/ruby -w ## Jason Ronallo # April, 2007 require 'rubygems' require 'zoom' require 'marc' require 'stringio' require 'yaml' $KCODE = 'u' require 'jcode' require 'unicode' #--my modules require 'zcc/marcadditions' require 'zcc/pickers' class Array include Pickers end class Object #MARC::Record include MARCAdditions end class String def usubstr(a, b) i = 0 buff = '' each_char do | c | i += 1 if i >= a: buff += c end if i == b: return buff end end end end $testing = ARGV[0] $testing = nil unless $testing == 'v' or $testing == 't' puts "We're testing " if $testing STDIN.gets if $testing #reading in config from yaml file yamlfile = YAML.load_file( "#{File.expand_path("~")}/.zcc/zcc.yaml" ) zservers = yamlfile['zservers'] labels = yamlfile['labels'] scripting = yamlfile['scripting'] $procs = yamlfile['procs'] TO_SHOW = yamlfile['to_show'] HOW_TO_SAVE = yamlfile['save_filename'] TIMESTAMP = Time.now.strftime("%Y%m%d%H%M%S") SCRIPTING = yamlfile['scripting_on'] CSV = yamlfile['csv_on'] LINTER = yamlfile['linter_on'] FORMAT_TO_SAVE = yamlfile['save_record_syntax'] #class String # def to_proc # eval "Proc.new #{self} " # or "Proc.new #{self}" in my case # end #end def continue? loop { print "Enter to search again. q to quit: " enter_quit = STDIN.gets.chomp if enter_quit == '' puts "Again!\n" return "again" elsif enter_quit == 'q' puts "Please! No more!" exit end } end def zoomer (search_term, server, port, database ) puts "\n-------------------------------------\n#{server} #{port} #{database} \n" conn = ZOOM::Connection.new conn.connect(server, port) #do |conn| conn.set_option('charset', 'UTF-8') conn.preferred_record_syntax = 'MARC21' #STDIN.gets conn.database_name = database #conn.preferred_record_syntax = 'MARC21'#'USMARC' #was 'XML' #conn.set_option('charset', 'UTF-8') full_search = '' if search_term =~ /\d+[X||\d]/ and !(search_term.match( /[a-wyz]/i )) puts "Searching for ISBN: #{search_term}" if server == 'z3950.loc.gov' full_search = "@attr 1=1007 #{search_term}" else full_search = "@attr 1=7 #{search_term}" end elsif ( search_term[/[a-z]/i] ) #-- This check for a string could be better! puts "searching for title:#{search_term}" search_term = "\'#{search_term}\'" full_search = "@attr 1=4 \"#{search_term}\"" else puts "What is this search? Does not compute." exit end rset = conn.search(full_search) if ( rset.length == 0) return nil end rset_recs = rset[0,TO_SHOW] #puts rset_recs #STDIN.gets puts "rset.length #{rset.length}" #puts rset.get_option('charset') #STDIN.gets xmlsrecs = '' #--xmlsrecs = rset.records rset_recs.each do |rsetrec| #--puts "moving record into array" #puts rsetrec.syntax #puts rsetrec #STDIN.gets xmlsrecs += rsetrec.xml('MARC-8', 'UTF-8') #change this to detect character encoding before assuming marc8 to utf8 #puts xmlsrecs end #puts xmlsrecs return records = MARC::XMLReader.new(StringIO.new(string=xmlsrecs)) #end rescue Exception => except puts "Error! #{except}" return nil end #++# All the main logic of the program is just one simple big loop. puts "Zcc, Copyright (C) 2007 Jason Ronallo\nZcc comes with ABSOLUTELY NO WARRANTY.\nZcc is released under the terms of the GPL v.2 or later. See LICENSE for full information." loop { taken = [] print "Enter a title or ISBN to search for: " search_term = STDIN.gets.chomp zservers.each do |zserver| records = zoomer(search_term, zserver[0], zserver[1], zserver[2]) puts "\n" if records.nil?: print "nil records returned"; next; end recs_array = records.to_a taker = [] taker = recs_array.select_good_marc('multi') #--puts taker.inspect another_zserver = taker.pop if taker[-1] == 'done' next if taker == nil taker.each do |taker| puts "\ntaker: #{taker['245']}\n\n" taken << taker end break if another_zserver == 'done' end taken.flatten! taken.compact! puts "taken.length #{taken.length}" if taken.length > 1 puts "\n\n!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\nWe only need one record per book, so make your choice now!\n!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n\n" taken = taken.select_good_marc('one') next if taken == 'none' puts "after selecting final record taken.length = #{taken.length}" puts taken[0]['245'] , "\n\n" elsif taken.length == 0 print "You didn't get any records!\n" next if continue? == 'again' end final_taken = taken[0] puts final_taken final_taken.local_script(scripting) if SCRIPTING puts "--------------FINAL RECORD----------------------------" puts final_taken puts "--------------FINAL RECORD----------------------------\a" puts "Above is how your final record looks. Are you happy with it? (anything or n)" to_save_or_not_to_save = STDIN.gets.chomp if to_save_or_not_to_save == 'n' next if continue? == 'again' end csv = final_taken.marc_to_csv(labels) if CSV print "\n#{csv}\n" if HOW_TO_SAVE == 'search_term' filename = search_term.gsub(' ','_') elsif HOW_TO_SAVE == 'timestamp' filename = TIMESTAMP end #puts Dir.getwd #STDIN.gets #Dir.chdir("./saved") do if FORMAT_TO_SAVE == 'marc' aFile = File.new("#{filename}\.mrc","a+") aFile.write(final_taken.to_marc) puts "#{final_taken['245']['a']} saved in #{filename}\.mrc" aFile.close elsif FORMAT_TO_SAVE == 'marcxml' puts final_taken.to_xml aFile = File.new("#{filename}\.xml","a+") aFile.write(final_taken.to_xml) puts "#{final_taken['245']['a']} saved in #{filename}\.xml" aFile.close end if CSV csvFile = File.new("csv-#{filename}\.txt","a+") csvFile.write(csv) puts "csv for #{taken[0]['245']['a']} saved in csv-#{filename}\.txt" csvFile.close end #end next if continue? == 'again' }