Posted by mazet on 25 Jul 2021 in Bash
Shell
#!/bin/bash -f | |
# epubinfo - a script to retrieve info from epub | |
# Copyright (C) 2021 Laurent Mazet | |
| |
# This program is free software; you can redistribute it and/or modify | |
# it under the terms of the GNU General Public License as published by | |
# the Free Software Foundation; either version 2, or (at your option) | |
# any later version. | |
| |
# This program is distributed in the hope that it will be useful, | |
# but WITHOUT ANY WARRANTY; without even the implied warranty of | |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
# GNU General Public License for more details. | |
| |
# You should have received a copy of the GNU General Public License | |
# along with this program; if not, write to the Free Software Foundation, | |
# Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ | |
| |
# Changelog: | |
# - 1.0.0 23/07/2021 | |
# * initial version | |
| |
#DEBUG=yes | |
| |
# formating functions | |
function title () { echo -e "\033[0;1m$*\033[0;0m"; } | |
function pass () { echo -e "\033[1;32m$*\033[0;0m"; } | |
function warn () { echo -e "\033[1;33m$*\033[0;0m"; } | |
function fail () { echo -e "\033[1;31m$*\033[0;0m"; } | |
function debug () { [ "$DEBUG" = yes ] && echo -e "\033[1;34m$*\033[0;0m"; } | |
| |
# main loop | |
for file; do | |
title "Processing: $file" | |
| |
# get file | |
debug "input: $file" | |
echo $file | grep -q '^[~/]' || file="$(pwd)/$file" | |
[ -f "$file" ] || { warn "usage: $(basename $0) <file>"; exit 1; } | |
| |
# check file | |
debug "file: $file" | |
unzip -qt "$file" >&,/dev/null || { warn "$file is not a epub"; continue; } | |
| |
# get info file | |
info=$(zipinfo "$file" | awk '/content\./ || /metadata\./ { print $(NF) }') | |
[[ "$info" =~ opf ]] && type=opf | |
[[ "$info" =~ xml ]] && type=xml | |
debug "info: $info ($type)" | |
[ "$type" ] || { warn "$file is not a epub"; continue; } | |
| |
# get info | |
case $type in | |
opf) | |
unzip -c "$file" $info | \ | |
awk '/creator/ && /dc/ { gsub(/ *<[^<]*>/, ""); if (c) c=c",";c=c" "$0 } | |
/title/ && /dc/ { gsub(/ *<[^<]*>/, ""); t=" "$0 } | |
/language/ && /dc/ { gsub(/ *<[^<]*>/, ""); l=" "$0 } | |
END { printf "Author:%s\nTitle:%s\nLanguage:%s\n", c, t, l }' | |
;; | |
xml) | |
unzip -c "$file" $info | \ | |
awk '/*<name>/ { gsub(/ *<[^<]*>/, ""); if (c) c=c","; c=c" "$0 } | |
/*<title>/ { gsub(/ *<[^<]*>/, ""); t=" "$0 } | |
/*<language>/ { gsub(/ *<[^<]*>/, ""); l=" "$0 } | |
END { printf "Author:%s\nTitle:%s\nLanguage:%s\n", c, t, l }' | |
;; | |
esac | |
| |
done |