Skip to content

Instantly share code, notes, and snippets.

@piroyon
Last active April 15, 2019 02:38
Show Gist options
  • Save piroyon/5d335bce881db0273e1bb0501023157e to your computer and use it in GitHub Desktop.
Save piroyon/5d335bce881db0273e1bb0501023157e to your computer and use it in GitHub Desktop.
get the details of the paper from DOI list
#!/usr/bin/perl
=head1 SCRIPT NAME
getPaper_fromDOI.pl
=head1 DESCRIPTION
Get the details of the paper from DOI using crossref API.
=head1 USAGE
$ cat doi_list.txt
10.1016/j.ydbio.2015.08.013
10.1037/0003-066X.59.1.29
...
$ perl getPaper_fromDOI.pl doi_list.txt
=cut
use LWP::UserAgent;
use JSON;
my $ua = LWP::UserAgent->new;
my $uri = "https://api.crossref.org/works/";
while(<>) {
chomp;
$doi = $_;
$url = $uri . $doi . '/';
my $res = $ua->get($url);
die $res->status_line if !$res->is_success;
$title = $page = $mag = $vol = $year = $author_list = '';
$content = decode_json($res->content);
$title = shift @{$content->{'message'}->{'title'}};
$page = $content->{'message'}->{'page'};
$vol = $content->{'message'}->{'volume'};
$mag = shift @{$content->{'message'}->{'short-container-title'}};
if (@{$content->{'message'}->{'published-print'}->{'date-parts'}}) {
$year = shift @{shift @{$content->{'message'}->{'published-print'}->{'date-parts'}}};
} elsif (@{$content->{'message'}->{'published-online'}->{'date-parts'}}) {
$year = shift @{shift @{$content->{'message'}->{'published-online'}->{'date-parts'}}};
}
foreach $i (@{$content->{'message'}->{'author'}}) {
$family = $i->{'family'};
$given = substr($i->{'given'}, 0, 1);
$name = $family . ', ' . $given . '.';
push @all_author, $name;
}
if ($#all_author == 0) {
$author_list = $all_author[0];
} elsif ($#all_author == 1) {
$author_list = $all_author[0] . ' and ' . $all_author[1];
} else {
$last_author = pop @all_author;
$author_list = join(", ", @all_author);
$author_list .= " and $last_author";
}
print "$author_list, ";
print "$title, ";
print "$mag, ";
print 'Vol.'.$vol.', ';
print "$page ";
print '(' . $year . ')';
print "\n";
undef $res;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment