5 |
|
|
6 |
use WWW::Mechanize; |
use WWW::Mechanize; |
7 |
use MARC::Record; |
use MARC::Record; |
8 |
use File::Slurp; |
use Data::Dump qw/dump/; |
9 |
|
|
10 |
binmode STDOUT, ':utf8'; |
binmode STDOUT, ':utf8'; |
11 |
|
|
17 |
}, |
}, |
18 |
205 => { a => [ 250 , 'a' ] }, |
205 => { a => [ 250 , 'a' ] }, |
19 |
210 => { |
210 => { |
20 |
a => [ 250 , 'a' ], |
a => [ 260 , 'a' ], |
21 |
c => [ 260 , 'b' ], |
c => [ 260 , 'b' ], |
22 |
d => [ 260 , 'c' ], |
d => [ 260 , 'c' ], |
23 |
}, |
}, |
64 |
|
|
65 |
}; |
}; |
66 |
|
|
|
sub usemap { |
|
|
my $f = shift || die; |
|
|
$usemap->{$f}; |
|
|
} |
|
|
|
|
67 |
sub search { |
sub search { |
68 |
my ( $self, $query ) = @_; |
my ( $self, $query ) = @_; |
69 |
|
|
71 |
|
|
72 |
my $url = 'http://cobiss.izum.si/scripts/cobiss?ukaz=GETID&lani=en'; |
my $url = 'http://cobiss.izum.si/scripts/cobiss?ukaz=GETID&lani=en'; |
73 |
|
|
74 |
diag "# get $url"; |
diag "get $url"; |
75 |
|
|
76 |
$mech->get( $url ); |
$mech->get( $url ); |
77 |
|
|
78 |
diag "# got session"; |
diag "got session"; |
79 |
|
|
80 |
$mech->follow_link( text_regex => qr/union/ ); |
$mech->follow_link( text_regex => qr/union/ ); |
81 |
|
|
82 |
diag "# switch to advanced form (select)"; |
diag "switch to advanced form (select)"; |
83 |
|
|
84 |
$mech->follow_link( url_regex => qr/mode=3/ ); |
$mech->follow_link( url_regex => qr/mode=3/ ); |
85 |
|
|
86 |
diag "# submit search $query"; |
diag "submit search $query"; |
87 |
|
|
88 |
$mech->submit_form( |
$mech->submit_form( |
89 |
fields => { |
fields => { |
99 |
return; |
return; |
100 |
} |
} |
101 |
|
|
102 |
diag "# got $hits results, get first one"; |
diag "got $hits results, get first one"; |
103 |
|
|
104 |
$mech->follow_link( url_regex => qr/ukaz=DISP/ ); |
$mech->follow_link( url_regex => qr/ukaz=DISP/ ); |
105 |
|
|
106 |
diag "# in COMARC format"; |
diag "in COMARC format"; |
107 |
|
|
108 |
$mech->follow_link( url_regex => qr/fmt=13/ ); |
$mech->follow_link( url_regex => qr/fmt=13/ ); |
109 |
} |
} |
110 |
|
|
111 |
|
|
112 |
sub fetch_marc { |
sub fetch_rec { |
113 |
my ($self) = @_; |
my ($self,$format) = @_; |
114 |
|
|
115 |
|
$format ||= 'unimarc'; |
116 |
|
|
117 |
|
die "unknown format: $format" unless $format =~ m{(uni|us)marc}; |
118 |
|
|
119 |
my $comarc; |
my $comarc; |
120 |
|
|
121 |
if ( $mech->content =~ m{<pre>\s*(.+?(\d+\.)\s+ID=(\d+).+?)\s*</pre>}s ) { |
if ( $mech->content =~ m{<pre>\s*(.+?(\d+)\.\s+ID=(\d+).+?)\s*</pre>}s ) { |
122 |
|
|
123 |
my $comarc = $1; |
my $comarc = $1; |
124 |
my $nr = $2; |
my $nr = $2; |
125 |
my $id = $3; |
my $id = $3; |
126 |
|
|
127 |
diag "# fetch_marc $nr [$id]"; |
diag "fetch_marc $nr [$id] $format"; |
128 |
|
|
129 |
$comarc =~ s{</?b>}{}gs; |
$comarc =~ s{</?b>}{}gs; |
130 |
$comarc =~ s{<font[^>]*>}{<s>}gs; |
$comarc =~ s{<font[^>]*>}{<s>}gs; |
131 |
$comarc =~ s{</font>}{<e>}gs; |
$comarc =~ s{</font>}{<e>}gs; |
132 |
|
|
133 |
write_file "comarc/$id", $comarc; |
open(my $out, '>:utf8', "comarc/$id"); |
134 |
|
print $out $comarc; |
135 |
|
close($out); |
136 |
|
|
137 |
print $comarc; |
print $comarc; |
138 |
|
|
139 |
my $marc = MARC::Record->new; |
my $marc = MARC::Record->new; |
140 |
|
|
141 |
foreach my $line ( split(/[\r\n]+/, $comarc) ) { |
foreach my $line ( split(/[\r\n]+/, $comarc) ) { |
|
our @f; |
|
142 |
|
|
143 |
if ( $line !~ s{^(\d\d\d)([01 ])([01 ])}{} ) { |
if ( $line !~ s{^(\d\d\d)([01 ])([01 ])}{} ) { |
144 |
diag "SKIP: $line"; |
diag "SKIP: $line"; |
145 |
} else { |
} else { |
146 |
|
our @f = ( $1, $2, $3 ); |
147 |
$line .= "<eol>"; |
$line .= "<eol>"; |
148 |
|
|
149 |
@f = ( $1, $2, $3 ); |
if ( $format eq 'unimarc' ) { |
150 |
sub sf { push @f, @_; } |
|
151 |
$line =~ s{<s>(\w)<e>([^<]+)\s*}{sf($1, $2)}ges; |
diag dump(@f), "line: $line"; |
152 |
diag "# f:", join('|', @f), " left: |$line|"; |
sub sf_uni { |
153 |
$marc->add_fields( @f ); |
warn "sf ",dump(@_); |
154 |
|
push @f, @_; |
155 |
|
} |
156 |
|
$line =~ s{<s>(\w)<e>([^<]+)\s*}{sf_uni($1, $2)}ges; |
157 |
|
diag "f:", dump(@f), " left: |$line|"; |
158 |
|
$marc->add_fields( @f ); |
159 |
|
|
160 |
|
} elsif ( $format eq 'usmarc' ) { |
161 |
|
|
162 |
|
my ( $f, $i1, $i2 ) = @f; |
163 |
|
|
164 |
|
our $out = {}; |
165 |
|
|
166 |
|
sub sf_us { |
167 |
|
my ($f,$sf,$v) = @_; |
168 |
|
if ( my $m = $cobiss_marc21->{$f}->{$sf} ) { |
169 |
|
push @{ $out->{ $m->[0] } }, ( $m->[1], $v ); |
170 |
|
} |
171 |
|
return; |
172 |
|
} |
173 |
|
$line =~ s{<s>(\w)<e>([^<]+)\s*}{sf_us($f,$1, $2)}ges; |
174 |
|
|
175 |
|
diag "converted marc21 ",dump( $out ); |
176 |
|
|
177 |
|
foreach my $f ( keys %$out ) { |
178 |
|
$marc->add_fields( $f, $i1, $i2, @{ $out->{$f} } ); |
179 |
|
} |
180 |
|
} |
181 |
} |
} |
182 |
} |
} |
183 |
|
|
184 |
open(my $out, '>:utf8', "marc/$id"); |
my $path = "marc/$id.$format"; |
185 |
|
|
186 |
|
open($out, '>:utf8', $path); |
187 |
print $out $marc->as_usmarc; |
print $out $marc->as_usmarc; |
188 |
close($out); |
close($out); |
189 |
|
|
190 |
|
diag "created $path ", -s $path, " bytes"; |
191 |
|
|
192 |
diag $marc->as_formatted; |
diag $marc->as_formatted; |
193 |
|
|
194 |
|
$nr++; |
195 |
|
$mech->follow_link( url_regex => qr/rec=$nr/ ); |
196 |
|
|
197 |
return $marc->as_usmarc; |
return $marc->as_usmarc; |
198 |
} else { |
} else { |
199 |
die "can't fetch COMARC format from ", $mech->content; |
die "can't fetch COMARC format from ", $mech->content; |