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
73 my ( $self, $query ) = @_;
75 die "need query" unless defined $query;
77 my $url = 'http://cobiss.izum.si/scripts/cobiss?ukaz=GETID&lani=en';
85 $mech->follow_link( text_regex => qr/union/ );
87 diag "switch to advanced form (select)";
89 $mech->follow_link( url_regex => qr/mode=3/ );
91 diag "submit search $query";
100 if ( $mech->content =~ m{hits:\s*<b>\s*(\d+)\s*</b>}s ) {
103 diag "get't find results in ", $mech->content;
107 diag "got $hits results, get first one";
109 $mech->follow_link( url_regex => qr/ukaz=DISP/ );
111 diag "in COMARC format";
113 $mech->follow_link( url_regex => qr/fmt=13/ );
118 my ($self,$format) = @_;
120 $format ||= 'unimarc';
122 die "unknown format: $format" unless $format =~ m{(uni|us)marc};
126 if ( $mech->content =~ m{<pre>\s*(.+?(\d+)\.\s+ID=(\d+).+?)\s*</pre>}s ) {
132 diag "fetch_marc $nr [$id] $format";
134 $comarc =~ s{</?b>}{}gs;
135 $comarc =~ s{<font[^>]*>}{<s>}gs;
136 $comarc =~ s{</font>}{<e>}gs;
138 open(my $out, '>:utf8', "comarc/$id");
144 my $marc = MARC::Record->new;
146 foreach my $line ( split(/[\r\n]+/, $comarc) ) {
148 if ( $line !~ s{^(\d\d\d)([01 ])([01 ])}{} ) {
151 our @f = ( $1, $2, $3 );
154 if ( $format eq 'unimarc' ) {
156 diag dump(@f), "line: $line";
161 $line =~ s{<s>(\w)<e>([^<]+)\s*}{sf_uni($1, $2)}ges;
162 diag "f:", dump(@f), " left: |$line|";
163 $marc->add_fields( @f );
165 } elsif ( $format eq 'usmarc' ) {
167 my ( $f, $i1, $i2 ) = @f;
173 if ( my $m = $cobiss_marc21->{$f}->{$sf} ) {
174 push @{ $out->{ $m->[0] } }, ( $m->[1], $v );
178 $line =~ s{<s>(\w)<e>([^<]+)\s*}{sf_us($f,$1, $2)}ges;
180 diag "converted marc21 ",dump( $out );
182 foreach my $f ( keys %$out ) {
183 $marc->add_fields( $f, $i1, $i2, @{ $out->{$f} } );
189 my $path = "marc/$id.$format";
191 open($out, '>:utf8', $path);
192 print $out $marc->as_usmarc;
195 diag "created $path ", -s $path, " bytes";
197 diag $marc->as_formatted;
200 $mech->follow_link( url_regex => qr/rec=$nr/ );
202 return $marc->as_usmarc;
204 die "can't fetch COMARC format from ", $mech->content;