View | Details | Raw Unified | Return to bug 18514
Collapse All | Expand All

(-)a/misc/cronjobs/overdrive_get_collection.pl (-1 / +163 lines)
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::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 = ['move_field(id,Local-number)','move_field(_id,es_id)'];
48
49
50
# create an indexer object
51
my $indexer = Koha::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
                          });
100
101
if ( $results->{pages} ){
102
    foreach ( 2 .. $results->{pages} ) {
103
104
        # deal with any more results
105
        $search_params{page} = $_;
106
        my $next_results = $od_client->native_search( \%search_params );
107
        $importer =
108
          Catmandu->importer( 'JSON', file => $next_results->{items}, fix => $fixes );
109
        $indexer->store->bag->add_many($importer);
110
        $indexer->store->bag->commit;
111
    }
112
}
113
=head1 NAME
114
115
overdrive_get_collection.pl - Fetches an indexes a libraries collection from overdrive
116
117
=head1 SYNOPSIS
118
119
overdrive_get_collection.pl 
120
  [ -f ]
121
122
 Options:
123
   -h               brief help message
124
   --m              full documentation
125
   -v               verbose
126
   -f               full reindex
127
128
=head1 OPTIONS
129
130
=over 8
131
132
=item B<-h>
133
134
Prints a brief help message and exits
135
136
=item B<--man>
137
138
Prints the manual page and exits
139
140
=item B<-v>
141
142
Verbose, without this only fatal errors are reported
143
144
=item B<-f>
145
146
Full, fetches all records and reindexes
147
148
=back
149
150
=head1 DESCRIPTION
151
152
This script is designed to grab overdrive collections and create MARC records
153
and store them 
154
155
=head1 USAGE EXAMPLES
156
157
C<overdrive_get_collection.pl> - In this most basic usage, with no command arguments
158
we get the udpdated records since last time we checked, and index them
159
160
C<overdrive_get_collection.pl -f -v> - Gets the entire collection and indexes it, being
161
verbose in the process
162
163
=cut

Return to bug 18514