bin/zcc in zcc-0.0.3 vs bin/zcc in zcc-0.1.0

- old
+ new

@@ -1,202 +1,228 @@ -#!/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' -require 'zcc/subfieldeditor' -require 'zcc/zoomer' -#require 'wcid' -require 'highline/import' -#require 'curses' - -termsize = HighLine::SystemExtensions.terminal_size -$term_width = termsize[0] -$term_height = termsize[1] -$clear_code = %x{clear} - - -ft = HighLine::ColorScheme.new do |cs| - cs[:headline] = [ :bold, :white, :on_black ] - cs[:horizontal_line] = [ :bold, :white, :on_blue] - cs[:even_row] = [ :green ] - cs[:odd_row] = [ :magenta ] - cs[:marc_tag] = [:bold] - cs[:bolder] = [:bold] - cs[:field_hilite] = [:bold, :blue] - cs[:index] = [:bold, :red] - end -HighLine.color_scheme = ft - - -##Setting up testing parameter -$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'] -EDITING = yamlfile['subfield_editing'] -CSV = yamlfile['csv_on'] -LINTER = yamlfile['linter_on'] -FORMAT_TO_SAVE = yamlfile['save_record_syntax'] -#ZEBRA_ON = yamlfile['zebra'] -#ZEBRA_CONF = yamlfile['zebraconf'] - -print $clear_code -#printed once when starting the program. -say("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.\nSee LICENSE for full information.\n") -say("Introductory help. For now see: http://zcc.rubyforge.org/zcc.html") - -#++# All the main logic of the program is just one simple big loop. -# Copy cataloging forever.... -clear_code_do = false -loop { - taken = [] - print $clear_code if clear_code_do - clear_code_do = true - search_term = ask("\n<%= color('Enter search. [ISBN, title, LCCN (with dash), or :q to quit]', :bolder) %>\nsearch> ", String){ |q| q.readline = true } - exit if search_term == ':q' - - zservers.each do |zserver| - records = ZCC.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.zcc_select_good_marc('multi') - 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 - say("\a<%= color('#{'!' * 10}We only need one record per book, so make your choice now!#{'!' * 10}', :headline) %>") - sleep 2 - taken = taken.zcc_select_good_marc('one') - if taken == 'none' - next - print $clear_code - end - puts "after selecting final record taken.length = #{taken.length}" - puts taken[0]['245'] , "\n\n" - elsif taken.length == 0 - print $clear_code - print "You didn't get any records!\n" - clear_code_do = false - next #if continue? == 'again' - end - - final_taken = taken[0] - #puts final_taken - - #SCRIPTING and EDITING - if SCRIPTING - print $clear_code - print "\a" - say("#{ZCC.zcc_marc_str_bold(final_taken.to_s, 'record')}") - say("<%= color('SCRIPTING...', :headline) %>") - final_taken.local_script(scripting) - end - if EDITING - print $clear_code - print "\a" - say("#{ZCC.zcc_marc_str_bold(final_taken.to_s, 'record')}") - say("Would you like to <%= color('edit', :headline) %> this record [yes or ANYTHING]? ") - edit = STDIN.gets.chomp - if edit == ('y' or 'yes') - print $clear_code - say("#{ZCC.zcc_marc_str_bold(final_taken.to_s, 'record')}") - final_taken.edit - end - end - - print $clear_code - say("<%= color('#{'-'*20}FINAL RECORD#{'-'*20}', :headline) %>") - say("#{ZCC.zcc_marc_str_bold(final_taken.to_s, 'record')}") - say("<%= color('#{'-'*20}FINAL RECORD#{'-'*20}\a', :headline) %>") - print "Would you like to keep this FINAL RECORD? (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 - - if CSV - print $clear_code - print "\a" - say("#{ZCC.zcc_marc_str_bold(final_taken.to_s, 'record')}") - say("<%= color('Label making time!', :headline) %>") - csv = final_taken.marc_to_csv(labels) - print "Final csv:\n#{csv}\n" - end - - print $clear_code - clear_code_do = false - - - #saves in the directory in which zcc was run - if HOW_TO_SAVE == 'search_term' - filename = search_term.gsub(' ','_') - elsif HOW_TO_SAVE == 'timestamp' - filename = TIMESTAMP - end - - 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 - elsif FORMAT_TO_SAVE == 'zebra' - #puts final_taken.to_xml - File.open("#{File.expand_path("~")}/.zcc/zebra/records/#{Time.now.strftime("%Y%m%d%H%M%S")}\.xml", "w") do |f| - f.write final_taken.to_xml - end - 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 - -} +#!/usr/bin/ruby -w +## Jason Ronallo +# July, 2007 +#zcc-new + +require 'rubygems' +require 'zoom' +require 'marc' +require 'highline/import' +require 'term/ansicolor' +#include Term::ANSIColor + +require 'stringio' +require 'yaml' +require 'tempfile' + +$KCODE = 'u' +require 'jcode' +require 'unicode' + +require 'zcc' +include ZCC + +termsize = HighLine::SystemExtensions.terminal_size +$term_width = termsize[0] +$term_height = termsize[1] +$clear_code = %x{clear} +HighLine.track_eof = false + +##Setting up testing parameter +$testing = ARGV[0] +$testing = nil unless $testing == 'v' or $testing == 't' +file_mode = true if ARGV[0] == 'file' +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'] + +if zservers[0][0] =~ /Include/ + filename = zservers[0][0][8, 99] + contents = YAML.load_file("#{File.expand_path("~")}/.zcc/#{filename}") + contents.each{|c| zservers.push(c)} + zservers[0] = nil +end +zservers.compact! + +labels = yamlfile['labels'] +scripting = yamlfile['scripting'] +$procs = yamlfile['procs'] + +TO_SHOW = yamlfile['to_show'] +SHOW_PER_PAGE = yamlfile['show_per_page'] +FIELDS_TO_SHOW = yamlfile['fields_to_show'] +HOW_TO_SAVE = yamlfile['save_filename'] +TIMESTAMP = Time.now.strftime("%Y%m%d%H%M%S") +SCRIPTING = yamlfile['scripting_on'] +EDITING = yamlfile['subfield_editing'] +CSV = yamlfile['csv_on'] +LINTER = yamlfile['linter_on'] +FORMAT_TO_SAVE = yamlfile['save_record_syntax'] + +print $clear_code +#printed once when starting the program. +say("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.\nSee LICENSE for full information.\n".bold) +say("Simply enter a title, LCCN or ISBN and hit enter.\nTitle: enter the title exactly as it is on the source\n for better relevancy ranking by title.\nLCCN: Include the dash, like 81-21962\nEnter ISBNs without dashes.\nNot all servers accept it, but you can also do a narrowing search\nby including an author's name after the title like so:\nFinnegans Wake :au Joyce") +say("For more help see: http://zcc.rubyforge.org/zcc.html") + +zserver_objects = [] +zservers.each do |server| + zserver_objects << Zserver.new(server[0], server[1], server[2], server[3]) +end + +server_h = Hash.new {|hash, key| hash[key] = [] } +zserver_objects.each {|z| server_h[z.group] << z} + +groups = [] +server_h.each_key{|k| groups << k} +groups.uniq! +groups.sort! + +loop { + search_term = ask("\nEnter search. [ISBN, title, LCCN (with dash), or :q to quit]\nsearch> ".boldz, String){ |q| q.readline = true } + exit if search_term == ':q' + + taken = ResultSet.new + groups.each do |group| + print $clear_code + servers = server_h[group] + query = Query.new(search_term, servers) + results = query.search(TO_SHOW) + results.index_pos = SHOW_PER_PAGE - 1 + if results.records.empty? + puts "There are no results from group #{group} servers!\n\n\n" + another_group = ask("Do you want to continue searcing the next group of servers? [y], anything ".boldz) + if another_group == 'y' || another_group == '' + next + else + print $clear_code + break + end + end + results.rank_by_relevance! + return_value = zcc_select_good_marc(results, 'multi') + taken << results + break if return_value == 'done' + end + + taken.remove_unselected! + next if taken.empty? + continue = true + print $clear_code + say("\aThis is your combined result set.\nWe will winnow until you choose ".white.bold.on_black + "'done'".red.bold + "\nor you unselect all records.".white.bold.on_black) + sleep 3 + while continue + return_value = zcc_select_good_marc(taken, 'multi') + continue = false if taken.selected_length == 0 + continue = false if return_value == 'done' + end + taken.remove_unselected! + unless taken.length == 0 + puts "Here are the records you've selected for processing:" + taken.each do |record| + puts record.title + end + sleep 2 + end + + taken.records.each do |final_taken| + + #SCRIPTING and EDITING + if SCRIPTING + print $clear_code + print "\a" + say("#{ZCC.zcc_marc_str_bold(final_taken.to_s, 'record')}") + say("SCRIPTING...".bold.white.on_black) + final_taken.local_script(scripting) + end + + if EDITING + print $clear_code + print "\a" + say("#{ZCC.zcc_marc_str_bold(final_taken.to_s, 'record')}") + edit = ask("Would you like to " + "edit".headline + " this record [yes or ANYTHING]? ") + if edit == ('y' or 'yes') + print $clear_code + say("#{ZCC.zcc_marc_str_bold(final_taken.to_s, 'record')}") + final_taken.edit + end + end + + print $clear_code + say("#{'-'*20}FINAL RECORD#{'-'*20}".headline) + say("#{ZCC.zcc_marc_str_bold(final_taken.to_s, 'record')}") + say("#{'-'*20}FINAL RECORD#{'-'*20}".headline) + to_save_or_not_to_save = ask("Would you like to keep this FINAL RECORD? (ANYTHING or n)") + + if to_save_or_not_to_save == 'n' + next #if continue? == 'again' + end + + if CSV + print $clear_code + print "\a" + say("#{ZCC.zcc_marc_str_bold(final_taken.to_s, 'record')}") + say("Label making time!".headline) + csv = final_taken.marc_to_csv(labels) + print "Final csv:\n#{csv}\n" + end + + print $clear_code + clear_code_do = false + + + #saves in the directory in which zcc was run + if HOW_TO_SAVE == 'search_term' + filename = search_term.gsub(' ','_') + elsif HOW_TO_SAVE == 'timestamp' + filename = TIMESTAMP + end + + FORMAT_TO_SAVE.each do |format| + if format == 'marc' + puts + aFile = File.new("#{filename}\.mrc","a+") + aFile.write(final_taken.marc.to_marc) + puts "#{final_taken.marc['245']['a']} saved in #{filename}\.mrc" + aFile.close + puts + elsif format == 'xml' + #puts final_taken.marc.to_xml + aFile = File.new("#{filename}\.xml","a+") + aFile.write(final_taken.marc.to_xml) + puts "#{final_taken.marc['245']['a']} saved in #{filename}\.xml" + aFile.close + elsif format == 'zebra' + #puts final_taken.to_xml + File.open("#{File.expand_path("~")}/.zcc/zebra/records/#{Time.now.strftime("%Y%m%d%H%M%S")}\.mrc", "w") do |f| + f.write final_taken.marc.to_marc + end + Dir.chdir("#{File.expand_path("~")}/.zcc/zebra") do + record_update = `zebraidx update records` + puts record_update + end + #saving to koha2 is untested. Please let me know if it doesn't work--or even if it does. + elsif format == 'koha2' + puts + out = Tempfile.new("koha2-") + out << final_taken.marc.to_marc + out.close + koha_save = `perl /usr/local/koha/intranet/scripts/misc/migration_tools/bulkmarcimport.pl -v 6 -file #{out.path}` + puts koha_save + puts + end + end + + if CSV + puts + csvFile = File.new("csv-#{filename}\.txt","a+") + csvFile.write(csv) + puts "csv for #{final_taken.marc['245']['a']} saved in csv-#{filename}\.txt" + csvFile.close + puts + end + end + +}