line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package App::Kramerius::V4; |
2
|
|
|
|
|
|
|
|
3
|
2
|
|
|
2
|
|
85815
|
use strict; |
|
2
|
|
|
|
|
11
|
|
|
2
|
|
|
|
|
85
|
|
4
|
2
|
|
|
2
|
|
28
|
use warnings; |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
89
|
|
5
|
|
|
|
|
|
|
|
6
|
2
|
|
|
2
|
|
1305
|
use Class::Utils qw(set_params); |
|
2
|
|
|
|
|
65254
|
|
|
2
|
|
|
|
|
34
|
|
7
|
2
|
|
|
2
|
|
1124
|
use Data::Kramerius; |
|
2
|
|
|
|
|
11213
|
|
|
2
|
|
|
|
|
64
|
|
8
|
2
|
|
|
2
|
|
14
|
use Error::Pure qw(err); |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
80
|
|
9
|
2
|
|
|
2
|
|
3956
|
use Getopt::Std; |
|
2
|
|
|
|
|
100
|
|
|
2
|
|
|
|
|
116
|
|
10
|
2
|
|
|
2
|
|
935
|
use IO::Barf qw(barf); |
|
2
|
|
|
|
|
1152
|
|
|
2
|
|
|
|
|
26
|
|
11
|
2
|
|
|
2
|
|
1557
|
use JSON::XS; |
|
2
|
|
|
|
|
14996
|
|
|
2
|
|
|
|
|
115
|
|
12
|
2
|
|
|
2
|
|
1400
|
use LWP::UserAgent; |
|
2
|
|
|
|
|
87178
|
|
|
2
|
|
|
|
|
1775
|
|
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
our $VERSION = 0.01; |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
# Constructor. |
17
|
|
|
|
|
|
|
sub new { |
18
|
0
|
|
|
0
|
1
|
|
my ($class, @params) = @_; |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
# Create object. |
21
|
0
|
|
|
|
|
|
my $self = bless {}, $class; |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
# LWP::UserAgent object. |
24
|
0
|
|
|
|
|
|
$self->{'lwp_user_agent'} = undef; |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
# Process parameters. |
27
|
0
|
|
|
|
|
|
set_params($self, @params); |
28
|
|
|
|
|
|
|
|
29
|
0
|
|
|
|
|
|
$self->{'_kramerius'} = Data::Kramerius->new; |
30
|
|
|
|
|
|
|
|
31
|
0
|
0
|
|
|
|
|
if (defined $self->{'lwp_user_agent'}) { |
32
|
0
|
0
|
|
|
|
|
if (! $self->{'lwp_user_agent'}->isa('LWP::UserAgent')) { |
33
|
0
|
|
|
|
|
|
err "Parameter 'lwp_user_agent' must be a LWP::UserAgent object."; |
34
|
|
|
|
|
|
|
} |
35
|
|
|
|
|
|
|
} else { |
36
|
0
|
|
|
|
|
|
$self->{'lwp_user_agent'} = LWP::UserAgent->new; |
37
|
0
|
|
|
|
|
|
$self->{'lwp_user_agent'}->agent(__PACKAGE__.'/'.$VERSION); |
38
|
|
|
|
|
|
|
} |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
# Object. |
41
|
0
|
|
|
|
|
|
return $self; |
42
|
|
|
|
|
|
|
} |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
# Run. |
45
|
|
|
|
|
|
|
sub run { |
46
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
# Process arguments. |
49
|
0
|
|
|
|
|
|
$self->{'_opts'} = { |
50
|
|
|
|
|
|
|
'h' => 0, |
51
|
|
|
|
|
|
|
'o' => undef, |
52
|
|
|
|
|
|
|
'q' => 0, |
53
|
|
|
|
|
|
|
}; |
54
|
0
|
0
|
0
|
|
|
|
if (! getopts('ho:q', $self->{'_opts'}) || @ARGV < 2 |
|
|
|
0
|
|
|
|
|
55
|
|
|
|
|
|
|
|| $self->{'_opts'}->{'h'}) { |
56
|
|
|
|
|
|
|
|
57
|
0
|
|
|
|
|
|
print STDERR "Usage: $0 [-h] [-o out_file] [-q] [--version] kramerius_id document_uuid\n"; |
58
|
0
|
|
|
|
|
|
print STDERR "\t-h\t\tHelp.\n"; |
59
|
0
|
|
|
|
|
|
print STDERR "\t-o out_file\tOutput file.\n"; |
60
|
0
|
|
|
|
|
|
print STDERR "\t-q\t\tQuiet mode.\n"; |
61
|
0
|
|
|
|
|
|
print STDERR "\t--version\tPrint version.\n"; |
62
|
0
|
|
|
|
|
|
print STDERR "\tkramerius_id\tKramerius system id. e.g. ". |
63
|
|
|
|
|
|
|
"mzk\n"; |
64
|
0
|
|
|
|
|
|
print STDERR "\tdocument_uuid\tDocument UUID in Kramerius system\n"; |
65
|
0
|
|
|
|
|
|
return 1; |
66
|
|
|
|
|
|
|
} |
67
|
0
|
|
|
|
|
|
$self->{'_kramerius_id'} = shift @ARGV; |
68
|
0
|
|
|
|
|
|
$self->{'_doc_uuid'} = shift @ARGV; |
69
|
|
|
|
|
|
|
|
70
|
0
|
|
|
|
|
|
$self->{'_kramerius_obj'} = $self->{'_kramerius'}->get($self->{'_kramerius_id'}); |
71
|
0
|
0
|
|
|
|
|
if ($self->{'_kramerius_obj'}->version != 4) { |
72
|
0
|
|
|
|
|
|
err "Kramerius system for '$self->{'_kramerius_id'}' isn't version 4 of API."; |
73
|
|
|
|
|
|
|
} |
74
|
|
|
|
|
|
|
|
75
|
0
|
|
|
|
|
|
my $suffix = ''; |
76
|
0
|
0
|
|
|
|
|
if (! $self->{'_opts'}->{'o'}) { |
77
|
0
|
|
|
|
|
|
$suffix = $self->_get_suffix; |
78
|
|
|
|
|
|
|
} |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
my $kramerius_full_url = $self->{'_kramerius_obj'}->url. |
81
|
|
|
|
|
|
|
'search/api/v5.0/item/uuid:'. |
82
|
0
|
|
|
|
|
|
$self->{'_doc_uuid'}.'/full'; |
83
|
0
|
|
|
|
|
|
$self->_message("Download $kramerius_full_url"); |
84
|
0
|
|
|
|
|
|
my $full_res = $self->{'lwp_user_agent'}->get($kramerius_full_url); |
85
|
0
|
0
|
|
|
|
|
if (! $full_res->is_success) { |
86
|
0
|
|
|
|
|
|
err "Cannot download '$kramerius_full_url'."; |
87
|
|
|
|
|
|
|
} |
88
|
|
|
|
|
|
|
my $output_file = $self->{'_opts'}->{'o'} ? $self->{'_opts'}->{'o'} |
89
|
0
|
0
|
|
|
|
|
: $self->{'_doc_uuid'}; |
90
|
0
|
0
|
|
|
|
|
if (! $self->{'_opts'}->{'o'}) { |
91
|
0
|
|
|
|
|
|
$output_file .= '.'.$suffix; |
92
|
|
|
|
|
|
|
} |
93
|
0
|
|
|
|
|
|
$self->_message("Save $output_file"); |
94
|
0
|
|
|
|
|
|
barf($output_file, $full_res->content); |
95
|
|
|
|
|
|
|
|
96
|
0
|
|
|
|
|
|
return 0; |
97
|
|
|
|
|
|
|
} |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
sub _get_suffix { |
100
|
0
|
|
|
0
|
|
|
my $self = shift; |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
# Construct URL for metadata. |
103
|
|
|
|
|
|
|
my $kramerius_streams_url = $self->{'_kramerius_obj'}->url. |
104
|
|
|
|
|
|
|
'search/api/v5.0/item/uuid:'. |
105
|
0
|
|
|
|
|
|
$self->{'_doc_uuid'}.'/streams'; |
106
|
0
|
|
|
|
|
|
$self->_message("Download $kramerius_streams_url"); |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
# Get metadata. |
109
|
0
|
|
|
|
|
|
my $stream_res = $self->{'lwp_user_agent'}->get($kramerius_streams_url); |
110
|
0
|
0
|
|
|
|
|
if (! $stream_res->is_success) { |
111
|
0
|
|
|
|
|
|
err "Cannot download '$kramerius_streams_url'."; |
112
|
|
|
|
|
|
|
} |
113
|
0
|
|
|
|
|
|
my $struct_hr = decode_json($stream_res->content); |
114
|
0
|
0
|
|
|
|
|
if (! defined $struct_hr) { |
115
|
0
|
|
|
|
|
|
err "Cannot decode content of '$kramerius_streams_url' as JSON."; |
116
|
|
|
|
|
|
|
} |
117
|
0
|
0
|
|
|
|
|
if (! exists $struct_hr->{'IMG_FULL'}) { |
118
|
0
|
|
|
|
|
|
err "Object with '$self->{'_doc_uuid'}' isn't document."; |
119
|
|
|
|
|
|
|
} |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
# Detect suffix. |
122
|
0
|
|
|
|
|
|
my $suffix = ''; |
123
|
0
|
0
|
|
|
|
|
if ($struct_hr->{'IMG_FULL'}->{'mimeType'} eq 'image/jp2') { |
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
124
|
0
|
|
|
|
|
|
$suffix = 'jp2'; |
125
|
|
|
|
|
|
|
} elsif ($struct_hr->{'IMG_FULL'}->{'mimeType'} eq 'image/jpeg') { |
126
|
0
|
|
|
|
|
|
$suffix = 'jpg'; |
127
|
|
|
|
|
|
|
} elsif ($struct_hr->{'IMG_FULL'}->{'mimeType'} eq 'application/pdf') { |
128
|
0
|
|
|
|
|
|
$suffix = 'pdf'; |
129
|
|
|
|
|
|
|
} else { |
130
|
0
|
|
|
|
|
|
my $mime_type = $struct_hr->{'IMG_FULL'}->{'mimeType'}; |
131
|
0
|
0
|
|
|
|
|
if (defined $mime_type) { |
132
|
0
|
|
|
|
|
|
err "Unsupported image format '$mime_type'."; |
133
|
|
|
|
|
|
|
} else { |
134
|
0
|
|
|
|
|
|
err "Unsupported image format. Unknown issue."; |
135
|
|
|
|
|
|
|
} |
136
|
|
|
|
|
|
|
} |
137
|
|
|
|
|
|
|
|
138
|
0
|
|
|
|
|
|
return $suffix; |
139
|
|
|
|
|
|
|
} |
140
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
sub _message { |
142
|
0
|
|
|
0
|
|
|
my ($self, $message) = @_; |
143
|
|
|
|
|
|
|
|
144
|
0
|
0
|
|
|
|
|
if (! $self->{'_opts'}->{'q'}) { |
145
|
0
|
|
|
|
|
|
print "$message\n"; |
146
|
|
|
|
|
|
|
} |
147
|
|
|
|
|
|
|
|
148
|
0
|
|
|
|
|
|
return; |
149
|
|
|
|
|
|
|
} |
150
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
1; |
152
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
=pod |
154
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
=encoding utf8 |
156
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
=head1 NAME |
158
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
App::Kramerius::V4 - Base class for kramerius4 script. |
160
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
=head1 SYNOPSIS |
162
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
use App::Kramerius::V4; |
164
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
my $app = App::Kramerius::V4->new; |
166
|
|
|
|
|
|
|
my $exit_code = $app->run; |
167
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
=head1 METHODS |
169
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
=head2 C<new> |
171
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
my $app = App::Kramerius::V4->new; |
173
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
Constructor. |
175
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
Returns instance of object. |
177
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
=head2 C<run> |
179
|
|
|
|
|
|
|
|
180
|
|
|
|
|
|
|
my $exit_code = $app->run; |
181
|
|
|
|
|
|
|
|
182
|
|
|
|
|
|
|
Run. |
183
|
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
Returns 1 for error, 0 for success. |
185
|
|
|
|
|
|
|
|
186
|
|
|
|
|
|
|
=head1 ERRORS |
187
|
|
|
|
|
|
|
|
188
|
|
|
|
|
|
|
new(): |
189
|
|
|
|
|
|
|
Parameter 'lwp_user_agent' must be a LWP::UserAgent object. |
190
|
|
|
|
|
|
|
|
191
|
|
|
|
|
|
|
run(): |
192
|
|
|
|
|
|
|
Cannot download '%s'. |
193
|
|
|
|
|
|
|
Cannot decode content of '%s' as JSON. |
194
|
|
|
|
|
|
|
Kramerius system for '%s' isn't version 4 of API. |
195
|
|
|
|
|
|
|
Object with '%s' isn't document. |
196
|
|
|
|
|
|
|
Unsupported image format '%s'. |
197
|
|
|
|
|
|
|
Unsupported image format. Unknown issue. |
198
|
|
|
|
|
|
|
|
199
|
|
|
|
|
|
|
=head1 EXAMPLE |
200
|
|
|
|
|
|
|
|
201
|
|
|
|
|
|
|
use strict; |
202
|
|
|
|
|
|
|
use warnings; |
203
|
|
|
|
|
|
|
|
204
|
|
|
|
|
|
|
use App::Kramerius::V4; |
205
|
|
|
|
|
|
|
|
206
|
|
|
|
|
|
|
# Arguments. |
207
|
|
|
|
|
|
|
@ARGV = ( |
208
|
|
|
|
|
|
|
'mzk', |
209
|
|
|
|
|
|
|
'224d66f8-f48e-4a92-b41e-87c88a076dc0', |
210
|
|
|
|
|
|
|
); |
211
|
|
|
|
|
|
|
|
212
|
|
|
|
|
|
|
# Run. |
213
|
|
|
|
|
|
|
exit App::Kramerius::V4->new->run; |
214
|
|
|
|
|
|
|
|
215
|
|
|
|
|
|
|
# Output like: |
216
|
|
|
|
|
|
|
# Download http://kramerius.mzk.cz/search/api/v5.0/item/uuid:224d66f8-f48e-4a92-b41e-87c88a076dc0/streams |
217
|
|
|
|
|
|
|
# Download http://kramerius.mzk.cz/search/api/v5.0/item/uuid:224d66f8-f48e-4a92-b41e-87c88a076dc0/full |
218
|
|
|
|
|
|
|
# Save 224d66f8-f48e-4a92-b41e-87c88a076dc0.jpg |
219
|
|
|
|
|
|
|
|
220
|
|
|
|
|
|
|
=head1 DEPENDENCIES |
221
|
|
|
|
|
|
|
|
222
|
|
|
|
|
|
|
L<Class::Utils>, |
223
|
|
|
|
|
|
|
L<Data::Kramerius>, |
224
|
|
|
|
|
|
|
L<Error::Pure>, |
225
|
|
|
|
|
|
|
L<Getopt::Std>, |
226
|
|
|
|
|
|
|
L<IO::Barf>, |
227
|
|
|
|
|
|
|
L<JSON::XS>, |
228
|
|
|
|
|
|
|
L<LWP::UserAgent>. |
229
|
|
|
|
|
|
|
|
230
|
|
|
|
|
|
|
=head1 SEE ALSO |
231
|
|
|
|
|
|
|
|
232
|
|
|
|
|
|
|
=over |
233
|
|
|
|
|
|
|
|
234
|
|
|
|
|
|
|
=item L<App::Kramerius::URI> |
235
|
|
|
|
|
|
|
|
236
|
|
|
|
|
|
|
Base class for kramerius-uri script. |
237
|
|
|
|
|
|
|
|
238
|
|
|
|
|
|
|
=item L<Data::Kramerius> |
239
|
|
|
|
|
|
|
|
240
|
|
|
|
|
|
|
Information about all Kramerius systems. |
241
|
|
|
|
|
|
|
|
242
|
|
|
|
|
|
|
=back |
243
|
|
|
|
|
|
|
|
244
|
|
|
|
|
|
|
=head1 REPOSITORY |
245
|
|
|
|
|
|
|
|
246
|
|
|
|
|
|
|
L<https://github.com/michal-josef-spacek/App-Kramerius-V4> |
247
|
|
|
|
|
|
|
|
248
|
|
|
|
|
|
|
=head1 AUTHOR |
249
|
|
|
|
|
|
|
|
250
|
|
|
|
|
|
|
Michal Josef Špaček L<mailto:skim@cpan.org> |
251
|
|
|
|
|
|
|
|
252
|
|
|
|
|
|
|
L<http://skim.cz> |
253
|
|
|
|
|
|
|
|
254
|
|
|
|
|
|
|
=head1 LICENSE AND COPYRIGHT |
255
|
|
|
|
|
|
|
|
256
|
|
|
|
|
|
|
© 2021 Michal Josef Špaček |
257
|
|
|
|
|
|
|
|
258
|
|
|
|
|
|
|
BSD 2-Clause License |
259
|
|
|
|
|
|
|
|
260
|
|
|
|
|
|
|
=head1 VERSION |
261
|
|
|
|
|
|
|
|
262
|
|
|
|
|
|
|
0.01 |
263
|
|
|
|
|
|
|
|
264
|
|
|
|
|
|
|
=cut |