使用bash读取和解析kodi nfo文件的XML

时间:2018-09-25 07:14:11

标签: xml bash kodi

我有一个文件夹,其中包含最初由kodi创建的电影,因此它们都包含kodi .nfo文件。 (example nfo file)

我想用bash脚本遍历文件夹,并根据包含nfo文件的数据重命名文件夹名称,以便它们遵循规则

“标题-生产国家/地区YY-类型-解析度-简短描述-三个主要演员的姓名。文件格式”

1 个答案:

答案 0 :(得分:0)

我借助以下质量检查解决了此问题:

通过以下方式读取目录:

并使用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