| 1 | package CORION::Apache::Tika; | ||
| 2 | use strict; | ||
| 3 | use Moo; | ||
| 4 | use JSON::XS qw(decode_json); | ||
| 5 | |||
| 6 | use vars '$VERSION'; | ||
| 7 | $VERSION = '0.06'; | ||
| 8 | |||
| 9 | =head1 NAME | ||
| 10 | |||
| 11 | CORION::Apache::Tika - connect to Apache::Tika | ||
| 12 | |||
| 13 | =head1 SYNOPSIS | ||
| 14 | |||
| 15 | use CORION::Apache::Tika; | ||
| 16 | |||
| 17 | my $tika= CORION::Apache::Tika::Server->new; | ||
| 18 | |||
| 19 | my $fn= shift; | ||
| 20 | |||
| 21 | use Data::Dumper; | ||
| 22 | print Dumper $tika->get_meta($fn); | ||
| 23 | print Dumper $tika->get_text($fn); | ||
| 24 | |||
| 25 | =head1 WHY? | ||
| 26 | |||
| 27 | I had written this module earlier, but now there is L<Apache::Tika>. | ||
| 28 | This module will likely be rewritten to make use of Apache::Tika and/or | ||
| 29 | to be API-compatible with it. Do not rely on this temporary name please. | ||
| 30 | |||
| 31 | =cut | ||
| 32 | |||
| 33 | has java => ( | ||
| 34 | is => 'rw', | ||
| 35 | #isa => 'Str', | ||
| 36 | default => 'java', | ||
| 37 | ); | ||
| 38 | |||
| 39 | has 'jarfile' => ( | ||
| 40 | is => 'rw', | ||
| 41 | #isa => 'Str', | ||
| 42 | #default => 'jar/tika-server-1.5-20130816.014724-18.jar', | ||
| 43 | default => sub { | ||
| 44 | # Do a natural sort on the dot-version | ||
| 45 | (sort { my $ad; $a =~ /server-1.(\d+)/ and $ad=$1; | ||
| 46 | my $bd; $b =~ /server-1.(\d+)/ and $bd=$1; | ||
| 47 | $bd <=> $ad | ||
| 48 | } glob 'jar/tika-server-*.jar')[0] | ||
| 49 | }, | ||
| 50 | ); | ||
| 51 | |||
| 52 | has java_args => ( | ||
| 53 | is => 'rw', | ||
| 54 | #isa => 'Array', | ||
| 55 | builder => sub { [] }, | ||
| 56 | ); | ||
| 57 | |||
| 58 | has tika_args => ( | ||
| 59 | is => 'rw', | ||
| 60 | #isa => 'Array', | ||
| 61 | default => sub { [ ] }, | ||
| 62 | ); | ||
| 63 | |||
| 64 | sub cmdline { | ||
| 65 | my( $self )= @_; | ||
| 66 | $self->java, | ||
| 67 | @{$self->java_args}, | ||
| 68 | '-jar', | ||
| 69 | $self->jarfile, | ||
| 70 | @{$self->tika_args}, | ||
| 71 | }; | ||
| 72 | |||
| 73 | sub fetch { | ||
| 74 | my( $self, %options )= @_; | ||
| 75 | my @cmd= $self->cmdline; | ||
| 76 | push @cmd, $options{ type }; | ||
| 77 | push @cmd, $options{ filename }; | ||
| 78 | @cmd= map { qq{"$_"} } @cmd; | ||
| 79 | die "Fetching from local process is currently disabled"; | ||
| 80 | #warn "[@cmd]"; | ||
| 81 | ''.`@cmd` | ||
| 82 | } | ||
| 83 | |||
| 84 | sub decode_csv { | ||
| 85 | my( $self, $line )= @_; | ||
| 86 | $line =~ m!"([^"]+)"!g; | ||
| 87 | } | ||
| 88 | |||
| 89 | sub get_meta { | ||
| 90 | my( $self, $file )= @_; | ||
| 91 | #return decode_json($self->fetch( filename => $file, type => 'meta' )); | ||
| 92 | # Hacky CSV-to-hash decode :-/ | ||
| 93 | return $self->fetch( filename => $file, type => 'meta' )->meta; | ||
| 94 | }; | ||
| 95 | |||
| 96 | sub get_text { | ||
| 97 | my( $self, $file )= @_; | ||
| 98 | return $self->fetch( filename => $file, type => 'text' ); | ||
| 99 | }; | ||
| 100 | |||
| 101 | sub get_test { | ||
| 102 | my( $self, $file )= @_; | ||
| 103 | return $self->fetch( filename => $file, type => 'test' ); | ||
| 104 | }; | ||
| 105 | |||
| 106 | sub get_all { | ||
| 107 | my( $self, $file )= @_; | ||
| 108 | return $self->fetch( filename => $file, type => 'all' ); | ||
| 109 | }; | ||
| 110 | |||
| 111 | sub get_language { | ||
| 112 | my( $self, $file )= @_; | ||
| 113 | return $self->fetch( filename => $file, type => 'language' ); | ||
| 114 | }; | ||
| 115 | |||
| 116 | __PACKAGE__->meta->make_immutable; | ||
| 117 | |||
| 118 | 1; | ||
| 119 | |||
| 120 | =head1 REPOSITORY | ||
| 121 | |||
| 122 | The public repository of this module is | ||
| 123 | L<https://github.com/Corion/apache-tika>. | ||
| 124 | |||
| 125 | =head1 SUPPORT | ||
| 126 | |||
| 127 | The public support forum of this module is | ||
| 128 | L<https://perlmonks.org/>. | ||
| 129 | |||
| 130 | =head1 BUG TRACKER | ||
| 131 | |||
| 132 | Please report bugs in this module via the RT CPAN bug queue at | ||
| 133 | L<https://rt.cpan.org/Public/Dist/Display.html?Name=CORION-Apache-Tika> | ||
| 134 | or via mail to L<corion-apache-tika-Bugs@rt.cpan.org>. | ||
| 135 | |||
| 136 | =head1 AUTHOR | ||
| 137 | |||
| 138 | Max Maischein C<corion@cpan.org> | ||
| 139 | |||
| 140 | =head1 COPYRIGHT (c) | ||
| 141 | |||
| 142 | Copyright 2014-2016 by Max Maischein C<corion@cpan.org>. | ||
| 143 | |||
| 144 | =head1 LICENSE | ||
| 145 | |||
| 146 | This module is released under the same terms as Perl itself. | ||
| 147 | |||
| 148 | =cut |