TOC of the series
I finish this series with a couple of new modules (each on its own file) that minimally accomplish the rest of the sub-tasks mentioned in the first post of the series.
Let's recall those tasks this time with a reference to the responsible module.
- Read the text contained in the pdf pages
- Search for the ISBN on those pages
- Make a query for the ISBN to a remote server that provides information about books.
- Parse the response of the server to represent it as a Racket data type.
The first and second tasks above are carried out by "pdf-read.rkt" with the aid of "isbn.rkt".
The third and forth tasks, in turn, are solved by "book-info.rkt".
"pdf-isbn" does the same thing as "isbn" but over pdf files. It uses the latter along with the
pdf-read package, a Racket interface to the popular
libpoppler library, available by default on Linux and, to my knowledge, on MacOS systems. You probably need to install
pdf-read before running the code. Don't worry, installing packages from DrRacket is very easy and self-explanatory.
As for relevant Racket constructs used in "pdf-read" it is worth to mention
error, that raises an exception when the pdf file doesn't exist. For testing exceptions you should use
Another interesting divergence between Racket and *SL languages has to do with
or and conditionals in general. In Racket everything there except
#false is treated as
#true. This fact allows us to use something like:
(or (extract/format 'isbn-13) (extract/format 'isbn-10)))
The module "book-info" requests information to a remote provider (via functions belonging to the
net/url package) to get bibliographic information about a book and parses that information in two phases: 1. parses the JSON response via
read-json (from the
json package), and 2. parses the output of
read-json into the structure
book-info. For the second phase I have resorted to the package
json-pointer that you may need to install on your system.
Note that only the Open Library provider is supported. Other providers like Google Books, The Library of Congress, etc. could be also supported in a similar way.
A few new frequently used Racket constructs applied by "book-info" are
define-values, which allows to bind multiple identifiers at once, and
with-handlers, that allows to handle an exception as wished.