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

(-)a/misc/maintenance/erm/ebsco/populate_from_holdingsiq.pl (-267 lines)
Lines 1-266 Link Here
1
use Modern::Perl;
2
3
use Getopt::Long qw( GetOptions );
4
use Pod::Usage qw( pod2usage );
5
6
use JSON qw( from_json to_json );
7
use HTTP::Request;
8
use LWP::UserAgent;
9
10
use Koha::Acquisition::Booksellers;
11
use Koha::ERM::EHoldings::Packages;
12
use Koha::ERM::EHoldings::Titles;
13
use Koha::ERM::EHoldings::Resources;
14
15
our ( $custid, $api_key );
16
my( $help, $verbose );
17
GetOptions(
18
    'help|h' => \$help,
19
    'verbose|v' => \$verbose,
20
    'custid:s' => \$custid,
21
    'api-key:s' => \$api_key,
22
) || podusage(1);
23
24
pod2usage(1) if $help;
25
pod2usage("Parameters 'custid' and 'api-key' are mandatory") unless $custid && $api_key;
26
27
#my $status = get_status();
28
#say "Status of the snapshot: $status";
29
#if ( $status ne 'Completed' && $status ne 'In Progress' ) {
30
#    say "Populate holdings...";
31
#    populate_holdings();
32
#}
33
#
34
#while ($status ne 'Completed') {
35
#    sleep(60);
36
#    $status = get_status();
37
#    say "Status: " . $status;
38
#}
39
40
sub get_status {
41
    my $response= request(GET => '/holdings/status');
42
    my $result = from_json( $response->decoded_content );
43
    return $result->{status};
44
}
45
46
sub populate_holdings {
47
    my $response = request( POST => '/holdings' );
48
    if ( $response->code != 202 && $response->code != 409 ) {
49
        my $result = from_json( $response->decoded_content );
50
        die sprintf "ERROR - code %s: %s\n", $response->code,
51
          $result->{message};
52
    }
53
}
54
55
sub request {
56
    my ( $method, $url ) = @_;
57
58
    my $base_url = 'https://api.ebsco.io/rm/rmaccounts/' . $custid;
59
    my $request = HTTP::Request->new( $method => $base_url . $url);
60
    $request->header( 'x-api-key' => $api_key );
61
    my $ua = LWP::UserAgent->new;
62
    return $ua->simple_request($request);
63
}
64
65
my @lines;
66
while(my $line = <DATA>){chomp $line;push @lines, $line}
67
my $json = join "", @lines;
68
my $result = from_json($json);
69
for my $h ( @{$result->{holdings}} ) {
70
    say $h->{publication_title};
71
    my $vendor = Koha::Acquisition::Booksellers->search({name => $h->{vendor_name}});
72
    if ($vendor->count) {
73
        $vendor = $vendor->next;
74
        if ( !$vendor->external_id ) {
75
            $vendor->external_id( $h->{vendor_id} )->store;
76
        }
77
        elsif ( $vendor->external_id != $h->{vendor_id} ) {
78
            die "Cannot update external vendor id for " . $h->{vendor_name};
79
        }
80
    }
81
    else {
82
        $vendor = Koha::Acquisition::Bookseller->new(
83
            { name => $h->{vendor_name}, external_id => $h->{vendor_id} } )
84
          ->store;
85
    }
86
87
    my $package =
88
      Koha::ERM::EHoldings::Packages->search( { name => $h->{package_name} } );
89
    if ($package->count) {
90
        $package = $package->next;
91
        if ( !$package->external_id ) {
92
            $package->external_id( $h->{package_id} );
93
        }
94
        elsif ( $package->external_id != $h->{package_id} ) {
95
            die "Cannot update external package id for " . $h->{package_name};
96
        }
97
98
    }
99
    else {
100
        $package = Koha::ERM::EHoldings::Package->new(
101
            { name => $h->{package_name}, external_id => $h->{package_id} } );
102
    }
103
    $package->vendor_id($vendor->id);
104
    $package->store;
105
106
    my $title = Koha::ERM::EHoldings::Titles->search(
107
        { publication_title => $h->{publication_title} } );
108
    if ($title->count) {
109
        $title = $title->next;
110
        if ( !$title->external_id ) {
111
            $title->external_id( $h->{title_id} )->store;
112
        }
113
        elsif ( $title->external_id != $h->{title_id} ) {
114
            die "Cannot update external title id for "
115
              . $h->{publication_title};
116
        }
117
    }
118
    else {
119
        $title = Koha::ERM::EHoldings::Title->new(
120
            {
121
                publication_title => $h->{publication_title},
122
                external_id       => $h->{title_id}
123
            }
124
        )->store;
125
    }
126
127
    $title->update(
128
        {
129
            print_identifier        => $h->{print_identifier},
130
            online_identifier       => $h->{online_identifier},
131
            date_first_issue_online => $h->{date_first_issue_online},
132
            num_first_vol_online    => $h->{num_first_vol_online},
133
            num_first_issue_online  => $h->{num_first_issue_online},
134
            date_last_issue_online  => $h->{date_last_issue_online},
135
            num_last_vol_online     => $h->{num_last_vol_online},
136
            num_last_issue_online   => $h->{num_last_issue_online},
137
            title_url               => $h->{title_url},
138
            first_author            => $h->{first_author},
139
            title_id                => $h->{title_id},
140
            embargo_info            => $h->{embargo_info},
141
            coverage_depth          => $h->{coverage_depth},
142
            notes                   => $h->{notes},
143
            publisher_name          => $h->{publisher_name},
144
            publication_type        => $h->{publication_type},
145
            date_monograph_published_print =>
146
              $h->{date_monograph_published_print},
147
            date_monograph_published_online =>
148
              $h->{date_monograph_published_online},
149
            monograph_volume            => $h->{monograph_volume},
150
            monograph_edition           => $h->{monograph_edition},
151
            first_editor                => $h->{first_editor},
152
            parent_publication_title_id => $h->{parent_publication_title_id},
153
            preceeding_publication_title_id =>
154
              $h->{preceeding_publication_title_id},
155
            access_type => $h->{access_type},
156
157
            # "resource_type": "Database" ?
158
        }
159
    );
160
161
    Koha::ERM::EHoldings::Resource->new({title_id => $title->title_id, package_id => $package->package_id})->store;
162
}
163
164
__DATA__
165
{
166
  "offset": 1,
167
  "format": "kbart2",
168
  "holdings": [
169
    {
170
      "publication_title": "PsycFIRST",
171
      "print_identifier": "",
172
      "online_identifier": "",
173
      "date_first_issue_online": "",
174
      "num_first_vol_online": "",
175
      "num_first_issue_online": "",
176
      "date_last_issue_online": "",
177
      "num_last_vol_online": "",
178
      "num_last_issue_online": "",
179
      "title_url": "http://firstsearch.oclc.org/FSIP?db=PsycFIRST",
180
      "first_author": "",
181
      "title_id": "482857",
182
      "embargo_info": "",
183
      "coverage_depth": "abstract",
184
      "notes": "",
185
      "publisher_name": "Unspecified",
186
      "publication_type": "monograph",
187
      "date_monograph_published_print": "2000",
188
      "date_monograph_published_online": "2000",
189
      "monograph_volume": "",
190
      "monograph_edition": "",
191
      "first_editor": "",
192
      "parent_publication_title_id": "",
193
      "preceeding_publication_title_id": "",
194
      "access_type": "P",
195
      "package_name": "PsycFIRST",
196
      "package_id": "2976",
197
      "vendor_name": "OCLC",
198
      "vendor_id": "21",
199
      "resource_type": "Database"
200
    },
201
    {
202
      "publication_title": "PsycFIRST",
203
      "print_identifier": "",
204
      "online_identifier": "",
205
      "date_first_issue_online": "",
206
      "num_first_vol_online": "",
207
      "num_first_issue_online": "",
208
      "date_last_issue_online": "",
209
      "num_last_vol_online": "",
210
      "num_last_issue_online": "",
211
      "title_url": "http://firstsearch.oclc.org/FSIP?db=PsycFIRST",
212
      "first_author": "",
213
      "title_id": "482857",
214
      "embargo_info": "",
215
      "coverage_depth": "abstract",
216
      "notes": "",
217
      "publisher_name": "Unspecified",
218
      "publication_type": "monograph",
219
      "date_monograph_published_print": "2001",
220
      "date_monograph_published_online": "2001",
221
      "monograph_volume": "",
222
      "monograph_edition": "",
223
      "first_editor": "",
224
      "parent_publication_title_id": "",
225
      "preceeding_publication_title_id": "",
226
      "access_type": "P",
227
      "package_name": "PsycFIRST",
228
      "package_id": "2976",
229
      "vendor_name": "OCLC",
230
      "vendor_id": "21",
231
      "resource_type": "Database"
232
    },
233
    {
234
      "publication_title": "Idea Engineering: Creative Thinking and Innovation",
235
      "print_identifier": "978-1-60650-472-7",
236
      "online_identifier": "978-1-60650-473-4",
237
      "date_first_issue_online": "",
238
      "num_first_vol_online": "",
239
      "num_first_issue_online": "",
240
      "date_last_issue_online": "",
241
      "num_last_vol_online": "",
242
      "num_last_issue_online": "",
243
      "title_url": "",
244
      "first_author": "Harris, La Verne Abe.",
245
      "title_id": "2461469",
246
      "embargo_info": "",
247
      "coverage_depth": "fulltext",
248
      "notes": "",
249
      "publisher_name": "Momentum Press, LLC",
250
      "publication_type": "monograph",
251
      "date_monograph_published_print": "2014",
252
      "date_monograph_published_online": "2014",
253
      "monograph_volume": "",
254
      "monograph_edition": "",
255
      "first_editor": "",
256
      "parent_publication_title_id": "",
257
      "preceeding_publication_title_id": "",
258
      "access_type": "P",
259
      "package_name": "Ebrary Business Expert Press Digital Library 2014",
260
      "package_id": "22589",
261
      "vendor_name": "Ebrary",
262
      "vendor_id": "269",
263
      "resource_type": "Book"
264
    }
265
  ]
266
}
267
- 

Return to bug 32030