Files
LihonT/services/members/community.py
cheb0chik 9de69113b5 LihonT 2 Serena
LihonT Store: a separate app with a catalog of avatar frames and chat bubbles made by members (browse, add with "+", author
profiles, publishing), signed in through the TikTok that carries the mod (an exported no-window activity that answers only the
store, checked by package name and signing key). The mod gets frames made from any picture (a photo is cut into a ring), the
library of the store in the frame picker and for bubbles, other members' published bubbles and one picture per chat for both
people. The members service gets the catalog, the account library, uploads that are re-made with Pillow, an AI first pass
(Groq) and the owner's Telegram bot with accept and reject buttons. The build tools pass long file lists through an argument
file. Nothing a member sends is shown to others before it is approved.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-05 15:56:00 +03:00

62 lines
2.3 KiB
Python

"""Pictures that members make themselves (avatar frames, chat pictures): the safe handling of the bytes.
Nothing a member sends is kept as it came: the picture is decoded, cropped, scaled and written again as a fresh PNG, so
metadata, odd chunks and anything that is not pixels is gone. What is left is judged by the AI and by the owner.
"""
from __future__ import annotations
import io
import re
UID = re.compile(r"[1-9][0-9]{4,24}\Z")
INSTALL = re.compile(r"[a-f0-9]{64}\Z")
def valid_uid(value):
return isinstance(value, str) and UID.fullmatch(value) is not None
MAX_UPLOAD = 400_000
MAX_PIXELS = 16_000_000
FRAME_SIDE = 256
BUBBLE_SIDE = 512
MAX_PNG = 150 * 1024
def normalize_png(data: bytes, side: int, square: bool, limit: int = MAX_PNG) -> bytes:
"""A fresh PNG of at most `side` pixels (exactly side x side when `square`, cropped in the middle), at most `limit` bytes."""
from PIL import Image
if not isinstance(data, (bytes, bytearray)) or not 0 < len(data) <= MAX_UPLOAD:
raise ValueError("The picture is too large")
try:
image = Image.open(io.BytesIO(data))
if image.width * image.height > MAX_PIXELS:
raise ValueError("The picture is too large")
image.load()
except ValueError:
raise
except Exception:
raise ValueError("Not a picture")
image = image.convert("RGBA")
if square:
edge = min(image.width, image.height)
left, top = (image.width - edge) // 2, (image.height - edge) // 2
image = image.crop((left, top, left + edge, top + edge)).resize((side, side), Image.LANCZOS)
else:
image.thumbnail((side, side), Image.LANCZOS)
for colors in (0, 256, 128, 64):
shown = image if colors == 0 else image.quantize(colors=colors, method=Image.FASTOCTREE)
out = io.BytesIO()
shown.save(out, "PNG", optimize=True)
if out.tell() <= limit:
return out.getvalue()
raise ValueError("The picture is too complicated")
def clean_name(name, default="Без названия", limit=30) -> str:
"""A name for the screen: no control characters, no line breaks, at most `limit` characters."""
text = "".join(ch for ch in (name if isinstance(name, str) else "") if ch >= " " and ch != "\x7f")
text = " ".join(text.split())[:limit].strip()
return text or default