line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Catmandu::Importer::MendeleyCatalog; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
24094
|
use Catmandu::Sane; |
|
1
|
|
|
|
|
79286
|
|
|
1
|
|
|
|
|
9
|
|
4
|
1
|
|
|
1
|
|
250
|
use Moo; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
5
|
|
5
|
1
|
|
|
1
|
|
676
|
use OAuth::Lite2::Client::ClientCredentials; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
use HTTP::Tiny; |
7
|
|
|
|
|
|
|
use JSON::XS; |
8
|
|
|
|
|
|
|
use Catmandu::Util qw(is_value); |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
with 'Catmandu::Importer'; |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
my $DOCUMENT_CONTENT_TYPE = 'application/vnd.mendeley-document.1+json'; |
13
|
|
|
|
|
|
|
my $CATALOG_SEARCH_PATH = '/search/catalog'; |
14
|
|
|
|
|
|
|
my $CATALOG_PATH = '/catalog'; |
15
|
|
|
|
|
|
|
my @QUERY_FIELDS = qw(title author source abstract); |
16
|
|
|
|
|
|
|
my @IDENTIFIER_FIELDS = qw(arxiv doi isbn issn pmid scopus filehash); |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
has client_id => (is => 'ro', required => 1); |
19
|
|
|
|
|
|
|
has client_secret => (is => 'ro', required => 1); |
20
|
|
|
|
|
|
|
has client => (is => 'lazy'); |
21
|
|
|
|
|
|
|
has params => (is => 'ro', default => sub { +{} }); |
22
|
|
|
|
|
|
|
has path => (is => 'rwp', default => sub { $CATALOG_SEARCH_PATH }); |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
sub BUILD { |
25
|
|
|
|
|
|
|
my ($self, $args) = @_; |
26
|
|
|
|
|
|
|
my @query_keys = grep { is_value($args->{$_}) } @QUERY_FIELDS; |
27
|
|
|
|
|
|
|
my @identifier_keys = grep { is_value($args->{$_}) } @IDENTIFIER_FIELDS; |
28
|
|
|
|
|
|
|
my $params = $self->params; |
29
|
|
|
|
|
|
|
$params->{view} = $args->{view} || 'all'; |
30
|
|
|
|
|
|
|
# get by id |
31
|
|
|
|
|
|
|
if (is_value($args->{id})) { |
32
|
|
|
|
|
|
|
$self->_set_path("$CATALOG_PATH/$args->{id}"); |
33
|
|
|
|
|
|
|
# query search |
34
|
|
|
|
|
|
|
} elsif (is_value($args->{query})) { |
35
|
|
|
|
|
|
|
$params->{query} = $args->{query}; |
36
|
|
|
|
|
|
|
$params->{limit} = $args->{limit} if is_value($args->{limit}); |
37
|
|
|
|
|
|
|
# fielded search |
38
|
|
|
|
|
|
|
} elsif (@query_keys) { |
39
|
|
|
|
|
|
|
$params->{$_} = $args->{$_} for @query_keys; |
40
|
|
|
|
|
|
|
$params->{limit} = $args->{limit} if is_value($args->{limit}); |
41
|
|
|
|
|
|
|
# identifier search |
42
|
|
|
|
|
|
|
} elsif (@identifier_keys) { |
43
|
|
|
|
|
|
|
$params->{$_} = $args->{$_} for @identifier_keys; |
44
|
|
|
|
|
|
|
$self->_set_path($CATALOG_PATH); |
45
|
|
|
|
|
|
|
} else { |
46
|
|
|
|
|
|
|
die "Missing required arguments"; |
47
|
|
|
|
|
|
|
} |
48
|
|
|
|
|
|
|
} |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
sub _build_client { |
51
|
|
|
|
|
|
|
my ($self) = @_; |
52
|
|
|
|
|
|
|
OAuth::Lite2::Client::ClientCredentials->new( |
53
|
|
|
|
|
|
|
id => $self->client_id, |
54
|
|
|
|
|
|
|
secret => $self->client_secret, |
55
|
|
|
|
|
|
|
access_token_uri => 'https://api.mendeley.com/oauth/token', |
56
|
|
|
|
|
|
|
); |
57
|
|
|
|
|
|
|
} |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
sub generator { |
60
|
|
|
|
|
|
|
my ($self) = @_; |
61
|
|
|
|
|
|
|
sub { |
62
|
|
|
|
|
|
|
state $docs = $self->_get_documents; |
63
|
|
|
|
|
|
|
shift @$docs; |
64
|
|
|
|
|
|
|
}; |
65
|
|
|
|
|
|
|
} |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
sub _get_documents { |
68
|
|
|
|
|
|
|
my ($self) = @_; |
69
|
|
|
|
|
|
|
my $token = $self->client->get_access_token->access_token; |
70
|
|
|
|
|
|
|
my $http = HTTP::Tiny->new; |
71
|
|
|
|
|
|
|
my $path = $self->path; |
72
|
|
|
|
|
|
|
my $params = $http->www_form_urlencode($self->params); |
73
|
|
|
|
|
|
|
my $res = $http->get("https://api.mendeley.com/$path?$params", { |
74
|
|
|
|
|
|
|
headers => { |
75
|
|
|
|
|
|
|
Accept => $DOCUMENT_CONTENT_TYPE, |
76
|
|
|
|
|
|
|
Authorization => sprintf(q{Bearer %s}, $token) |
77
|
|
|
|
|
|
|
} |
78
|
|
|
|
|
|
|
}); |
79
|
|
|
|
|
|
|
decode_json($res->{content}); |
80
|
|
|
|
|
|
|
} |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
1; |
83
|
|
|
|
|
|
|
|