From 06e2d2df7729f5640739a21d8f20c3af477ce159 Mon Sep 17 00:00:00 2001 From: Medvidek77 Date: Thu, 20 Mar 2025 20:11:45 +0100 Subject: [PATCH] Added some important features and update few things --- src/tidler | 182 ++++++++++++++++++++++++++++++++++-------------- src/tidler.conf | 3 + 2 files changed, 132 insertions(+), 53 deletions(-) diff --git a/src/tidler b/src/tidler index b443cc7..e620c9a 100755 --- a/src/tidler +++ b/src/tidler @@ -34,6 +34,19 @@ if [ -n "$COVER_RESOLUTION" ]; then else cover_resolution="1280" # Default value fi + +if [ -n "$MAX_ATTEMPTS" ]; then + if [ "$MAX_ATTEMPTS" -ge 1 -a "$MAX_ATTEMPTS" -le 100 ]; then + max_attempts="$MAX_ATTEMPTS" + else + echo "Bad MAX_ATTEMPTS option. Allowed are numbers from 1 to 100." + echo "Using default value..." + + max_attempts=10 # Default value + fi +else + max_attempts=10 # Default value +fi downloadTrack() { if [ "$#" -ge 1 ]; then @@ -50,25 +63,53 @@ downloadTrack() { album_dir="" fi - json_data=$(curl -s "$proxy_url/track/?id=$id&quality=$quality") + attempt_num=1 + success=false - if [ "$?" -gt 0 ]; then - echo "Error" - exit 1 + while [ $attempt_num -le $max_attempts ]; do + json_data=$(curl -# "$proxy_url/track/?id=$id&quality=$quality") + + if [ $? -eq 0 ]; then + track_name=$(echo "$json_data" | jq -r '.[0].title // empty') + + if [ -n "$track_name" ]; then + artist_name=$(echo "$json_data" | jq -r '.[0].artist.name // empty') + + if [ -n "$artist_name" ]; then + album_name=$(echo "$json_data" | jq -r '.[0].album.title // empty') + + if [ -n "$album_name" ]; then + url=$(echo "$json_data" | jq -r '.[-1].OriginalTrackUrl // empty') + + if [ -n "$url" ]; then + cover_data=$(curl -s "$proxy_url/cover/?id=$id") + cover_url=$(echo "$cover_data" | jq -r '.[]["1280"] // empty') + + if [ -n "$cover_url" ]; then + track_number=$(echo "$json_data" | jq -r '.[0].trackNumber') + filename="$track_name.flac" + cover_name="cover.png" + success=true + break + fi + fi + fi + fi + fi + fi + + if [ "$success" = false ]; then + echo "Attempt $attempt_num failed. Retrying..." + ((attempt_num++)) + sleep 1 # TODO: random sleep time + fi + done + + if [ "$success" = false ]; then + echo "Failed to download after $max_attempts attempts." + exit 1 fi - track_name=$(echo "$json_data" | jq -r '.[0].title') - - artist_name=$(echo "$json_data" | jq -r '.[0].artist.name') - album_name=$(echo "$json_data" | jq -r '.[0].album.title') - url=$(echo "$json_data" | jq -r '.[-1].OriginalTrackUrl') - cover_data=$(curl -s "$proxy_url/cover/?id=$id") - cover_url=$(echo "$cover_data" | jq -r '.[]["1280"]') - - track_number=$(echo "$json_data" | jq -r '.[0].trackNumber') - filename="$track_name.flac" - cover_name="cover.png" - if [ -n "$DOWNLOADS_DIR" ]; then download_dir="$DOWNLOADS_DIR" else @@ -76,9 +117,9 @@ downloadTrack() { fi if [ -z "$album_dir" ]; then - final_path="$download_dir" + final_path="$download_dir" else - final_path="$download_dir/$album_dir" + final_path="$download_dir/$album_dir" fi mkdir -p "$final_path" @@ -96,7 +137,6 @@ downloadTrack() { "$final_path/$filename" } - searchTrack() { if [ "$#" -ge 1 ]; then track_name="$1" @@ -105,7 +145,7 @@ searchTrack() { read -r track_name fi track_name=$(echo "$track_name" | sed 's/ /%20/g') - tracks=$(curl -s "$proxy_url/search/?s=$track_name" | jq -r '.items[] | "\(.id) - \(.title) by \(.artist.name)"') + tracks=$(curl -# "$proxy_url/search/?s=$track_name" | jq -r '.items[] | "\(.id) - \(.title) by \(.artist.name)"') tracks_list=() while IFS= read -r list; do @@ -138,60 +178,96 @@ downloadAlbum() { else base_dir="$(pwd)" fi - json_data=$(curl -s "$proxy_url/album/?id=$id&quality=$quality") - album_name=$(echo "$json_data" | jq -r '.[0].title') - artist_name=$(echo "$json_data" | jq -r '.[0].artist.name') - album_title="$artist_name - $album_name" - album_dir="$album_title" - album_tracks_ids=$(curl -s "$proxy_url/album/?id=$id&quality=$quality" | jq -r '.[1].items[] | .item.id') - tracks_ids_list=() - while IFS= read -r list; do - tracks_ids_list+=("$list") - done <<< "$album_tracks_ids" - for track_id in "${tracks_ids_list[@]}" - do - downloadTrack "$track_id" "$album_dir" + attempt_num=1 + success=false + + while [ $attempt_num -le $max_attempts ]; do + json_data=$(curl -# "$proxy_url/album/?id=$id&quality=$quality") + if [ $? -eq 0 ]; then + album_name=$(echo "$json_data" | jq -r '.[0].title') + artist_name=$(echo "$json_data" | jq -r '.[0].artist.name') + album_title="$artist_name - $album_name" + album_dir="$album_title" + album_tracks_ids=$(echo "$json_data" | jq -r '.[1].items[] | .item.id') + tracks_ids_list=() + while IFS= read -r list; do + tracks_ids_list+=("$list") + done <<< "$album_tracks_ids" + + for track_id in "${tracks_ids_list[@]}"; do + downloadTrack "$track_id" "$album_dir" + done + success=true + break + else + echo "Attempt $attempt_num failed. Retrying..." + ((attempt_num++)) + sleep 1 + fi done + + if [ "$success" = false ]; then + echo "Failed to download album after $max_attempts attempts." + exit 1 + fi } + + searchAlbum() { if [ "$#" -ge 1 ]; then - album_name="$1" + album_name="$1" else - echo "Enter album name:" - read -r album_name + echo "Enter album name:" + read -r album_name fi album_name=$(echo "$album_name" | sed 's/ /%20/g') - albums=$(curl -s "$proxy_url/search/?al=$album_name" | jq -r '.albums.items[] | "\(.id): \(.title)"') + attempt_num=1 + success=false - albums_list=() - while IFS= read -r list; do - albums_list+=("$list") - done <<< "$albums" + while [ $attempt_num -le $max_attempts ]; do + albums=$(curl -# "$proxy_url/search/?al=$album_name" | jq -r '.albums.items[] | "\(.id): \(.title)"') - if [ "${#albums_list[@]}" -eq 1 ]; then - echo "No albums found :(" - exit 1 - fi + albums_list=() + while IFS= read -r list; do + albums_list+=("$list") + done <<< "$albums" - PS3="Please select an album: " - select t in "${albums_list[@]}"; do - if [ -n "$t" ]; then - album_id=$(echo "$t" | awk -F ': ' '{print $1}') - echo "You selected: $t" - downloadAlbum "$album_id" - break + if [ "${#albums_list[@]}" -eq 0 ]; then + echo "No albums found. Retrying... (Attempt $attempt_num/$max_attempts)" + ((attempt_num++)) + sleep 1 else - echo "Invalid selection. Please try again." + PS3="Please select an album: " + select t in "${albums_list[@]}"; do + if [ -n "$t" ]; then + album_id=$(echo "$t" | awk -F ': ' '{print $1}') + echo "You selected: $t" + downloadAlbum "$album_id" + success=true + break + else + echo "Invalid selection. Please try again." + fi + done + if [ "$success" = true ]; then + break + fi fi done + + if [ "$success" = false ]; then + echo "Failed to find and download album after $max_attempts attempts." + exit 1 + fi } + echo "Welcome to TiDLer!" && echo "" if [ "$1" = "search" ]; then diff --git a/src/tidler.conf b/src/tidler.conf index 4ec5d0d..676c976 100644 --- a/src/tidler.conf +++ b/src/tidler.conf @@ -10,3 +10,6 @@ QUALITY="LOSSLESS" # Set cover art resolution -> "1280" = 1280x1280, "640" = 640x640, "80" = 80x80 COVER_RESOLUTION="1280" + +# If there is any problem with the API, Tidler will attempt to download the track again -> possible values are "1" to "100" +MAX_ATTEMPTS="10"