Author Topic: bash script to properly rename music releases  (Read 772 times)

Offline hanshansen

  • Junior Indexer
  • **
  • Posts: 41
  • Helpful: +1/-0
bash script to properly rename music releases
« on: 2017-02-26, 01:45:35 PM »
Hi,

I solved my problem where headphones didn't find a lot of music releases with the bash script below. The problem occurs when releases are renamed to their preDB name and don't get renamed again when getAudioInfo was successful.

The script works as following:

- gets all id's from the releases table where musicinfo_id AND a predb_id exist (>0)
- loops through all id's and gets categories_id, musicinfo_id and searchname from the releases table and artist, title and year from the musicinfo table

Since the musicinfo is far from 100% correct, the script then checks if all words from the artist and title and the year appear in the search name (= the predb name). Before that, I remove things like everything in brackets [] () from the title.  If all words match, it renames the release to "artist - title (year) category", sets predb_id=0 (so it won't be processed again) and saves the old searchname to the fromname, in case I might need it in the future. You could remove that if you don't see the need of that.

After I ran the script I update the sphinx search index with "php populate_rt_indexes.php releases_rt"


Code: [Select]
#!/bin/bash
nzb_dir="/var/www/nZEDb"
config="${nzb_dir}/nzedb/config/config.php"

db_host=$(grep "DB_HOST" $config|cut -d"'" -f 4)
db_user=$(grep "DB_USER" $config|cut -d"'" -f 4)
db_password=$(grep "DB_PASSWORD" $config|cut -d"'" -f 4)
db_name=$(grep "DB_NAME" $config|cut -d"'" -f 4)

function mysqlcommand {
        mysql --skip-column-names -u $db_user -p$db_password -h $db_host $db_name -e "$1"
}


ids=$(mysqlcommand "SELECT id FROM releases WHERE musicinfo_id > 0 AND predb_id > 0;")

for id in $ids;do
        IFS=$'\n'
        data=$(mysqlcommand "SELECT categories_id,musicinfo_id,searchname FROM releases WHERE id = '$id';")
        categories_id=$(echo $data|cut -d$'\t' -f1)
        musicinfo_id=$(echo $data|cut -d$'\t' -f2)
        searchname=$(echo $data|cut -d$'\t' -f3)

        case $categories_id in
                3010)
                        category="MP3"
                        ;;
                3040)
                        category="FLAC"
                        ;;
                *)
                        category=""
                        ;;
        esac

        data=$(mysqlcommand "SELECT artist,title,year FROM musicinfo WHERE id = '$musicinfo_id';")
        artist=$(echo $data|cut -d$'\t' -f1)
        title=$(echo $data|cut -d$'\t' -f2|sed 's/\[.*\]//g;s/([^()]*)//g')
        year=$(echo $data|cut -d$'\t' -f3)
        IFS=" "
        match=1
        for i in $artist $title $year;do
                i=$(echo $i|sed 's/[][()!,&:\x27]/\ /g')
                echo "$searchname"|grep -i -e "$i" 2>&1 > /dev/null||match="0"
        done
        if [ $match -eq 1 ];then
                mysqlcommand "UPDATE releases SET searchname='$artist - $title ($year) $category',predb_id='0',fromname='$searchname' WHERE id='$id';"
                echo "$artist - $title ($year) $category                      $searchname"
        fi
done


As always, please do a backup before using this script. I don't take responsibility, if something goes wrong.
« Last Edit: 2017-02-26, 03:15:56 PM by hanshansen »