Dockerizing DeKindle


DeDRM and KindleUnpack are packages designed to extract the contents from ebooks purchased through Amazon, with multi-platform point-and-click GUI wrappers. (DeDRM includes decrypters for many other platforms as well, but I don’t need any of them)

My Japanese-book-hacking workflow doesn’t play nicely with point-and-click, and now that I’ve gone to the effort of resurrecting and updating it to work with modern versions of Perl and TeXLive, I thought it was time to dig out the core of the extraction tools and repackage them in a format more useful to me.


Dockerfile

(updated to collapse into one RUN statement and remove the build tools, reducing the final image size to 48.7 MB, most of which is in /usr/lib/python2.7)

# docker build -t dekindle:latest .
FROM alpine:latest

ENV DEDRM https://github.com/apprenticeharper/DeDRM_tools/archive/v6.6.3.tar.gz
ENV DDIR DeDRM_tools-6.6.3/dedrm_src
ENV KUNPACK https://github.com/kevinhendricks/KindleUnpack/archive/v082.tar.gz
ENV KDIR KindleUnpack-082/lib

RUN apk update \
    && apk upgrade \
    && apk add curl build-base python2 python2-dev py2-pip \
    && pip --no-cache-dir install pycrypto pylzma \
    && curl -sLS $DEDRM | tar xzf - $DDIR \
    && mv $DDIR D \
    && curl -sLS $KUNPACK | tar xzf - $KDIR \
    && mv $KDIR K \
    && apk del curl build-base python2-dev py2-pip \
    && rm -rf /var/cache/apk/* \
    && mkdir /in /out

COPY dekindle.ash /root
ENTRYPOINT /root/dekindle.ash $@

dekindle.ash

Since my ebooks were downloaded for eInk Kindles tied to both the US and Japan Amazon stores, I’m passing in their serial numbers for decryption. Different methods are available for Android, Windows, and Mac clients, but they rely on now-obsolete clients, while you can still get decryptable files directly from Amazon’s web sites if you have a physical Kindle (“Download and transfer via USB”).

Output is rather verbose at the moment, for testing.

#!/bin/ash

if [ -z "$2" ]; then
    echo Usage: dekindle serial file1.azw3 ...
    exit 1
fi
INDIR=/in
OUTDIR=/out
NODRM=_nodrm

KINDLE_SERIAL=$1
shift
for i in "$@"; do
    NAME=$(basename "$i" .azw3)
    python D/k4mobidedrm.py -s $KINDLE_SERIAL "$INDIR/$NAME.azw3" $OUTDIR
    python K/kindleunpack.py -i "$OUTDIR/$NAME$NODRM.azw3" "$OUTDIR/$NAME"
done

dekindle.sh

…and here’s the wrapper that runs the docker container and tells it to decrypt and unpack all the files into the out subdirectory of the current directory.

#!/bin/bash

if [ -z "$2" ]; then
    echo Usage: dekindle.sh kindle-serial file1.azw3 ...
    exit 1
fi
mkdir -p out

docker run --rm -v $(pwd):/in -v $(pwd)/out:/out \
    dekindle:latest -- "$@"

Works great, and as a bonus, I discovered that some of the purchases I made back in 2013 have updated versions available, so I was able to quickly unpack them all.


Comments via Isso

Markdown formatting and simple HTML accepted.

Sometimes you have to double-click to enter text in the form (interaction between Isso and Bootstrap?). Tab is more reliable.