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