| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package Catmandu::Importer::PLoS; |
|
2
|
|
|
|
|
|
|
|
|
3
|
2
|
|
|
2
|
|
57091
|
use Catmandu::Sane; |
|
|
2
|
|
|
|
|
241080
|
|
|
|
2
|
|
|
|
|
12
|
|
|
4
|
2
|
|
|
2
|
|
527
|
use Moo; |
|
|
2
|
|
|
|
|
5
|
|
|
|
2
|
|
|
|
|
9
|
|
|
5
|
2
|
|
|
2
|
|
1483
|
use Furl; |
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
use XML::LibXML::Simple qw(XMLin); |
|
7
|
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
with 'Catmandu::Importer'; |
|
9
|
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
# INFO: |
|
12
|
|
|
|
|
|
|
# http://api.plos.org/solr/search-fields/ |
|
13
|
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
# Constants. ------------------------------------------------------------------- |
|
16
|
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
use constant BASE_URL => 'http://api.plos.org/search'; |
|
18
|
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
# Properties. ------------------------------------------------------------------ |
|
21
|
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
# required. |
|
23
|
|
|
|
|
|
|
has base => (is => 'ro', default => sub { return BASE_URL; }); |
|
24
|
|
|
|
|
|
|
has query => (is => 'ro', required => 1); |
|
25
|
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
# optional. |
|
27
|
|
|
|
|
|
|
has api_key => (is => 'ro'); |
|
28
|
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
# internal stuff. |
|
30
|
|
|
|
|
|
|
has _currentRecordSet => (is => 'ro'); |
|
31
|
|
|
|
|
|
|
has _n => (is => 'ro', default => sub { 0 }); |
|
32
|
|
|
|
|
|
|
has _numFound => (is => 'ro', default => sub { 0 }); |
|
33
|
|
|
|
|
|
|
has _start => (is => 'ro', default => sub { 0 }); |
|
34
|
|
|
|
|
|
|
has _rows => (is => 'ro', default => sub { 10 }); |
|
35
|
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
# Internal Methods. ------------------------------------------------------------ |
|
38
|
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
# Internal: HTTP GET something. |
|
40
|
|
|
|
|
|
|
# |
|
41
|
|
|
|
|
|
|
# $url - the url. |
|
42
|
|
|
|
|
|
|
# |
|
43
|
|
|
|
|
|
|
# Returns the raw response object. |
|
44
|
|
|
|
|
|
|
sub _request { |
|
45
|
|
|
|
|
|
|
my ($self, $url) = @_; |
|
46
|
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
my $furl = Furl->new( |
|
48
|
|
|
|
|
|
|
agent => 'Mozilla/5.0', |
|
49
|
|
|
|
|
|
|
timeout => 10 |
|
50
|
|
|
|
|
|
|
); |
|
51
|
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
my $res = $furl->get($url); |
|
53
|
|
|
|
|
|
|
die $res->status_line unless $res->is_success; |
|
54
|
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
return $res; |
|
56
|
|
|
|
|
|
|
} |
|
57
|
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
# Internal: Converts XML to a perl hash. |
|
59
|
|
|
|
|
|
|
# |
|
60
|
|
|
|
|
|
|
# $in - the raw XML input. |
|
61
|
|
|
|
|
|
|
# |
|
62
|
|
|
|
|
|
|
# Returns a hash representation of the given XML. |
|
63
|
|
|
|
|
|
|
sub _hashify { |
|
64
|
|
|
|
|
|
|
my ($self, $in) = @_; |
|
65
|
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
my $xs = XML::LibXML::Simple->new(); |
|
67
|
|
|
|
|
|
|
my $out = $xs->XMLin( |
|
68
|
|
|
|
|
|
|
$in, |
|
69
|
|
|
|
|
|
|
ForceArray => [ 'doc' ] |
|
70
|
|
|
|
|
|
|
); |
|
71
|
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
return $out; |
|
73
|
|
|
|
|
|
|
} |
|
74
|
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
# Internal: Make a call to the PLoS API. |
|
76
|
|
|
|
|
|
|
# |
|
77
|
|
|
|
|
|
|
# Returns the XML response body. |
|
78
|
|
|
|
|
|
|
sub _api_call { |
|
79
|
|
|
|
|
|
|
my ($self) = @_; |
|
80
|
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
# construct the url |
|
82
|
|
|
|
|
|
|
my $url = $self->base; |
|
83
|
|
|
|
|
|
|
$url .= '?q='.$self->query; |
|
84
|
|
|
|
|
|
|
$url .= '&start='.$self->{_start}; |
|
85
|
|
|
|
|
|
|
$url .= '&rows='.$self->{_rows}; |
|
86
|
|
|
|
|
|
|
$url .= '&api_key='.$self->api_key if $self->api_key; |
|
87
|
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
# http get the url. |
|
89
|
|
|
|
|
|
|
my $res = $self->_request($url); |
|
90
|
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
# return the response body. |
|
92
|
|
|
|
|
|
|
return $res->{content}; |
|
93
|
|
|
|
|
|
|
} |
|
94
|
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
# Internal: gets the next set of results. |
|
96
|
|
|
|
|
|
|
# |
|
97
|
|
|
|
|
|
|
# Returns a array representation of the resultset. |
|
98
|
|
|
|
|
|
|
sub _nextRecordSet { |
|
99
|
|
|
|
|
|
|
my ($self) = @_; |
|
100
|
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
# fetch the xml response and hashify it. |
|
102
|
|
|
|
|
|
|
my $xml = $self->_api_call(); |
|
103
|
|
|
|
|
|
|
my $hash = $self->_hashify($xml); |
|
104
|
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
# on first request, get total number of results. |
|
106
|
|
|
|
|
|
|
$self->{_numFound} = $hash->{result}->{numFound} unless $self->_numFound; |
|
107
|
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
# get to the point. |
|
109
|
|
|
|
|
|
|
my $set = $hash->{result}->{doc}; |
|
110
|
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
# return a reference to the hash. |
|
112
|
|
|
|
|
|
|
return \@{$set}; |
|
113
|
|
|
|
|
|
|
} |
|
114
|
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
# Internal: gets the next record from our current resultset. |
|
116
|
|
|
|
|
|
|
# |
|
117
|
|
|
|
|
|
|
# Returns a hash representation of the next record. |
|
118
|
|
|
|
|
|
|
sub _nextRecord { |
|
119
|
|
|
|
|
|
|
my ($self) = @_; |
|
120
|
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
# fetch recordset if we don't have one yet. |
|
122
|
|
|
|
|
|
|
$self->{_currentRecordSet} = $self->_nextRecordSet unless $self->_currentRecordSet; |
|
123
|
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
# check for a exhaused recordset. |
|
125
|
|
|
|
|
|
|
if ($self->{_n} >= $self->_rows) { |
|
126
|
|
|
|
|
|
|
$self->{_currentRecordSet} = $self->_nextRecordSet; |
|
127
|
|
|
|
|
|
|
$self->{_start} += $self->_rows; |
|
128
|
|
|
|
|
|
|
$self->{_n} = 0; |
|
129
|
|
|
|
|
|
|
} |
|
130
|
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
# return the next record. |
|
132
|
|
|
|
|
|
|
return $self->_currentRecordSet->[$self->{_n}++]; |
|
133
|
|
|
|
|
|
|
} |
|
134
|
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
# Public Methods. -------------------------------------------------------------- |
|
137
|
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
sub generator { |
|
139
|
|
|
|
|
|
|
my ($self) = @_; |
|
140
|
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
return sub { |
|
142
|
|
|
|
|
|
|
$self->_nextRecord; |
|
143
|
|
|
|
|
|
|
}; |
|
144
|
|
|
|
|
|
|
} |
|
145
|
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
# PerlDoc. --------------------------------------------------------------------- |
|
148
|
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
=head1 NAME |
|
150
|
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
Catmandu::Importer::PLoS - Package that imports PLoS data. |
|
152
|
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
=head1 SYNOPSIS |
|
154
|
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
use Catmandu::Importer::PLoS; |
|
156
|
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
my %attrs = ( |
|
158
|
|
|
|
|
|
|
query => 'github', |
|
159
|
|
|
|
|
|
|
api_key => '' |
|
160
|
|
|
|
|
|
|
); |
|
161
|
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
my $importer = Catmandu::Importer::PLoS->new(%attrs); |
|
163
|
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
my $n = $importer->each(sub { |
|
165
|
|
|
|
|
|
|
my $hashref = $_[0]; |
|
166
|
|
|
|
|
|
|
# ... |
|
167
|
|
|
|
|
|
|
}); |
|
168
|
|
|
|
|
|
|
|
|
169
|
|
|
|
|
|
|
=cut |
|
170
|
|
|
|
|
|
|
|
|
171
|
|
|
|
|
|
|
=head1 SEE ALSO |
|
172
|
|
|
|
|
|
|
|
|
173
|
|
|
|
|
|
|
L |
|
174
|
|
|
|
|
|
|
|
|
175
|
|
|
|
|
|
|
=cut |
|
176
|
|
|
|
|
|
|
|
|
177
|
|
|
|
|
|
|
1; |