xmlbashkodi

read and parse XML of kodi nfo files with bash


I have a folder with movies initially created with kodi, so they all contain a kodi .nfo file. (example nfo file)

I want to loop through the folder with a bash script and rename the foldernames accordingly to the data in the contianing nfo files so they follow the rule

"title - country of production YY - genre - resolution - short description - the three main actor's names.fileformat"


Solution

  • I solved this with the help of these QAs:

    read the directory with:

    and parse xml with bash with the solution here:


    #!/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