xml - 使用 bash 读取和解析 kodi nfo 文件的 XML
问题描述
我有一个文件夹,里面有最初用 kodi 创建的电影,所以它们都包含一个 kodi.nfo
文件。(示例 nfo 文件)
我想用 bash 脚本遍历文件夹,并根据 contianing nfo 文件中的数据重命名文件夹名称,以便它们遵循规则
《片名-制作国家YY-流派-解析度-简短描述-三位主要演员的名字.fileformat》
解决方案
我在这些 QA 的帮助下解决了这个问题:
读取目录:
并使用 bash 解析 xml 并使用此处的解决方案:
#!/bin/bash
read_dom () {
local IFS=\>
read -d \< ENTITY CONTENT
}
for f in */; do
if [ "$f" != "System Volume Information/" ]; then
NFO=$(find "$f" -name "*.nfo")
if [ "$NFO" != "" ]; then
if [[ -d "$f" && ! -L "$f" ]]; then
title=""
genre=""
name=""
countname=0
inactor=0
while read_dom; do
if [[ $ENTITY = "title" ]]; then title="$CONTENT"; fi
if [[ $ENTITY = "year" ]]; then year="$CONTENT"; fi
if [[ $ENTITY = "outline" ]]; then
# take only the first 100 caharacters of the ouotline for the filename
outline="${CONTENT:0:99}";
# remove last word
outline=${outline% *}"..."
fi
if [[ $ENTITY = "height" ]]; then height="$CONTENT"; fi
if [[ $ENTITY = "width" ]]; then width="$CONTENT"; fi
if [[ $ENTITY = "country" ]]; then country="$CONTENT"; fi
if [[ $ENTITY = "genre" ]] && [[ $genre = "" ]]; then genre="$CONTENT"; fi
if [[ $ENTITY = "actor" ]]; then inactor=1; fi
if [[ $inactor -eq 1 ]] && [[ $ENTITY = "name" ]] && [[ $countname -lt 3 ]]; then
name="$name $CONTENT"
countname=$(( $countname + 1 ))
fi
done < "$NFO"
# sanitize
if [[ $country = "Deutschland" ]]; then country="D"; fi
if [[ $country = "Frankreich" ]]; then country="F"; fi
if [[ $country = "Spanien" ]]; then country="SP"; fi
if [[ $country = "Vereinigtes Königreich" ]]; then country="GB"; fi
if [[ $country = "Italien" ]]; then country="I"; fi
if [[ $country = "Schweden" ]]; then country="S"; fi
if [[ $country = "Australien" ]]; then country="AUS"; fi
if [[ $country = "Dänemark" ]]; then country="DK"; fi
if [[ $country = "Russland" ]]; then country="RUS"; fi
if [[ $country = "Belgien" ]]; then country="B"; fi
if [[ $country = "East Germany" ]]; then country="DDR"; fi
if [[ $country = "Norwegen" ]]; then country="N"; fi
if [[ $country = "Griechenland" ]]; then country="GR"; fi
if [[ $country = "China" ]]; then country="CN"; fi
if [[ $country = "Hongkong" ]]; then country="HGK"; fi
if [[ $country = "Österreich" ]]; then country="Ö"; fi
if [[ $country = "Japan" ]]; then country="JAP"; fi
if [[ $country = "Kanada" ]]; then country="KAN"; fi
if [[ $country = "Tschechische Republik" ]]; then country="CZ"; fi
if [[ $country = "Vereinigte Arabische Emirate" ]]; then country="VAE"; fi
if [[ $country = "Irland" ]]; then country="IRL"; fi
if [[ $country = "Polen" ]]; then country="PL"; fi
if [[ $country = "Südafrika" ]]; then country="SAFR"; fi
if [[ $country = "United States of America" ]]; then country="USA"; fi
if [[ $country = "Vereinigte Staaten von Amerika" ]]; then country="USA"; fi
# check with egrep -v '(panien|USA| D | SP | I | IRL |mv| F | GB| VAE| Ö | DK | JAP | CZ | KAN | AUS | S | RUS | SAFR| PL | DDR | N | B | GR | HGK)'
if [[ $width = "1920" ]]; then
resolution="1080p";
elif [[ $width = "1024" ]]; then
resolution="720p";
elif [[ $width = "720" ]]; then
resolution="480p";
else
resolution=$width"x"$height;
fi
#"title - country of production YY - genre - resolution - short description - the three main actor's names.fileformat"
if [ "$title" != "" ]; then
newname="$title - $country $year - $genre - $resolution - $outline -$name"
LEN=$(echo "$newname"|wc -c)
if [ $LEN -gt 230 ]; then
echo "warning: length $LEN"
fi
echo -e " mv '$f' \n'$newname'\n"
mv -vi "$f" "$newname"
else
echo "no title in $f"
fi
fi
else
echo "no nfo in $f"
fi
fi
done
推荐阅读
- python - 我如何判断 vmware 上的 pyvmomi 造成的压力是什么?
- r - Shiny App 的 RStudio DT DataTables 包出现 Ajax 错误
- c++ - 对重载函数 find_first_not_of 的模糊调用
- python - 如果块中的标签出现 Tkinter 问题
- batch-file - 如何在 Windows 10 上的批处理块中使用 ~dp0
- python - 陷入 Python Zybook 挑战问题
- java - [Ljava.lang.String; 不能转换为 java.lang.String
- kubernetes - 尽管创建了入口资源,但未创建 GCE LoadBalancer
- html - CSS - 为列表项添加填充无法正常工作
- python - Python pandas - 显示同一列值之间的关系