8 use Data::Dump qw/dump/;
10 binmode STDOUT, ':utf8';
21 '010' => { a => [ '020', 'a' ] },
26 205 => { a => [ 250 , 'a' ] },
48 # Bib-1 @and @and @and @and @and @and @and @or
54 # @attr 1=21 subject-holding
55 # @attr 1=12 control-no
56 # @attr 1=1007 standard-id
60 8 => 'BN', # FIXME check
61 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';
81 my $mech = $self->{mech} = WWW::Mechanize->new();
87 $mech->follow_link( text_regex => qr/union/ );
89 diag "switch to advanced form (select)";
91 $mech->follow_link( url_regex => qr/mode=3/ );
93 diag "submit search $query";
102 if ( $mech->content =~ m{hits:\s*<b>\s*(\d+)\s*</b>}s ) {
105 diag "get't find results in ", $mech->content;
109 diag "got $hits results, get first one";
111 $mech->follow_link( url_regex => qr/ukaz=DISP/ );
113 diag "in COMARC format";
115 $mech->follow_link( url_regex => qr/fmt=13/ );
122 my ($self,$format) = @_;
124 my $mech = $self->{mech} || die "no mech?";
126 $format ||= 'unimarc';
128 die "unknown format: $format" unless $format =~ m{(uni|us)marc};
132 if ( $mech->content =~ m{<pre>\s*(.+?(\d+)\.\s+ID=(\d+).+?)\s*</pre>}s ) {
138 diag "fetch_marc $nr [$id] $format";
140 $comarc =~ s{</?b>}{}gs;
141 $comarc =~ s{<font[^>]*>}{<s>}gs;
142 $comarc =~ s{</font>}{<e>}gs;
144 open(my $out, '>:utf8', "comarc/$id");
150 my $marc = MARC::Record->new;
152 foreach my $line ( split(/[\r\n]+/, $comarc) ) {
154 if ( $line !~ s{^(\d\d\d)([01 ])([01 ])}{} ) {
157 our @f = ( $1, $2, $3 );
160 if ( $format eq 'unimarc' ) {
162 diag dump(@f), "line: $line";
167 $line =~ s{<s>(\w)<e>([^<]+)\s*}{sf_uni($1, $2)}ges;
168 diag "f:", dump(@f), " left: |$line|";
169 $marc->add_fields( @f );
171 } elsif ( $format eq 'usmarc' ) {
173 my ( $f, $i1, $i2 ) = @f;
179 if ( my $m = $cobiss_marc21->{$f}->{$sf} ) {
180 push @{ $out->{ $m->[0] } }, ( $m->[1], $v );
184 $line =~ s{<s>(\w)<e>([^<]+)\s*}{sf_us($f,$1, $2)}ges;
186 diag "converted marc21 ",dump( $out );
188 foreach my $f ( keys %$out ) {
189 $marc->add_fields( $f, $i1, $i2, @{ $out->{$f} } );
195 my $path = "marc/$id.$format";
197 open($out, '>:utf8', $path);
198 print $out $marc->as_usmarc;
201 diag "created $path ", -s $path, " bytes";
203 diag $marc->as_formatted;
206 $mech->follow_link( url_regex => qr/rec=$nr/ );
208 return $marc->as_usmarc;
210 die "can't fetch COMARC format from ", $mech->content;