From 1dffb649ed73bcc3ce4e681e149f11647d30446d Mon Sep 17 00:00:00 2001 From: Hugo Thunnissen Date: Sat, 22 Feb 2020 20:56:33 +0100 Subject: [PATCH] Initial commit --- .gitignore | 2 ++ README.md | 2 ++ dirkjan-bot | 67 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 71 insertions(+) create mode 100644 .gitignore create mode 100644 README.md create mode 100755 dirkjan-bot diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..84ecf0d --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +/data/ +/bot-token \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..8de3225 --- /dev/null +++ b/README.md @@ -0,0 +1,2 @@ +# Dirkjan bot +Bot that posts daily dirkjan comics to @dirkjanbot@botsin.space. diff --git a/dirkjan-bot b/dirkjan-bot new file mode 100755 index 0000000..cb2637d --- /dev/null +++ b/dirkjan-bot @@ -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"