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