Added lyrics downloading

This commit is contained in:
Medvidek77 2025-04-20 11:24:27 +02:00
parent 9973ccc5df
commit 4a60838f93

View file

@ -2,7 +2,7 @@
. tidler.conf # Load ENVs from config file . tidler.conf # Load ENVs from config file
version="0.3.0" version="0.3.1"
if [ -n "$PROXY_URL" ]; then if [ -n "$PROXY_URL" ]; then
@ -50,6 +50,7 @@ else
max_attempts="10" # Default value max_attempts="10" # Default value
fi fi
downloadTrack() { downloadTrack() {
if [ "$#" -ge 1 ]; then if [ "$#" -ge 1 ]; then
echo "Downloading track with ID: $1" echo "Downloading track with ID: $1"
@ -59,14 +60,10 @@ downloadTrack() {
read -r id read -r id
fi fi
if [ "$#" -ge 2 ]; then
album_dir="$2"
else
album_dir=""
fi
attempt_num=1 attempt_num=1
success=false success=false
lrc_success=false
while [ $attempt_num -le $max_attempts ]; do while [ $attempt_num -le $max_attempts ]; do
json_data=$(curl -# "$proxy_url/track/?id=$id&quality=$quality") json_data=$(curl -# "$proxy_url/track/?id=$id&quality=$quality")
@ -87,11 +84,32 @@ downloadTrack() {
cover_data=$(curl -# "$proxy_url/cover/?id=$id") cover_data=$(curl -# "$proxy_url/cover/?id=$id")
cover_url=$(echo "$cover_data" | jq -r '.[]["1280"] // empty') cover_url=$(echo "$cover_data" | jq -r '.[]["1280"] // empty')
if [ -n "$cover_url" ]; then
track_number=$(echo "$json_data" | jq -r '.[0].trackNumber') if [ -n "$cover_url" ]; then
filename="$track_name.flac" date_data=$(echo "$json_data" | jq -r '.[0].streamStartDate // empty' )
cover_name="cover.png" date=$(expr substr "$date_data" 1 10)
success=true year=$(expr substr "$date_data" 1 4)
if [ -n "$year" ]; then
track_number=$(echo "$json_data" | jq -r '.[0].trackNumber')
filename="$track_name.flac"
cover_name="tidler_temp_cover_$track_name.png"
success=true
lyrics_data=$(curl -# "$proxy_url/lyrics/?id=$id")
if [ $? -eq 0 ]; then
timed_lyrics=$(echo "$lyrics_data" | jq -r '.[0].subtitles')
if [ -n "$timed_lyrics" ]; then
lrc_success=true
elif [ "$timed_lyrics" == "null" ]; then
echo "Lyrics not found"
fi
fi
fi
break break
fi fi
fi fi
@ -100,45 +118,51 @@ downloadTrack() {
fi fi
fi fi
if [ "$success" = false ]; then if [ "$success" = false ]; then
echo "Attempt $attempt_num failed. Retrying..." echo "Attempt $attempt_num failed. Retrying..."
((attempt_num++)) ((attempt_num++))
sleep "1.$(tr -cd 0-9 </dev/urandom | head -c 5)" sleep "1.$(tr -cd 0-9 < /dev/urandom | head -c 5)"
fi fi
done done
if [ "$success" = false ]; then if [ "$success" = false ]; then
echo "Failed to download after $max_attempts attempts." echo "Failed to download after $max_attempts attempts."
exit 1 exit 1
fi fi
if [ -n "$DOWNLOADS_DIR" ]; then if [ -n "$DOWNLOADS_DIR" ]; then
download_dir="$DOWNLOADS_DIR" download_dir="$DOWNLOADS_DIR"
else else
download_dir="$(pwd)" download_dir="$(pwd)"
fi fi
if [ -z "$album_dir" ]; then
final_path="$download_dir" final_path="$download_dir/$artist_name/$album_name"
else
final_path="$download_dir/$album_dir"
fi
mkdir -p "$final_path" mkdir -p "$final_path"
curl -# "$url" -o "$final_path/$filename" curl -# "$url" -o "$final_path/$filename"
curl -# "$cover_url" -o "$final_path/$cover_name" curl -# "$cover_url" -o "/tmp/$cover_name"
# Metadata
metaflac \ metaflac \
--set-tag="NAME=$track_name" \ --set-tag="NAME=$track_name" \
--set-tag="ARTIST=$artist_name" \ --set-tag="ARTIST=$artist_name" \
--set-tag="ALBUM=$album_name" \ --set-tag="ALBUM=$album_name" \
--set-tag="TRACKNUMBER=$track_number" \ --set-tag="TRACKNUMBER=$track_number" \
--import-picture-from="$final_path/$cover_name" \ ${lrc_success:+--set-tag="LYRICS=$timed_lyrics"} \
--set-tag="DATE=$date" \
--set-tag="YEAR=$year" \
--import-picture-from="/tmp/$cover_name" \
"$final_path/$filename" "$final_path/$filename"
rm "/tmp/$cover_name"
} }
searchTrack() { searchTrack() {
if [ "$#" -ge 1 ]; then if [ "$#" -ge 1 ]; then
track_name="$1" track_name="$1"
@ -268,6 +292,13 @@ searchAlbum() {
fi fi
} }
#searchArtist() {
#}
#downloadArtist() {
#}
if [ "$1" = "search" ]; then if [ "$1" = "search" ]; then
if [ "$#" -ge 2 ]; then if [ "$#" -ge 2 ]; then
@ -277,12 +308,14 @@ if [ "$1" = "search" ]; then
else else
searchTrack searchTrack
fi fi
elif [ "$1" = "download" ]; then elif [ "$1" = "download" ]; then
if [ "$#" -ge 2 ]; then if [ "$#" -ge 2 ]; then
downloadTrack "$2" downloadTrack "$2"
else else
downloadTrack downloadTrack
fi fi
elif [ "$1" = "album" ]; then elif [ "$1" = "album" ]; then
if [ "$#" -ge 2 ]; then if [ "$#" -ge 2 ]; then
shift shift
@ -291,6 +324,7 @@ elif [ "$1" = "album" ]; then
else else
searchAlbum searchAlbum
fi fi
elif [ "$1" == "version" ]; then elif [ "$1" == "version" ]; then
echo "TiDLer $version by Medvidek77" echo "TiDLer $version by Medvidek77"
@ -318,7 +352,9 @@ else
echo "2. Search single track with text" echo "2. Search single track with text"
echo "" echo ""
echo "3. Search and download whole album with text" echo "3. Search and download whole album with text"
read option read option
if [ "$option" -eq 1 ]; then if [ "$option" -eq 1 ]; then
downloadTrack downloadTrack
elif [ "$option" -eq 2 ]; then elif [ "$option" -eq 2 ]; then
@ -327,3 +363,4 @@ else
searchAlbum searchAlbum
fi fi
fi fi