| 1 |
#!/usr/bin/env perl |
| 2 |
|
| 3 |
=pod |
| 4 |
|
| 5 |
=head1 NAME |
| 6 |
|
| 7 |
bibzbladd.pl - add Zbl numbers to papers in a given bib file |
| 8 |
|
| 9 |
=head1 SYNOPSIS |
| 10 |
|
| 11 |
bibzbladd [-d] [B<-f>] [B<-e> 1|0] [B<-o> I<output>] [B<-p> I<probability>] [B<-v|-q>] I<bib_file> |
| 12 |
|
| 13 |
=head1 OPTIONS |
| 14 |
|
| 15 |
=over 4 |
| 16 |
|
| 17 |
=item B<-d> |
| 18 |
|
| 19 |
Debug mode |
| 20 |
|
| 21 |
=item B<-e> |
| 22 |
|
| 23 |
If 1 (default), add an empty zblnumber if a zbl cannot be found. This |
| 24 |
prevents repeated searches for the same entries if you add new entries |
| 25 |
to the file. Calling C<-e 0> suppresses this behavior. |
| 26 |
|
| 27 |
=item B<-f> |
| 28 |
|
| 29 |
Force searching for Zbl numbers even if the entry already has one. |
| 30 |
|
| 31 |
=item B<-o> I<output> |
| 32 |
|
| 33 |
Output file. If this option is not used, the name for the |
| 34 |
output file is formed by adding C<_zbl> to the input file |
| 35 |
|
| 36 |
=item B<-p> I<probability> |
| 37 |
|
| 38 |
Zbmath.org now outputs a probability of match. We disregard the |
| 39 |
matches with the probability lower than I<probability>. The default |
| 40 |
is 0.9 |
| 41 |
|
| 42 |
=item B<-v> |
| 43 |
|
| 44 |
Verbose mode (the default). Add to the output the intermediate |
| 45 |
results of zbl search |
| 46 |
|
| 47 |
=item B<-q> |
| 48 |
|
| 49 |
Quiet mode. Do not add to the output the intermediate results |
| 50 |
of zbl search. |
| 51 |
|
| 52 |
=back |
| 53 |
|
| 54 |
=head1 DESCRIPTION |
| 55 |
|
| 56 |
The script reads a BibTeX file. It checks whether the entries have |
| 57 |
Zbls. If not, it tries to find the numbers from Internet sites. The |
| 58 |
result is a BibTeX file with C<zblnumber=...> fields added. |
| 59 |
|
| 60 |
The name of the output file is either set by the B<-o> option or |
| 61 |
is derived by adding the suffix C<_zbl> to the output file. |
| 62 |
|
| 63 |
See the C<bibdoiadd> script for more details on the processing. |
| 64 |
|
| 65 |
=head1 AUTHOR |
| 66 |
|
| 67 |
Boris Veytsman |
| 68 |
|
| 69 |
=head1 COPYRIGHT AND LICENSE |
| 70 |
|
| 71 |
Copyright (C) 2014-2025 Boris Veytsman |
| 72 |
|
| 73 |
This is free software. You may redistribute copies of it under the |
| 74 |
terms of the GNU General Public License |
| 75 |
L<http://www.gnu.org/licenses/gpl.html>. There is NO WARRANTY, to the |
| 76 |
extent permitted by law. |
| 77 |
|
| 78 |
=cut |
| 79 |
|
| 80 |
use strict; |
| 81 |
BEGIN { |
| 82 |
# find files relative to our installed location within TeX Live |
| 83 |
chomp(my $TLMaster = `kpsewhich -var-value=TEXMFROOT`); # TL root |
| 84 |
if (length($TLMaster)) { |
| 85 |
unshift @INC, "$TLMaster/texmf-dist/scripts/bibtexperllibs"; |
| 86 |
} |
| 87 |
} |
| 88 |
use IO::File; |
| 89 |
use BibTeX::Parser; |
| 90 |
use Getopt::Std; |
| 91 |
use URI::Escape; |
| 92 |
use LWP::UserAgent; |
| 93 |
use JSON; |
| 94 |
$ENV{PERL_LWP_SSL_VERIFY_HOSTNAME}=0; |
| 95 |
|
| 96 |
my $USAGE="USAGE: $0 [-d] [-e 1|0] [-f] [-o output] [-p probability] [-v|-q] file\n"; |
| 97 |
my $VERSION = <<END; |
| 98 |
bibzbladd v2.3 |
| 99 |
This is free software. You may redistribute copies of it under the |
| 100 |
terms of the GNU General Public License |
| 101 |
http://www.gnu.org/licenses/gpl.html. There is NO WARRANTY, to the |
| 102 |
extent permitted by law. |
| 103 |
$USAGE |
| 104 |
END |
| 105 |
my %opts; |
| 106 |
getopts('de:fo:p:hV',\%opts) or die $USAGE; |
| 107 |
|
| 108 |
if ($opts{h} || $opts{V}){ |
| 109 |
print $VERSION; |
| 110 |
exit 0; |
| 111 |
} |
| 112 |
|
| 113 |
################################################################ |
| 114 |
# Defaults and parameters |
| 115 |
################################################################ |
| 116 |
|
| 117 |
my $inputfile = shift; |
| 118 |
|
| 119 |
my $outputfile = $inputfile; |
| 120 |
|
| 121 |
$outputfile =~ s/\.([^\.]*)$/_zbl.$1/; |
| 122 |
|
| 123 |
if ($opts{o}) { |
| 124 |
$outputfile = $opts{o}; |
| 125 |
} |
| 126 |
|
| 127 |
my $forceSearch=$opts{f}; |
| 128 |
|
| 129 |
my $forceEmpty = 1; |
| 130 |
if (exists $opts{e}) { |
| 131 |
$forceEmpty = $opts{e}; |
| 132 |
} |
| 133 |
|
| 134 |
my $debug = $opts{d}; |
| 135 |
|
| 136 |
my $prob = 0.9; |
| 137 |
if (exists $opts{p}) { |
| 138 |
$prob = $opts{p}; |
| 139 |
} |
| 140 |
|
| 141 |
my $json = JSON->new->allow_nonref; |
| 142 |
|
| 143 |
my $quiet = 0; |
| 144 |
if (exists $opts{q}) { |
| 145 |
$quiet = 1; |
| 146 |
} |
| 147 |
if (exists $opts{v}) { |
| 148 |
$quiet = 0; |
| 149 |
} |
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
my $input= IO::File->new($inputfile) or |
| 154 |
die "Cannot find BibTeX file $inputfile\n$USAGE\n"; |
| 155 |
my $output = IO::File->new("> $outputfile") or |
| 156 |
die "Cannot write to $outputfile\n$USAGE\n"; |
| 157 |
|
| 158 |
my $parser=new BibTeX::Parser($input); |
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
# Creating the HTTP parameters |
| 163 |
my $mirror = |
| 164 |
"https://zbmath.org/citationmatching/bibtex/match"; |
| 165 |
my $userAgent = LWP::UserAgent->new (agent => 'Mozilla/5.0'); |
| 166 |
|
| 167 |
while (my $entry = $parser->next ) { |
| 168 |
if (!$entry->parse_ok()) { |
| 169 |
print STDERR "Cannot understand entry: "; |
| 170 |
$entry->print(*STDERR); |
| 171 |
print STDERR "Skipping this entry\n"; |
| 172 |
next; |
| 173 |
} |
| 174 |
if ($entry->has('zblnumber') && !$forceSearch) { |
| 175 |
print $output $entry->raw_bibtex(), "\n\n"; |
| 176 |
if ($debug) { |
| 177 |
print STDERR "DEBUG: entry ", $entry->key(), |
| 178 |
" has zblnumber ", $entry->field('zblnumber'), |
| 179 |
" and no forced search is requested\n"; |
| 180 |
} |
| 181 |
next; |
| 182 |
} |
| 183 |
|
| 184 |
|
| 185 |
# Now we have an entry with no Zbl. Let us get to work. |
| 186 |
if ($debug) { |
| 187 |
print STDERR "DEBUG: Searching for zbl number for entry ", |
| 188 |
$entry->key, "\n"; |
| 189 |
} |
| 190 |
my $zbl = GetZbl($entry, $userAgent, $mirror); |
| 191 |
if (length($zbl) || $forceEmpty) { |
| 192 |
$entry->field('zblnumber',$zbl); |
| 193 |
} |
| 194 |
print $output $entry->to_string(), "\n\n"; |
| 195 |
|
| 196 |
} |
| 197 |
|
| 198 |
$input->close(); |
| 199 |
$output->close(); |
| 200 |
exit 0; |
| 201 |
|
| 202 |
############################################################### |
| 203 |
# Getting one Zbl |
| 204 |
############################################################### |
| 205 |
|
| 206 |
sub GetZbl { |
| 207 |
my $entry=shift; |
| 208 |
my $userAgent=shift; |
| 209 |
my $mirror=shift; |
| 210 |
|
| 211 |
my @query; |
| 212 |
|
| 213 |
my $string=uri_escape_utf8($entry->to_string()); |
| 214 |
|
| 215 |
if ($debug) { |
| 216 |
print STDERR "DEBUG: query: $mirror?bibtex=$string\n" ; |
| 217 |
} |
| 218 |
|
| 219 |
|
| 220 |
my $response = $userAgent->get("$mirror?bibtex=$string"); |
| 221 |
if ($debug) { |
| 222 |
print STDERR "DEBUG: response: ", |
| 223 |
$response->decoded_content, "\n"; |
| 224 |
} |
| 225 |
|
| 226 |
my $content = $json->decode($response->decoded_content); |
| 227 |
|
| 228 |
if (!exists $content->{results}) { |
| 229 |
if ($debug) { |
| 230 |
print STDERR "DEBUG: Did not get zbl\n"; |
| 231 |
} |
| 232 |
return(""); |
| 233 |
} |
| 234 |
|
| 235 |
if (!$quiet) { |
| 236 |
print $output "% ZBL search:\n"; |
| 237 |
} |
| 238 |
|
| 239 |
my $results = $content->{results}; |
| 240 |
if (!$quiet) { |
| 241 |
my $string = $json->pretty->encode( $results ); |
| 242 |
$string =~ s/^(.)/% \1/mg; |
| 243 |
print $output "$string\n"; |
| 244 |
} |
| 245 |
|
| 246 |
foreach my $result (@{$results}) { |
| 247 |
if (!exists $result->{probability} || |
| 248 |
$result->{probability} < $prob || |
| 249 |
!exists $result->{zbl_id} |
| 250 |
) { |
| 251 |
next; |
| 252 |
} |
| 253 |
my $zbl = $result->{zbl_id}; |
| 254 |
if ($debug) { |
| 255 |
print STDERR "DEBUG: got zbl: $zbl\n", |
| 256 |
} |
| 257 |
return($zbl); |
| 258 |
} |
| 259 |
|
| 260 |
if ($debug) { |
| 261 |
print STDERR "DEBUG: Did not get zbl\n", |
| 262 |
} |
| 263 |
return (""); |
| 264 |
} |