From e38d1df07b3c0fbafb2dcc08428cb246ec72bbf0 Mon Sep 17 00:00:00 2001 From: Matheus Albino Date: Sun, 8 Dec 2024 18:45:54 -0300 Subject: [PATCH] Many upgrades for images-indexer.sh --- scripts/shell/images-indexer.sh | 381 ++++++++++++++++++++++---------- 1 file changed, 265 insertions(+), 116 deletions(-) diff --git a/scripts/shell/images-indexer.sh b/scripts/shell/images-indexer.sh index 8fa7c04..3c8223c 100755 --- a/scripts/shell/images-indexer.sh +++ b/scripts/shell/images-indexer.sh @@ -6,9 +6,14 @@ show_help() { echo "Options:" echo " --help | -h (optional) Display script's help text" echo " --filter-mode | -f (optional) The script iterate over the files of a selected folder and store the ones that should be deleted" + echo " --urls | -u (optional) List of urls to iterate with preview. Separation by comma (,)" + echo " Also use -i to make the script ask which folder to use for every url passed" } filter_mode=false +urls_list_arg="" +interactive_mode=false + while [ "$#" -gt 0 ]; do case "$1" in --help | -h) @@ -19,11 +24,19 @@ while [ "$#" -gt 0 ]; do filter_mode=true shift ;; + --urls | -u) + urls_list_arg="$2" + shift + ;; + -i) + interactive_mode=true + shift 2 + ;; *) shift ;; esac done -commands_to_check=(gallery-dl gdl.sh feh xdotool) +commands_to_check=(gallery-dl gdl.sh feh xdotool stoml) commands_not_found=() for command in ${commands_to_check[@]}; do @@ -42,8 +55,7 @@ if [ ${#commands_not_found[@]} -ne 0 ]; then exit 1 fi -cur_dir=$(pwd) -furry_commission_ideas_path=/mnt/e/clouds/nextcloud/furry-downloads +furry_commission_ideas_path=/mnt/e/home/downloads/furry-downloads-2 furry_commission_ideas_urls_filename="urls.txt" scripts_path=/home/cloud/repos/personal-devboot/scripts/shell @@ -57,72 +69,72 @@ if [ ! -d $scripts_path ]; then exit 1 fi -input_media_url() { - read -p "[INFO] Please inform the media's url: " media_url - echo "$media_url" +convert_csv_to_array() { + csv_arg=$1 + res_arr=$(echo $csv_arg | tr ',' "\n") + echo $res_arr } -media_url='' +read_command() { + message=$@ -if ! $filter_mode; then - media_url=$(input_media_url) + read -p "${message}: " media_url + echo $media_url +} + +input_media_url() { + read_command_message="Please inform a URL" + + media_url=$(read_command $read_command_message) while [ -z "$media_url" ]; do - media_url=$(input_media_url) + media_url=$(read_command $read_command_message) done # remove trailing slashes media_url=$(sed 's:/*$::' <<<"$media_url") - url_regex='(https?)://[-[:alnum:]\+&@#/%?=~_|!:,.;]*[-[:alnum:]\+&@#/%=~_|]' - if [[ ! $media_url =~ $url_regex ]]; then - echo "The URL informed is not valid." - exit 1 + echo "$media_url" +} + +urls_list=() + +fill_urls_list_array() { + if [ ! -z "$urls_list_arg" ]; then + urls_list=($(convert_csv_to_array "$urls_list_arg")) + else + if ! $filter_mode; then + media_url=$(input_media_url) + urls_list=("${media_url}") + fi fi -fi +} + +fill_urls_list_array cd $furry_commission_ideas_path -ignore_paths=("artists" "kinks" "ideas" "irl") -ignore_paths_depth_2=("artists" "favorites") - -find_command="find * -type d" - -for path in "${ignore_paths[@]}"; do - find_command+=" -not -path $path" -done - -for path in "${ignore_paths_depth_2[@]}"; do - find_command+=" -not -path $path/\*" -done - -existing_folders=($(eval $find_command)) - input_create_folder() { read -p "[INFO] Please create one: " new_foldername echo "$new_foldername" } -selected_folder='' - -if [ -z "${existing_folders}" ]; then - echo "[INFO] There are no folders in the 'furry-commission-ideas' folder." - - new_foldername=$(input_create_folder) - while [ -z "$new_foldername" ]; do - new_foldername=$(input_create_folder) - done - - mkdir --parents "$new_foldername" - selected_folder="$new_foldername" -fi - input_create_folder() { - read -p "[INFO] Please informe a name for the new folder: " new_foldername + read -p "[INFO] Please inform a name for the new folder: " new_foldername echo "$new_foldername" } -input_select_folder() { +input_select_folder_read_command() { read -p "[INFO] Please inform the desired option: " selected_option + echo "$selected_option" +} + +input_select_folder() { + read_command_message="Please select a folder" + + selected_folder=$(read_command $read_command_message) + while [ -z "$selected_folder" ]; do + selected_folder=$(read_command $read_command_message) + done if [ "$selected_option" == 'n' ]; then selected_folder=$(input_create_folder) @@ -138,71 +150,112 @@ input_select_folder() { echo "$selected_folder" } -should_colored_output=true +show_folder_selection_list_and_fill_selected_folder_variable() { + should_colored_output=true -if $should_colored_output; then - # Define color codes for up to 6 levels - color_reset="\033[0m" - color_level_1="\033[1;34m" # Blue for the first level - color_level_2="\033[1;32m" # Green for the second level - color_level_3="\033[1;36m" # Cyan for the third level - color_level_4="\033[1;33m" # Yellow for the fourth level - color_level_5="\033[1;35m" # Magenta for the fifth level - color_level_6="\033[1;31m" # Red for the sixth level + if $should_colored_output; then + # Define color codes for up to 6 levels + color_reset="\033[0m" + color_level_1="\033[1;34m" # Blue for the first level + color_level_2="\033[1;32m" # Green for the second level + color_level_3="\033[1;36m" # Cyan for the third level + color_level_4="\033[1;33m" # Yellow for the fourth level + color_level_5="\033[1;35m" # Magenta for the fifth level + color_level_6="\033[1;31m" # Red for the sixth level - # Array of colors to apply to subfolder levels - colors=("$color_level_1" "$color_level_2" "$color_level_3" "$color_level_4" "$color_level_5" "$color_level_6") + # Array of colors to apply to subfolder levels + colors=("$color_level_1" "$color_level_2" "$color_level_3" "$color_level_4" "$color_level_5" "$color_level_6") - if [ -z "$selected_folder" ]; then - printf "\n" - echo "[INFO] Folders:" - for ((i = 0; i < ${#existing_folders[@]}; i++)); do - foldername=${existing_folders[$i]} + if [ -z "$selected_folder" ]; then + printf "\n" + echo "[INFO] Folders:" + for ((i = 0; i < ${#existing_folders[@]}; i++)); do + foldername=${existing_folders[$i]} - # Split the foldername by slashes and color each part - IFS='/' read -ra parts <<<"$foldername" - colored_foldername="${colors[0]}${parts[0]}${color_reset}" - for j in "${!parts[@]}"; do - if [ $j -gt 0 ]; then - # Apply colors cyclically based on the depth level - color_index=$((j % ${#colors[@]})) - colored_foldername+="/${colors[$color_index]}${parts[$j]}${color_reset}" - fi + # Split the foldername by slashes and color each part + IFS='/' read -ra parts <<<"$foldername" + colored_foldername="${colors[0]}${parts[0]}${color_reset}" + for j in "${!parts[@]}"; do + if [ $j -gt 0 ]; then + # Apply colors cyclically based on the depth level + color_index=$((j % ${#colors[@]})) + colored_foldername+="/${colors[$color_index]}${parts[$j]}${color_reset}" + fi + done + + echo -e " $i) $colored_foldername" # Use -e flag for echo done - echo -e " $i) $colored_foldername" # Use -e flag for echo - done + echo " n) (create a new folder)" - echo " n) (create a new folder)" + printf "\n" - printf "\n" - - selected_folder=$(input_select_folder) - while [ -z "$selected_folder" ]; do selected_folder=$(input_select_folder) - done - fi -else - if [ -z "$selected_folder" ]; then - printf "\n" - echo "[INFO] Folders:" - for ((i = 0; i < ${#existing_folders[@]}; i++)); do - foldername=${existing_folders[$i]} - echo " $i) $foldername" - done + while [ -z "$selected_folder" ]; do + selected_folder=$(input_select_folder) + done + fi + else + if [ -z "$selected_folder" ]; then + printf "\n" + echo "[INFO] Folders:" + for ((i = 0; i < ${#existing_folders[@]}; i++)); do + foldername=${existing_folders[$i]} + echo " $i) $foldername" + done - echo " n) (create a new folder)" + echo " n) (create a new folder)" - printf "\n" + printf "\n" - selected_folder=$(input_select_folder) - while [ -z "$selected_folder" ]; do selected_folder=$(input_select_folder) - done + while [ -z "$selected_folder" ]; do + selected_folder=$(input_select_folder) + done + fi fi -fi +} -cd $selected_folder +existing_folders=() + +fill_existing_folders_array() { + ignore_paths=("artists" "kinks" "ideas" "irl") + ignore_paths_depth_2=("artists" "favorites") + ignore_paths_depth_last=("no-source" ".bsky.social") + + find_command="find * -type d" + + for path in "${ignore_paths[@]}"; do + find_command+=" -not -path $path" + done + + for path in "${ignore_paths_depth_2[@]}"; do + find_command+=" -not -path $path/\*" + done + + for path in "${ignore_paths_depth_last[@]}"; do + find_command+=" -not -path *$path" + done + + find_command+=" 2>/dev/null" + + existing_folders=($(eval $find_command)) + + selected_folder='' + + if [ -z "${existing_folders}" ]; then + echo "" + echo "[INFO] There are no folders in the 'furry-commission-ideas' folder." + + new_foldername=$(input_create_folder) + while [ -z "$new_foldername" ]; do + new_foldername=$(input_create_folder) + done + + mkdir --parents "$new_foldername" + selected_folder="$new_foldername" + fi +} function confirm() { while true; do @@ -236,7 +289,14 @@ escape_string() { echo "$escaped" } -if $filter_mode; then +feh_command() { + feh_arg=$1 + + # eval "feh --geometry 1280x1440+1280+0 --auto-zoom --scale-down $eval_string &" + eval "feh --geometry 1200x1250+1308+28 --auto-zoom --scale-down $feh_arg &" +} + +filter_mode_steps() { files=() eval_string='' for file in ./*; do @@ -249,8 +309,7 @@ if $filter_mode; then eval_string+=' ' done - # eval "feh --geometry 1280x1440+1280+0 --auto-zoom --scale-down $eval_string &" - eval "feh --geometry 1200x1250+1308+28 --auto-zoom --scale-down $eval_string &" + feh_command "$eval_string" files_to_delete=() files_count=${#files[@]} @@ -316,28 +375,118 @@ if $filter_mode; then filename_escaped=$(escape_string "${file}") sed -i "/$filename_escaped/d" urls.txt done +} + +get_information_from_gallery_dl_media() { + url=$1 + information=$2 + + tmp_file=$(mktemp --quiet) + + gallery-dl --list-keyword "$url" >$tmp_file + + information=$(grep -A1 "$information" "$tmp_file" | tail -n1 | sed 's/^[ \t]*//;s/[ \t]*$//') + + echo $information +} + +unsucessful_downloads=() + +generate_random_string_with_suffix() { + suffix=$1 + + random_string=$(cat /dev/urandom | tr -cd 'a-f0-9' | head -c 32) + random_string+=".${suffix}" + + echo $random_string +} + +image_download_steps() { + url_to_download=$1 + + if [ -f "$furry_commission_ideas_urls_filename" ] && grep -qE "$url_to_download" -i "$furry_commission_ideas_urls_filename"; then + echo "" + + if [ $interactive_mode ]; then + echo "[INFO] The folder informed already has the URL informed" + echo " URL: ${url_to_download}" + else + echo "[INFO] This folder already has the url informed." + fi + + return + fi + + media_extension=$(get_information_from_gallery_dl_media "$url_to_download" "extension") + temp_dirpath="/tmp" + temp_filename=$(generate_random_string_with_suffix "$media_extension") + + gallery-dl --directory ${temp_dirpath} --filename ${temp_filename} $url_to_download + + if [[ $? -ne 0 ]]; then + unsucessful_downloads+="$url_to_download" + return + fi + + final_temp_filepath="${temp_dirpath}/${temp_filename}" + + final_temp_filepath_md5=($(md5sum $final_temp_filepath)) + + final_filename="${final_temp_filepath_md5}.${media_extension}" + + mv ${final_temp_filepath} ./${final_filename} + + echo "$final_filename,$url_to_download" >>$furry_commission_ideas_urls_filename +} + +fill_existing_folders_array +show_folder_selection_list_and_fill_selected_folder_variable + +is_url_string() { + str=$1 + + url_regex='(https?)://[-[:alnum:]\+&@#/%?=~_|!:,.;]*[-[:alnum:]\+&@#/%=~_|]' + + media_url_is_url=true + + if [[ ! $str =~ $url_regex ]]; then + media_url_is_url=false + fi + + echo "$media_url_is_url" +} + +if $filter_mode; then + filter_mode_steps else - if [ -f "$furry_commission_ideas_urls_filename" ] && grep -qE "$media_url" -i "$furry_commission_ideas_urls_filename"; then - printf "\n" - echo "[INFO] This folder already has the media informed." - echo "[INFO] Exiting..." - exit 0 - fi + flag_first_iteration_occured=false - if ! command_output=$($scripts_path/gdl.sh $media_url); then - exit 1 - fi + for url in "${urls_list[@]}"; do + while [ "$(is_url_string $url)" == "false" ]; do + echo "" + echo "The provided URL is not a valid URL." + echo " Invalid URL: ${url}" + echo "Please input a valid URL." + url=$(input_media_url) + done - media_filenames=() + if [ $interactive_mode ] && [ $flag_first_iteration_occured ]; then + show_folder_selection_list_and_fill_selected_folder_variable + fi - for ((i = 0; i < ${#command_output[@]}; i++)); do - filename=${command_output[$i]} - media_filenames+=$(basename "$filename") - done + cd "${furry_commission_ideas_path}/${selected_folder}" - for ((i = 0; i < ${#media_filenames[@]}; i++)); do - media_filename=${media_filenames[$i]} + image_download_steps "${url}" - echo "$media_filename: $media_url" >>$furry_commission_ideas_urls_filename + flag_first_iteration_occured=true + done +fi + +if [ ! -z "${unsucessful_downloads}" ]; then + echo "" + echo "There were unsucessful downloads:" + + for url in "${unsucessful_downloads}"; do + echo " ${url}" done fi