Kovid changed --for-machine to use JSON output. Since then, I have taken a look at this, and came up with the following.
Requirements -- the command-line tool "jshon" for parsing json. (We could probably work something out with awk/sed, but let's use an actual json tool, why don't we. Much easier.)
So let's go ahead and
Code:
sudo apt-get install jshon
Code:
#!/bin/bash
# Save path for JSON list
librarylog=/tmp/library-list.json
# Generate JSON of library formats
calibredb list -s "not formats:\"=EPUB\"" --for-machine -f formats > $librarylog
echo "finished saving list of books"
# How many books do we have
number_of_books=$(jshon -l < $librarylog)
## Let's do the conversion
# Loop through all indices of the booklist
for ((i=0;i<=number_of_books-1;i++)); do
# Where is the book.FMT
bookpath=$(jshon -e $i -e "formats" -e 0 -u < $librarylog)
# Convert to EPUB and leave in current dir
ebook-convert "$bookpath" ".epub"
done
Either run this from the directory all the books should go in, or add
cd /path/to/savedir
to the code.
We can give the code acrobatics, and save the format to calibre using the following for loop instead:
Code:
# What is the book's id #
book_id=$(jshon -e $i -e "id" -u < $librarylog)
# Where is the book.FMT
bookpath=$(jshon -e $i -e "formats" -e 0 -u < $librarylog)
# convert to EPUB and save to temp
ebook-convert "$bookpath" "/tmp/${book_id}.epub"
# Add new format to calibre book record
calibredb add_format "$book_id" "/tmp/${book_id}.epub"