Many upgrades for images-indexer.sh

This commit is contained in:
Matheus Albino
2024-12-08 18:45:54 -03:00
parent 9ff4de4890
commit e38d1df07b

View File

@@ -6,9 +6,14 @@ show_help() {
echo "Options:" echo "Options:"
echo " --help | -h (optional) Display script's help text" 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 " --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 filter_mode=false
urls_list_arg=""
interactive_mode=false
while [ "$#" -gt 0 ]; do while [ "$#" -gt 0 ]; do
case "$1" in case "$1" in
--help | -h) --help | -h)
@@ -19,11 +24,19 @@ while [ "$#" -gt 0 ]; do
filter_mode=true filter_mode=true
shift shift
;; ;;
--urls | -u)
urls_list_arg="$2"
shift
;;
-i)
interactive_mode=true
shift 2
;;
*) shift ;; *) shift ;;
esac esac
done done
commands_to_check=(gallery-dl gdl.sh feh xdotool) commands_to_check=(gallery-dl gdl.sh feh xdotool stoml)
commands_not_found=() commands_not_found=()
for command in ${commands_to_check[@]}; do for command in ${commands_to_check[@]}; do
@@ -42,8 +55,7 @@ if [ ${#commands_not_found[@]} -ne 0 ]; then
exit 1 exit 1
fi fi
cur_dir=$(pwd) furry_commission_ideas_path=/mnt/e/home/downloads/furry-downloads-2
furry_commission_ideas_path=/mnt/e/clouds/nextcloud/furry-downloads
furry_commission_ideas_urls_filename="urls.txt" furry_commission_ideas_urls_filename="urls.txt"
scripts_path=/home/cloud/repos/personal-devboot/scripts/shell scripts_path=/home/cloud/repos/personal-devboot/scripts/shell
@@ -57,72 +69,72 @@ if [ ! -d $scripts_path ]; then
exit 1 exit 1
fi fi
input_media_url() { convert_csv_to_array() {
read -p "[INFO] Please inform the media's url: " media_url csv_arg=$1
echo "$media_url" res_arr=$(echo $csv_arg | tr ',' "\n")
echo $res_arr
} }
media_url='' read_command() {
message=$@
if ! $filter_mode; then read -p "${message}: " media_url
media_url=$(input_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 while [ -z "$media_url" ]; do
media_url=$(input_media_url) media_url=$(read_command $read_command_message)
done done
# remove trailing slashes # remove trailing slashes
media_url=$(sed 's:/*$::' <<<"$media_url") media_url=$(sed 's:/*$::' <<<"$media_url")
url_regex='(https?)://[-[:alnum:]\+&@#/%?=~_|!:,.;]*[-[:alnum:]\+&@#/%=~_|]' echo "$media_url"
if [[ ! $media_url =~ $url_regex ]]; then }
echo "The URL informed is not valid."
exit 1 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 fi
}
fill_urls_list_array
cd $furry_commission_ideas_path 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() { input_create_folder() {
read -p "[INFO] Please create one: " new_foldername read -p "[INFO] Please create one: " new_foldername
echo "$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() { 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" echo "$new_foldername"
} }
input_select_folder() { input_select_folder_read_command() {
read -p "[INFO] Please inform the desired option: " selected_option 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 if [ "$selected_option" == 'n' ]; then
selected_folder=$(input_create_folder) selected_folder=$(input_create_folder)
@@ -138,9 +150,10 @@ input_select_folder() {
echo "$selected_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 if $should_colored_output; then
# Define color codes for up to 6 levels # Define color codes for up to 6 levels
color_reset="\033[0m" color_reset="\033[0m"
color_level_1="\033[1;34m" # Blue for the first level color_level_1="\033[1;34m" # Blue for the first level
@@ -182,7 +195,7 @@ if $should_colored_output; then
selected_folder=$(input_select_folder) selected_folder=$(input_select_folder)
done done
fi fi
else else
if [ -z "$selected_folder" ]; then if [ -z "$selected_folder" ]; then
printf "\n" printf "\n"
echo "[INFO] Folders:" echo "[INFO] Folders:"
@@ -200,9 +213,49 @@ else
selected_folder=$(input_select_folder) selected_folder=$(input_select_folder)
done done
fi 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() { function confirm() {
while true; do while true; do
@@ -236,7 +289,14 @@ escape_string() {
echo "$escaped" 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=() files=()
eval_string='' eval_string=''
for file in ./*; do for file in ./*; do
@@ -249,8 +309,7 @@ if $filter_mode; then
eval_string+=' ' eval_string+=' '
done done
# eval "feh --geometry 1280x1440+1280+0 --auto-zoom --scale-down $eval_string &" feh_command "$eval_string"
eval "feh --geometry 1200x1250+1308+28 --auto-zoom --scale-down $eval_string &"
files_to_delete=() files_to_delete=()
files_count=${#files[@]} files_count=${#files[@]}
@@ -316,28 +375,118 @@ if $filter_mode; then
filename_escaped=$(escape_string "${file}") filename_escaped=$(escape_string "${file}")
sed -i "/$filename_escaped/d" urls.txt sed -i "/$filename_escaped/d" urls.txt
done 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 else
if [ -f "$furry_commission_ideas_urls_filename" ] && grep -qE "$media_url" -i "$furry_commission_ideas_urls_filename"; then flag_first_iteration_occured=false
printf "\n"
echo "[INFO] This folder already has the media informed."
echo "[INFO] Exiting..."
exit 0
fi
if ! command_output=$($scripts_path/gdl.sh $media_url); then for url in "${urls_list[@]}"; do
exit 1 while [ "$(is_url_string $url)" == "false" ]; do
fi echo ""
echo "The provided URL is not a valid URL."
media_filenames=() echo " Invalid URL: ${url}"
echo "Please input a valid URL."
for ((i = 0; i < ${#command_output[@]}; i++)); do url=$(input_media_url)
filename=${command_output[$i]}
media_filenames+=$(basename "$filename")
done done
for ((i = 0; i < ${#media_filenames[@]}; i++)); do if [ $interactive_mode ] && [ $flag_first_iteration_occured ]; then
media_filename=${media_filenames[$i]} show_folder_selection_list_and_fill_selected_folder_variable
fi
echo "$media_filename: $media_url" >>$furry_commission_ideas_urls_filename cd "${furry_commission_ideas_path}/${selected_folder}"
image_download_steps "${url}"
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 done
fi fi