-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathpodbook
executable file
·337 lines (277 loc) · 9.32 KB
/
podbook
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
#!/bin/bash
readonly program="$(basename "${0}")"
readonly audio_extentions=('aac' 'm4a' 'm4b' 'mp3')
readonly audio_files_regex="$(sed 's/ /|/g;s/^/^.*\\.(/;s/$/)$/' <<< "${audio_extentions[@]}")"
readonly extraction_dir="$(mktemp -d)"
function usage {
echo "
Usage:
${program} [options] <directory...>
Options:
-t <string>, --title <string> Book title. Defaults to trying to extract it from the first audio file or the directory’s name (in that order). In the latter case, '_' will be interpreted as ':'.
-a <string>, --author <string> Book author. Defaults to trying to extract it from the first audio file.
-c <url>, --cover <url> URL or path to image of book cover. Defaults to trying to extract it from the first audio file.
-h, --help Show this help.
" | sed -E 's/^ {4}//'
}
function color_message {
local color="${1}"
local message="${2}"
local -r all_colors=('black' 'red' 'green' 'yellow' 'blue' 'magenta' 'cyan' 'white')
for i in "${!all_colors[@]}"; do
if [[ "${all_colors[${i}]}" == "${color}" ]]; then
local -r color_index="${i}"
echo -e "$(tput setaf "${i}")${message}$(tput sgr0)"
break
fi
done
if [[ -z "${color_index}" ]]; then
echo "${FUNCNAME[0]}: '${color}' is not a valid color."
exit 1
fi
}
function bold_message {
echo "$(tput bold)${1}$(tput sgr0)"
}
function fail_message {
color_message 'red' "${1}" >&2
exit 1
}
function warning_message {
color_message 'yellow' "${1}"
}
function absolute_path {
local -r relative_path="${1}"
if [[ -e "${relative_path}" ]]; then
if [[ -d "${relative_path}" ]]; then
cd "${relative_path}" || return 1
pwd -P
else
cd "$(dirname "${relative_path}")" || return 1
echo "$(pwd -P)/$(basename "${1%/}")"
fi
else
echo "${FUNCNAME[0]}: no such file or directory: ${relative_path}" >&2
return 1
fi
}
function is_number {
[[ "${1}" =~ ^[0-9]+$ ]]
}
function fail_if_no_arguments {
if [[ "${#}" -eq 0 ]]; then
usage
exit 1
fi
}
function fail_unless_dir {
[[ -d "${1}" ]] || fail_message 'You need to point the script at a directory.'
}
function depends_on {
local -r all_deps=("${@}")
local missing_deps=()
for dep in "${all_deps[@]}"; do
hash "${dep}" 2> /dev/null || missing_deps+=("${dep}")
done
if [[ "${#missing_deps[@]}" -gt 0 ]]; then
echo 'Missing required tools:' >&2
printf ' %s\n' "${missing_deps[@]}" >&2
exit 1
fi
}
function upload_file {
local file_name="${1}"
# Escape quotes, so we can quote curl's "--form"
# to allow for filenames with commas and semicolons
readonly escaped_file_name="${file_name//\"/\\\"}"
echo "Uploading $(basename "${file_name}")…" >&2
curl --globoff --progress-bar --form "file=@\"${escaped_file_name}\"" 'https://0x0.st'
}
function extract_title {
local file_name book_dir book_title
readonly file_name="${1}"
readonly book_dir="${2}"
readonly book_title="$(exiftool -ignoreMinorErrors -binary -album "${file_name}")"
[[ -n "${book_title}" ]] && echo -n "${book_title}" || sed 's/_/:/g' < <(basename "${book_dir}")
}
function extract_author {
local file_name book_author
readonly file_name="${1}"
readonly book_author="$(exiftool -ignoreMinorErrors -binary -artist "${file_name}")"
[[ -n "${book_author}" ]] && echo -n "${book_author}" || request_author
}
function request_author {
local book_author
echo 'The book author could not be automatically determined.' >&2
read -rp 'Insert book author: ' book_author
echo -n "${book_author}"
}
function extract_cover {
local file_name book_dir book_cover tmp_cover image_ext
readonly file_name="${1}"
readonly book_dir="${2}"
readonly tmp_cover="${extraction_dir}/Cover"
exiftool -ignoreMinorErrors -binary -picture "${file_name}" > "${tmp_cover}"
readonly image_ext="$(file --brief --extension "${tmp_cover}" | cut -d'/' -f1)"
readonly book_cover="${tmp_cover}.${image_ext}"
mv "${tmp_cover}" "${book_cover}"
if [[ -s "${book_cover}" ]]; then # If 'exiftool' did't extract a cover, the file will be empty
upload_file "${book_cover}"
else
request_cover
fi
}
function request_cover {
local book_cover
echo 'The book cover could not be automatically extracted.' >&2
read -rp 'Insert URL or local path to cover image: ' book_cover
if [[ -f "${book_cover}" ]]; then
upload_file "${book_cover}"
else
echo -n "${book_cover}"
fi
}
function extract_duration {
local file_name file_duration
readonly file_name="${1}"
readonly file_duration="$(exiftool -ignoreMinorErrors -duration "${file_name}" | awk '{print $3}')" # Using '-binary' here would give the result in microseconds
echo -n "${file_duration}"
}
function extract_mime {
local file_name file_mime
readonly file_name="${1}"
readonly file_mime="$(file --mime-type --brief "${file_name}")"
echo -n "${file_mime}"
}
function extract_size {
local file_name file_size
readonly file_name="${1}"
readonly file_size="$(du "${file_name}" | perl -pe 's/\t.*//')"
echo -n "${file_size}"
}
function inform_details {
local book_title book_author book_cover
readonly book_title="${1}"
readonly book_author="${2}"
readonly book_cover="${3}"
echo "
Book details:
$(bold_message 'title:') ${book_title}
$(bold_message 'author:') ${book_author}
$(bold_message 'cover image:') ${book_cover}
" | sed -E 's/^ {4}//' >&2
}
function start_feed {
local book_title book_author book_cover book_feed book_dir book_duration
readonly book_title="${1}"
readonly book_author="${2}"
readonly book_cover="${3}"
readonly book_feed="${4}"
readonly book_dir="${5}"
readonly book_duration="$(human-media-time --unit h "${book_dir}")h"
echo "
<rss xmlns:dc='http://purl.org/dc/elements/1.1/' xmlns:sy='http://purl.org/rss/1.0/modules/syndication/' xmlns:admin='http://webns.net/mvcb/' xmlns:atom='http://www.w3.org/2005/Atom/' xmlns:rdf='http://www.w3.org/1999/02/22-rdf-syntax-ns#' xmlns:content='http://purl.org/rss/1.0/modules/content/' xmlns:itunes='http://www.itunes.com/dtds/podcast-1.0.dtd' version='2.0'>
<channel>
<title>${book_title}</title>
<description>${book_duration}</description>
<image><url>${book_cover}</url></image>
<pubDate>$(date -R)</pubDate>
<itunes:author>${book_author}</itunes:author>
<itunes:type>serial</itunes:type>
" > "${book_feed}"
}
function add_to_feed {
local item_title file_counter file_time file_duration file_mime file_size file_url book_feed
item_title="${1}"
file_counter="${2}"
file_time="${3}"
file_duration="${4}"
file_mime="${5}"
file_size="${6}"
file_url="${7}"
readonly book_feed="${8}"
echo "
<item>
<title>${item_title}</title>
<enclosure url='${file_url}' length='${file_size}' type='${file_mime}' />
<itunes:duration>${file_duration}</itunes:duration>
<itunes:season>1</itunes:season>
<itunes:episode>${file_counter}</itunes:episode>
<pubDate>${file_time}</pubDate>
</item>
" >> "${book_feed}"
}
function end_feed {
local -r book_feed="${1}"
echo "
</channel>
</rss>
" >> "${book_feed}"
}
function main {
local book_dir book_feed first_audio_file book_title book_author book_cover file_counter file_time file_duration file_mime file_size item_title file_url
readonly book_dir="$(absolute_path "${1}")"
fail_unless_dir "${book_dir}"
book_feed="${extraction_dir}/feed.rss"
readonly first_audio_file="$(find -E "${book_dir}" -iregex "${audio_files_regex}" | head -1)"
[[ -n "${given_title}" ]] && readonly book_title="${given_title}" || readonly book_title="$(extract_title "${first_audio_file}" "${book_dir}")"
[[ -n "${given_author}" ]] && readonly book_author="${given_author}" || readonly book_author="$(extract_author "${first_audio_file}")"
[[ -n "${given_cover}" ]] && readonly book_cover="${given_cover}" || readonly book_cover="$(extract_cover "${first_audio_file}" "${book_dir}")"
inform_details "${book_title}" "${book_author}" "${book_cover}"
start_feed "${book_title}" "${book_author}" "${book_cover}" "${book_feed}" "${book_dir}"
file_counter='0'
while IFS= read -r -d '' file; do
file_counter="$(bc <<< "${file_counter} + 1")"
file_time="$(date -R -v +"${file_counter}"M)"
file_duration="$(extract_duration "${file}")"
file_mime="$(extract_mime "${file}")"
file_size="$(extract_size "${file}")"
item_title="$(basename "${file%.*}")"
file_url="$(upload_file "${file}")"
add_to_feed "${item_title}" "${file_counter}" "${file_time}" "${file_duration}" "${file_mime}" "${file_size}" "${file_url}" "${book_feed}"
done < <(find -E "${book_dir}" -iregex "${audio_files_regex}" -print0 | sort --zero-terminated)
end_feed "${book_feed}"
echo
upload_file "${book_feed}"
}
depends_on 'exiftool' 'human-media-time'
# Options
args=()
while [[ "${1}" ]]; do
case "${1}" in
-h | --help)
usage
exit 0
;;
-t | --title)
readonly given_title="${2}"
shift
;;
-a | --author)
readonly given_author="${2}"
shift
;;
-c | --cover)
readonly given_cover="${2}"
shift
;;
--)
shift
args+=("${@}")
break
;;
-*)
echo "Unrecognised option: ${1}"
exit 1
;;
*)
args+=("${1}")
;;
esac
shift
done
set -- "${args[@]}"
fail_if_no_arguments "${@}"
for dir in "${@}"; do
main "${dir}"
done