2023-09-29 04:51:25 +00:00
|
|
|
#!/bin/bash
|
|
|
|
|
|
|
|
USE_TOR=false
|
|
|
|
DELAY=1
|
2023-12-25 08:20:20 +00:00
|
|
|
USE_DATE=false
|
2023-09-29 04:51:25 +00:00
|
|
|
|
|
|
|
function usage {
|
2023-12-25 11:15:44 +00:00
|
|
|
echo "./$(basename "$0") [-t] [-s]"
|
2023-09-29 19:53:53 +00:00
|
|
|
echo "Mass tagger for Gelbooru"
|
2023-09-29 04:51:25 +00:00
|
|
|
echo "Tags existing pictures inside a folder"
|
|
|
|
echo " -h shows this help message"
|
|
|
|
echo " -t downloads using tor (requires torsocks)"
|
|
|
|
echo " -s sets the delay after each request, defaults to 1"
|
2023-12-25 08:20:20 +00:00
|
|
|
echo " -d sets the date of the file downloaded to the date it was uploaded to Gelbooru"
|
|
|
|
|
2023-09-29 04:51:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
# list of arguments expected in the input
|
2023-12-25 08:20:20 +00:00
|
|
|
optstring=":hts:d"
|
2023-09-29 04:51:25 +00:00
|
|
|
|
|
|
|
while getopts ${optstring} arg; do
|
|
|
|
case ${arg} in
|
|
|
|
h)
|
|
|
|
usage
|
|
|
|
exit
|
|
|
|
;;
|
|
|
|
t)
|
|
|
|
USE_TOR=true
|
|
|
|
echo -n "Using Tor with IP: "
|
|
|
|
torsocks curl ip.me
|
|
|
|
;;
|
|
|
|
s)
|
|
|
|
DELAY="${OPTARG}"
|
|
|
|
;;
|
2023-12-25 08:20:20 +00:00
|
|
|
d)
|
|
|
|
USE_DATE=true
|
|
|
|
;;
|
2023-09-29 04:51:25 +00:00
|
|
|
:)
|
|
|
|
echo "$0: Must supply an argument to -$OPTARG." >&2
|
|
|
|
exit 1
|
|
|
|
;;
|
|
|
|
?)
|
|
|
|
echo "Invalid option: -${OPTARG}."
|
|
|
|
exit 2
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
|
|
|
|
for FILE in *; do
|
|
|
|
echo "$FILE"
|
|
|
|
# GET MD5 HASH
|
2023-12-25 11:15:44 +00:00
|
|
|
FILE_MD5=$(md5sum "$FILE" | awk '{print $1}')
|
2023-09-29 04:51:25 +00:00
|
|
|
# DOWNLOAD JSON
|
|
|
|
if $USE_TOR; then
|
2023-12-25 11:15:44 +00:00
|
|
|
JSON=$(torsocks curl -s "https://gelbooru.com/index.php?page=dapi&s=post&q=index&json=1&tags=md5:$FILE_MD5" | jq .)
|
2023-09-29 04:51:25 +00:00
|
|
|
else
|
2023-12-25 11:15:44 +00:00
|
|
|
JSON=$(curl -s "https://gelbooru.com/index.php?page=dapi&s=post&q=index&json=1&tags=md5:$FILE_MD5" | jq .)
|
2023-09-29 04:51:25 +00:00
|
|
|
fi
|
|
|
|
# STORE TAGS INTO VARIABLES
|
2023-12-25 11:15:44 +00:00
|
|
|
FILE_TAGS=$(echo "$JSON" | jq -r '.post | .[] | ."tags"' | sed 's/\ /,/g')
|
|
|
|
FILE_DATE=$(echo "$JSON" | jq -r '.post | .[] | ."created_at"')
|
2023-09-29 04:51:25 +00:00
|
|
|
# ADD TAGS TO IMAGE
|
|
|
|
setfattr -n user.xdg.tags -v "$FILE_TAGS" "$FILE"
|
2023-12-25 08:20:20 +00:00
|
|
|
if $USE_DATE; then
|
|
|
|
touch -d "$FILE_DATE" "$FILE"
|
|
|
|
fi
|
2023-09-29 04:51:25 +00:00
|
|
|
# DELAY BEFORE NEXT FETCH
|
2023-12-25 11:15:44 +00:00
|
|
|
sleep "$DELAY"
|
2023-12-25 08:20:20 +00:00
|
|
|
done
|