1 |
package Meteor::Koha; |
2 |
|
3 |
use strict; |
4 |
use warnings; |
5 |
|
6 |
use LWP::UserAgent; |
7 |
|
8 |
my $ua = LWP::UserAgent->new; |
9 |
|
10 |
our $cache; |
11 |
|
12 |
use HTML::Query qw/Query/; |
13 |
use File::Slurp; |
14 |
use Encode qw/decode/; |
15 |
use Data::Dump qw/dump/; |
16 |
|
17 |
sub item { |
18 |
my ( $class, $client, $item ) = @_; |
19 |
|
20 |
my $html; |
21 |
my $status = 200; |
22 |
|
23 |
my $path = "cache/$item.html"; |
24 |
|
25 |
if ( $html = $cache->{$item} ) { |
26 |
|
27 |
warn "# koha item $item from cache\n"; |
28 |
|
29 |
} elsif ( 0 && -e $path ) { # FIXME enable |
30 |
|
31 |
$html = $cache->{$item} = read_file( $path ); |
32 |
|
33 |
} else { |
34 |
|
35 |
my $url = 'http://koha.ffzg.hr/cgi-bin/koha/opac-detail.pl?biblionumber=' . $item; |
36 |
|
37 |
my $response = $ua->get( $url ); |
38 |
|
39 |
if ($response->is_success) { |
40 |
|
41 |
$html = $response->content; |
42 |
|
43 |
write_file( "cache/$item.koha.html", $html ); |
44 |
|
45 |
warn "## size before: ", length( $html ); |
46 |
|
47 |
$html = decode( 'utf-8', $html ); |
48 |
my $q = Query( text => $html, 'div.container' ) |
49 |
|| die; |
50 |
$html = $q->as_HTML->[0]; |
51 |
|
52 |
warn "## size after: ", length( $html ), dump( $html ); |
53 |
|
54 |
$cache->{$item} = $html; |
55 |
write_file( $path, $html ); |
56 |
|
57 |
} else { |
58 |
|
59 |
$html = qq{Can't load $item } . $response->status_line; |
60 |
# $status = 404; |
61 |
|
62 |
} |
63 |
} |
64 |
|
65 |
my $len = length($html); |
66 |
|
67 |
warn "# koha item $item [$len]\n"; |
68 |
|
69 |
$client->write( |
70 |
"HTTP/1.1 $status OK\r\n" . |
71 |
"Content-Type: text/html\r\n" . |
72 |
"Content-length: $len\r\n" . |
73 |
"Connection: close\r\n" . |
74 |
"\r\n\r\n$html\r\n" |
75 |
); |
76 |
|
77 |
} |
78 |
|
79 |
1; |