Skip to content

Instantly share code, notes, and snippets.

@daktak
Last active February 18, 2023 19:24
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save daktak/8347325 to your computer and use it in GitHub Desktop.
Save daktak/8347325 to your computer and use it in GitHub Desktop.
Sabnzbd script to auto convert your epubs to mobis and send to kindle account using Calibre
#!/bin/bash
#based off
#https://github.com/ghuntley/sabToCalibre
#Requires Calibre, sabnzbd
#latest version at
#https://gist.github.com/daktak/8347325
DOWNLOAD_DIRECTORY="/pub/sabnzbd/books"
CALIBRE_PATH=/usr/bin
EXPECTED_ARGS=7
PORT="25"
SERVER="localhost"
ENCRYPTION_METHOD="NONE"
KINDLE_EMAIL="kindle_user@kindle.com"
FROM_EMAIL="authorized@email.com"
PROFILE="kindle_pw"
CONVERT_FORMAT="cbz
cbr
cbc
chm
epub
fb2
lit
lrf
odt
prc
pdb
pdf
pml
rb
rtf
snb
tcr"
OUTPUT_FORMAT="mobi"
if [ $# -ne $EXPECTED_ARGS ]
then
echo "Usage: `basename $0` {arg}"
cat <<'END_HEREDOC'
1 The final directory of the job (full path)
2 The original name of the NZB file
3 Clean version of the job name (no path info and ".nzb" removed)
4 Indexer's report number (if supported)
5 User-defined category
6 Group that the NZB was posted in e.g. alt.binaries.x
7 Status of post processing. 0 = OK, 1=failed verification, 2=failed unpack, 3=1+21
END_HEREDOC
exit 1
fi
# Test to ensure the directory which was passed on the command line from SABz exists
if [ ! -d "$1" ]
then
echo "`basename $0`: Directory does not exist ($1)"
exit 1
fi
if [[ $1 != *$DOWNLOAD_DIRECTORY* ]]
then
echo "`basename $0`: Unsafe directory specified ($1)"
exit 1
fi
# Only process successfully completed downloads
if [ $7 -eq 0 ]
then
echo "`basename $0`: Converting $3 to $OUTPUT_FORMAT...."
#$CALIBRE_PATH/calibredb add --one-book-per-directory --recurse "$1"
shopt -s nullglob
for EXT in $CONVERT_FORMAT; do
for n in "$1"/*.$EXT; do
in=$n
out=${n%.*}.$OUTPUT_FORMAT
#echo " In: $in "
#echo " Out: $out";
#remove xvfb-run if you dont have X or dont mind SVG
xvfb-run $CALIBRE_PATH/ebook-convert "${in}" "${out}" --output-profile $PROFILE;
done
done
RC=$?
if [ $RC -eq 0 ]
then
echo "`basename $0`: Directory does not exist ($1)"
exit 1
fi
if [[ $1 != *$DOWNLOAD_DIRECTORY* ]]
then
echo "`basename $0`: Unsafe directory specified ($1)"
exit 1
fi
# Only process successfully completed downloads
if [ $7 -eq 0 ]
then
echo "`basename $0`: Converting $3 to $OUTPUT_FORMAT...."
#$CALIBRE_PATH/calibredb add --one-book-per-directory --recurse "$1"
shopt -s nullglob
for EXT in $CONVERT_FORMAT; do
for n in "$1"/*.$EXT; do
in=$n
out=${n%.*}.$OUTPUT_FORMAT
#echo " In: $in "
#echo " Out: $out";
#remove xvfb-run if you don't have X for SVG
xvfb-run $CALIBRE_PATH/ebook-convert "${in}" "${out}" --output-profile $PROFILE;
done
done
RC=$?
if [ $RC -eq 0 ]
then
echo "`basename $0`: Sending to Kindle...."
cd "$1";
shopt nullglob
for n in *.$OUTPUT_FORMAT; do
#zip it to try and squeeze through email if larger file type
out=$3.zip
zip "${out}" "${n}"
$CALIBRE_PATH/calibre-smtp -r $SERVER --port $PORT -e $ENCRYPTION_METHOD $FROM_EMAIL $KINDLE_EMAIL 'Attached' -a "$out";
done
RC=$?
if [ $RC -eq 0 ]
then
echo "`basename $0`: Success!"
exit 0
else
echo "`basename $0`: Failed to send to Kindle"
exit 1
fi
else
echo "`basename $0`: Failed to convert $3 to $OUTPUT_FORMAT (RC=$RC)...."
exit 1
fi
fi
else
echo "`basename $0`: Incomplete download detected (RC=$7 || 0 = OK, 1=failed verification,
2=failed unpack, 3=1+21) removing..."
exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment