| 1 |
#!/usr/bin/env perl |
| 2 |
|
| 3 |
=pod |
| 4 |
|
| 5 |
=head1 NAME |
| 6 |
|
| 7 |
bibdoiadd.pl - add DOI numbers to papers in a given bib file |
| 8 |
|
| 9 |
=head1 SYNOPSIS |
| 10 |
|
| 11 |
bibdoiadd [B<-c> I<config_file>] [B<-C> 1|0] [B<-e> 1|0] [B<-f>] [B<-o> I<output>] I<bib_file> |
| 12 |
|
| 13 |
=head1 OPTIONS |
| 14 |
|
| 15 |
=over 4 |
| 16 |
|
| 17 |
=item B<-c> I<config_file> |
| 18 |
|
| 19 |
Configuration file. If this file is absent, some defaults are used. |
| 20 |
See below for its format. |
| 21 |
|
| 22 |
=item B<-C> 1|0 |
| 23 |
|
| 24 |
Whether to canonicalize names in the output (1) or not (0). By default, 1. |
| 25 |
|
| 26 |
=item B<-e> |
| 27 |
|
| 28 |
If 1 (default), add empty doi if a doi cannot be found. This prevents |
| 29 |
repeated searches for the same entries if you add new entries to the |
| 30 |
file. Calling C<-e 0> suppresses this behavior. |
| 31 |
|
| 32 |
=item B<-f> |
| 33 |
|
| 34 |
Force checking doi number even if one is present |
| 35 |
|
| 36 |
=item B<-o> I<output> |
| 37 |
|
| 38 |
Output file. If this option is not used, the name for the |
| 39 |
output file is formed by adding C<_doi> to the input file |
| 40 |
|
| 41 |
=back |
| 42 |
|
| 43 |
=head1 DESCRIPTION |
| 44 |
|
| 45 |
The script reads a BibTeX file. It checks whether the entries have |
| 46 |
DOIs. If not, it tries to contact http://www.crossref.org to get the |
| 47 |
corresponding DOI. The result is a BibTeX file with the fields |
| 48 |
C<doi=...> added. |
| 49 |
|
| 50 |
The name of the output file is either set by the B<-o> option or |
| 51 |
is derived by adding the suffix C<_doi> to the output file. |
| 52 |
|
| 53 |
Every BibTeX record in the input is parsed, using BibTeX::Parser, but |
| 54 |
only the ones that do not have the C<doi> field (or C<mrnumber> or |
| 55 |
C<zblnumber> for the sibling scripts) are processed. These entries |
| 56 |
without the requested field are written back, as described in |
| 57 |
BibTeX::Parser::Entry. |
| 58 |
|
| 59 |
The bib records that are not processed (because they already have the |
| 60 |
requested field) are written back as-is, without any reformatting. |
| 61 |
|
| 62 |
There are (were?) two options for making queries with Crossref: free |
| 63 |
account and paid membership. In the first case you still must register |
| 64 |
with Crossref and are limited to a small number of queries, see the |
| 65 |
agreement at |
| 66 |
C<http://www.crossref.org/01company/free_services_agreement.html>. In |
| 67 |
the second case you have a username and password, and can use them for |
| 68 |
automatic queries. I am not sure whether the use of this script is |
| 69 |
allowed for the free account holders. At any rate, if you want to add |
| 70 |
DOIs to a large number of entries, you should register as a paid member. |
| 71 |
|
| 72 |
|
| 73 |
=head1 CONFIGURATION FILE |
| 74 |
|
| 75 |
The configuration file relates to the Crossref queries, and is mostly |
| 76 |
self-explanatory: it has comments (starting with C<#>) and assginments |
| 77 |
in the form |
| 78 |
|
| 79 |
$field = value ; |
| 80 |
|
| 81 |
The important parameters are C<$mode> (C<'free'> or C<'paid'>), |
| 82 |
C<$email> (for free users) and C<$username> & C<$password> for paid |
| 83 |
members. |
| 84 |
|
| 85 |
|
| 86 |
=head1 EXAMPLES |
| 87 |
|
| 88 |
bibdoiadd -c bibdoiadd.cfg -o - citations.bib > result.bib |
| 89 |
bibdoiadd -c bibdoiadd.cfg -o result.bib citations.bib |
| 90 |
|
| 91 |
=head1 AUTHOR |
| 92 |
|
| 93 |
Boris Veytsman |
| 94 |
|
| 95 |
=head1 COPYRIGHT AND LICENSE |
| 96 |
|
| 97 |
Copyright (C) 2014-2024 Boris Veytsman |
| 98 |
|
| 99 |
This is free software. You may redistribute copies of it under the |
| 100 |
terms of the GNU General Public License |
| 101 |
L<http://www.gnu.org/licenses/gpl.html>. There is NO WARRANTY, to the |
| 102 |
extent permitted by law. |
| 103 |
|
| 104 |
=cut |
| 105 |
|
| 106 |
use strict; |
| 107 |
BEGIN { |
| 108 |
# find files relative to our installed location within TeX Live |
| 109 |
chomp(my $TLMaster = `kpsewhich -var-value=TEXMFROOT`); # TL root |
| 110 |
if (length($TLMaster)) { |
| 111 |
unshift @INC, "$TLMaster/texmf-dist/scripts/bibtexperllibs"; |
| 112 |
} |
| 113 |
} |
| 114 |
use IO::File; |
| 115 |
use BibTeX::Parser; |
| 116 |
use LaTeX::ToUnicode qw (convert); |
| 117 |
use Getopt::Std; |
| 118 |
use URI::Escape; |
| 119 |
use LWP::Simple; |
| 120 |
# Sometimes AMS forgets to update certificates |
| 121 |
$ENV{PERL_LWP_SSL_VERIFY_HOSTNAME}=0; |
| 122 |
|
| 123 |
my $USAGE="USAGE: $0 [-c config] [-C 1|0] [-e 1|0] [-f] [-o output] file\n"; |
| 124 |
my $VERSION = <<END; |
| 125 |
bibdoiadd v2.3 |
| 126 |
This is free software. You may redistribute copies of it under the |
| 127 |
terms of the GNU General Public License |
| 128 |
http://www.gnu.org/licenses/gpl.html. There is NO WARRANTY, to the |
| 129 |
extent permitted by law. |
| 130 |
$USAGE |
| 131 |
END |
| 132 |
our %opts; |
| 133 |
getopts('fe:c:C:o:hV',\%opts) or die $USAGE; |
| 134 |
|
| 135 |
if ($opts{h} || $opts{V}){ |
| 136 |
print $VERSION; |
| 137 |
exit 0; |
| 138 |
} |
| 139 |
|
| 140 |
################################################################ |
| 141 |
# Defaults and parameters |
| 142 |
################################################################ |
| 143 |
|
| 144 |
my $inputfile = shift; |
| 145 |
|
| 146 |
my $outputfile = $inputfile; |
| 147 |
|
| 148 |
$outputfile =~ s/\.([^\.]*)$/_doi.$1/; |
| 149 |
|
| 150 |
if (exists $opts{o}) { |
| 151 |
$outputfile = $opts{o}; |
| 152 |
} |
| 153 |
|
| 154 |
my $forceSearch=$opts{f}; |
| 155 |
my $forceEmpty = 1; |
| 156 |
if (exists $opts{e}) { |
| 157 |
$forceEmpty = $opts{e}; |
| 158 |
} |
| 159 |
|
| 160 |
my $canonizeNames = 1; |
| 161 |
if (exists $opts{C}) { |
| 162 |
$canonizeNames = $opts{C}; |
| 163 |
} |
| 164 |
|
| 165 |
our $mode='free'; |
| 166 |
our $email; |
| 167 |
our $username; |
| 168 |
our $password; |
| 169 |
|
| 170 |
if ($opts{c}) { |
| 171 |
if (-r $opts{c}) { |
| 172 |
push @INC, "."; |
| 173 |
require $opts{c}; |
| 174 |
} else { |
| 175 |
die "Cannot read options $opts{c}. $USAGE"; |
| 176 |
} |
| 177 |
} |
| 178 |
|
| 179 |
|
| 180 |
# Check the consistency |
| 181 |
|
| 182 |
if ($mode eq 'free' && !length($email)) { |
| 183 |
die "Crossref requires a registered e-mail for the free mode queries\n"; |
| 184 |
} |
| 185 |
|
| 186 |
if ($mode eq 'paid' && (!length($username) || !length($password))) { |
| 187 |
die |
| 188 |
"Crossref requires a username and password for the paid mode queries\n"; |
| 189 |
} |
| 190 |
|
| 191 |
my $input= IO::File->new($inputfile) or |
| 192 |
die "Cannot find BibTeX file $inputfile\n$USAGE\n"; |
| 193 |
my $output = IO::File->new("> $outputfile") or |
| 194 |
die "Cannot write to $outputfile\n$USAGE\n"; |
| 195 |
|
| 196 |
my $parser=new BibTeX::Parser($input); |
| 197 |
|
| 198 |
my $prefix = |
| 199 |
"http://www.crossref.org/openurl?redirect=false"; |
| 200 |
if ($mode eq 'free') { |
| 201 |
$prefix .= '&pid='.uri_escape($email); |
| 202 |
} else { |
| 203 |
$prefix .= '&pid='.uri_escape($username).":". |
| 204 |
uri_escape($password); |
| 205 |
} |
| 206 |
|
| 207 |
# Processing the input |
| 208 |
while (my $entry = $parser->next) { |
| 209 |
if (!$entry->parse_ok()) { |
| 210 |
print STDERR "Cannot understand entry: "; |
| 211 |
$entry->print(*STDERR); |
| 212 |
print STDERR "Skipping this entry\n"; |
| 213 |
next; |
| 214 |
} |
| 215 |
|
| 216 |
if (!($entry->type() eq 'ARTICLE') && !($entry->type() eq 'BOOK') |
| 217 |
&& !($entry->type() eq 'INCOLLECTION')) { |
| 218 |
print $output $entry->raw_bibtex(), "\n\n"; |
| 219 |
next; |
| 220 |
} |
| 221 |
if ($entry->has('doi') && !$forceSearch) { |
| 222 |
print $output $entry->raw_bibtex(), "\n\n"; |
| 223 |
next; |
| 224 |
} |
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
my $doi = GetDoi($prefix, $entry); |
| 229 |
if (length($doi) || $forceEmpty) { |
| 230 |
$entry->field('doi',$doi); |
| 231 |
} |
| 232 |
|
| 233 |
print $output |
| 234 |
$entry->to_string(canonize_names=>$canonizeNames), |
| 235 |
"\n\n"; |
| 236 |
|
| 237 |
|
| 238 |
} |
| 239 |
|
| 240 |
$input->close(); |
| 241 |
$output->close(); |
| 242 |
exit 0; |
| 243 |
|
| 244 |
############################################################### |
| 245 |
# Getting one doi |
| 246 |
############################################################### |
| 247 |
|
| 248 |
sub GetDoi { |
| 249 |
my ($url,$entry) = @_; |
| 250 |
if ($entry->has('issn')) { |
| 251 |
$url .= "&issn=".uri_escape_utf8(SanitizeText($entry->field('issn'))); |
| 252 |
} |
| 253 |
if ($entry->has('journal')) { |
| 254 |
$url .= "&title=".uri_escape_utf8(SanitizeText($entry->field('journal'))); |
| 255 |
} |
| 256 |
my @names=$entry->author(); |
| 257 |
if (scalar(@names)) { |
| 258 |
my $lastname = SanitizeText($names[0]->last()); |
| 259 |
$url .= "&aulast=".uri_escape_utf8($lastname); |
| 260 |
} |
| 261 |
if ($entry->has('volume')) { |
| 262 |
$url .= "&volume=".uri_escape_utf8($entry->field('volume')); |
| 263 |
} |
| 264 |
if ($entry->has('number')) { |
| 265 |
$url .= "&issue=".uri_escape_utf8($entry->field('number')); |
| 266 |
} |
| 267 |
if ($entry->has('pages')) { |
| 268 |
my $pages=$entry->field('pages'); |
| 269 |
$pages =~ s/-.*$//; |
| 270 |
$url .= "&spage=".uri_escape_utf8($pages); |
| 271 |
} |
| 272 |
if ($entry->has('year')) { |
| 273 |
$url .= "&date=".uri_escape_utf8($entry->field('year')); |
| 274 |
} |
| 275 |
|
| 276 |
my $result=get($url); |
| 277 |
|
| 278 |
if ($result =~ m/<doi [^>]*>(.*)<\/doi>/) { |
| 279 |
return $1; |
| 280 |
} else { |
| 281 |
return ""; |
| 282 |
} |
| 283 |
} |
| 284 |
|
| 285 |
############################################################### |
| 286 |
# Sanitization of a text string |
| 287 |
############################################################### |
| 288 |
sub SanitizeText { |
| 289 |
my $string = shift; |
| 290 |
$string = convert($string); |
| 291 |
$string =~ s/\\newblock//g; |
| 292 |
$string =~ s/\\bgroup//g; |
| 293 |
$string =~ s/\\egroup//g; |
| 294 |
$string =~ s/\\scshape//g; |
| 295 |
$string =~ s/\\urlprefix//g; |
| 296 |
$string =~ s/\\emph//g; |
| 297 |
$string =~ s/\\textbf//g; |
| 298 |
$string =~ s/\\enquote//g; |
| 299 |
$string =~ s/\\url/URL: /g; |
| 300 |
$string =~ s/\\doi/DOI: /g; |
| 301 |
$string =~ s/\\\\/ /g; |
| 302 |
$string =~ s/\$//g; |
| 303 |
$string =~ s/\\checkcomma/,/g; |
| 304 |
$string =~ s/~/ /g; |
| 305 |
$string =~ s/[\{\}]//g; |
| 306 |
return $string; |
| 307 |
} |