-
Notifications
You must be signed in to change notification settings - Fork 8
/
kindle-sync
executable file
·151 lines (119 loc) · 4.55 KB
/
kindle-sync
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#!/bin/sh
usage(){
fmt <<EOF
DESCRIPTION
Compile and sync ebooks in ~/documents/ebooks/ to an attached Kindle device, then eject it. Epub files are compiled to mobi files and cover thumbnails are transferred automatically.
USAGE
kindle-sync [-d,--dry-run] [-c,--compile-only]
-d,--dry-run Simulate the sync and don't
eject the device aftewards.
-c,--compile-only Compile only, don't sync to
the device. Can't be used
with the -d option.
EOF
exit
}
die(){ printf "Error: %s\n" "${1}" 1>&2; exit 1; }
require(){ command -v "${1}" > /dev/null 2>&1 || { suggestion=""; if [ ! -z "$2" ]; then suggestion=" $2"; fi; die "$1 is not installed.${suggestion}"; } }
if [ $# -eq 1 ]; then if [ "$1" = "--help" ] || [ "$1" = "-h" ]; then usage; fi fi
# End boilerplate
ebooksRoot="$HOME/documents/ebooks/" # Trailing slash required
kindleRoot="/media/Kindle" # No trailing slash please!
require "rsync" "Try: apt-get install rsync"
require "ebook-convert" "Try: apt-get install calibre"
require "ebook-extract"
convertFile(){
md5=$(md5sum "$1" | awk '{ print $1 }')
filename=$(basename "$1" ".epub")
fileLocation=$(dirname "$1")
mobiPath="${fileLocation}/${filename}.compiled.mobi"
tempMobiPath="/tmp/mobi" # No trailing slash please!
echo "Compiling ${mobiPath} ..."
# First convert epub to mobi
ebook-convert "$1" "${mobiPath}" > /dev/null 2>&1
# Store the md5 sum of the epub
echo "${md5}" > "${fileLocation}/last-epub-md5"
# Extract the mobi file to get the asin
rm -rf "${tempMobiPath}" > /dev/null 2>&1
ebook-extract -d="${tempMobiPath}" "${mobiPath}"
# Get the ASIN
asin=$(grep -o -E '<meta name="ASIN" content="(.*)"' "${tempMobiPath}"/mobi7/*.opf | sed -E 's/.*content="(.*)"/\1/')
# Generate the thumbnail
rm "${fileLocation}"/thumbnail_* > /dev/null 2>&1
# Get the name of the cover image
coverPath=$(grep -o -E '<item id="cover_img" media-type=".*?" href="(.*)"' "${tempMobiPath}"/mobi7/*.opf | sed -E 's/.*href\="(.*)"/\1/')
# Resize the cover
convert "${tempMobiPath}/mobi7/${coverPath}" -resize 216x330 "${fileLocation}/thumbnail_${asin}_EBOK_portrait.jpg" > /dev/null 2>&1
}
dryRun=false
compileOnly=false
if [ $# -eq 1 ]; then
if [ "$1" = "--dry-run" ] || [ "$1" = "-d" ]; then
dryRun="true"
fi
if [ "$1" = "--compile-only" ] || [ "$1" = "-c" ]; then
compileOnly="true"
fi
fi
if [ "${compileOnly}" != "true" ]; then
if [ ! -d "${kindleRoot}" ]; then
die "Kindle not mounted."
fi
fi
find "${ebooksRoot}" -name "*.epub" | while read filePath;
do
# Ignore epub files that end in .original.epub
if [ "${filePath}" = *.original.epub ]; then
continue
fi
# Ignore epub files that end in .wip.epub
if [ "${filePath}" = *.wip.epub ]; then
continue
fi
# Do we have a cached copy of the mobi file?
fileLocation=$(dirname "${filePath}")
if ls "${fileLocation}/last-epub-md5" > /dev/null 2>&1 ; then
# Mobi exists
currentMd5=$(md5sum "${filePath}" | awk '{ print $1 }')
cachedMd5=$(cat "${fileLocation}/last-epub-md5")
# Have we made changes?
if [ "${currentMd5}" != "${cachedMd5}" ]; then
convertFile "${filePath}"
fi
else
# Mobi does not exist, create it for the first time
convertFile "${filePath}"
fi
done
# Rsync thumbnails to the device
rm -rf "/tmp/images-sync/" > /dev/null 2>&1
mkdir -p "/tmp/images-sync/" > /dev/null 2>&1
# Don't sync for a dry run
if [ "${compileOnly}" != "true" ] && [ "${dryRun}" != "true" ]; then
find "${ebooksRoot}" -name "*.jpg" | while read filePath;
do
cp "${filePath}" "/tmp/images-sync/"
done
rsync -rlcvz --itemize-changes --include="*.jpg" --delete "/tmp/images-sync/" "${kindleRoot}/system/thumbnails" > /dev/null 2>&1
fi
# Rsync books to the device
# Ignore .sdr folders and .original.mobi files
rm -rf "/tmp/mobi-sync" > /dev/null 2>&1
if [ "${compileOnly}" != "true" ]; then
if [ ! -d "${kindleRoot}/documents/ebooks" ]; then
mkdir -p "${kindleRoot}/documents/ebooks"
fi
fi
if [ "${dryRun}" = "true" ]; then
echo Starting rsync dry run ...
rsync -rlcvz --dry-run --itemize-changes --filter="P *.sdr" --include="*/" --exclude="*.original.mobi" --include="*.mobi" --exclude="*" --delete "${ebooksRoot}" "${kindleRoot}/documents/ebooks"
elif [ "${compileOnly}" != "true" ]; then
# Empty my clippings
echo "" > "${kindleRoot}/documents/My Clippings.txt"
rsync -rlcvz --itemize-changes --filter="P *.sdr" --include="*/" --exclude="*.original.mobi" --include="*.mobi" --exclude="*" --delete "${ebooksRoot}" "${kindleRoot}/documents/ebooks"
fi
if [ "${dryRun}" != "true" ] && [ "${compileOnly}" != "true" ] ; then
echo "Ejecting Kindle ..."
sudo eject "${kindleRoot}"
echo "Done."
fi