Fork me on GitHub

Herr Knedel/Great things with containers: Automatically tagging PDFs with Calibre and Docker

Created Thu, 27 Feb 2020 00:00:00 +0000 Modified Mon, 28 Mar 2022 18:27:24 +0000 Difficulty level: Can be done by anyone

248 Words

It can often be tedious to tag PDFs with the right meta info. I myself sort the downloaded PDFs from my Heise-IX subscription account, into my private Calibre library.

Because this process repeats itself every month, I came up with the following setup. I just drag and drop my new PDFs into my library. I have created a container for myself that gets my Calibre library as a volume (-v …:/books). In this container I installed the following packages:
x
+
Terminal

$ apt-get update && apt-get install -y xpdf calibre

Now my script searches for new PDFs that match the pattern “IX*.pdf”. From each PDF the first 5 pages are exported as text. Then all words are removed that appear on this word list: https://raw.githubusercontent.com/ChristianKnedel/heise-ix-reader-for-calibre/master/blacklist.txt

#!/bin/bash
export LANG=C.UTF-8
mkdir /tmp/worker1/

find /books/ -type f -iname '*.pdf' -newermt 20201201 -print0 | 
while IFS= read -r -d '' line; do 
        calibreID=$(echo  "$line" | sed -r 's/.*\(([0-9]+)\).*/\1/g')
        
        echo "bearbeite $clearName"
        echo "id $calibreID";

        cp "$line" /tmp/worker1/test.pdf

        echo "ocr "
        pdftotext -f 0 -l 5 /tmp/worker1/test.pdf /tmp/worker1/tmp.txt

        echo "text aufbereitung"
        cat /tmp/worker1/tmp.txt  | grep  -i -F -w -v -f  /books/blacklist.txt | sed -r s/[^a-zA-ZäöüÄÖÜ]+//g | grep -iE '[A-Za-z]{2,212}' |  sed ':begin;$!N;s/\n/,/;tbegin' > /tmp/worker1/final.txt

        calibredb set_metadata  --with-library /books/ --field cover:"cover.jpg" --field tags:"$(cat /tmp/worker1/final.txt) " --field series:"Heise IX" --field languages:"Deutsch" --field authors:"Heise Verkag" $calibreID
        
        rm /tmp/worker1/*
done


With the command “calibredb set_metadata” I set everything else as tags. The result looks like this:

The script is also available on Github: https://github.com/ChristianKnedel/heise-ix-reader-for-calibre .