from flask import Flask, render_template, Response from sonatoki.ilo import Ilo from sonatoki.Configs import PrefConfig from atproto import FirehoseSubscribeReposClient, parse_subscribe_repos_message from atproto import CAR, models import json import re import emoji import queue import threading from werkzeug.serving import run_simple app = Flask(__name__) message_queue = queue.Queue() # Your existing code for Ilo and JSONExtra remains the same ilo = Ilo(**PrefConfig) class JSONExtra(json.JSONEncoder): def default(self, obj): try: return json.JSONEncoder.default(self, obj) except: return repr(obj) def clean_text(text: str) -> str: text = emoji.replace_emoji(text, replace='') text = re.sub(r'https?://\S+', '', text) text = re.sub(r'[^A-Za-z\s]', '', text) text = text.strip() return text def process_firehose(): client = FirehoseSubscribeReposClient() def on_message_handler(message): commit = parse_subscribe_repos_message(message) if not isinstance(commit, models.ComAtprotoSyncSubscribeRepos.Commit): return car = CAR.from_bytes(commit.blocks) for op in commit.ops: if op.action in ["create"] and op.cid: raw = car.blocks.get(op.cid) cooked = models.get_or_create(raw, strict=False) if not cooked: continue if cooked.py_type == "app.bsky.feed.post": cleaned_text = clean_text(raw.get("text", "")) if not cleaned_text or len(cleaned_text.split()) < 3: continue if not ilo.is_toki_pona(cleaned_text.lower()): continue url = f'https://bsky.app/profile/{commit.repo}/post/{op.path.split("/")[1]}' message_queue.put({'text': raw.get("text", ""), 'url': url}) client.start(on_message_handler) def generate_sse(): while True: message = message_queue.get() yield f"data: {json.dumps(message)}\n\n" @app.route('/') def index(): return """