line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Catmandu::Importer::CrossRef; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
16742
|
use Catmandu::Sane; |
|
1
|
|
|
|
|
119992
|
|
|
1
|
|
|
|
|
5
|
|
4
|
1
|
|
|
1
|
|
621
|
use Catmandu::Importer::XML; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
use Furl; |
6
|
|
|
|
|
|
|
use Moo; |
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
with 'Catmandu::Importer'; |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
use constant BASE_URL => 'http://doi.crossref.org/search/doi'; |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
has base => ( is => 'ro', default => sub { return BASE_URL; } ); |
13
|
|
|
|
|
|
|
has doi => ( is => 'ro', required => 1 ); |
14
|
|
|
|
|
|
|
has usr => ( is => 'ro', required => 1 ); |
15
|
|
|
|
|
|
|
has pwd => ( is => 'ro', required => 0 ); |
16
|
|
|
|
|
|
|
has fmt => ( is => 'ro', default => sub {'unixref'} ); |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
has _api_key => ( |
19
|
|
|
|
|
|
|
is => 'lazy', |
20
|
|
|
|
|
|
|
builder => sub { |
21
|
|
|
|
|
|
|
my ($self) = @_; |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
my $key = $self->usr; |
24
|
|
|
|
|
|
|
$key .= ':' . $self->pwd if $self->pwd; |
25
|
|
|
|
|
|
|
return $key; |
26
|
|
|
|
|
|
|
} |
27
|
|
|
|
|
|
|
); |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
sub _request { |
30
|
|
|
|
|
|
|
my ( $self, $url ) = @_; |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
my $ua = Furl->new( timeout => 20 ); |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
my $res; |
35
|
|
|
|
|
|
|
try { |
36
|
|
|
|
|
|
|
$res = $ua->get($url); |
37
|
|
|
|
|
|
|
die $res->status_line unless $res->is_success; |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
return $res->content; |
40
|
|
|
|
|
|
|
} |
41
|
|
|
|
|
|
|
catch { |
42
|
|
|
|
|
|
|
Catmandu::Error->throw("Status code: $res->status_line"); |
43
|
|
|
|
|
|
|
}; |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
} |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
sub _hashify { |
48
|
|
|
|
|
|
|
my ( $self, $in ) = @_; |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
my $xml = Catmandu::Importer::XML->new( file => \$in ); |
51
|
|
|
|
|
|
|
return $xml->to_array; |
52
|
|
|
|
|
|
|
} |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
sub _api_call { |
55
|
|
|
|
|
|
|
my ($self) = @_; |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
my $url = $self->base; |
58
|
|
|
|
|
|
|
$url .= '?pid=' . $self->_api_key; |
59
|
|
|
|
|
|
|
$url .= '&doi=' . $self->doi; |
60
|
|
|
|
|
|
|
$url .= '&format=' . $self->fmt; |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
my $res = $self->_request($url); |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
return $res; |
65
|
|
|
|
|
|
|
} |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
sub _get_record { |
68
|
|
|
|
|
|
|
my ($self) = @_; |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
my $xml = $self->_api_call; |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
return $self->_hashify($xml); |
73
|
|
|
|
|
|
|
} |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
sub generator { |
76
|
|
|
|
|
|
|
my ($self) = @_; |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
return sub { |
79
|
|
|
|
|
|
|
state $stack = $self->_get_record; |
80
|
|
|
|
|
|
|
my $rec = pop @$stack; |
81
|
|
|
|
|
|
|
$rec->{doi_record}->{crossref} |
82
|
|
|
|
|
|
|
? return $rec->{doi_record}->{crossref} |
83
|
|
|
|
|
|
|
: return undef; |
84
|
|
|
|
|
|
|
}; |
85
|
|
|
|
|
|
|
} |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
1; |
88
|
|
|
|
|
|
|
__END__ |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
=head1 NAME |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
Catmandu::Importer::CrossRef - Package that imports data form CrossRef API |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
=head1 SYNOPSIS |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
use Catmandu::Importer::CrossRef; |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
my %attrs = ( |
99
|
|
|
|
|
|
|
doi => '<doi>', |
100
|
|
|
|
|
|
|
usr => '<your-crossref-username>', |
101
|
|
|
|
|
|
|
pwd => '<your-crossref-password>', |
102
|
|
|
|
|
|
|
fmt => '<xsd_xml | unixref | unixsd | info>' |
103
|
|
|
|
|
|
|
); |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
my $importer = Catmandu::Importer::CrossRef->new(%attrs); |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
my $n = $importer->each(sub { |
108
|
|
|
|
|
|
|
my $hashref = $_[0]; |
109
|
|
|
|
|
|
|
# do something here |
110
|
|
|
|
|
|
|
}); |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
=head1 DESCRIPTION |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
This L<Catmandu::Importer> imports data from the CrossRef API given a DOI. |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
=head1 CONFIGURATION |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
=over |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
=item base |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
Base url of the API. Default is to L<http://doi.crossref.org/search/doi>. |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
=item doi |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
Required. The DOI you want data about. |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
=item usr |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
Required. Your CrossRef username. Register first! |
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
=item fmt |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
The optional output format. Default is L<unixref|http://help.crossref.org/unixref-query-result-format>. |
135
|
|
|
|
|
|
|
Other possible values are L<unixsd|http://help.crossref.org/unixsd>, and |
136
|
|
|
|
|
|
|
L<xsd_xml|http://help.crossref.org/deprecated_q> (deprecated). |
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
=back |
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
=head1 SEE ALSO |
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
L<Catmandu::Importer::DOI> is an older version of this module. |
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
CrossRef also provides DOI data in RDF, which can be imported with L<Catmandu::RDF>: |
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
use Catmandu::Importer::RDF; |
147
|
|
|
|
|
|
|
my $doi = "10.2474/trol.7.147"; |
148
|
|
|
|
|
|
|
my $url = "http://dx.doi.org/$doi"; |
149
|
|
|
|
|
|
|
my $rdf = Catmandu::Importer::RDF->new( url => $url )->first; |
150
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
=cut |