#!/usr/bin/env python3
"""Reescribe el bloque DIRECTO de index.html con los últimos posts del pub.
Lee de stdin el JSON de tools/ultimos.js y sustituye lo que haya entre
y . Escritura atómica: el
servidor web nunca ve un fichero a medias.
node tools/ultimos.js @feed.ed25519 5 | python3 tools/directo.py /var/www/acampada26s.net/index.html
"""
import html
import json
import os
import re
import sys
import tempfile
from datetime import datetime
from urllib.parse import quote
from zoneinfo import ZoneInfo
INI = ""
FIN = ""
MAX = 400 # caracteres por post
TZ = ZoneInfo("Europe/Madrid")
def limpia(texto):
# fuera markdown de imágenes/enlaces y referencias ssb, queda el texto
texto = re.sub(r"!\[[^\]]*\]\([^)]*\)", "", texto)
texto = re.sub(r"\[([^\]]*)\]\([^)]*\)", r"\1", texto)
texto = " ".join(texto.split())
if len(texto) > MAX:
texto = texto[:MAX].rsplit(" ", 1)[0] + "…"
return texto
def bloque(posts):
li = []
for p in posts:
t = datetime.fromtimestamp(p["ts"] / 1000, tz=TZ)
img = ""
if p.get("img"):
src = "/c/blob/" + quote(p["img"], safe="")
img = f'\n '
li.append(
"
{html.escape(limpia(p['text']))}
\n" "