1
0
mirror of https://github.com/kmein/niveum synced 2026-03-19 03:21:10 +01:00
Files
niveum/configs/bots/tlg-wotd.nix

164 lines
5.6 KiB
Nix
Raw Normal View History

{
2022-03-10 21:52:12 +01:00
pkgs,
lib,
config,
2024-09-22 17:33:04 +02:00
niveumPackages,
2022-03-10 21:52:12 +01:00
...
}: let
mastodonEndpoint = "https://social.krebsco.de";
in {
2024-10-05 14:42:44 +02:00
systemd.services.bot-tlg-wotd = {
enable = true;
wants = ["network-online.target"];
startAt = "9:30";
path = [ pkgs.jq pkgs.curl pkgs.recode pkgs.deno pkgs.imagemagick pkgs.gawk pkgs.gnugrep pkgs.coreutils ];
environment = {
NPM_CONFIG_CACHE = "/tmp";
2024-09-22 17:33:04 +02:00
CLTK_DATA = "/tmp";
};
script = ''
set -efux
chat_id=@tlgwotd
2024-10-04 17:07:06 +02:00
export TELEGRAM_TOKEN="$(cat "$CREDENTIALS_DIRECTORY/telegram-token")"
export MASTODON_TOKEN="$(cat "$CREDENTIALS_DIRECTORY/mastodon-token")"
json_data=$(curl -sSL http://stephanus.tlg.uci.edu/Iris/Wotd | recode html..utf8)
word=$(echo "$json_data" | jq -r '.word')
compact_word=$(echo "$word" | sed 's/,.*$//')
definition=$(echo "$json_data" | jq -r '.definition | sub("<.*>"; "") | rtrimstr(" ")')
first_occurrence=$(echo "$json_data" | jq -r '.firstOccurrence')
total_occurrences=$(echo "$json_data" | jq -r '.totalOccurrences')
2024-10-05 14:42:44 +02:00
telegram_caption="*$word* $definition
2024-10-04 17:07:06 +02:00
First occurrence (century): $first_occurrence
Number of occurrences (in all Ancient Greek texts): $total_occurrences"
2024-10-05 14:42:44 +02:00
mastodon_caption="$word $definition
First occurrence (century): $first_occurrence
Number of occurrences (in all Ancient Greek texts): $total_occurrences"
#ancientgreek #classics #wotd #wordoftheday
transliteration=$(${pkgs.writers.makePythonWriter pkgs.python311 pkgs.python311Packages pkgs.python3Packages "translit.py" {
# revert to pkgs.writers.writePython3 once https://github.com/NixOS/nixpkgs/pull/353367 is merged
2025-06-03 20:08:04 +02:00
libraries = [ pkgs.python3Packages.cltk ];
2024-09-22 17:33:04 +02:00
} ''
import sys
from cltk.phonology.grc.transcription import Transcriber
probert = Transcriber("Attic", "Probert")
text = " ".join(sys.argv[1:])
ipa = probert.transcribe(text)
print(ipa)
''} "$compact_word")
photo_path=/tmp/output.png
hex_to_rgb() {
hex="$1"
r=$(printf "%d" "0x$(echo "$hex" | cut -c2-3)")
g=$(printf "%d" "0x$(echo "$hex" | cut -c4-5)")
b=$(printf "%d" "0x$(echo "$hex" | cut -c6-7)")
echo "$r $g $b"
}
calculate_luminance() {
r="$1"
g="$2"
b="$3"
r_l=$(echo "$r" | awk '{print ($1 / 255 <= 0.03928) ? $1 / 255 / 12.92 : (($1 / 255 + 0.055) / 1.055)^2.4}')
g_l=$(echo "$g" | awk '{print ($1 / 255 <= 0.03928) ? $1 / 255 / 12.92 : (($1 / 255 + 0.055) / 1.055)^2.4}')
b_l=$(echo "$b" | awk '{print ($1 / 255 <= 0.03928) ? $1 / 255 / 12.92 : (($1 / 255 + 0.055) / 1.055)^2.4}')
echo "$r_l $g_l $b_l" | awk '{print 0.2126*$1 + 0.7152*$2 + 0.0722*$3}'
}
hex_color="#$(echo "$compact_word" | md5sum | cut -c 1-6)"
if echo "$hex_color" | grep -qE '^#[0-9A-Fa-f]{6}$'; then
set -- $(hex_to_rgb "$hex_color")
r="$1"
g="$2"
b="$3"
fi
2024-10-04 17:07:06 +02:00
luminance=$(calculate_luminance "$r" "$g" "$b")
2024-10-04 17:07:06 +02:00
threshold="0.1"
echo "$r $g $b"
if [ "$(echo "$luminance" | awk -v threshold="$threshold" '{print ($1 > threshold)}')" -eq 1 ]; then
color1="black"
color2="#333"
else
color1="white"
color2=lightgrey
fi
magick -size 1400x846 \
xc:"$hex_color" \
-font "${pkgs.gentium}/share/fonts/truetype/GentiumBookPlus-Bold.ttf" \
-fill "$color1" \
-pointsize 150 -gravity west \
-annotate +100-160 "$compact_word" \
-font "${pkgs.gentium}/share/fonts/truetype/GentiumBookPlus-Regular.ttf" \
-fill "$color2" \
-pointsize 60 -gravity west \
-annotate +100+00 "$transliteration" \
-fill "$color1" \
-annotate +100+120 "$definition" \
-fill "$color2" \
-pointsize 40 -gravity southwest \
2024-10-05 14:42:44 +02:00
-annotate +100+60 "attested $total_occurrences times" \
2024-10-04 17:07:06 +02:00
-pointsize 40 -gravity southeast \
-annotate +100+60 "$(date -I)" \
"$photo_path"
2024-10-05 14:42:44 +02:00
curl -X POST "https://api.telegram.org/bot$TELEGRAM_TOKEN/sendPhoto" \
2024-10-04 17:07:06 +02:00
-F "chat_id=\"$chat_id\"" \
-F "photo=@$photo_path" \
-F parse_mode=Markdown \
2024-10-05 14:42:44 +02:00
-F caption="$telegram_caption"
2024-10-04 17:07:06 +02:00
mastodon_upload_response=$(curl -X POST "${mastodonEndpoint}/api/v2/media" \
2024-10-04 17:07:06 +02:00
-H "Authorization: Bearer $MASTODON_TOKEN" \
-F "file=@$photo_path" \
-F "description=$word $definition")
mastodon_image_id=$(echo $mastodon_upload_response | jq -r .id)
curl -X POST "${mastodonEndpoint}/api/v1/statuses" \
2024-10-04 17:07:06 +02:00
-H "Authorization: Bearer $MASTODON_TOKEN" \
2024-10-05 14:42:44 +02:00
-d "status=$mastodon_caption" \
-d "visibility=public" \
2024-10-04 17:07:06 +02:00
-d "media_ids[]=$mastodon_image_id"
'';
serviceConfig = {
Type = "oneshot";
DynamicUser = true;
StateDirectory = "tlgwotd";
PrivateTmp = true;
2024-10-04 17:07:06 +02:00
LoadCredential = [
"telegram-token:${config.age.secrets.telegram-token-kmein.path}"
"mastodon-token:${config.age.secrets.mastodon-token-tlgwotd.path}"
];
};
};
2022-05-22 11:47:59 +02:00
2024-10-04 17:07:06 +02:00
age.secrets = {
telegram-token-kmein.file = ../../secrets/telegram-token-kmein.age;
mastodon-token-tlgwotd.file = ../../secrets/mastodon-token-tlgwotd.age;
};
2022-05-22 11:47:59 +02:00
niveum.passport.services = [
{
title = "Thesaurus Linguae Graecae Word of the Day";
description = "sends <a href=\"https://stephanus.tlg.uci.edu/\">TLG</a>'s word of the day to Telegram.";
link = "https://t.me/tlgwotd";
}
];
}