-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathebook-organize
executable file
·73 lines (68 loc) · 1.99 KB
/
ebook-organize
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
#!/bin/sh
#
# NAME
#
# ebook-organize - rename and organize all ebooks in a directory
#
# SYNOPSIS
#
# ebook-organize <dir>...
#
# DESCRIPTION
#
# ebook-organize renames and classifies all ebooks in a directory.
# Each book will be put in a path of the form
#
# <directory>/<author>/<title>/<title>.<extension>
#
# Directories will be created if they don't exist.
#
# DEPENDENCIES
#
# ebook-meta(1)
#
[ "$*" ] || {
echo >&2 "usage: ${0##*/}: ebook-organize <dir>..."
exit 1
}
for dir
do
find "$dir" -type f -name '*.azw' \
-o -name '*.azw1' \
-o -name '*.azw3' \
-o -name '*.azw4' \
-o -name '*.epub' \
-o -name '*.fb2' \
-o -name '*.kf8' \
-o -name '*.lit' \
-o -name '*.mobi' \
-o -name '*.pdb' \
-o -name '*.prc' |
while IFS= read -r ebook
do
metadata=$(ebook-meta "$ebook" 2>/dev/null)
# Canonicalize author name and title.
author=$(printf "%s" "$metadata" |
grep '^Author(s) *:' |
sed 's/^Author(s) *://
s/ [[&].*//
s/\(.*\), \(.*\)/\2 \1/
s/[[:space:]]\{1,\}/ /g
s/^[[:space:]]*//
s/[[:space:]]*$//')
title=$(printf "%s" "$metadata" |
grep '^Title *:' |
sed 's/^Title *://
s/: .*$//
s/[[:space:]]\{1,\}/ /g
s/^[[:space:]]*//
s/[[:space:]]*$//')
if [ -z "$title" ] || [ -z "$author" ]
then
echo >&2 "${0##*/}: error extracting metadata from $ebook"
continue
fi
mkdir -v -p -- "${dir}/${author}/${title}"
mv -v -- "$ebook" "${dir}/${author}/${title}/${title}.${ebook##*.}"
done
done