| 1 | #!/usr/bin/env perl |
| 2 | $|scip-perl cpan Catmandu-Z3950 0.06 main/`$|`.++; |
| 3 | |
| 4 | use Catmandu::Importer::Z3950scip-perl cpan Catmandu-Z3950 0.06 Catmandu/Importer/Z3950/; |
| 5 | use Data::Dumperscip-perl cpan Catmandu-Z3950 0.06 Data/Dumper/; |
| 6 | use MARC::Recordscip-perl cpan Catmandu-Z3950 0.06 MARC/Record/; |
| 7 | use Getopt::Longscip-perl cpan Catmandu-Z3950 0.06 Getopt/Long/; |
| 8 | |
| 9 | my $host; |
| 10 | my $port; |
| 11 | my $databaseName; |
| 12 | my $preferredRecordSyntax; |
| 13 | my $queryType; |
| 14 | my $user; |
| 15 | my $password; |
| 16 | my $num; |
| 17 | my $help; |
| 18 | my $query; |
| 19 | my $sleep; |
| 20 | my $verbose; |
| 21 | my $conf = 'loc'; |
| 22 | |
| 23 | my $std_conf = { |
| 24 | 'loc' => { host => 'lx2.loc.gov' , port => 210 , databaseName => 'lcdb' , preferredRecordSyntax => 'USMARC' , queryType => 'PQF'} , |
| 25 | 'ugent' => { host => 'aleph.ugent.be' , port => 9991 , databaseName => 'RUG01' , preferredRecordSyntax => 'USMARC' , queryType => 'PQF'} , |
| 26 | 'innopac' => { host => 'library.anu.edu.au' , port => 210 , databaseName => 'INNOPAC' , preferredRecordSyntax => 'USMARC' , queryType => 'PQF'} , |
| 27 | }; |
| 28 | |
| 29 | GetOptionsscip-perl cpan Catmandu-Z3950 0.06 main/GetOptions().( "h" => \$help , |
| 30 | "conf=s" => \$conf , |
| 31 | "host=s" => \$host , |
| 32 | "port=i" => \$port , |
| 33 | "base=s" => \$databaseName , |
| 34 | "syntax=s" => \$preferredRecordSyntax , |
| 35 | "type=s" => \$queryType , |
| 36 | "sleep=i" => \$sleep , |
| 37 | "verbose" => \$verbose , |
| 38 | "user" => \$user , |
| 39 | "password" => \$password , |
| 40 | "num=i" => \$num); |
| 41 | |
| 42 | &usagescip-perl cpan Catmandu-Z3950 0.06 main/usage(). if $help; |
| 43 | |
| 44 | binmodescip-perl cpan Catmandu-Z3950 0.06 main/binmode().(STDOUT,':encoding(UTF-8)'); |
| 45 | |
| 46 | if (@ARGVscip-perl cpan Catmandu-Z3950 0.06 main/`@ARGV`.) { |
| 47 | my $query = shiftscip-perl cpan Catmandu-Z3950 0.06 main/shift().; |
| 48 | |
| 49 | my $importer = &importerscip-perl cpan Catmandu-Z3950 0.06 main/importer().; |
| 50 | $importer->queryscip-perl cpan Catmandu-Z3950 0.06 main/query().($query); |
| 51 | |
| 52 | my $n = $importer->slicescip-perl cpan Catmandu-Z3950 0.06 main/slice().(0,$num)->eachscip-perl cpan Catmandu-Z3950 0.06 main/each().(sub { |
| 53 | process_recordscip-perl cpan Catmandu-Z3950 0.06 main/process_record().(0,$_scip-perl cpan Catmandu-Z3950 0.06 main/`@_`.[0]); |
| 54 | }); |
| 55 | |
| 56 | print STDERR "processed: $n record\n"; |
| 57 | } |
| 58 | else { |
| 59 | my $count = 0; |
| 60 | my $results = 0; |
| 61 | my $importer = &importerscip-perl cpan Catmandu-Z3950 0.06 main/importer().; |
| 62 | |
| 63 | while (<STDIN>) { |
| 64 | chompscip-perl cpan Catmandu-Z3950 0.06 main/chomp().; |
| 65 | my ($prefix,$query) = splitscip-perl cpan Catmandu-Z3950 0.06 main/split().(/\s+/,$_scip-perl cpan Catmandu-Z3950 0.06 main/$_.,2); |
| 66 | my $n; |
| 67 | |
| 68 | my $error = 0; |
| 69 | do { |
| 70 | $importer->queryscip-perl cpan Catmandu-Z3950 0.06 main/query().($query); |
| 71 | |
| 72 | $count++; |
| 73 | print STDERR "executing: $prefix\t$query hits: " if $verbose; |
| 74 | |
| 75 | eval { |
| 76 | $n = $importer->slicescip-perl cpan Catmandu-Z3950 0.06 main/slice().(0,$num)->eachscip-perl cpan Catmandu-Z3950 0.06 main/each().(sub { |
| 77 | process_recordscip-perl cpan Catmandu-Z3950 0.06 main/process_record().($prefix,$_scip-perl cpan Catmandu-Z3950 0.06 main/`@_`.[0]); |
| 78 | }); |
| 79 | }; |
| 80 | |
| 81 | if ($@scip-perl cpan Catmandu-Z3950 0.06 main/`$@`.) { |
| 82 | print STDERR "caught: $@ sleeping 10 seconds " if $verbose; |
| 83 | sleepscip-perl cpan Catmandu-Z3950 0.06 main/sleep(). 10; |
| 84 | $n = 0; |
| 85 | print STDERR "reconnecting " if $verbose; |
| 86 | $importer = &importerscip-perl cpan Catmandu-Z3950 0.06 main/importer().; |
| 87 | } |
| 88 | else { $error = 0 } |
| 89 | } while ($error == 1); |
| 90 | |
| 91 | $results += 1 if $n > 0; |
| 92 | |
| 93 | printf STDERR "$n queries with results: $results/$count %-3.1f %%\n", 100 * $results/$count if $verbose; |
| 94 | |
| 95 | sleepscip-perl cpan Catmandu-Z3950 0.06 main/sleep().($sleep) if $sleep; |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | sub importerscip-perl cpan Catmandu-Z3950 0.06 main/importer(). { |
| 100 | my $std = $std_conf->{$conf} || {}; |
| 101 | $std->{host} = $host if $host; |
| 102 | $std->{port} = $port if $port; |
| 103 | $std->{user} = $user if $user; |
| 104 | $std->{password} = $password if $password; |
| 105 | $std->{databaseName} = $databaseName if $databaseName; |
| 106 | $std->{preferredRecordSyntax} = $preferredRecordSyntax if $preferredRecordSyntax; |
| 107 | $std->{queryType} = $queryType if $queryType; |
| 108 | return Catmandu::Importer::Z3950->newscip-perl cpan Catmandu-Z3950 0.06 Catmandu/Importer/Z3950/new().(%$std); |
| 109 | } |
| 110 | |
| 111 | sub process_recordscip-perl cpan Catmandu-Z3950 0.06 main/process_record(). { |
| 112 | my ($prefix,$ref) = @_scip-perl cpan Catmandu-Z3950 0.06 main/`@_`.; |
| 113 | my $marc = MARC::Record->new_from_usmarcscip-perl cpan Catmandu-Z3950 0.06 MARC/Record/new_from_usmarc().($ref); |
| 114 | |
| 115 | printfscip-perl cpan Catmandu-Z3950 0.06 main/printf(). "%-9.9d %-3.3s L BK\n" , $prefix, 'FMT'; |
| 116 | |
| 117 | for my $field ($marc->fieldsscip-perl cpan Catmandu-Z3950 0.06 MARC/Record/fields().) { |
| 118 | my $tag = $field->tagscip-perl cpan Catmandu-Z3950 0.06 main/tag().; |
| 119 | my $ind1 = $field->indicatorscip-perl cpan Catmandu-Z3950 0.06 main/indicator().(1) // ' '; |
| 120 | my $ind2 = $field->indicatorscip-perl cpan Catmandu-Z3950 0.06 main/indicator().(2) // ' '; |
| 121 | |
| 122 | printfscip-perl cpan Catmandu-Z3950 0.06 main/printf(). "%-9.9d %-3.3s%s%s L " , $prefix, $tag, $ind1, $ind2; |
| 123 | |
| 124 | if ($field->is_control_fieldscip-perl cpan Catmandu-Z3950 0.06 main/is_control_field().) { |
| 125 | printfscip-perl cpan Catmandu-Z3950 0.06 main/printf(). "%s" , $field->datascip-perl cpan Catmandu-Z3950 0.06 main/data().; |
| 126 | } |
| 127 | else { |
| 128 | for my $subfield ($field->subfieldsscip-perl cpan Catmandu-Z3950 0.06 main/subfields().) { |
| 129 | printfscip-perl cpan Catmandu-Z3950 0.06 main/printf(). "\$\$%s%s" , $subfield->[0], $subfield->[1]; |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | printscip-perl cpan Catmandu-Z3950 0.06 main/print(). "\n"; |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | sub usagescip-perl cpan Catmandu-Z3950 0.06 main/usage(). { |
| 138 | print STDERR <<EOF; |
| 139 | usage: |
| 140 | |
| 141 | $0 [options] query |
| 142 | $0 [options] < file |
| 143 | |
| 144 | where: file like |
| 145 | prefix query |
| 146 | prefix query |
| 147 | prefix query |
| 148 | . |
| 149 | . |
| 150 | . |
| 151 | |
| 152 | options: |
| 153 | -h |
| 154 | --conf=(loc|ugent) |
| 155 | --host=$host |
| 156 | --port=$port |
| 157 | --user=XXXX |
| 158 | --password=XXXX |
| 159 | --base=$databaseName |
| 160 | --syntax=$preferredRecordSyntax |
| 161 | --type=$queryType |
| 162 | --num=<max_number_of_records> |
| 163 | --sleep=<seconds> |
| 164 | --prefix=$prefix |
| 165 | |
| 166 | example: |
| 167 | |
| 168 | --host lx2.loc.gov --port 210 --base lcdb --type PQF |
| 169 | |
| 170 | query: \@attr 1=7 9781615300068 (search for 9781615300068 in ISBN-keyword) |
| 171 | query: \@attr 1=8 0028-0836 (search for 0028-0836 in ISSN) |
| 172 | |
| 173 | Use: http://www.loc.gov/z3950/lcdbz3950.xml for more options |
| 174 | |
| 175 | usage: |
| 176 | |
| 177 | $0 "\@attr 1=7 9781615300068" |
| 178 | $0 < example.txt |
| 179 | EOF |
| 180 | exitscip-perl cpan Catmandu-Z3950 0.06 main/exit(). 1; |
| 181 | } |