FediStatusPosterExtras/boorubot.sh

88 lines
3.1 KiB
Bash
Raw Normal View History

2024-03-09 18:29:43 -08: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"
2024-04-11 22:55:52 -07:00
# DEFAULT_CREDENTIALS="${RUN_DIR}/credentials/hentaibot.sh.gpg"
# DEFAULT_KEYFILE="/zfs_media/fs1/BotKeys/hentaibot.sh.key"
2024-03-09 18:29:43 -08:00
BOORU_URL="gelbooru.com"
2024-04-11 22:55:52 -07:00
POS_ARGS=6
2024-03-09 18:29:43 -08:00
2024-04-11 22:55:52 -07:00
[ ! $# -ge $POS_ARGS ] &&\
2024-03-09 18:29:43 -08:00
echo "Invalid number of arguments..." &&\
2024-04-11 22:55:52 -07:00
echo "$(basename "$0") [default_credentials] [default_keyfile] [blacklist] [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" &&\
2024-03-09 18:29:43 -08:00
echo " blacklist - A text list of newline deliminated tags to blacklist" &&\
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
2024-04-11 22:55:52 -07:00
DEFAULT_CREDENTIALS="$1"
DEFAULT_KEYFILE="$2"
TAGS_BLACKLIST="$3"
TAGS="$4"
MSG_SFW="$5"
MSG_NSFW="$6"
shift $POS_ARGS
2024-03-09 18:29:43 -08:00
2024-03-09 21:38:33 -08:00
TAGS_FMT=`echo "$TAGS" | tr ' ' '\n' | xargs echo | tr ' ' '+'`
2024-03-09 18:29:43 -08:00
TAGS_BLK_FMT=`xargs -a "$TAGS_BLACKLIST" echo | sed 's|\ |+-|g'`
TAGS_SEARCH="${TAGS_FMT}+sort:random+-${TAGS_BLK_FMT}"
# RESULT=`curl -s --get "https://${BOORU_URL}/index.php" -d "page=dapi&s=post&q=index&json=1&limit=1&tags=${TAGS_SEARCH}"`
RESULT=`curl -s --get "https://${BOORU_URL}/index.php" -d "page=dapi&s=post&q=index&json=1&tags=${TAGS_SEARCH}"`
2024-03-09 18:29:43 -08:00
LENGTH="$(echo "$RESULT" | jq -r '. | .post | length')"
SELECTED=`shuf -i 0-$(expr $LENGTH - 1) -n 1`
URL_FILE="$(echo "$RESULT" | jq -r '. | .post['"$SELECTED"'].file_url')"
RATING="$(echo "$RESULT" | jq -r '. | .post['"$SELECTED"'].rating')"
2024-03-09 18:29:43 -08:00
NSFW="--nsfw"
TEXT="$MSG_NSFW"
PICKED="/tmp/$(basename "$URL_FILE")"
# Always delete the temporary file
trap "rm ${PICKED}" EXIT
2024-04-11 19:19:15 -07:00
sleep 1
2024-03-09 18:29:43 -08:00
curl -L -s "$URL_FILE" -o "$PICKED"
[ "$RATING" = "general" ] && NSFW="--safe" && TEXT="$MSG_SFW"
2024-04-11 22:55:52 -07:00
[ "$RATING" = "sensitive" ] && NSFW="--safe" && TEXT="$MSG_SFW"
2024-03-09 18:29:43 -08:00
[ ! -f "$PICKED" ] && echo "Failed to download: ${PICKED}" && exit 1
case "$(file --mime-type "$PICKED")" in
*': image/'*) ;;
*': video/'*) ;;
*)
echo "File is not an image or video: ${PICKED}"
;;
esac
"$ENTRY" "$DEFAULT_CREDENTIALS" "$DEFAULT_KEYFILE" "$NSFW" "$@" "$TEXT" "$PICKED"