-
-
Save douo/204923325f5f7526176108a39f038034 to your computer and use it in GitHub Desktop.
监听目录变化并自动导入 calibre
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /bin/bash | |
# set if empty | |
if [[ -z $CALIBRE_LIBRARY_PATH ]] | |
then | |
CALIBRE_LIBRARY_PATH='/mnt/s12/books/calibre' | |
fi | |
# 过滤 calibre 支持的格式,手动移除了 HTML | |
if [[ -z $CALIBRE_PATTERN ]] | |
then | |
CALIBRE_PATTERN=".*\.((AZW)|(AZW3)|(AZW4)|(CBZ)|(CBR)|(CBC)|(CHM)|(DJVU)|(DOCX)|(EPUB)|(FB2)|(FBZ)|(HTMLZ)|(LIT)|(LRF)|(MOBI)|(ODT)|(PDF)|(PRC)|(PDB)|(PML)|(RB)|(RTF)|(SNB)|(TCR)|(TXT)|(TXTZ))" | |
fi | |
# python3 输出可能不在 journalctl 显示 | |
# alias calibredb=python3 -u `which calibredb` | |
# 监听的目录,rslsync 的下载目录 | |
if [[ -z $WATCH_PATH ]] | |
then | |
WATCH_PATH='/mnt/s12/books/rslsync/' | |
fi | |
# 把目录作为 tag 的起始索引 ex: /a/b/c/x/y/z/book.pdf x、y、z 作为 tag 的话,值设为 5 | |
if [[ -z $TAG_PREFIX_INDEX ]] | |
then | |
TAG_PREFIX_INDEX=6 | |
fi | |
function add_to_calibre() | |
{ | |
file=$1 | |
IFS='/'; read -ra array <<< $file # 按目录分割为数组 | |
unset IFS # 不重置所有带 / 的字符串都会被替换为 space | |
len=`expr ${#array[@]} - $TAG_PREFIX_INDEX` | |
# 无视掉 /mnt/s3/books/rslsync 和文件名本身,剩下的作为 tags | |
tags=`echo ${array[@]:5:${len}} | sed -n 's/\s/,/gp'` | |
# 日志 | |
echo Adding ${array[-1]} ${tags} | |
if [ -n "$tags" ] | |
then | |
# --duplicates 很多书元数据缺少,同名为 Untitled 过不了 calibre 的同名检测 | |
time calibredb --library-path ${CALIBRE_LIBRARY_PATH} add -T "${tags}" "${file}" --duplicates | |
else | |
time calibredb --library-path ${CALIBRE_LIBRARY_PATH} add "${file}" --duplicates | |
fi | |
} | |
# 通过目录反查 id ,未使用 | |
# TODO identifiers 可能有多个,通过 cut 取第一个 | |
function identifiers_by_file(){ | |
ebook-meta "$1" | sed -n -e "s/Identifiers\s*:\s*\(.*\)/\1/p" | xargs -L 1 -d '\n' -I {} calibredb --library-path "$CALIBRE_LIBRARY_PATH" search identifiers:"{}" | |
} | |
# 配合 init_db | |
function process_find(){ | |
while read -r input; do | |
add_to_calibre "$input" | |
done | |
} | |
# inotifywait 的 close_write moved_to 事件 | |
function process_inotify(){ | |
while read -r log; do | |
echo $log # 打印日志 | |
echo $log >> /tmp/calibre_helper.log | |
# 日志格式 [06/07/20 23:57 CLOSE_WRITE,CLOSE] /tmp/test/atc abs.pdf | |
file=`sed -n 's/\[.*\]\s//p' <<< $log` | |
if grep -E -i -x "$CALIBRE_PATTERN" <<< $file > /dev/null | |
then | |
# 目标文件 | |
add_to_calibre "$file" | |
#else | |
# ignore | |
fi | |
done | |
} | |
# 首次运行将监听目录的当前所有电子书添加到 calibre | |
function init_db(){ | |
find "$WATCH_PATH" | grep -E -i -x "$CALIBRE_PATTERN" | process_find | |
} | |
# 监听 rslsync 目录,忽略 .sync | |
function watch(){ | |
inotifywait -e close_write -e moved_to -mr --timefmt '%d/%m/%y %H:%M' --format "[%T %e] %w%f" "$WATCH_PATH" --exclude "\.sync" | process_inotify | |
} | |
#init_db | |
watch |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment