5 |
|
|
6 |
use WWW::Mechanize; |
use WWW::Mechanize; |
7 |
use MARC::Record; |
use MARC::Record; |
8 |
|
use File::Slurp; |
9 |
|
|
10 |
binmode STDOUT, ':utf8'; |
binmode STDOUT, ':utf8'; |
11 |
|
|
31 |
}, |
}, |
32 |
}; |
}; |
33 |
|
|
34 |
|
our $mech = WWW::Mechanize->new(); |
35 |
|
our $hits; |
36 |
|
|
37 |
|
sub diag { |
38 |
|
print "# ", @_, $/; |
39 |
|
} |
40 |
|
|
41 |
|
# Koha Z39.50 query: |
42 |
|
# |
43 |
|
# Bib-1 @and @and @and @and @and @and @and @or |
44 |
|
# @attr 1=8 isbn-issn |
45 |
|
# @attr 1=7 isbn-issn |
46 |
|
# @attr 1=4 title |
47 |
|
# @attr 1=1003 author |
48 |
|
# @attr 1=16 dewey |
49 |
|
# @attr 1=21 subject-holding |
50 |
|
# @attr 1=12 control-no |
51 |
|
# @attr 1=1007 standard-id |
52 |
|
# @attr 1=1016 any |
53 |
|
|
54 |
|
our $usemap = { |
55 |
|
8 => 'BN', # FIXME check |
56 |
|
7 => 'SN', # FIXME check |
57 |
|
4 => 'TI', |
58 |
|
1003 => 'TI', |
59 |
|
16 => 'CU', |
60 |
|
21 => 'SU', |
61 |
|
# 12 => '', |
62 |
|
# 1007 => '', |
63 |
|
# 1016 => '', |
64 |
|
|
65 |
|
}; |
66 |
|
|
67 |
|
sub usemap { |
68 |
|
my $f = shift || die; |
69 |
|
$usemap->{$f}; |
70 |
|
} |
71 |
|
|
72 |
sub search { |
sub search { |
73 |
|
my ( $self, $query ) = @_; |
74 |
|
|
75 |
|
die "need query" unless defined $query; |
76 |
|
|
77 |
my $url = 'http://cobiss.izum.si/scripts/cobiss?ukaz=GETID&lani=en'; |
my $url = 'http://cobiss.izum.si/scripts/cobiss?ukaz=GETID&lani=en'; |
78 |
|
|
79 |
warn "# get $url\n"; |
diag "# get $url"; |
80 |
|
|
|
my $mech = WWW::Mechanize->new(); |
|
81 |
$mech->get( $url ); |
$mech->get( $url ); |
82 |
|
|
83 |
warn "# got session\n"; |
diag "# got session"; |
84 |
|
|
85 |
$mech->follow_link( text_regex => qr/union/ ); |
$mech->follow_link( text_regex => qr/union/ ); |
86 |
|
|
87 |
warn "# submit search\n"; |
diag "# switch to advanced form (select)"; |
88 |
|
|
89 |
|
diag $mech->content; |
90 |
|
|
91 |
|
$mech->follow_link( url_regex => qr/mode=3/ ); |
92 |
|
|
93 |
|
diag "# submit search $query"; |
94 |
|
|
95 |
$mech->submit_form( |
$mech->submit_form( |
96 |
fields => { |
fields => { |
97 |
'SS1' => 'Krleza', |
'SS1' => $query, |
98 |
}, |
}, |
99 |
); |
); |
100 |
|
|
101 |
my $hits = 1; |
$hits = 0; |
102 |
if ( $mech->content =~ m{hits:\s*<b>\s*(\d+)\s*</b>}s ) { |
if ( $mech->content =~ m{hits:\s*<b>\s*(\d+)\s*</b>}s ) { |
103 |
$hits = $1; |
$hits = $1; |
104 |
} else { |
} else { |
105 |
warn "get't find results in ", $mech->content; |
diag "get't find results in ", $mech->content; |
106 |
|
return; |
107 |
} |
} |
108 |
|
|
109 |
warn "# got $hits results, get first one\n"; |
diag "# got $hits results, get first one"; |
110 |
|
|
111 |
$mech->follow_link( url_regex => qr/ukaz=DISP/ ); |
$mech->follow_link( url_regex => qr/ukaz=DISP/ ); |
112 |
|
|
113 |
warn "# in COMARC format\n"; |
diag "# in COMARC format"; |
114 |
|
|
115 |
$mech->follow_link( url_regex => qr/fmt=13/ ); |
$mech->follow_link( url_regex => qr/fmt=13/ ); |
116 |
|
} |
117 |
|
|
118 |
|
|
119 |
|
sub fetch_marc { |
120 |
|
my ($self) = @_; |
121 |
|
|
122 |
my $comarc; |
my $comarc; |
123 |
|
|
124 |
if ( $mech->content =~ m{<pre>\s*(.+1\..+?)\s*</pre>}s ) { |
if ( $mech->content =~ m{<pre>\s*(.+?(\d+\.)\s+ID=(\d+).+?)\s*</pre>}s ) { |
125 |
|
|
126 |
my $comarc = $1; |
my $comarc = $1; |
127 |
|
my $nr = $2; |
128 |
|
my $id = $3; |
129 |
|
|
130 |
|
diag "# fetch_marc $nr [$id]"; |
131 |
|
|
132 |
$comarc =~ s{</?b>}{}gs; |
$comarc =~ s{</?b>}{}gs; |
133 |
$comarc =~ s{<font[^>]*>}{<s>}gs; |
$comarc =~ s{<font[^>]*>}{<s>}gs; |
134 |
$comarc =~ s{</font>}{<e>}gs; |
$comarc =~ s{</font>}{<e>}gs; |
135 |
|
|
136 |
|
write_file "comarc/$id", $comarc; |
137 |
|
|
138 |
print $comarc; |
print $comarc; |
139 |
|
|
140 |
my $marc = MARC::Record->new; |
my $marc = MARC::Record->new; |
142 |
foreach my $line ( split(/[\r\n]+/, $comarc) ) { |
foreach my $line ( split(/[\r\n]+/, $comarc) ) { |
143 |
our @f; |
our @f; |
144 |
|
|
145 |
if ( $line !~ s{(\d\d\d)([01 ])([01 ])}{} ) { |
if ( $line !~ s{^(\d\d\d)([01 ])([01 ])}{} ) { |
146 |
warn "SKIP: $line\n"; |
diag "SKIP: $line"; |
147 |
} else { |
} else { |
148 |
$line .= "<eol>"; |
$line .= "<eol>"; |
149 |
|
|
150 |
@f = ( $1, $2, $3 ); |
@f = ( $1, $2, $3 ); |
151 |
sub sf { warn "sf",@_,"|",@f; push @f, @_; } |
sub sf { push @f, @_; } |
152 |
$line =~ s{<s>(\w)<e>([^<]+)\s*}{sf($1, $2)}ges; |
$line =~ s{<s>(\w)<e>([^<]+)\s*}{sf($1, $2)}ges; |
153 |
warn "# f:", join(' ', @f), " left:|$line|\n"; |
diag "# f:", join('|', @f), " left: |$line|"; |
154 |
$marc->add_fields( @f ); |
$marc->add_fields( @f ); |
155 |
} |
} |
156 |
} |
} |
157 |
|
|
158 |
open(my $out, '>:utf8', 'out.marc'); |
open(my $out, '>:utf8', "marc/$id"); |
159 |
print $out $marc->as_usmarc; |
print $out $marc->as_usmarc; |
160 |
close($out); |
close($out); |
161 |
|
|
162 |
warn $marc->as_formatted; |
diag $marc->as_formatted; |
163 |
|
|
164 |
return $comarc; |
return $marc->as_usmarc; |
165 |
} else { |
} else { |
166 |
die "can't fetch COMARC format from ", $mech->content; |
die "can't fetch COMARC format from ", $mech->content; |
167 |
} |
} |