From 902b6414a5801739168f0fb1dcdcd8882583374e Mon Sep 17 00:00:00 2001 From: DarkCat09 Date: Fri, 3 Feb 2023 12:06:41 +0400 Subject: [PATCH] Tagging, 755 mode --- .convert_helper.sh | 0 .gitignore | 1 + .id3tag_helper.sh | 81 ++++++++++++++++++++++++++++++++++++++++++++++ id3tag.sh | 22 +++++++++++++ 4 files changed, 104 insertions(+) mode change 100644 => 100755 .convert_helper.sh create mode 100755 .id3tag_helper.sh create mode 100755 id3tag.sh diff --git a/.convert_helper.sh b/.convert_helper.sh old mode 100644 new mode 100755 diff --git a/.gitignore b/.gitignore index ee0306b..e7b5387 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ files/ convert/ +tagged/ .vscode/ diff --git a/.id3tag_helper.sh b/.id3tag_helper.sh new file mode 100755 index 0000000..0c8e2a0 --- /dev/null +++ b/.id3tag_helper.sh @@ -0,0 +1,81 @@ +#!/usr/bin/env bash +# Should not be called manually + +conv_title () { + echo "$1" | \ + # remove directory name + sed -E 's#\./(convert|files)/##' | \ + # remove video ID and ext + sed 's/---[A-Za-z0-9_-]*\.[A-Za-z0-9]*//' | \ + # underscore -> space + sed 's/_/ /g' | \ + # remove "(Official Audio)" + sed -E 's/\(.*\)//' | \ + # trim spaces + xargs +} + +rm_quotes () { + cat | sed 's/^"//' | sed 's/"$//' +} + +title=$(conv_title "$2") +title_prev="$title" + +echo 'Correct the song title if needed:' +read -e -r -i "$title" title +title=${title:-$title_prev} + +echo 'Searching on Genius' + +link="https://genius.com/api/search/multi?q=${title// /%20}" +echo "URL: $link" + +song=$(curl -sL "$link" | jq '.response.sections[1].hits[0].result') +title=$(echo "$song" | jq '.title' | rm_quotes) +artist=$(echo "$song" | jq '.primary_artist.name' | rm_quotes) +year=$(echo "$song" | jq '.release_date_components.year') +page_url=$(echo "$song" | jq '.url' | rm_quotes) + +echo "Title: $title" +echo "Artist: $artist" +echo "Lyrics: $page_url" + +echo 'Parsing lyrics page' + +page=$(curl -sL "$page_url") +album=$(echo "$page" | pup -p 'a[class^="PrimaryAlbum__Title"] text{}' | sed -E 's#\([0-9]+\)$##' | xargs) +tracknum=$(echo "$page" | pup -p 'div[class^="HeaderTracklist__AlbumWrapper"]' | grep -oE 'Track [0-9]+' | grep -oE '[0-9]+') +trackall=$(echo "$page" | pup -p 'ol[class^="AlbumTracklist__Container"] > li:last-child div[class^="AlbumTracklist__TrackNumber"] text{}' | grep -oE '[0-9]+') +lyrics=$(echo "$page" | pup -p 'div[data-lyrics-container="true"] text{}' | sed 's#^\[#\n[#') + +# remove first blank line +if [[ $(echo "$lyrics" | sed -n '1p') == "" ]]; then + lyrics=$(echo "$lyrics" | sed '1d') +fi + +echo "Album: $album" + +if [[ $1 == 1 ]]; then + newdir="./tagged/${artist}/${album}" + mkdir -p "$newdir" + newpath="${newdir}/${tracknum}. ${title}.mp3" + echo "Copying to $newpath" + cp -f "$2" "$newpath" +else + newpath="$2" +fi + +echo +echo "$lyrics" +echo + +echo 'Adding ID3v2 tags' +mid3v2 \ +--artist "$artist" \ +--album "$album" \ +--song "$title" \ +--year "$year" \ +--track "${tracknum}/${trackall}" \ +--USLT "$lyrics" \ +"$newpath" diff --git a/id3tag.sh b/id3tag.sh new file mode 100755 index 0000000..376c59f --- /dev/null +++ b/id3tag.sh @@ -0,0 +1,22 @@ +#!/usr/bin/env bash + +ask () { + read -r -p "[Y/n] " answer + if [[ $answer =~ [NnНн] ]]; then + echo 0 + else + echo 1 + fi +} + +echo "Enter directory where your music is located:" +read -e -r -i "./convert" directory +directory="${directory:-./convert}" + +echo +echo "Copy files into ./tagged/artist/album directory?" +copy_arg=$(ask) +echo + +find "$directory" -type f -name "*.mp3" -exec \ +bash ./.id3tag_helper.sh "$copy_arg" {} \;