A Night at the Book Repository
Posted on May 26, 2021
It is a lot of work to write out a catalog when you hoard books like you’ll ever have time to read them all, so I came up with a scheme to get the book details, cover, and author details from scanning the UPC.
Every UPC database I could find had some kind of pay model attached to it, making it difficult to justify putting in the work if I wasn’t going to get to scanning right that minute. Thankfully, the good people at openlibrary.org exist and made a simple to use API with the terms of use that amounts to “do not spam our servers.”
Needed dependencies for the script were:
- bash - for bashing
- wget - for web getting
- jq - for json querying
- sftp - for secure FTP putting
- sshpass - for giving a password to SFTP
the script will wait for barcodes to be scanned (or typed), pull the information from the UPC for the book, then pull the author and book cover if available, deletes the JPG cover if it is shows up as a GIF (not sure what is up there- something openlibrary is doing), and will upload to your SFTP server once you type “quit” instead of entering a UPC.
I’m not particularly fond of JSON, so I put it in a .md file, pipe delimiting for later PHP goodness.
Did you find this useful in any way, or have you come up with your own solution? How would you improve my script? Email me or comment below, I’d like to hear from you!
#!/usr/bin/env bash
## Purpose: to generate an inventory of the books I own.
## Many thanks to openlibrary.org for making this possible.
CACHEDIR=$HOME/.cache/bookinventory
bookdbmd=$HOME/Sync/booksdb.md
authordbmd=$HOME/Sync/authorsdb.md
red=$'\e[1;31m'
green=$'\e[1;32m'
normalcol="$(tput sgr0)"
ftp_dir=
ftp_usr=
export SSHPASS=
ftp_host=
clear
echo -e "Start scanning barcodes to continue.\nType \"quit\" to finish.\n\n"
while :
do
[ ! -d $CACHEDIR ] && mkdir $CACHEDIR
[ ! -f $bookdbmd ] && touch $bookdbmd
[ ! -d $CACHEDIR/cover ] && mkdir $CACHEDIR/cover
echo -n "$green UPC: $normalcol"
read -r
UPC=$REPLY
if [ "$UPC" -eq "quit" ]; then
break
fi
if [ $(grep $UPC $bookdbmd) ];
then
echo book already scanned.
else
bookJSON=$(wget -qO - https://openlibrary.org/isbn/$UPC.json)
title=$(echo $bookJSON |jq -r '.title')
subjects=$(echo $bookJSON |jq -r '.subjects[0]')
number_of_pages=$(echo $bookJSON |jq -r '.number_of_pages')
authors=$(echo $bookJSON |jq -r '.authors[0].key')
notes=$(echo $bookJSON |jq -r '.notes.value')
authorID=$(echo $authors |cut -d"/" -f3)
publishers=$(echo $bookJSON |jq -r '.publishers[0]')
authorJSON=$(wget -qO - https://openlibrary.org$authors.json)
author=$(echo $authorJSON |jq -r '.name')
bio=$(echo $authorJSON |jq -r '.bio.value')
birth_date=$(echo $authorJSON |jq -r '.birth_date')
death_date=$(echo $authorJSON |jq -r '.death_date')
[ ! -f $CACHEDIR/cover/$UPC-L.jpg ] && wget -qO $CACHEDIR/cover/$UPC-L.jpg http://covers.openlibrary.org/b/isbn/$UPC-L.jpg
if [ $(cat $CACHEDIR/cover/$UPC-L.jpg |head -1 |grep -a GIF89|tr -d '\0') ];
then
rm $CACHEDIR/cover/$UPC-L.jpg
fi
if [ -z $title ];
then
echo "$red Title not available."
else
echo "$UPC|$title|$authorID|$publishers|$number_of_pages|$subjects|${notes//[$'\r\n']}" >> $bookdbmd
if [ ! $(grep $authorID $authordbmd) ];
then
echo "$authorID|$author|$birth_date|$death_date|${bio//[$'\r\n']}" >> $authordbmd
fi
fi
fi
done
cp $bookdbmd $CACHEDIR/booksdb_bak.md
cat $CACHEDIR/booksdb_bak.md |sort > $bookdbmd
cd $HOME/Sync
sshpass -e sftp -oBatchMode=no -b - $ftp_usr@$ftp_host << !
cd $ftp_dir
put booksdb.md
put authorsdb.md
dir
bye
!
cd $HOME/.cache/bookinventory/cover
sshpass -e sftp -oBatchMode=no -b - $ftp_usr@$ftp_host << !
cd $ftp_dir/covers
put *.jpg
dir
bye
!
exit $?
Archives
- February 2022 (1)
- August 2021 (2)
- May 2021 (2)
- April 2021 (1)
- January 2021 (2)
- December 2020 (2)
- November 2020 (2)
- August 2020 (4)
Comments (0)
Add a Comment