Initial commit

master
Hugo Thunnissen 4 years ago
commit 1dffb649ed

2
.gitignore vendored

@ -0,0 +1,2 @@
/data/
/bot-token

@ -0,0 +1,2 @@
# Dirkjan bot
Bot that posts daily dirkjan comics to @dirkjanbot@botsin.space.

@ -0,0 +1,67 @@
#!/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() {
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"
mkdir -p "$data_dir"
outfile="$data_dir/dirkjan $date.png"
wget "$strip_url" -O "$outfile" >&2
printf '%s' "$outfile"
)
api-curl() {
token="$1"
shift || return $?
curl -H 'Authorization: bearer '"$bot_token" "$@"
}
img_file="$(download-dirkjan-img "$dirkjan_url" "$here")"
bot_token="$(cat "$here/bot-token")" || exit $?
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\\n#dagelijks #nederlandstalig #strip #daily #dutch #comic",
"media_ids": [ "%s" ],
"visibility": "unlisted"
}'
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"
Loading…
Cancel
Save