Line 0
Link Here
|
|
|
1 |
package Koha::Carousel; |
2 |
|
3 |
# Copyright 2016 Mason James |
4 |
# |
5 |
# This file is part of Koha. |
6 |
# |
7 |
# Koha is free software; you can redistribute it and/or modify it under the |
8 |
# terms of the GNU General Public License as published by the Free Software |
9 |
# Foundation; either version 3 of the License, or (at your option) any later |
10 |
# version. |
11 |
# |
12 |
# Koha is distributed in the hope that it will be useful, but WITHOUT ANY |
13 |
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR |
14 |
# A PARTICULAR PURPOSE. See the GNU General Public License for more details. |
15 |
# |
16 |
# You should have received a copy of the GNU General Public License along |
17 |
# with Koha; if not, write to the Free Software Foundation, Inc., |
18 |
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
19 |
|
20 |
use strict; |
21 |
use warnings; |
22 |
use C4::Koha; |
23 |
use C4::Biblio; |
24 |
use C4::Debug; |
25 |
|
26 |
use LWP::Simple; |
27 |
use List::Util qw(shuffle ); |
28 |
use Business::ISBN; |
29 |
|
30 |
if ( $ENV{DEBUG} ) { |
31 |
use Time::HiRes qw/gettimeofday tv_interval/; |
32 |
use Carp; |
33 |
} |
34 |
|
35 |
use vars qw($VERSION @ISA @EXPORT); |
36 |
|
37 |
BEGIN { |
38 |
$VERSION = 1.00; |
39 |
|
40 |
require Exporter; |
41 |
@ISA = qw( Exporter ); |
42 |
|
43 |
push @EXPORT, qw( |
44 |
&GetRecentBibs |
45 |
); |
46 |
} |
47 |
|
48 |
=head1 NAME |
49 |
|
50 |
Koha::Carousel - A module to handle subroutines for the 'new items' carousel |
51 |
|
52 |
=head1 DESCRIPTION |
53 |
|
54 |
This module has 1 subroutine, |
55 |
|
56 |
GetRecentBibs() returns a hashref of bibs with a matching amazon bookcover url |
57 |
|
58 |
=head1 SYNOPSIS |
59 |
|
60 |
use Koha::Carousel |
61 |
|
62 |
# give me a hashref of 10 random new bibs |
63 |
my $new_bibs_href = GetRecentBibs(); |
64 |
|
65 |
# give me a hashref of 20 random new bibs |
66 |
my $new_bibs_href = GetRecentBibs('20'); |
67 |
|
68 |
=cut |
69 |
|
70 |
sub GetRecentBibs { |
71 |
|
72 |
my $caro_num = shift; |
73 |
|
74 |
# speed stats for debugging. |
75 |
my $tt0; |
76 |
my $tt1; |
77 |
if ( $ENV{DEBUG} ) { |
78 |
$tt0 = [gettimeofday]; |
79 |
} |
80 |
|
81 |
# set vars = 0, to shush warnings |
82 |
my ( $amz_hit_cnt, $amz_miss_cnt, $dupes, $cache_hits, $fetches, $hits, |
83 |
$cache_misses ) |
84 |
= (0) x 7; |
85 |
my ( @bibs_stage1, @bibs_stage2, @bibs_stage3 ); |
86 |
|
87 |
my $ua = new LWP::UserAgent( 'max_redirect' => 0 ); |
88 |
|
89 |
# 3 different limits, for narrowing the list of bib results |
90 |
my $stage1_limit = 300; |
91 |
my $stage2_limit = 150; |
92 |
my $stage3_limit = $caro_num ? $caro_num : 10; |
93 |
|
94 |
# Initial SQL query to get recently added bibs |
95 |
# we choose a 'reasonable' limit here, to ensure we don't make our dataset too small |
96 |
|
97 |
my $bib_rs = Koha::Database->new()->schema->resultset('Biblio')->search( |
98 |
{ 'biblioitems.isbn' => { '!=', undef }, }, |
99 |
{ |
100 |
join => 'biblioitems', |
101 |
'+select' => ['biblioitems.isbn'], |
102 |
'+as' => ['isbn'], |
103 |
|
104 |
rows => $stage1_limit, |
105 |
desc => 'datecreated', |
106 |
} |
107 |
); |
108 |
|
109 |
# run a loop to get $stage1_limit bibs with checksum verified ISBNs. |
110 |
my $i; |
111 |
while ( my $r1 = $bib_rs->next ) { |
112 |
|
113 |
# test checksum of ISBN10 |
114 |
my $isbn_tmp = $r1->get_column('isbn'); |
115 |
my $isbn; |
116 |
|
117 |
my @ibs = split( /\|/, $isbn_tmp ); |
118 |
foreach my $ii (@ibs) { |
119 |
|
120 |
# strip trailing (comments) from isbn |
121 |
$ii =~ s/\(.*$//g; |
122 |
|
123 |
my $isbn_obj = Business::ISBN->new($ii); |
124 |
next unless $isbn_obj; |
125 |
|
126 |
my $isbn10; |
127 |
$isbn10 = $isbn_obj->as_isbn10; |
128 |
if ( $isbn10->is_valid() ) { |
129 |
$isbn = $isbn10->as_string( [] ); |
130 |
last; |
131 |
} |
132 |
} |
133 |
|
134 |
next unless $isbn; |
135 |
|
136 |
my $bib = { |
137 |
biblionumber => $r1->biblionumber, |
138 |
title => $r1->title, |
139 |
author => $r1->author, |
140 |
isbn => $isbn, |
141 |
}; |
142 |
|
143 |
push @bibs_stage1, $bib; |
144 |
|
145 |
} # end while |
146 |
|
147 |
# run a loop to remove any dupes |
148 |
$i = 0; |
149 |
foreach my $bib (@bibs_stage1) { |
150 |
|
151 |
# check for dups before adding |
152 |
my $dupe = 0; |
153 |
foreach my $b (@bibs_stage2) { |
154 |
if ( $bib->{isbn} eq $b->{isbn} ) { |
155 |
$dupe = 1; |
156 |
$dupes++; |
157 |
last; |
158 |
} |
159 |
} |
160 |
next if $dupe == 1; |
161 |
push @bibs_stage2, $bib; |
162 |
last if ++$i == $stage2_limit; |
163 |
} # end while |
164 |
|
165 |
#randomise our bibs |
166 |
@bibs_stage2 = shuffle @bibs_stage2; |
167 |
|
168 |
# loop, until we get enough cover images |
169 |
# add each amazon lookup attempt to the carousel db table, for future caching |
170 |
|
171 |
for my $bib (@bibs_stage2) { |
172 |
|
173 |
# check for an existing ISBN row, in caro table |
174 |
my $caro_rs = |
175 |
Koha::Database->new()->schema->resultset('Carousel') |
176 |
->search( { 'isbn' => { '=', $bib->{'isbn'} }, }, { rows => '1' } ); |
177 |
|
178 |
my $cnt = $caro_rs->count; |
179 |
|
180 |
my $rs = $caro_rs->next; |
181 |
my $amz_miss; |
182 |
my $image_url; |
183 |
|
184 |
# if not db match, do amazon lookup.... |
185 |
if ( $cnt == 0 ) { |
186 |
|
187 |
# no row in table, so fetch from amazon |
188 |
$image_url = |
189 |
'http://images.amazon.com/images/P/' |
190 |
. $bib->{isbn} |
191 |
. '.01._THUMBZZZ.jpg'; |
192 |
|
193 |
my $req = HTTP::Request->new( 'GET', $image_url ); |
194 |
my $res = $ua->request($req); |
195 |
my $res_size = $res->headers->{'content-length'}; |
196 |
|
197 |
$fetches++; |
198 |
|
199 |
# warn $res_size; |
200 |
# 43 bytes returned, means lookup was a miss :'( |
201 |
$amz_miss = 1 if ( $res_size == 43 ); |
202 |
|
203 |
# insert lookup result into table |
204 |
$caro_rs->create( |
205 |
{ |
206 |
isbn => $bib->{'isbn'}, |
207 |
image_url => ( $amz_miss ? undef : $image_url ) |
208 |
} |
209 |
); |
210 |
|
211 |
if ($amz_miss) { |
212 |
$amz_miss_cnt++; |
213 |
next; |
214 |
} |
215 |
else { |
216 |
# else, got a match from amazon |
217 |
$amz_hit_cnt++; |
218 |
} |
219 |
|
220 |
} # end |
221 |
# else, got result from db.. |
222 |
else { |
223 |
|
224 |
# but no image, so skip :/ |
225 |
unless ( $rs->get_column('image_url') ) { |
226 |
$cache_misses++; |
227 |
next; |
228 |
} |
229 |
else { |
230 |
# db result has image, so use ;) |
231 |
$image_url = $rs->get_column('image_url'); |
232 |
$cache_hits++; |
233 |
} |
234 |
} # end for |
235 |
|
236 |
# remove glitchy trailing chars from title/author for display /:, |
237 |
foreach ( $bib->{title}, $bib->{author} ) { |
238 |
next unless $_; # shush warns |
239 |
s|/$||; |
240 |
s|:$||; |
241 |
s|,$||; |
242 |
s/[ \t]+$//g; |
243 |
} |
244 |
|
245 |
$bib->{image_url} = $image_url; |
246 |
$bib->{id} = $hits; |
247 |
|
248 |
push @bibs_stage3, $bib; |
249 |
|
250 |
$hits++; |
251 |
last if $hits == $stage3_limit; |
252 |
} |
253 |
|
254 |
# if debug, print some extra stats |
255 |
if ( $ENV{DEBUG} ) { |
256 |
$tt1 = [gettimeofday]; |
257 |
carp 'carousel: load time ' . tv_interval( $tt0, $tt1 ); |
258 |
carp "carousel: db_hits: $cache_hits, amz_fetches:$fetches"; |
259 |
carp |
260 |
"amz_hit:$amz_hit_cnt, amz_miss:$amz_miss_cnt, dupes:$dupes, hits: $hits, misses: $cache_misses, needs: $stage3_limit"; |
261 |
} |
262 |
|
263 |
return \@bibs_stage3; |
264 |
} |
265 |
|
266 |
=head1 EXPORT |
267 |
|
268 |
None by default. |
269 |
|
270 |
=head1 AUTHOR |
271 |
|
272 |
Mason James, E<lt>mason@calyx.net.auE<gt> |
273 |
|
274 |
=cut |
275 |
|
276 |
1; |
277 |
|
278 |
__END__ |