Skip to content

Instantly share code, notes, and snippets.

@KevCui
Last active July 13, 2019 16:25
Show Gist options
  • Save KevCui/141874a19e00603b7bca5754847c16d2 to your computer and use it in GitHub Desktop.
Save KevCui/141874a19e00603b7bca5754847c16d2 to your computer and use it in GitHub Desktop.
Send ebook to Kindle using Calibre CLI tools
#!/usr/bin/env bash
#
#/ This scrip will send selected ebook to device
#/ Go to ebook folder created by calibre and run this script
#/
#/ Usage:
#/ ./calibrecli.sh "<ebook_file>" "<device_path>"
set -e
set -u
usage() {
printf "\n%b\n" "$(grep '^#/' "$0" | cut -c4-)" && exit 0
}
set_var() {
expr "$*" : ".*--help" > /dev/null && usage
if [[ -z "${1:-}" ]]; then
echo "Missing input file!" && usage
fi
if [[ -z "${2:-}" ]]; then
echo "Missing device path!" && usage
fi
_FILE="$1"
_DEVICE="$2"
_METAFILE="./metadata.opf"
_METADATA="$_DEVICE/metadata.calibre"
_THUMBNAIL="$_DEVICE/system/thumbnails"
_DOCUMENT="$_DEVICE/documents"
_COVER="./cover.jpg"
_HXSELECT=$(command -v hxselect) || "'hxselect' command not found!"
_CDB=$(command -v calibredb) || "'calibredb' command not found!"
_JQ=$(command -v jq) || "'jq' command not found!"
_CONVERT=$(command -v convert) || "'convert' command not found!"
}
generate_json() {
uuid=$($_HXSELECT -c '#uuid_id' < "$_METAFILE")
title=$(grep 'dc:title' "$_METAFILE" | sed -E 's/.*<dc:title>//;s/<.*//')
author=$(grep 'dc:creator' "$_METAFILE" | sed -E 's/.*aut">//;s/<.*//')
publisher=$(grep 'dc:publisher' "$_METAFILE" | sed -E 's/.*<dc:publisher>//;s/<.*//')
date=$(grep 'dc:date' "$_METAFILE" | sed -E 's/.*<dc:date>//;s/<.*//')
isbn=$(grep 'ISBN' "$_METAFILE" | sed -E 's/.*ISBN\">//;s/<.*//')
appid=$($_CDB list -f uuid | grep "$uuid" | awk '{print $1}' | tail -1)
size=$(wc -c "$_FILE" | awk '{print $1}')
json='{
"user_metadata": {},
"uuid": "'$uuid'",
"identifiers": {
"isbn": "'$isbn'",
"mobi-asin": "'$uuid'"
},
"mime": "application/x-mobi8-ebook",
"author_sort": "'"$author"'",
"languages": [],
"rating": null,
"pubdate": "'"$date"'",
"tags": [],
"rights": null,
"comments": null,
"user_categories": {},
"book_producer": null,
"lpath": "documents/'"$_FILE"'",
"series_index": null,
"db_id": null,
"size": '"$size"',
"author_link_map": {},
"title_sort": null,
"last_modified": "None",
"authors": [
"'"$author"'"
],
"cover": null,
"publication_type": null,
"title": "'"$title"'",
"author_sort_map": {},
"series": null,
"timestamp": "None",
"application_id": '"$appid"',
"publisher": "'"$publisher"'",
"thumbnail": null
}'
echo "$json"
}
send_cover_to_device(){
uuid=$(echo "$1" | tr '\r\n' ' ' | $_JQ -r '.uuid')
$_CONVERT "$_COVER" -resize x500 "$_THUMBNAIL/thumbnail_${uuid}_EBOK_portrait.jpg"
}
edit_metadata() {
sed -i '$ d' "$_METADATA"
sed -i '$ d' "$_METADATA"
echo " }," >> "$_METADATA"
echo "$1" >> "$_METADATA"
echo "]" >> "$_METADATA"
}
copy_ebook_to_device() {
cp "$_FILE" "$_DOCUMENT/"
}
main() {
set_var "$@"
local data
data=$(generate_json)
copy_ebook_to_device
send_cover_to_device "$data"
edit_metadata "$data"
}
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
main "$@"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment