line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Catmandu::Importer::Inspire; |
2
|
|
|
|
|
|
|
|
3
|
2
|
|
|
2
|
|
63923
|
use Catmandu::Sane; |
|
2
|
|
|
|
|
280038
|
|
|
2
|
|
|
|
|
15
|
|
4
|
2
|
|
|
2
|
|
1652
|
use Catmandu::Importer::XML; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
use Furl; |
6
|
|
|
|
|
|
|
use Moo; |
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
with 'Catmandu::Importer'; |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
use constant BASE_URL => 'http://inspirehep.net/'; |
11
|
|
|
|
|
|
|
use constant DEFAULT_FORMAT => 'endnote'; |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
has base => ( is => 'ro', default => sub { return BASE_URL; } ); |
14
|
|
|
|
|
|
|
has fmt => ( is => 'ro', default => sub { return DEFAULT_FORMAT; } ); |
15
|
|
|
|
|
|
|
has doi => ( is => 'ro' ); |
16
|
|
|
|
|
|
|
has query => ( is => 'ro' ); |
17
|
|
|
|
|
|
|
has id => ( is => 'ro' ); |
18
|
|
|
|
|
|
|
has limit => ( is => 'ro', default => sub { return 25; } ); |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
my %FORMAT_MAPPING = ( |
21
|
|
|
|
|
|
|
'endnote' => 'xe', |
22
|
|
|
|
|
|
|
'nlm' => 'xn', |
23
|
|
|
|
|
|
|
'marc' => 'xm', |
24
|
|
|
|
|
|
|
'dc' => 'xd', |
25
|
|
|
|
|
|
|
); |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
my %PATH_MAPPING = ( |
28
|
|
|
|
|
|
|
'endnote' => 'record', |
29
|
|
|
|
|
|
|
'nlm' => 'article', |
30
|
|
|
|
|
|
|
'marc' => 'record', |
31
|
|
|
|
|
|
|
'dc' => 'dc:dc', |
32
|
|
|
|
|
|
|
); |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
sub BUILD { |
35
|
|
|
|
|
|
|
my $self = shift; |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
Catmandu::BadVal->throw("Either ID or DOI or a QUERY is required.") |
38
|
|
|
|
|
|
|
unless $self->id || $self->doi || $self->query; |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
Catmandu::BadVal->throw( |
41
|
|
|
|
|
|
|
"Format '$self->fmt' is not allowed. Possible choices are endnote, nlm, marc, dc." |
42
|
|
|
|
|
|
|
) unless exists $FORMAT_MAPPING{ $self->fmt }; |
43
|
|
|
|
|
|
|
} |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
sub _request { |
46
|
|
|
|
|
|
|
my ( $self, $url ) = @_; |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
my $furl = Furl->new( |
49
|
|
|
|
|
|
|
agent => 'Mozilla/5.0', |
50
|
|
|
|
|
|
|
timeout => 10, |
51
|
|
|
|
|
|
|
); |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
my $res = $furl->get($url); |
54
|
|
|
|
|
|
|
die $res->status_line unless $res->is_success; |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
return $res; |
57
|
|
|
|
|
|
|
} |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
sub _parse { |
60
|
|
|
|
|
|
|
my ( $self, $in ) = @_; |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
my $path = $PATH_MAPPING{ $self->fmt }; |
63
|
|
|
|
|
|
|
my $xml = Catmandu::Importer::XML->new( file => \$in, path => $path ); |
64
|
|
|
|
|
|
|
return $xml->to_array; |
65
|
|
|
|
|
|
|
} |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
sub _call { |
68
|
|
|
|
|
|
|
my ($self) = @_; |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
my $url = $self->base; |
71
|
|
|
|
|
|
|
my $fmt = $FORMAT_MAPPING{ $self->fmt }; |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
if ( $self->id ) { |
74
|
|
|
|
|
|
|
$url .= 'record/' . $self->id . '/export/' . $fmt; |
75
|
|
|
|
|
|
|
} |
76
|
|
|
|
|
|
|
else { |
77
|
|
|
|
|
|
|
$url .= 'search?p='; |
78
|
|
|
|
|
|
|
( $self->doi ) |
79
|
|
|
|
|
|
|
? ( $url .= 'doi%3A' . $self->doi ) |
80
|
|
|
|
|
|
|
: ( $url .= $self->query ); |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
$url .= '&of=' . $fmt; |
83
|
|
|
|
|
|
|
$url .= '&rg=' . $self->limit; |
84
|
|
|
|
|
|
|
$url .= '&action_search=Suchen'; |
85
|
|
|
|
|
|
|
} |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
my $res = $self->_request($url); |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
return $res->{content}; |
90
|
|
|
|
|
|
|
} |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
sub _get_record { |
93
|
|
|
|
|
|
|
my ($self) = @_; |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
my $xml = $self->_call; |
96
|
|
|
|
|
|
|
my $stack = $self->_parse($xml); |
97
|
|
|
|
|
|
|
return $stack; |
98
|
|
|
|
|
|
|
} |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
sub generator { |
101
|
|
|
|
|
|
|
my ($self) = @_; |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
return sub { |
104
|
|
|
|
|
|
|
state $stack = $self->_get_record; |
105
|
|
|
|
|
|
|
return pop @$stack; |
106
|
|
|
|
|
|
|
}; |
107
|
|
|
|
|
|
|
} |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
1; |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
=head1 NAME |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
Catmandu::Importer::Inspire - Package that imports Inspire data http://inspirehep.net/. |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
=head1 SYNOPSIS |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
use Catmandu::Importer::Inspire; |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
my %attrs = ( |
120
|
|
|
|
|
|
|
id => '1203476', |
121
|
|
|
|
|
|
|
fmt => 'endnote', |
122
|
|
|
|
|
|
|
); |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
my $importer = Catmandu::Importer::Inspire->new(%attrs); |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
my $n = $importer->each(sub { |
127
|
|
|
|
|
|
|
my $hashref = $_[0]; |
128
|
|
|
|
|
|
|
# ... |
129
|
|
|
|
|
|
|
}); |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
=head1 CONFIGURATION |
132
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
=over |
134
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
=item id |
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
Retrieve record by its Inspire ID. |
138
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
=item doi |
140
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
Retrieve record by its DOI from Inspire database. |
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
=item query |
144
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
Get results by an arbitrary query. |
146
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
=item fmt |
148
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
Specify the format to be delivered. Default is to 'endnote'. Other formats are 'nlm', 'marc' and 'dc'. |
150
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
=item limit |
152
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
Maximum number of records. Default is to 25. |
154
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
=back |
156
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
=head1 SEE ALSO |
158
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
L, L, L |
160
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
=cut |