This commit is contained in:
Jaidyn Ann 2024-04-10 17:21:09 -05:00
commit 454c050ac5

104
manicito Executable file
View File

@ -0,0 +1,104 @@
#!/bin/sh
#―――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――
# Name:
# Desc:
# Reqs:
# Date:
#―――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――
MANICITO_TEMPLATE='$USER (<a href="$USER_URL">$USERHOST</a>) <a href="$POST_URL">posted</a> about $TAG: <blockquote>$(echo "$POST" | head -c 200)</blockquote>'
# Sanitize a template-string.
# AKA, escape quotation-marks.
# prep_template $template
prep_template() {
local template="$1"
echo "$template" \
| sed 's%\"%\\\"%g'
}
# Rough replacement for gettexts envsubst. Safe!
# This will evaluate a strings shell variables (somewhat) safely
# Probably not good for general use — but for our purposes, it only subsitutes
# along the “first level.” So environment variables are replaced, but those
# variables contents (AKA, post contents) arent evaluated.
# env_subst $template
# env_subst "$SHELL" → "/bin/sh"
env_subst() {
local template="$1"
eval "echo \"$(prep_template "$template")\""
}
post_body() {
local tagged_post_json="$1"
POST="$(echo "$tagged_post_json" | jq -r .content | tr -d "\"'\n")" #lynx -stdin -dump | tr -d "\"'\n")"
echo "$POST" > /tmp/a
POST_URL="$(echo "$tagged_post_json" | jq -r .uri)"
USER="$(echo "$tagged_post_json" | jq -r .account.display_name)"
USER_URL="$(echo "$tagged_post_json" | jq -r .account.url)"
USERHOST="$(echo "$tagged_post_json" | jq -r .account.acct)"
env_subst "$MANICITO_TEMPLATE" \
| tr -d '"'
}
filter_posts() {
local history_file="$1"
# If the history file exists and has more than five characters, use it to filter… (sanity check)
if test -f "$history_file" -a "$(wc --bytes "$history_file" | awk '{print $1}')" -gt 5; then
jq -cr .[] \
| grep --invert-match $(sort "$history_file" | uniq | grep -v '^[[:space:]]*$' | sed 's%^%-e %')
else
jq -cr .[]
fi
}
post_json() {
local tagged_post_json="$1"
printf '{ "content_type": "text/html", "visibility": "unlisted",'
printf '"quote_id": "%s",' "$(echo "$tagged_post_json" | jq -r .id)"
printf '"status": "%s" }\n' "$(post_body "$tagged_post_json")"
}
quote_post() {
local tagged_post_json="$1"
curl --fail \
--request POST \
--header "Authorization: Bearer $FEDI_AUTH" \
--header 'Content-Type: application/json' \
--data "$(post_json "$tagged_post_json")" "$FEDI_SERVER/api/v1/statuses"
}
quote_posts() {
local IFS="
"
while read -r tagged_post_line; do
quote_post "$tagged_post_line"
if test "$?" -ne 0; then
echo "Failed to post about post $(echo "$tagged_post_line" | jq .id)!" 1>&2
exit 1
fi
echo "$tagged_post_line"
done
}
update_filter() {
local history_file="$1"
if test -f "$history_file"; then
jq -r '(.created_at + "\t" + .id)' \
| sort -n \
| awk -F '\t' '{print $2}' \
>> "$history_file"
fi
}
FEDI_SERVER="https://jam.xwx.moe"
curl "$FEDI_SERVER/api/v1/timelines/tag/esperanto" \
| filter_posts "m" \
| quote_posts \
| update_filter "m"