summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/scripts/crossrefware/bibzbladd.pl
blob: 4059b7b0a12f54804fa2868890988014e2992ef6 (plain)
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
206
207
208
209
210
211
212
213
214
215
#!/usr/bin/env perl

=pod

=head1 NAME

bibzbladd.pl - add Zbl numbers to papers in a given bib file

=head1 SYNOPSIS

bibzbladd  [B<-o> I<output>] I<bib_file>

=head1 OPTIONS

=over 4


=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 now, 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  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;
use Text::BibTeX;
use Text::BibTeX::Name;
use Getopt::Std;
use URI::Escape;
use LWP::UserAgent;
use TeX::Encode;

my $USAGE="USAGE: $0  [-o output] file\n";
my $VERSION = <<END;
bibzbladd v1.0
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('o: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 $input = new Text::BibTeX::File "$inputfile" or 
    die "Cannot BibTeX file $inputfile\n";
my $output = new Text::BibTeX::File "> $outputfile" or 
    die "Cannot write to $outputfile\n";


# Creating the HTTP parameters
my $mirror =
    "http://www.zentralblatt-math.org/MIRROR/zmath/en/search/";
my $userAgent = LWP::UserAgent->new;

# Processing the input
while (my $entry = new Text::BibTeX::Entry $input) {
    if (!$entry->parse_ok()) {
	print STDERR "Cannot understand entry: ";
	$entry->print(*STDERR);
	print STDERR "Skipping this entry\n";
	next;
    }
    if (!(($entry->metatype() eq BTE_REGULAR))) {
	$entry->write($output);
	next;
    }
    if ($entry->exists('zblnumber')) {
	$entry->write($output);
	next;
    }
    

    # Now we have an entry with no Zbl.  Let us get to work.
    my $zbl = GetZbl($entry, $userAgent, $mirror);
    if (length($zbl)) {
	$entry->set('zblnumber',$zbl);
    }
    $entry->write($output);

}

$input->close();
$output->close();
exit 0;

###############################################################
#  Getting one Zbl
###############################################################

sub GetZbl {
    my $entry=shift;
    my $userAgent=shift;
    my $mirror=shift;
    
    my @query;

    my @names=$entry->names ('author');
    if (scalar(@names)) {
	foreach my $name (@names) {
	    my @lastnames = $name->part ('last');
	    if (scalar(@lastnames)) {
		foreach my $lastname (@lastnames) {
		    push @query, "au:$lastname";
		}
	    }
	}
    }
    if (my $title = $entry->get('title')) {
	push @query, "ti:$title";
    }
    
    if (my $year = $entry->get('year')) {
	push @query, "py:$year";
    }

    if (my $year = $entry->get('year')) {
	push @query, "py:$year";
    }

    my $type = $entry->type;
    if ($type eq 'article') {
	push @query, "dt:j";
    }
    if ($type eq 'book') {
	push @query, "dt:b";
    }
    if ($type eq 'inproceedings' || $type eq 'incollection') {
	push @query, "dt:a";
    }

    my $source = "";
    if ($type eq 'article') {
	if ($entry->get('journal')) {
	    $source .= $entry->get('journal');
	}
	if (my $vol=$entry->get('volume')) {
	    $source .= ",$vol";
	}
	if (my $pages=$entry->get('pages')) {
	    $source .= ",$pages";
	}
    } else {
	if (my $bt=$entry->get('booktitle')) {
	    $source .= "$bt";
	}
    }

    if ($source) {
	push @query, "so:$source";
    }


    my $qstring = join(" & ", @query);
#    print STDERR "$qstring\n";

    my $form;

    $form->{name}='form';
    $form->{q} =  $qstring;
    $form->{type} = "ascii";
    $form->{submit} = 'Search';

    my $response = $userAgent->post($mirror, $form);
    if ($response->decoded_content =~ /^an:\s*Zbl\s*(\S+)\s*$/m) {
	return $1;
    } else {
	return ("");
    }
}