Line 0
Link Here
|
0 |
- |
1 |
#!/usr/bin/perl |
|
|
2 |
|
3 |
# This file is part of Koha. |
4 |
# |
5 |
# Koha is free software; you can redistribute it and/or modify it |
6 |
# under the terms of the GNU General Public License as published by |
7 |
# the Free Software Foundation; either version 3 of the License, or |
8 |
# (at your option) any later version. |
9 |
# |
10 |
# Koha is distributed in the hope that it will be useful, but |
11 |
# WITHOUT ANY WARRANTY; without even the implied warranty of |
12 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13 |
# GNU General Public License for more details. |
14 |
# |
15 |
# You should have received a copy of the GNU General Public License |
16 |
# along with Koha; if not, see <http://www.gnu.org/licenses>. |
17 |
|
18 |
use Modern::Perl; |
19 |
use WebService::ILS::OverDrive::Library; |
20 |
use Getopt::Long; |
21 |
use Pod::Usage; |
22 |
use JSON; |
23 |
|
24 |
my $help; |
25 |
my $man; |
26 |
my $verbose; |
27 |
my $full; |
28 |
|
29 |
GetOptions( |
30 |
'h' => \$help, |
31 |
'man' => \$man, |
32 |
'v' => \$verbose, |
33 |
'f' => \$full, |
34 |
) or pod2usage(2); |
35 |
pod2usage(1) if $help; |
36 |
pod2usage( -verbose => 2 ) if $man; |
37 |
|
38 |
use Koha::SearchEngine; |
39 |
use Koha::SearchEngine::ElasticSearch::Indexer; |
40 |
use C4::Context; |
41 |
|
42 |
use Data::Dumper; |
43 |
|
44 |
|
45 |
# We should put this in config eventually, but it is dependent on the Overdrive API |
46 |
# |
47 |
my $fixes = ['copy_field(id,Local-number)','move_field(_id,es_id)']; |
48 |
|
49 |
|
50 |
# create an indexer object |
51 |
my $indexer = Koha::SearchEngine::ElasticSearch::Indexer->new( |
52 |
{ index => $Koha::SearchEngine::BIBLIOS_INDEX } ); |
53 |
|
54 |
unless ( $indexer->store ) { |
55 |
my $params = $indexer->get_elasticsearch_params(); |
56 |
$indexer->store( |
57 |
Catmandu::Store::ElasticSearch->new( |
58 |
%$params, |
59 |
index_settings => $indexer->get_elasticsearch_settings(), |
60 |
index_mappings => $indexer->get_elasticsearch_mappings(), |
61 |
) |
62 |
); |
63 |
} |
64 |
|
65 |
my $client_id = C4::Context->preference('OverDriveClientKey'); |
66 |
my $client_secret = C4::Context->preference('OverDriveClientSecret'); |
67 |
my $library_id = C4::Context->preference('OverDriveLibraryID'); |
68 |
my $od_client = WebService::ILS::OverDrive::Library->new( |
69 |
{ |
70 |
test => 1, |
71 |
client_id => $client_id, |
72 |
client_secret => $client_secret, |
73 |
library_id => $library_id |
74 |
} |
75 |
); |
76 |
|
77 |
my $results; |
78 |
my %search_params; |
79 |
|
80 |
|
81 |
unless ($full) { |
82 |
$search_params{'lastTitleUpdateTime='} = ''; |
83 |
} |
84 |
|
85 |
$search_params{'limit'} = 20; |
86 |
|
87 |
$results = $od_client->native_search( \%search_params ); |
88 |
|
89 |
# set up our importer to import in the JSON files |
90 |
# Overdrive paginates, so we need to deal with that |
91 |
|
92 |
# import and index the first page of results |
93 |
my $json = to_json($results->{products} ); |
94 |
my $importer = |
95 |
Catmandu->importer( 'JSON', file => \$json, fix => $fixes ); |
96 |
$importer->each(sub { |
97 |
my $item = shift; |
98 |
my $stored = $indexer->store->bag->add($item); |
99 |
$indexer->store->bag->commit; |
100 |
}); |
101 |
|
102 |
if ( $results->{pages} ){ |
103 |
foreach ( 2 .. $results->{pages} ) { |
104 |
|
105 |
# deal with any more results |
106 |
$search_params{page} = $_; |
107 |
my $next_results = $od_client->native_search( \%search_params ); |
108 |
$importer = |
109 |
Catmandu->importer( 'JSON', file => $next_results->{items}, fix => $fixes ); |
110 |
$indexer->store->bag->add_many($importer); |
111 |
$indexer->store->bag->commit; |
112 |
} |
113 |
} |
114 |
=head1 NAME |
115 |
|
116 |
overdrive_get_collection.pl - Fetches an indexes a libraries collection from overdrive |
117 |
|
118 |
=head1 SYNOPSIS |
119 |
|
120 |
overdrive_get_collection.pl |
121 |
[ -f ] |
122 |
|
123 |
Options: |
124 |
-h brief help message |
125 |
--m full documentation |
126 |
-v verbose |
127 |
-f full reindex |
128 |
|
129 |
=head1 OPTIONS |
130 |
|
131 |
=over 8 |
132 |
|
133 |
=item B<-h> |
134 |
|
135 |
Prints a brief help message and exits |
136 |
|
137 |
=item B<--man> |
138 |
|
139 |
Prints the manual page and exits |
140 |
|
141 |
=item B<-v> |
142 |
|
143 |
Verbose, without this only fatal errors are reported |
144 |
|
145 |
=item B<-f> |
146 |
|
147 |
Full, fetches all records and reindexes |
148 |
|
149 |
=back |
150 |
|
151 |
=head1 DESCRIPTION |
152 |
|
153 |
This script is designed to grab overdrive collections and create MARC records |
154 |
and store them |
155 |
|
156 |
=head1 USAGE EXAMPLES |
157 |
|
158 |
C<overdrive_get_collection.pl> - In this most basic usage, with no command arguments |
159 |
we get the udpdated records since last time we checked, and index them |
160 |
|
161 |
C<overdrive_get_collection.pl -f -v> - Gets the entire collection and indexes it, being |
162 |
verbose in the process |
163 |
|
164 |
=cut |