2024-04-30 15:36:43 -07:00
|
|
|
#! /usr/bin/env sh
|
|
|
|
|
|
|
|
# FediStatusPoster - Simple CLI tools and scripts to post to the fediverse
|
|
|
|
# Copyright (C) 2024 <@Anon@yandere.cc>
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
# Requires:
|
|
|
|
# curl
|
|
|
|
# jq
|
|
|
|
|
|
|
|
# Get the runtime path of the bot
|
|
|
|
ABS_PATH="$(readlink -f "$0")"
|
|
|
|
RUN_DIR="$(dirname "$ABS_PATH")"
|
|
|
|
ENTRY="${RUN_DIR}/../FediStatusPoster/runconfig.sh"
|
|
|
|
|
|
|
|
# DEFAULT_CREDENTIALS="${RUN_DIR}/credentials/hentaibot.sh.gpg"
|
|
|
|
# DEFAULT_KEYFILE="/zfs_media/fs1/BotKeys/hentaibot.sh.key"
|
|
|
|
BOORU_URL="gelbooru.com"
|
2024-10-16 23:23:02 -07:00
|
|
|
POS_ARGS=9
|
2024-04-30 15:36:43 -07:00
|
|
|
|
|
|
|
[ ! $# -ge $POS_ARGS ] &&\
|
|
|
|
echo "Invalid number of arguments..." &&\
|
|
|
|
echo "$(basename "$0") [default_credentials] [default_keyfile] [blacklist] [md5_history] [md5_tail_history] [searchtags] [msg_sfw] [msg_nsfw] usage:" &&\
|
|
|
|
echo " default_credentials - Path to the credentials file generated by create_app.py" &&\
|
|
|
|
echo " default_keyfile - Path to the keyfile if encryption was used for credentials" &&\
|
|
|
|
echo " blacklist - A text list of newline deliminated tags to blacklist" &&\
|
|
|
|
echo " md5_history - md5 history file to write to in order to avoid excessive duplicates" &&\
|
|
|
|
echo " md5_tail_history - the number of history items to blacklist" &&\
|
2024-10-16 23:10:37 -07:00
|
|
|
echo " md5_blacklist - A permanent blacklist file" &&\
|
2024-04-30 15:36:43 -07:00
|
|
|
echo " searchtags - The tags to search for" &&\
|
|
|
|
echo " msg_sfw - Display message if image is safe for work" &&\
|
|
|
|
echo " msg_nsfw - Display message if image is not safe for work" &&\
|
|
|
|
exit 1
|
|
|
|
|
|
|
|
DEFAULT_CREDENTIALS="$1"
|
|
|
|
DEFAULT_KEYFILE="$2"
|
|
|
|
TAGS_BLACKLIST="$3"
|
|
|
|
MD5_HISTORY="$4"
|
|
|
|
MD5_NUM="$5"
|
2024-10-16 23:10:37 -07:00
|
|
|
MD5_BLACKLIST="$6"
|
|
|
|
TAGS="$7"
|
|
|
|
MSG_SFW="$8"
|
|
|
|
MSG_NSFW="$9"
|
2024-04-30 15:36:43 -07:00
|
|
|
shift $POS_ARGS
|
|
|
|
|
|
|
|
TMP=$(mktemp)
|
|
|
|
tail -n "$MD5_NUM" "$MD5_HISTORY" > "$TMP"
|
|
|
|
trap "rm ${TMP}" EXIT
|
|
|
|
|
|
|
|
TAGS_FMT=`echo "$TAGS" | tr ' ' '\n' | xargs echo | tr ' ' '+'`
|
|
|
|
TAGS_BLK_FMT=`xargs -a "$TAGS_BLACKLIST" echo | sed 's|\ |+-|g'`
|
|
|
|
TAGS_SEARCH="${TAGS_FMT}+sort:random+-${TAGS_BLK_FMT}"
|
2024-05-13 23:54:53 -07:00
|
|
|
URL="https://${BOORU_URL}/index.php"
|
|
|
|
DATA_POST="page=dapi&s=post&q=index&json=1&tags=${TAGS_SEARCH}"
|
2024-04-30 15:36:43 -07:00
|
|
|
|
|
|
|
# RESULT=`curl -s --get "https://${BOORU_URL}/index.php" -d "page=dapi&s=post&q=index&json=1&limit=1&tags=${TAGS_SEARCH}"`
|
2024-05-13 23:54:53 -07:00
|
|
|
RESULT=`curl -L -s --get "${URL}" -d "${DATA_POST}"`
|
2024-05-05 15:30:32 -07:00
|
|
|
[ -z "$RESULT" ] && echo "Empty response for ${TAGS_SEARCH}" && exit 1
|
2024-04-30 15:36:43 -07:00
|
|
|
LENGTH="$(echo "$RESULT" | jq -r '. | .post | length')"
|
2024-05-05 15:30:32 -07:00
|
|
|
[ "$LENGTH" = 0 ] && echo "Zero length for ${TAGS_SEARCH}" && exit 1
|
|
|
|
|
2024-05-04 19:04:21 -07:00
|
|
|
LAST_INDEX=`expr $LENGTH - 1`
|
|
|
|
SELECTION=`seq 0 $LAST_INDEX | shuf`
|
2024-04-30 15:36:43 -07:00
|
|
|
|
|
|
|
for i in $SELECTION;do
|
2024-04-30 16:00:17 -07:00
|
|
|
MD5="$(echo "$RESULT" | jq -r '. | .post['"$i"'].md5')"
|
2024-04-30 15:36:43 -07:00
|
|
|
[ -z "$MD5" ] && continue
|
|
|
|
grep -q "$MD5" "$TMP" && continue
|
2024-10-16 23:10:37 -07:00
|
|
|
grep -q "$MD5" "$MD5_BLACKLIST" && continue
|
2024-04-30 16:00:17 -07:00
|
|
|
URL_FILE="$(echo "$RESULT" | jq -r '. | .post['"$i"'].file_url')"
|
|
|
|
RATING="$(echo "$RESULT" | jq -r '. | .post['"$i"'].rating')"
|
2024-04-30 15:36:43 -07:00
|
|
|
|
|
|
|
NSFW="--nsfw"
|
|
|
|
TEXT="$MSG_NSFW"
|
|
|
|
|
|
|
|
PICKED="/tmp/$(basename "$URL_FILE")"
|
2024-05-05 15:30:32 -07:00
|
|
|
|
2024-04-30 15:36:43 -07:00
|
|
|
# Always delete the temporary file
|
|
|
|
trap "rm ${PICKED} ${TMP}" EXIT
|
|
|
|
|
|
|
|
sleep 1
|
|
|
|
curl -L -s "$URL_FILE" -o "$PICKED"
|
|
|
|
|
|
|
|
[ "$RATING" = "general" ] && NSFW="--safe" && TEXT="$MSG_SFW"
|
|
|
|
[ "$RATING" = "sensitive" ] && NSFW="--safe" && TEXT="$MSG_SFW"
|
|
|
|
|
2024-06-19 13:56:25 -07:00
|
|
|
[ ! -f "$PICKED" ] && echo "Failed to download: ${URL_FILE} to ${PICKED}" && continue
|
2024-04-30 15:36:43 -07:00
|
|
|
|
|
|
|
case "$(file --mime-type "$PICKED")" in
|
|
|
|
*': image/'*) ;;
|
|
|
|
*': video/'*) ;;
|
|
|
|
*)
|
2024-04-30 16:24:47 -07:00
|
|
|
rm "$PICKED" && continue
|
2024-04-30 15:36:43 -07:00
|
|
|
;;
|
|
|
|
esac
|
2024-04-30 15:58:24 -07:00
|
|
|
echo "$MD5" >> "$MD5_HISTORY"
|
2024-04-30 15:36:43 -07:00
|
|
|
"$ENTRY" "$DEFAULT_CREDENTIALS" "$DEFAULT_KEYFILE" "$NSFW" "$@" "$TEXT" "$PICKED"
|
|
|
|
exit 0
|
|
|
|
done
|
2024-05-05 15:30:32 -07:00
|
|
|
echo "Exited 1 for ${TAGS_SEARCH}"
|
2024-04-30 15:36:43 -07:00
|
|
|
exit 1
|