8 use Data::Dump qw/dump/;
10 binmode STDOUT, ':utf8';
13 '010' => { a => [ '020', 'a' ] },
18 205 => { a => [ 250 , 'a' ] },
34 our $mech = WWW::Mechanize->new();
43 # Bib-1 @and @and @and @and @and @and @and @or
49 # @attr 1=21 subject-holding
50 # @attr 1=12 control-no
51 # @attr 1=1007 standard-id
55 8 => 'BN', # FIXME check
56 7 => 'SN', # FIXME check
68 my ( $self, $query ) = @_;
70 die "need query" unless defined $query;
72 my $url = 'http://cobiss.izum.si/scripts/cobiss?ukaz=GETID&lani=en';
80 $mech->follow_link( text_regex => qr/union/ );
82 diag "switch to advanced form (select)";
84 $mech->follow_link( url_regex => qr/mode=3/ );
86 diag "submit search $query";
95 if ( $mech->content =~ m{hits:\s*<b>\s*(\d+)\s*</b>}s ) {
98 diag "get't find results in ", $mech->content;
102 diag "got $hits results, get first one";
104 $mech->follow_link( url_regex => qr/ukaz=DISP/ );
106 diag "in COMARC format";
108 $mech->follow_link( url_regex => qr/fmt=13/ );
113 my ($self,$format) = @_;
115 $format ||= 'unimarc';
117 die "unknown format: $format" unless $format =~ m{(uni|us)marc};
121 if ( $mech->content =~ m{<pre>\s*(.+?(\d+)\.\s+ID=(\d+).+?)\s*</pre>}s ) {
127 diag "fetch_marc $nr [$id] $format";
129 $comarc =~ s{</?b>}{}gs;
130 $comarc =~ s{<font[^>]*>}{<s>}gs;
131 $comarc =~ s{</font>}{<e>}gs;
133 open(my $out, '>:utf8', "comarc/$id");
139 my $marc = MARC::Record->new;
141 foreach my $line ( split(/[\r\n]+/, $comarc) ) {
143 if ( $line !~ s{^(\d\d\d)([01 ])([01 ])}{} ) {
146 our @f = ( $1, $2, $3 );
149 if ( $format eq 'unimarc' ) {
151 diag dump(@f), "line: $line";
156 $line =~ s{<s>(\w)<e>([^<]+)\s*}{sf_uni($1, $2)}ges;
157 diag "f:", dump(@f), " left: |$line|";
158 $marc->add_fields( @f );
160 } elsif ( $format eq 'usmarc' ) {
162 my ( $f, $i1, $i2 ) = @f;
168 if ( my $m = $cobiss_marc21->{$f}->{$sf} ) {
169 push @{ $out->{ $m->[0] } }, ( $m->[1], $v );
173 $line =~ s{<s>(\w)<e>([^<]+)\s*}{sf_us($f,$1, $2)}ges;
175 diag "converted marc21 ",dump( $out );
177 foreach my $f ( keys %$out ) {
178 $marc->add_fields( $f, $i1, $i2, @{ $out->{$f} } );
184 my $path = "marc/$id.$format";
186 open($out, '>:utf8', $path);
187 print $out $marc->as_usmarc;
190 diag "created $path ", -s $path, " bytes";
192 diag $marc->as_formatted;
195 $mech->follow_link( url_regex => qr/rec=$nr/ );
197 return $marc->as_usmarc;
199 die "can't fetch COMARC format from ", $mech->content;