fixed one small bug. if Include for zservers.yaml is not included in zcc.yaml then...
[zcc.git] / bin / zcc
blobd5ef3663a85fc1306988c3b1872f8a985be4670e
1 #!/usr/bin/ruby -w
2 ## Jason Ronallo
3 # July, 2007
4 #zcc-new
6 require 'rubygems'
7 require 'zoom'
8 require 'marc'
9 require 'highline/import'
10 require 'term/ansicolor'
11 #include Term::ANSIColor
13 require 'stringio'
14 require 'yaml'
15 require 'tempfile'
17 $KCODE = 'u'
18 require 'jcode'
19 require 'unicode'
21 require 'zcc'
22 include ZCC
24 termsize = HighLine::SystemExtensions.terminal_size
25 $term_width = termsize[0]
26 $term_height = termsize[1]
27 $clear_code = %x{clear}
28 HighLine.track_eof = false
30 ##Setting up testing parameter
31 $testing = ARGV[0]
32 $testing = nil unless $testing == 'v' or $testing == 't'
33 file_mode = true if ARGV[0] == 'file'
34 puts "We're testing " if $testing
35 STDIN.gets if $testing
37 #reading in config from yaml file
38 yamlfile = YAML.load_file( "#{File.expand_path("~")}/.zcc/zcc.yaml" )
39 zservers = yamlfile['zservers']
40 #puts zservers[0][0]
41 #STDIN.gets
42 #puts zservers[0][0].class
43 #STDIN.gets
44 #puts zservers[0][0] =~ /Include/
45 #STDIN.gets
46 if zservers[0][0] =~ /Include/
47 #puts "MATCH"
48 filename = zservers[0][0][8, 99]
49 contents = YAML.load_file("#{File.expand_path("~")}/.zcc/#{filename}")
50 #puts contents.class
51 #STDIN.gets
52 contents.each{|c| zservers.push(c)}
53 #zservers << contents
54 zservers[0] = nil
55 end
56 #puts zservers.inspect
57 #STDIN.gets
59 zservers.compact!
61 labels = yamlfile['labels']
62 scripting = yamlfile['scripting']
63 $procs = yamlfile['procs']
65 TO_SHOW = yamlfile['to_show']
66 SHOW_PER_PAGE = yamlfile['show_per_page']
67 HOW_TO_SAVE = yamlfile['save_filename']
68 TIMESTAMP = Time.now.strftime("%Y%m%d%H%M%S")
69 SCRIPTING = yamlfile['scripting_on']
70 EDITING = yamlfile['subfield_editing']
71 CSV = yamlfile['csv_on']
72 LINTER = yamlfile['linter_on']
73 FORMAT_TO_SAVE = yamlfile['save_record_syntax']
75 print $clear_code
76 #printed once when starting the program.
77 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)
78 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")
79 say("For more help see: http://zcc.rubyforge.org/zcc.html")
81 zserver_objects = []
82 zservers.each do |server|
83 zserver_objects << Zserver.new(server[0], server[1], server[2], server[3])
84 end
86 server_h = Hash.new {|hash, key| hash[key] = [] }
87 zserver_objects.each {|z| server_h[z.group] << z}
89 groups = []
90 server_h.each_key{|k| groups << k}
91 groups.uniq!
92 groups.sort!
94 loop {
95 search_term = ask("\nEnter search. [ISBN, title, LCCN (with dash), or :q to quit]\nsearch> ".boldz, String){ |q| q.readline = true }
96 exit if search_term == ':q'
98 taken = ResultSet.new
99 groups.each do |group|
100 print $clear_code
101 #puts group
102 servers = server_h[group] #zserver_objects.find_all{|server| server.group == group }
103 #puts "searching the following servers: #{servers.join("...\n")}"
104 #puts "gets here"
105 query = Query.new(search_term, servers)
106 results = query.search(TO_SHOW)
107 results.index_pos = SHOW_PER_PAGE - 1
108 #print "results.class" ; puts results.class
109 if results.records.empty?
110 puts "There are no results from group #{group} servers!\n\n\n"
111 another_group = ask("Do you want to continue searcing the next group of servers? [y], anything ".boldz)
112 if another_group == 'y' || another_group == ''
113 next
114 else
115 print $clear_code
116 break
119 #puts results.class
120 results.rank_by_relevance!
121 return_value = zcc_select_good_marc(results, 'multi')
122 taken << results
123 break if return_value == 'done'
126 #puts taken.to_s
127 taken.remove_unselected!
128 next if taken.empty?
129 continue = true
130 print $clear_code
131 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)
132 sleep 3
133 while continue
134 return_value = zcc_select_good_marc(taken, 'multi')
135 #taken.remove_unselected!
136 #continue = false if taken.length == 0
137 continue = false if taken.selected_length == 0
138 continue = false if return_value == 'done'
140 taken.remove_unselected!
141 unless taken.length == 0
142 puts "Here are the records you've selected for processing:"
143 taken.each do |record|
144 puts record.title
146 sleep 2
149 taken.records.each do |final_taken|
151 #SCRIPTING and EDITING
152 if SCRIPTING
153 print $clear_code
154 print "\a"
155 say("#{ZCC.zcc_marc_str_bold(final_taken.to_s, 'record')}")
156 say("SCRIPTING...".bold.white.on_black)
157 final_taken.local_script(scripting)
160 if EDITING
161 print $clear_code
162 print "\a"
163 say("#{ZCC.zcc_marc_str_bold(final_taken.to_s, 'record')}")
164 edit = ask("Would you like to " + "edit".headline + " this record [yes or ANYTHING]? ")
165 if edit == ('y' or 'yes')
166 print $clear_code
167 say("#{ZCC.zcc_marc_str_bold(final_taken.to_s, 'record')}")
168 final_taken.edit
172 print $clear_code
173 say("#{'-'*20}FINAL RECORD#{'-'*20}".headline)
174 say("#{ZCC.zcc_marc_str_bold(final_taken.to_s, 'record')}")
175 say("#{'-'*20}FINAL RECORD#{'-'*20}".headline)
176 to_save_or_not_to_save = ask("Would you like to keep this FINAL RECORD? (ANYTHING or n)")
178 if to_save_or_not_to_save == 'n'
179 next #if continue? == 'again'
182 if CSV
183 print $clear_code
184 print "\a"
185 say("#{ZCC.zcc_marc_str_bold(final_taken.to_s, 'record')}")
186 say("Label making time!".headline)
187 csv = final_taken.marc_to_csv(labels)
188 print "Final csv:\n#{csv}\n"
191 print $clear_code
192 clear_code_do = false
195 #saves in the directory in which zcc was run
196 if HOW_TO_SAVE == 'search_term'
197 filename = search_term.gsub(' ','_')
198 elsif HOW_TO_SAVE == 'timestamp'
199 filename = TIMESTAMP
202 FORMAT_TO_SAVE.each do |format|
203 if format == 'marc'
204 puts
205 aFile = File.new("#{filename}\.mrc","a+")
206 aFile.write(final_taken.marc.to_marc)
207 puts "#{final_taken.marc['245']['a']} saved in #{filename}\.mrc"
208 aFile.close
209 puts
210 elsif format == 'xml'
211 #puts final_taken.marc.to_xml
212 aFile = File.new("#{filename}\.xml","a+")
213 aFile.write(final_taken.marc.to_xml)
214 puts "#{final_taken.marc['245']['a']} saved in #{filename}\.xml"
215 aFile.close
216 elsif format == 'zebra'
217 #puts final_taken.to_xml
218 File.open("#{File.expand_path("~")}/.zcc/zebra/records/#{Time.now.strftime("%Y%m%d%H%M%S")}\.mrc", "w") do |f|
219 f.write final_taken.marc.to_marc
221 Dir.chdir("#{File.expand_path("~")}/.zcc/zebra") do
222 record_update = `zebraidx update records`
223 puts record_update
225 #saving to koha2 is untested. Please let me know if it doesn't work--or even if it does.
226 elsif format == 'koha2'
227 puts
228 out = Tempfile.new("koha2-")
229 out << final_taken.marc.to_marc
230 out.close
231 koha_save = `perl /usr/local/koha/intranet/scripts/misc/migration_tools/bulkmarcimport.pl -v 6 -file #{out.path}`
232 puts koha_save
233 puts
237 if CSV
238 puts
239 csvFile = File.new("csv-#{filename}\.txt","a+")
240 csvFile.write(csv)
241 puts "csv for #{final_taken.marc['245']['a']} saved in csv-#{filename}\.txt"
242 csvFile.close
243 puts