Add invocation/command arguments/help message
Now it’s actually somewhat useable! ^_^
This commit is contained in:
parent
fbcc683cd9
commit
7f82bb79ee
104
manicito
104
manicito
|
@ -6,7 +6,41 @@
|
|||
# Date: 2024-04-10
|
||||
#―――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――
|
||||
|
||||
MANICITO_TEMPLATE='$USER (<a href="$USER_URL">$USERHOST</a>) <a href="$POST_URL">posted</a> about $TAG: <blockquote>$(echo "$POST" | head -c 200)</blockquote>'
|
||||
if test -z "$MANICITO_TEMPLATE"; then
|
||||
MANICITO_TEMPLATE='$USER (<a href="$USER_URL">$USERHOST</a>) <a href="$POST_URL">posted</a> about $HASHTAG: <blockquote>$(echo "$POST" | head -c 200)</blockquote>'
|
||||
fi
|
||||
|
||||
# Prints a simple usage message.
|
||||
usage() {
|
||||
echo "usage: $(basename $0) [-h] [-H HISTORY_FILE] HASHTAG SERVER_URL"
|
||||
echo 'Mastodon/Pleroma/etc script to quote-posts all posts of a given hashtag.'
|
||||
echo
|
||||
echo ' -h print this message and exit'
|
||||
echo ' -H file used to store/check quoted posts; to avoid duplicates'
|
||||
echo
|
||||
echo 'Posts tagged under HASHTAG will all be quote-posted to the'
|
||||
echo 'Mastodon-compatible server at SERVER_URL.'
|
||||
echo
|
||||
echo 'In order to make these posts, you must set the environment variable $FEDI_AUTH'
|
||||
echo 'to your authorization token. To find your authorization token, you can snoop '
|
||||
echo 'through request headers in your web-browser. In Firefox, you can do:'
|
||||
echo ' Developer Tools (F12) → Network → Headers'
|
||||
echo 'Look for your $FEDI_AUTH value in the Authorization header like so:'
|
||||
echo ' Authorization: Bearer $FEDI_AUTH'
|
||||
echo
|
||||
echo 'To avoid duplicate-posts, you should specify a “history” file with the -H'
|
||||
echo 'parameter. With this, all quoted post IDs will be saved in this file, and'
|
||||
echo 'on subsequent runs (so long as you don’t change the history-file), no post'
|
||||
echo 'will be quoted twice.'
|
||||
echo
|
||||
echo 'The environment variable $MANICITO_TEMPLATE is used create the body of your'
|
||||
echo 'quote-posts. It substitutes the following shell-variables in templates:'
|
||||
echo ' • $HASHTAG'
|
||||
echo ' • $POST'
|
||||
echo ' • $POST_URL'
|
||||
echo ' • $USER'
|
||||
echo ' • $USER_URL'
|
||||
}
|
||||
|
||||
|
||||
# Given a post’s JSON, create and submit a quote-post for it.
|
||||
|
@ -67,16 +101,28 @@ post_body() {
|
|||
# echo $post_array_json | filter_posts $history_file
|
||||
filter_posts() {
|
||||
local history_file="$1"
|
||||
local ids_to_filter="$(history_post_ids "$history_file")"
|
||||
# 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
|
||||
if test -n "$ids_to_filter" -a "$(wc --bytes "$ids_to_filter" | awk '{print $1}')" -gt 5; then
|
||||
jq -cr .[] \
|
||||
| grep --invert-match $(sort "$history_file" | uniq | grep -v '^[[:space:]]*$' | sed 's%^%-e %')
|
||||
| grep --invert-match $(echo "$ids_to_filter" | sed 's%^%-e %')
|
||||
else
|
||||
jq -cr .[]
|
||||
fi
|
||||
}
|
||||
|
||||
|
||||
history_post_ids() {
|
||||
local history_file="$1"
|
||||
if test -f "$history_file"; then
|
||||
sort -n "$history_file" \
|
||||
| uniq \
|
||||
| grep -v '^[[:space:]]*$' \
|
||||
| awk '{ print $2 }'
|
||||
fi
|
||||
}
|
||||
|
||||
|
||||
# Pipe in fediverse posts in JSON format; these will all be echoed into the given
|
||||
# “history file”, so that they can be filtered out and avoided on subsequent
|
||||
# runs.
|
||||
|
@ -86,8 +132,7 @@ update_filter() {
|
|||
if test -f "$history_file"; then
|
||||
jq -r '(.created_at + "\t" + .id)' \
|
||||
| sort -n \
|
||||
| awk -F '\t' '{print $2}' \
|
||||
>> "$history_file"
|
||||
>> "$history_file"
|
||||
fi
|
||||
}
|
||||
|
||||
|
@ -115,8 +160,49 @@ env_subst() {
|
|||
}
|
||||
|
||||
|
||||
FEDI_SERVER="https://jam.xwx.moe"
|
||||
curl "$FEDI_SERVER/api/v1/timelines/tag/esperanto" \
|
||||
| filter_posts "m" \
|
||||
|
||||
while getopts 'hH:' arg; do
|
||||
case $arg in
|
||||
h)
|
||||
usage
|
||||
exit 0
|
||||
;;
|
||||
H)
|
||||
HISTORY_FILE="$OPTARG"
|
||||
;;
|
||||
esac
|
||||
done
|
||||
|
||||
shift $((OPTIND-1))
|
||||
HASHTAG="$1"
|
||||
FEDI_SERVER="$2"
|
||||
|
||||
|
||||
if test -z "$HASHTAG"; then
|
||||
usage
|
||||
exit 1
|
||||
fi
|
||||
|
||||
if test -z "$FEDI_AUTH"; then
|
||||
1>&2 echo 'You need to set the environment variable $FEDI_AUTH!'
|
||||
1>&2 echo 'You can find your auth key by examining the "Authorization: Bearer" header'
|
||||
1>&2 echo "used in requests by your server's web-client."
|
||||
1>&2 echo 'In Firefox, F12→Network.'
|
||||
1>&2 echo ""
|
||||
exit 2
|
||||
fi
|
||||
|
||||
if test -z "$FEDI_SERVER"; then
|
||||
1>&2 echo 'No server specified!'
|
||||
1>&2 echo 'Make sure to provide a hashtag (like #esperanto) and a'
|
||||
1>&2 echo 'server URL (like https://fedi.server) as the last two arguments.'
|
||||
1>&2 echo
|
||||
1>&2 echo 'Run with -h for more information.'
|
||||
exit 3
|
||||
fi
|
||||
|
||||
|
||||
curl "$FEDI_SERVER/api/v1/timelines/tag/$HASHTAG" \
|
||||
| filter_posts "$HISTORY_FILE" \
|
||||
| quote_posts \
|
||||
| update_filter "m"
|
||||
| update_filter "$HISTORY_FILE"
|
||||
|
|
Ŝarĝante…
Reference in New Issue