| 1 | package Catmandu::Importer::Inspirescip-perl cpan Catmandu-Inspire 0.300 Catmandu/Importer/Inspire/; |
| 2 | |
| 3 | use Catmandu::Sanescip-perl cpan Catmandu-Inspire 0.300 Catmandu/Sane/; |
| 4 | use Catmandu::Importer::XMLscip-perl cpan Catmandu-Inspire 0.300 Catmandu/Importer/XML/; |
| 5 | use Furlscip-perl cpan Catmandu-Inspire 0.300 Furl/; |
| 6 | use Mooscip-perl cpan Catmandu-Inspire 0.300 Moo/; |
| 7 | |
| 8 | withscip-perl cpan Catmandu-Inspire 0.300 Catmandu/Importer/Inspire/with(). 'Catmandu::Importer'; |
| 9 | |
| 10 | use constantscip-perl cpan Catmandu-Inspire 0.300 constant/ BASE_URLscip-perl cpan Catmandu-Inspire 0.300 Catmandu/Importer/Inspire/BASE_URL(). => 'https://inspirehep.net/'; |
| 11 | use constantscip-perl cpan Catmandu-Inspire 0.300 constant/ DEFAULT_FORMATscip-perl cpan Catmandu-Inspire 0.300 Catmandu/Importer/Inspire/DEFAULT_FORMAT(). => 'endnote'; |
| 12 | |
| 13 | hasscip-perl cpan Catmandu-Inspire 0.300 Catmandu/Importer/Inspire/has(). base => ( is => 'ro', default => sub { return BASE_URL; } ); |
| 14 | hasscip-perl cpan Catmandu-Inspire 0.300 Catmandu/Importer/Inspire/has(). fmt => ( is => 'ro', default => sub { return DEFAULT_FORMAT; } ); |
| 15 | hasscip-perl cpan Catmandu-Inspire 0.300 Catmandu/Importer/Inspire/has(). doi => ( is => 'ro' ); |
| 16 | hasscip-perl cpan Catmandu-Inspire 0.300 Catmandu/Importer/Inspire/has(). query => ( is => 'ro' ); |
| 17 | hasscip-perl cpan Catmandu-Inspire 0.300 Catmandu/Importer/Inspire/has(). id => ( is => 'ro' ); |
| 18 | hasscip-perl cpan Catmandu-Inspire 0.300 Catmandu/Importer/Inspire/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 BUILDscip-perl cpan Catmandu-Inspire 0.300 Catmandu/Importer/Inspire/BUILD(). { |
| 35 | my $self = shiftscip-perl cpan Catmandu-Inspire 0.300 Catmandu/Importer/Inspire/shift().; |
| 36 | |
| 37 | Catmandu::BadVal->throwscip-perl cpan Catmandu-Inspire 0.300 Catmandu/BadVal/throw().("Either ID or DOI or a QUERY is required.") |
| 38 | unless $self->idscip-perl cpan Catmandu-Inspire 0.300 Catmandu/Importer/Inspire/id(). || $self->doiscip-perl cpan Catmandu-Inspire 0.300 Catmandu/Importer/Inspire/doi(). || $self->queryscip-perl cpan Catmandu-Inspire 0.300 Catmandu/Importer/Inspire/query().; |
| 39 | |
| 40 | Catmandu::BadVal->throwscip-perl cpan Catmandu-Inspire 0.300 Catmandu/BadVal/throw().( |
| 41 | "Format '$self->fmt' is not allowed. Possible choices are endnote, nlm, marc, dc." |
| 42 | ) unless existsscip-perl cpan Catmandu-Inspire 0.300 Catmandu/Importer/Inspire/exists(). $FORMAT_MAPPING{ $self->fmtscip-perl cpan Catmandu-Inspire 0.300 Catmandu/Importer/Inspire/fmt(). }; |
| 43 | } |
| 44 | |
| 45 | sub _requestscip-perl cpan Catmandu-Inspire 0.300 Catmandu/Importer/Inspire/_request(). { |
| 46 | my ( $self, $url ) = @_scip-perl cpan Catmandu-Inspire 0.300 Catmandu/Importer/Inspire/`@_`.; |
| 47 | |
| 48 | my $furl = Furl->newscip-perl cpan Catmandu-Inspire 0.300 Furl/new().( |
| 49 | agent => 'Mozilla/5.0', |
| 50 | timeout => 10, |
| 51 | ); |
| 52 | |
| 53 | my $res = $furl->getscip-perl cpan Catmandu-Inspire 0.300 Furl/get().($url); |
| 54 | diescip-perl cpan Catmandu-Inspire 0.300 Catmandu/Importer/Inspire/die(). $res->status_linescip-perl cpan Catmandu-Inspire 0.300 Catmandu/Importer/Inspire/status_line(). unless $res->is_successscip-perl cpan Catmandu-Inspire 0.300 Catmandu/Importer/Inspire/is_success().; |
| 55 | |
| 56 | return $res; |
| 57 | } |
| 58 | |
| 59 | sub _parsescip-perl cpan Catmandu-Inspire 0.300 Catmandu/Importer/Inspire/_parse(). { |
| 60 | my ( $self, $in ) = @_scip-perl cpan Catmandu-Inspire 0.300 Catmandu/Importer/Inspire/`@_`.; |
| 61 | |
| 62 | my $path = $PATH_MAPPING{ $self->fmtscip-perl cpan Catmandu-Inspire 0.300 Catmandu/Importer/Inspire/fmt(). }; |
| 63 | my $xml = Catmandu::Importer::XML->newscip-perl cpan Catmandu-Inspire 0.300 Catmandu/Importer/XML/new().( file => \$in, path => $path ); |
| 64 | return $xml->to_arrayscip-perl cpan Catmandu-Inspire 0.300 Catmandu/Importer/XML/to_array().; |
| 65 | } |
| 66 | |
| 67 | sub _callscip-perl cpan Catmandu-Inspire 0.300 Catmandu/Importer/Inspire/_call(). { |
| 68 | my ($self) = @_scip-perl cpan Catmandu-Inspire 0.300 Catmandu/Importer/Inspire/`@_`.; |
| 69 | |
| 70 | my $url = $self->basescip-perl cpan Catmandu-Inspire 0.300 Catmandu/Importer/Inspire/base().; |
| 71 | my $fmt = $FORMAT_MAPPING{ $self->fmtscip-perl cpan Catmandu-Inspire 0.300 Catmandu/Importer/Inspire/fmt(). }; |
| 72 | |
| 73 | if ( $self->idscip-perl cpan Catmandu-Inspire 0.300 Catmandu/Importer/Inspire/id(). ) { |
| 74 | $url .= 'record/' . $self->idscip-perl cpan Catmandu-Inspire 0.300 Catmandu/Importer/Inspire/id(). . '/export/' . $fmt; |
| 75 | } |
| 76 | else { |
| 77 | $url .= 'search?p='; |
| 78 | ( $self->doiscip-perl cpan Catmandu-Inspire 0.300 Catmandu/Importer/Inspire/doi(). ) |
| 79 | ? ( $url .= 'doi%3A' . $self->doiscip-perl cpan Catmandu-Inspire 0.300 Catmandu/Importer/Inspire/doi(). ) |
| 80 | : ( $url .= $self->queryscip-perl cpan Catmandu-Inspire 0.300 Catmandu/Importer/Inspire/query(). ); |
| 81 | |
| 82 | $url .= '&of=' . $fmt; |
| 83 | $url .= '&rg=' . $self->limitscip-perl cpan Catmandu-Inspire 0.300 Catmandu/Importer/Inspire/limit().; |
| 84 | $url .= '&action_search=Suchen'; |
| 85 | } |
| 86 | |
| 87 | my $res = $self->_requestscip-perl cpan Catmandu-Inspire 0.300 Catmandu/Importer/Inspire/_request().($url); |
| 88 | |
| 89 | return $res->{content}; |
| 90 | } |
| 91 | |
| 92 | sub _get_recordscip-perl cpan Catmandu-Inspire 0.300 Catmandu/Importer/Inspire/_get_record(). { |
| 93 | my ($self) = @_scip-perl cpan Catmandu-Inspire 0.300 Catmandu/Importer/Inspire/`@_`.; |
| 94 | |
| 95 | my $xml = $self->_callscip-perl cpan Catmandu-Inspire 0.300 Catmandu/Importer/Inspire/_call().; |
| 96 | my $stack = $self->_parsescip-perl cpan Catmandu-Inspire 0.300 Catmandu/Importer/Inspire/_parse().($xml); |
| 97 | return $stack; |
| 98 | } |
| 99 | |
| 100 | sub generatorscip-perl cpan Catmandu-Inspire 0.300 Catmandu/Importer/Inspire/generator(). { |
| 101 | my ($self) = @_scip-perl cpan Catmandu-Inspire 0.300 Catmandu/Importer/Inspire/`@_`.; |
| 102 | |
| 103 | return sub { |
| 104 | state $stack = $self->_get_recordscip-perl cpan Catmandu-Inspire 0.300 Catmandu/Importer/Inspire/_get_record().; |
| 105 | return popscip-perl cpan Catmandu-Inspire 0.300 Catmandu/Importer/Inspire/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<Catmandu::Iterable>, L<Catmandu::ArXiv>, L<Catmandu::CrossRef> |
| 160 | |
| 161 | =cut |