#!/bin/sh # this script generates a variety of lists / databases. # artist.txt: a list of all artist in the torrent, including sub-units # album.txt: a list of all releases in the torrent # lossy.txt: a list of all lossy releases in the torrent # db.csv: a database of specific tags. this is required for other scripts to run properly. check() { [ ! -d "$dir" ] && echo "invalid directory!" && exit [ ! -d "$(dirname "$db")" ] && mkdir "$(dirname "$db")" [ ! -d "$(dirname "$log")" ] && mkdir "$(dirname "$log")" } artist() { printf "\033[1mgenerating artist list...\033[0m\n" [ -f "$art" ] && mv -f "$art" "$art".bak find "$dir" -mindepth 1 -maxdepth 2 -type d | grep -vE '(Lossless|Lossy)$' | sed "s|$dir/||;s|^.*/\[||;s|\]$||" | LC_COLLATE=C sort -f > "$art" } album() { printf "\033[1mgenerating album list...\033[0m\n" [ -f "$alb" ] && mv -f "$alb" "$alb".bak find "$dir" -type d -links 2 | sed "s|.*/||" | LC_COLLATE=C sort -f > "$alb" } lossy() { printf "\033[1mgenerating lossy album list...\033[0m\n" [ -f "$los" ] && mv -f "$los" "$los".bak find "$dir" -type d -links 2 | grep Lossy | sed "s|.*/||" | LC_COLLATE=C sort -f > "$los" } csv() { printf "\033[1mgenerating csv database...\033[0m\n" [ -f "$db" ] && mv -f "$db" "$db".bak echo "#TAG,ALBUMARTIST,ORGANIZATION,CONTENTGROUP" > "$db" # get artist list find "$dir" -mindepth 1 -maxdepth 2 -type d | grep -vE '(Lossless|Lossy)$' | while read -r parent; do # prefer using the most recent lossless korean release child="$(find "$parent" -mindepth 3 -maxdepth 3 -type d | grep '/Lossless/Korean/' | sort -n | tail -n 1)" # if there is none, use the most recent release of any type if [ -z "$child" ]; then child="$(find "$parent" -mindepth 3 -maxdepth 3 -type d | sort -n | tail -n 1)" fi if [ -n "$child" ]; then # prefer using a track without features file="$(find "$child" -type f -name '*.flac' | grep -ivE feat | head -n 1)" if [ "$(find "$child" -type f -name '*.flac' | wc -l)" -eq 1 ] || [ -z "$file" ]; then file="$(find "$child" -type f -name '*.flac' | head -n 1)" fi # tag sub-units echo "$parent" | grep -q '\[.*\]$' && tag="U" || tag="" artist="$(metaflac --show-tag=ARTIST "$file" 2>/dev/null | cut -d '=' -f 2)" org="$(metaflac --show-tag=ORGANIZATION "$file" 2>/dev/null | cut -d '=' -f 2)" group="$(metaflac --show-tag=CONTENTGROUP "$file" 2>/dev/null | cut -d '=' -f 2)" # only skip adding entry to the database if artist isn't found or goes over more than one line if [ -z "$artist" ] || [ "$(echo "$artist" | wc -l)" -gt 1 ]; then echo "$(basename "$parent"): cound not get artist tag. skipping..." >> "$log" && continue fi # print a warning for other missing tags [ -z "$org" ] && echo "$(basename "$parent"): could not get organization tag" >> "$log" [ -z "$group" ] && echo "$(basename "$parent"): could not get content group tag" >> "$log" printf "\n%s\t%s\t%s\t%s" "$tag" "$artist" "$org" "$group" >> "$db" else echo "$(basename "$parent"): error finding files. skipping..." && continue fi done } while getopts "d:arlch" o; do case "${o}" in d) dir="$OPTARG";; a) artist=1;; r) album=1;; l) lossy=1;; c) csv=1;; h|*) printf "usage: ./%s [OPTION]...\n\noptions: -d specify directory to scan -a generate artist list -r generate complete release list -l generate lossy release list -c generate csv database\n" "$(basename "$0")" && exit 1 esac done # set file locations log="../logs/list.log" art="../lists/artists.txt" alb="../lists/albums.txt" los="../lists/lossy.txt" db="../lists/db.csv" # check variables. check # begin new log. printf "\nscript run - %s\n" "$(date)" >> "$log" # generate lists [ -n "$artist" ] && artist [ -n "$album" ] && album [ -n "$lossy" ] && lossy [ -n "$csv" ] && csv printf "\n\033[1mscript completed! remember to check log for errors.\033[0m\n"