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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
|
#!/usr/bin/env perl
=pod
=head1 NAME
bibzbladd.pl - add Zbl numbers to papers in a given bib file
=head1 SYNOPSIS
bibzbladd [-d] [B<-f>] [B<-e> 1|0] [B<-o> I<output>] I<bib_file>
=head1 OPTIONS
=over 4
=item B<-d>
Debug mode
=item B<-e>
If 1 (default), add an empty zblnumber if a zbl cannot be found. This
prevents repeated searches for the same entries if you add new entries
to the file. Calling C<-e 0> suppresses this behavior.
=item B<-f>
Force searching for Zbl numbers even if the entry already has one.
=item B<-o> I<output>
Output file. If this option is not used, the name for the
output file is formed by adding C<_zbl> to the input file
=back
=head1 DESCRIPTION
The script reads a BibTeX file. It checks whether the entries have
Zbls. If not, tries to contact internet to get the numbers. The
result is a BibTeX file with the fields
C<zblnumber=...> added.
The name of the output file is either set by the B<-o> option or
is derived by adding the suffix C<_zbl> to the output file.
=head1 AUTHOR
Boris Veytsman
=head1 COPYRIGHT AND LICENSE
Copyright (C) 2014-2017 Boris Veytsman
This is free software. You may redistribute copies of it under the
terms of the GNU General Public License
L<http://www.gnu.org/licenses/gpl.html>. There is NO WARRANTY, to the
extent permitted by law.
=cut
use strict;
BEGIN {
# find files relative to our installed location within TeX Live
chomp(my $TLMaster = `kpsewhich -var-value=SELFAUTOPARENT`); # TL root
if (length($TLMaster)) {
unshift @INC, "$TLMaster/texmf-dist/scripts/bibtexperllibs";
}
}
use IO::File;
use BibTeX::Parser;
use Getopt::Std;
use URI::Escape;
use LWP::UserAgent;
$ENV{PERL_LWP_SSL_VERIFY_HOSTNAME}=0;
my $USAGE="USAGE: $0 [-d] [-e 1|0] [-f] [-o output] file\n";
my $VERSION = <<END;
bibzbladd v2.2
This is free software. You may redistribute copies of it under the
terms of the GNU General Public License
http://www.gnu.org/licenses/gpl.html. There is NO WARRANTY, to the
extent permitted by law.
$USAGE
END
my %opts;
getopts('de:fo:hV',\%opts) or die $USAGE;
if ($opts{h} || $opts{V}){
print $VERSION;
exit 0;
}
################################################################
# Defaults and parameters
################################################################
my $inputfile = shift;
my $outputfile = $inputfile;
$outputfile =~ s/\.([^\.]*)$/_zbl.$1/;
if ($opts{o}) {
$outputfile = $opts{o};
}
my $forceSearch=$opts{f};
my $forceEmpty = 1;
if (exists $opts{e}) {
$forceEmpty = $opts{e};
}
my $debug = $opts{d};
my $input= IO::File->new($inputfile) or
die "Cannot find BibTeX file $inputfile\n$USAGE\n";
my $output = IO::File->new("> $outputfile") or
die "Cannot write to $outputfile\n$USAGE\n";
my $parser=new BibTeX::Parser($input);
# Creating the HTTP parameters
my $mirror =
"https://zbmath.org/citationmatching/bibtex/match";
my $userAgent = LWP::UserAgent->new;
while (my $entry = $parser->next ) {
if (!$entry->parse_ok()) {
print STDERR "Cannot understand entry: ";
$entry->print(*STDERR);
print STDERR "Skipping this entry\n";
next;
}
if ($entry->has('zblnumber') && !$forceSearch) {
print $output $entry->raw_bibtex(), "\n\n";
if ($debug) {
print STDERR "DEBUG: entry ", $entry->key(),
" has zblnumber ", $entry->field('zblnumber'),
" and no forced search is requested\n";
}
next;
}
# Now we have an entry with no Zbl. Let us get to work.
if ($debug) {
print STDERR "DEBUG: Searching for zbl number for entry ",
$entry->key, "\n";
}
my $zbl = GetZbl($entry, $userAgent, $mirror);
if (length($zbl) || $forceEmpty) {
$entry->field('zblnumber',$zbl);
}
print $output $entry->to_string(), "\n\n";
}
$input->close();
$output->close();
exit 0;
###############################################################
# Getting one Zbl
###############################################################
sub GetZbl {
my $entry=shift;
my $userAgent=shift;
my $mirror=shift;
my @query;
my $string=uri_escape_utf8($entry->to_string());
if ($debug) {
print STDERR "DEBUG: query: $mirror?bibtex=$string\n" ;
}
my $response = $userAgent->get("$mirror?bibtex=$string");
if ($debug) {
print STDERR "DEBUG: response: ",
$response->decoded_content, "\n";
}
if ($response->decoded_content =~ /^\s*"zbl_id":\s*"(.*)",\s*$/m) {
if ($debug) {
print STDERR "DEBUG: got zbl: $1\n",
}
return $1;
} else {
if ($debug) {
print STDERR "DEBUG: Did not get zbl\n",
}
return ("");
}
}
|