line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Catmandu::Importer::Blacklight; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
91471
|
use Catmandu::Sane; |
|
1
|
|
|
|
|
202383
|
|
|
1
|
|
|
|
|
7
|
|
4
|
1
|
|
|
1
|
|
288
|
use Catmandu::Util qw(:is); |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
261
|
|
5
|
1
|
|
|
1
|
|
562
|
use REST::Client; |
|
1
|
|
|
|
|
42630
|
|
|
1
|
|
|
|
|
34
|
|
6
|
1
|
|
|
1
|
|
8
|
use URI::Escape; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
53
|
|
7
|
1
|
|
|
1
|
|
458
|
use JSON::MaybeXS; |
|
1
|
|
|
|
|
1098
|
|
|
1
|
|
|
|
|
48
|
|
8
|
1
|
|
|
1
|
|
6
|
use Moo; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
9
|
|
9
|
1
|
|
|
1
|
|
422
|
use feature 'state'; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
513
|
|
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
with 'Catmandu::Importer'; |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
has url => (is => 'ro', required => 1); |
14
|
|
|
|
|
|
|
has q => (is => 'ro', required => 1); |
15
|
|
|
|
|
|
|
has client => (is => 'lazy'); |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
sub _build_client { |
18
|
0
|
|
|
0
|
|
|
my $self; |
19
|
0
|
|
|
|
|
|
REST::Client->new(); |
20
|
|
|
|
|
|
|
} |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
sub generator { |
23
|
|
|
|
|
|
|
my ($self) = @_; |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
sub { |
26
|
|
|
|
|
|
|
state $response = $self->query($self->q(),1); |
27
|
|
|
|
|
|
|
state $idx = 0; |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
unless (defined($response)) { |
30
|
|
|
|
|
|
|
print STDERR "Catmandu::Importer::Blacklight no response from: " . $self->url . "\n"; |
31
|
|
|
|
|
|
|
return undef; |
32
|
|
|
|
|
|
|
} |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
if (defined $response && ! defined $response->{docs}->[$idx]) { |
35
|
|
|
|
|
|
|
$response = $self->query($self->q(),$response->{pages}->{next_page}); |
36
|
|
|
|
|
|
|
$idx = 0; |
37
|
|
|
|
|
|
|
} |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
return unless defined($response->{docs}->[0]); |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
my $doc = $response->{docs}->[$idx]; |
42
|
|
|
|
|
|
|
my $id = $doc->{id}; |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
$idx++; |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
{ '_id' => $id , %$doc}; |
47
|
|
|
|
|
|
|
}; |
48
|
|
|
|
|
|
|
} |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
sub query { |
51
|
0
|
|
|
0
|
0
|
|
my ($self,$q,$page) = @_; |
52
|
|
|
|
|
|
|
|
53
|
0
|
0
|
0
|
|
|
|
return undef unless defined($page) && $page =~ /^\d+$/; |
54
|
|
|
|
|
|
|
|
55
|
0
|
|
|
|
|
|
my $url = sprintf "%s?q=%s&page=%d" , $self->url, uri_escape($q), $page; |
56
|
0
|
|
|
|
|
|
my $response = $self->client->GET($url, { Accept => 'application/json' }); |
57
|
|
|
|
|
|
|
|
58
|
0
|
0
|
|
|
|
|
return undef unless ($response->responseCode eq '200'); |
59
|
|
|
|
|
|
|
|
60
|
0
|
|
|
|
|
|
my $json = $response->responseContent; |
61
|
0
|
|
|
|
|
|
my $perl = decode_json($json); |
62
|
|
|
|
|
|
|
|
63
|
0
|
|
|
|
|
|
$perl->{response}; |
64
|
|
|
|
|
|
|
} |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
1; |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
__END__ |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
=head1 NAME |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
Catmandu::Importer::Blacklight - Import records from a Blacklight catalog |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
=head1 SYNOPSIS |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
# From the command line |
77
|
|
|
|
|
|
|
$ catmandu convert Blacklight --url http://lib.ugent.be/catalog -q Schopenhauer |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
# In perl |
80
|
|
|
|
|
|
|
use Catmandu::Importer::Blacklight; |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
my $importer = Catmandu::Importer::Blacklight->new( |
83
|
|
|
|
|
|
|
url => "...", |
84
|
|
|
|
|
|
|
q => "..." |
85
|
|
|
|
|
|
|
); |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
my $n = $importer->each(sub { |
88
|
|
|
|
|
|
|
my $hashref = $_[0]; |
89
|
|
|
|
|
|
|
# ... |
90
|
|
|
|
|
|
|
}); |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
=head1 CONFIGURATION |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
=over |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
=item url |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
Blacklight catalog Base URL. Required |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
=item q |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
Query string to search. Required |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
=back |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
=head1 DESCRIPTION |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
Every Catmandu::Importer is a L<Catmandu::Iterable> all its methods are |
109
|
|
|
|
|
|
|
inherited. The Catmandu::Importer::Blacklight methods are not idempotent: Blacklight |
110
|
|
|
|
|
|
|
feeds can only be read once. |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
=head1 SEE ALSO |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
L<http://projectblacklight.org/> , |
115
|
|
|
|
|
|
|
L<Catmandu::Importer> , |
116
|
|
|
|
|
|
|
L<Catmandu::Iterable> |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
=cut |