Adds filter function to images-indexer script

This commit is contained in:
Matheus Albino
2024-07-08 00:00:56 -03:00
parent e142259896
commit 3ae42061ee

View File

@@ -1,5 +1,23 @@
#!/bin/bash #!/bin/bash
show_help() {
# echo "Usage: `basename $0` [--no-abort-on-found] [--parallel] [--custom-grep-search|-c <string>]"
echo "Usage: `basename $0` [--help|-h] [--filter-mode|-f]"
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"
}
filter_mode=false
while [ "$#" -gt 0 ]; do
case "$1" in
--help|-h) show_help; exit ;;
--filter-mode|-f) filter_mode=true; shift;;
*) shift ;;
esac
done
# commands_to_check=(gallery-dl gdl.sh xdotool)
commands_to_check=(gallery-dl gdl.sh) commands_to_check=(gallery-dl gdl.sh)
commands_not_found=() commands_not_found=()
@@ -25,7 +43,7 @@ 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
if [ ! -d $furry_commission_ideas_path ]; then if [ ! -d $furry_commission_ideas_path ]; then
echo "[ERROR] The ideas folder was not found (\"$furry_commission_ideas_path\")" echo "[ERROR] The images folder was not found (\"$furry_commission_ideas_path\")"
exit 1 exit 1
fi fi
@@ -39,18 +57,22 @@ input_media_url() {
echo "$media_url" echo "$media_url"
} }
media_url=`input_media_url` media_url=''
while [ -z "$media_url" ]; do
media_url=`input_media_url`
done
# remove trailing slashes if ! $filter_mode; then
media_url=`sed 's:/*$::' <<< "$media_url"` media_url=`input_media_url`
while [ -z "$media_url" ]; do
media_url=`input_media_url`
done
url_regex='(https?)://[-[:alnum:]\+&@#/%?=~_|!:,.;]*[-[:alnum:]\+&@#/%=~_|]' # remove trailing slashes
if [[ ! $media_url =~ $url_regex ]]; then media_url=`sed 's:/*$::' <<< "$media_url"`
echo "The URL informed is not valid."
exit 1 url_regex='(https?)://[-[:alnum:]\+&@#/%?=~_|!:,.;]*[-[:alnum:]\+&@#/%=~_|]'
if [[ ! $media_url =~ $url_regex ]]; then
echo "The URL informed is not valid."
exit 1
fi
fi fi
cd $furry_commission_ideas_path cd $furry_commission_ideas_path
@@ -129,27 +151,141 @@ fi
cd $selected_folder cd $selected_folder
if [ -f "$furry_commission_ideas_urls_filename" ] && grep -qE "$media_url" -i "$furry_commission_ideas_urls_filename"; then function confirm() {
printf "\n" while true; do
echo "[INFO] This folder already has the media informed." read -n 1 yn
echo "[INFO] Exiting..." case $yn in
[Yy]* ) return 0;;
[Nn]* ) return 1;;
[Uu]* ) return 2;;
[Cc]* ) exit;;
* ) echo "Please answer YES, NO, or CANCEL.";;
esac
done
}
escape_string() {
local input="$1"
local escaped=""
# Iterate over each character in the input string
while IFS= read -r -n1 char; do
case "$char" in
' '|'\'|'$'|'`'|'!'|'&'|'|'|';'|'<'|'>'|'"'|"'"|'*'|'?'|'['|']'|'{'|'}'|'('|')'|'#')
escaped="${escaped}\\${char}"
;;
*)
escaped="${escaped}${char}"
;;
esac
done <<< "$input"
echo "$escaped"
}
if $filter_mode; then
files=()
eval_string=''
for file in ./*; do
if [[ "$file" != *.png && "$file" != *.jpeg && "$file" != *.jpg ]]; then
continue
fi
files+=("$file")
eval_string+=$(escape_string "$file")
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 &"
files_to_delete=()
for ((i = 0; i < ${#files[@]};)); do
file="${files[$i]}"
file="${file#./}"
echo ""
echo "File: $file"
echo -n "Should this file be deleted? (y/n): "
confirm
confirm_result=$?
should_undo=false
case $confirm_result in
0)
if [[ ! ${files_to_delete[@]} =~ $file ]]; then
files_to_delete+=("$file")
fi
;;
1)
;;
2)
if (( i - 1 < 0 )); then
echo ""
echo ""
echo "Can't undo because this is the first image"
continue
else
((i--))
xdotool search --name feh key p
files_to_delete=("${files_to_delete[@]/$file}")
continue
fi
;;
esac
xdotool search --name feh key n
((i++))
done
echo ""
pkill feh
# debug
if false; then
echo "Files to be deleted:"
for ((i = 0; i < ${#files_to_delete[@]}; i++)); do
file="${files_to_delete[$i]}"
echo " $file"
done
echo ""
exit 0 exit 0
fi
for ((i = 0; i < ${#files_to_delete[@]}; i++)); do
file="${files_to_delete[$i]}"
rm "$file"
filename_escaped=$(escape_string "${file}")
sed -i "/$filename_escaped/d" urls.txt
done
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
if ! command_output=$($scripts_path/gdl.sh $media_url); then
exit 1
fi
media_filenames=()
for ((i = 0; i < ${#command_output[@]}; i++)); do
filename=${command_output[$i]}
media_filenames+=$(basename "$filename")
done
for ((i = 0; i < ${#media_filenames[@]}; i++)); do
media_filename=${media_filenames[$i]}
echo "$media_filename: $media_url" >> $furry_commission_ideas_urls_filename
done
fi fi
if ! command_output=$($scripts_path/gdl.sh $media_url); then
exit 1
fi
media_filenames=()
for ((i = 0; i < ${#command_output[@]}; i++)); do
filename=${command_output[$i]}
media_filenames+=$(basename "$filename")
done
for ((i = 0; i < ${#media_filenames[@]}; i++)); do
media_filename=${media_filenames[$i]}
echo "$media_filename: $media_url" >> $furry_commission_ideas_urls_filename
done