Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Reads ISBN numbers from a file, checks them for validity, fetches the corresponding data from, then outputs a BibTeX file with all fields set accordingly. An API key from is required; see NOTES below.
#!/usr/bin/env perl
# David Griffith <>
# isbn2bib version 0.4
# Copyright December 11, 2010
use strict;
use warnings;
use Getopt::Long qw(:config no_ignore_case);
use encoding "utf8";
use Text::BibTeX;
use WebService::ISBNDB::API::Books;
use Business::ISBN;
use Pod::Usage;
my $api_key = $ENV{ISBNDB_KEY} || 'KEY_GOES_HERE';
my %options;
GetOptions('usage|?' => \$options{usage},
'h|help' => \$options{help},
'v|verbose' => \$options{verbose},
'q|quiet' => \$options{quiet},
'o|output-file=s' => \$options{outputfile},
'e|error-file=s' => \$options{errorfile},
'n|number=s' => \$options{number}
pod2usage(1) if $options{usage};
pod2usage(-verbose => 2) if $options{help};
my $infile = shift;
my $outfile;
if ($options{quiet}) {
open STDERR, '>/dev/null';
} else {
if ($options{errorfile}) {
open STDERR, ">$options{errorfile}";
if ($options{outputfile}) {
$outfile = $options{outputfile};
} else {
$outfile = "/dev/stdout";
if (!$infile && !$options{number}) { pod2usage(1);}
# Weeds out blank lines, and invalid ISBN numbers.
# Invalid numbers and garbage are printed to STDERR
if ($options{verbose}) {
if ($options{number}) {
print "Getting ISBN data on $options{number}\n";
} else {
print "Reading ISBN numbers from $infile\n";
if (!$outfile) {
print "Writing ISBN information to standard output.\n";
} else {
print "Writing ISBN information to $outfile\n";
if ($infile) {
open(INFILE, "<$infile");
my $entry;
my $bib = Text::BibTeX::File->new('>'.$outfile);
my $book_count = 1;
if ($options{number}) {
} else {
while (<INFILE>) {
sub lookup {
my ($input, @junk) = @_;
my $book;
if ($options{verbose}) {
print "Processing line $book_count\n";
if ($input =~ /^$/) {
($input, @junk) = split(/\s+/, $input);
my $isbn = Business::ISBN->new("$input");
if (! $isbn) {
print STDERR "Rejected $input: Garbage.\n";
if (!$isbn->is_valid) {
print STDERR "Rejected $input: ".
$book = WebService::ISBNDB::API::Books->find( {
api_key => $api_key, isbn => $isbn->as_string});
if (!$book) {
print STDERR "Rejected $input: ",
"Null reply from server.\n";
# make a new bibtex entry
my $entry = new Text::BibTeX::Entry;
# set title field
$entry->set('title', $book->get_longtitle || $book->get_title);
# set author or editor field
my $authors = $book->get_authors_text;
# some clean-up
$authors =~ s/^by //;
$authors =~ s/,$//;
$authors =~ s/,\s+/ and /g;
$authors =~ s/;\s+/ and /g;
# authors or editors ?
if ( $authors =~ /^\s*\[?edited by\s+\]?(.*)$/i ) {
(my $editors = $1) =~ s/with/and/;
$entry->set('editor', $editors);
elsif ( $authors =~ /\(Editor\)/i ) {
$authors =~ s/\s*\(Editor\)//gi;
else {
$entry->set('author', $authors);
# parse publisher and edition fields for publisher and year data
if ( $book->get_publisher_text =~ m/^(.*?),\s+c?(\d{4}).?$/ ) {
$entry->set( 'publisher', $1 ) ;
$entry->set( 'year', $2 );
else {
$entry->set( 'publisher', $book->get_publisher_text ) ;
if ( $book->get_edition_info =~ m/(\d{4})/ ) {
$entry->set( 'year', $1 );
$entry->set( 'isbn', $isbn->as_string);
# miscellaneous fields
my $notes = $book->get_notes;
$entry->set( 'notes', $notes ) if $notes ;
my $abstract = $book->get_summary;
$entry->set( 'abstract', $abstract ) if $abstract ;
# Add escapes to characters with special meaning in TeX and LaTeX.
sub cleanup {
my ($mystring, @junk) = @_;
=head1 NAME - Convert a list of ISBN numbers to BibTeX records
=head1 SYNOPSIS [options...] [inputfile] [outputfile]
Use -h or --help for verbose help.
Reads ISBN numbers from a file, checks them for validity, fetches the
corresponding data from, then outputs a BibTeX file with all
fields set accordingly. An API key from is required; see
NOTES below.
=head2 Option flags
-? Print simple usage message.
-h --help Verbose help message.
-q --quiet Don't say anything, even if there's an error.
-v --verbose Be more chatty about what's going on.
-n N Look up just this ISBN number and output
--number N a corresponding BibTeX entry.
-o outfile Write BibTeX entries to the file named.
--output-file outfile
-e errfile Write error messages to the file named.
--error-file errfile
This script may be run on a text file containing a list of ISBN numbers,
a single ISBN number supplied on the command line, or from a pipe. When
accepting ISBN numbers from a pipe, this script will emit a single beep
for a successful lookup and double beep for a failed lookup. This
allows for immediate feedback when using a barcode scanner.
=head2 Examples
For a text file containing ISBN numbers:
isbn2bib isbn-numbers.txt output.bib
For use with a barcode scanner:
cat | isbn2bib -- output.bib
Now, make sure the xterm on which this script is running has focus,
then scan an ISBN barcode. Press Control-C to stop this.
=head1 NOTES
An API key is required to access services. To get one, use a
web browser to visit and create a new account. You
can do up to 500 lookups per day gratis.
You can either paste it in the source code or set the environment
variable ISBNDB_KEY.
This script is heavily based on i-blis's script for parsing PDF file
names for ISBN-10 numbers.
Version 0.4
December 11, 2010
=head1 LICENSE
Free to use and modifiy, same terms as Perl itself. This needs expansion.
=head1 AUTHOR
David Griffith <>

This comment has been minimized.

Copy link

@daavve daavve commented May 31, 2018

On Perl (v5.26.2) I get the following error:

$ perl
The encoding pragma is no longer supported at line 13.
BEGIN failed--compilation aborted at line 13.

This comment has been minimized.

Copy link
Owner Author

@DavidGriffith DavidGriffith commented Apr 14, 2019

Just comment out or remove line 13. This script is now deprecated because isbndb appears to no longer offer a free account of 500 lookups a day.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment