使用 bash 读取和解析 XML 个 kodi nfo 文件
read and parse XML of kodi nfo files with bash
我有一个文件夹,其中包含最初使用 kodi 创建的电影,因此它们都包含一个 kodi .nfo
文件。 (example nfo file)
我想使用 bash 脚本遍历文件夹并根据contianing nfo文件中的数据重命名文件夹名称,以便它们遵循规则
"title - country of production YY - genre - resolution - short description - the three main actor's names.fileformat"
我在这些 QA 的帮助下解决了这个问题:
阅读目录:
并用 bash 解析 xml,解决方案在这里:
- How to parse XML in Bash?
#!/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
我有一个文件夹,其中包含最初使用 kodi 创建的电影,因此它们都包含一个 kodi .nfo
文件。 (example nfo file)
我想使用 bash 脚本遍历文件夹并根据contianing nfo文件中的数据重命名文件夹名称,以便它们遵循规则
"title - country of production YY - genre - resolution - short description - the three main actor's names.fileformat"
我在这些 QA 的帮助下解决了这个问题:
阅读目录:
并用 bash 解析 xml,解决方案在这里:
- How to parse XML in Bash?
#!/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