You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

72 lines
1.7 KiB
Bash

#!/bin/bash
if ! type_output="$(type dirname readlink curl wget jq grep head tail date 2>&1)"; then
printf 'dirkjan-bot: Some required commands are missing, lookup results:\n%s\n' \
"$type_output" >&2
exit 1
fi
dirkjan_url='https://dirkjan.nl'
instance_url='https://botsin.space'
here="$(here="$(readlink -f "${BASH_SOURCE[0]}")" && dirname "$here")" || exit $?
get-dirkjan-img-url() {
local url="$1"
curl "$url/" | grep img | grep -Po '(?<=src=")[^"]+"' | head -n 2 | tail -n 1
}
download-dirkjan-img() (
set -e
dirkjan_url="$1"
here="$2"
strip_url="$(get-dirkjan-img-url "$dirkjan_url")"
date="$(date +'%H-%M-%S %d-%m-%Y')"
data_dir="$here/data"
[[ -d "$data_dir" ]] || mkdir -p "$data_dir"
outfile="$data_dir/dirkjan $date.png"
wget "$strip_url" -O "$outfile" >&2
printf '%s' "$outfile"
)
api-curl() {
local token="$1"
shift || return $?
curl -H 'Authorization: bearer '"$bot_token" "$@"
}
img_file="$(download-dirkjan-img "$dirkjan_url" "$here")"
if [[ -z "$DIRKJAN_BOT_TOKEN" ]]; then
bot_token="$(cat "$here/bot-token")" || exit $?
else
bot_token="$DIRKJAN_BOT_TOKEN"
fi
upload_result="$(api-curl "$bot_token" -F file=@"$img_file" "$instance_url/api/v1/media")" || exit $?
media_id="$(jq -r .id <<<"$upload_result")" || exit $?
echo heey
post_text="$(LC_ALL=nl_NL.UTF-8 date +'%A %d %B %Y')" || exit $?
echo ha
format_string='{
"status": "#dirkjan %s\\n\\nStrip door Mark Retera - https://dirkjan.nl\\n\\n#dagelijks #nederlandstalig #strip #daily #dutch #comic",
"media_ids": [ "%s" ],
"visibility": "public"
}'
printf -v json "$format_string" "$post_text" "$media_id"
api-curl "$bot_token" -d "$json" -H 'Content-Type: application/json' "$instance_url/api/v1/statuses"