forked from USGCRP/gcis-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadd-article-contributors.pl
More file actions
executable file
·156 lines (131 loc) · 4.5 KB
/
add-article-contributors.pl
File metadata and controls
executable file
·156 lines (131 loc) · 4.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#!/usr/bin/env perl
use Gcis::Client;
use Smart::Comments;
use Data::Dumper;
use YAML::XS qw/Dump/;
use Mojo::Util qw/html_unescape/;
use Getopt::Long qw/GetOptions/;
use v5.16;
use experimental 'signatures';
binmode(STDOUT, ':encoding(utf8)');
GetOptions(
'all' => \(my $all = 1), # all articles or just the first 20?
'dump' => \(my $dump = 0), # dump the list, don't change gcis
'dry_run|n' => \(my $dry_run = 0), # no changes
'url=s' => \(my $url),
) or die "bad opts";
die 'missing url' unless $url;
warn "dry run\n" if $dry_run;
warn "url : $url\n";
my $gcis = Gcis::Client->connect(url => $url);
my $orcid = Gcis::Client->new->url("http://pub.orcid.org")->accept("application/orcid+json");
my $xref = Gcis::Client->new->url("http://dx.doi.org")->accept("application/vnd.citationstyles.csl+json;q=0.5");
sub debug($) {
# warn "# @_\n";
}
sub get_orcid_authors($doi) {
my @authors;
my $r = $orcid->get("/v1.2/search/orcid-bio/", { q => qq[digital-object-ids:"$doi"] });
my $count = $r->{'orcid-search-results'}{'num-found'} or return \@authors;
for (0..$count-1) {
my $id = $orcid->tx->res->json("/orcid-search-results/orcid-search-result/$_/orcid-profile/orcid-identifier/path");
my $p = $orcid->tx->res->json("/orcid-search-results/orcid-search-result/$_/orcid-profile/orcid-bio/personal-details");
push @authors, {
last_name => html_unescape($p->{'family-name'}{'value'}),
first_name => html_unescape($p->{'given-names'}{'value'}),
orcid => $id,
};
}
return \@authors;
}
sub get_xref_authors($doi) {
my $r = $xref->get("/$doi");
my @authors;
for (@{ $r->{author} } ) {
push @authors, { last_name => html_unescape($_->{family}), first_name => html_unescape($_->{given}) };
}
return \@authors;
}
sub combine_author_list($some,$all) {
my @authors = @$all;
for my $p (@$some) {
my @matches = grep { $_->{last_name} eq $p->{last_name} } @authors;
if (@matches==1) {
@authors = grep { $_->{last_name} ne $p->{last_name} } @authors;
push @authors, $p;
}
if (@matches > 1) {
warn "too many matches, cannot merge".Dumper(\@matches);
return;
}
}
return \@authors;
}
sub find_or_create_gcis_person($person) {
my $match;
# ORCID
if ($person->{orcid} and $match = $gcis->get("/person/$person->{orcid}")) {
debug "Found orcid: $person->{orcid}";
return $match;
}
# Match first + last name
if ($match = $gcis->post_quiet("/person/lookup/name",
{ last_name => $person->{last_name},
first_name => $person->{first_name}
})) {
if ($match->{id}) {
return $match;
}
}
# Add more heuristics here
return if $dry_run;
unless ($person->{first_name}) {
debug "no first name ".Dumper($person);
return;
}
unless ($person->{last_name}) {
debug "no last name ".Dumper($person);
return;
}
debug "adding new person $person->{first_name} $person->{last_name}";
my $new = $gcis->post("/person" => {
first_name => $person->{first_name},
last_name => $person->{last_name},
orcid => $person->{orcid}
}) or do {
warn "Error creating ".Dumper($person)." : ".$gcis->error;
return;
};
return $new;
}
sub add_contributor_record($person,$article,$sort_key) {
warn "dry run" if $dry_run;
return if $dry_run;
my $uri = $article->{uri};
$uri =~ s[article][article/contributors];
$gcis->post( $uri => {
person_id => $person->{id},
role => 'author',
sort_key => $sort_key,
}) or debug "error posting to $uri: ".$gcis->error;
}
for my $article ($gcis->get("/article", { all => $all })) { ### Getting---> done
my $doi = $article->{doi} or next;
my $some = get_orcid_authors($doi);
my $all = get_xref_authors($doi) or die "no authors for $doi";
my $merged = combine_author_list($some,$all) or next;
if ($dump) {
printf "%100s\n",$doi;
for (@$merged) {
printf "%-25s %-30s %-30s\n",$_->{orcid},$_->{first_name},$_->{last_name};
}
next;
}
my $i = 10;
for my $person (@$merged) {
my $found = find_or_create_gcis_person($person);
next unless $found;
add_contributor_record($found, $article, $i);
$i += 10;
}
}