From 149659064def1db778f4dce1a2e5641cb4259dc9 Mon Sep 17 00:00:00 2001 From: retoor Date: Mon, 9 Jun 2025 06:55:37 +0200 Subject: [PATCH] Update. --- src/snek/__main__.py | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/src/snek/__main__.py b/src/snek/__main__.py index 611e5b8..161f469 100644 --- a/src/snek/__main__.py +++ b/src/snek/__main__.py @@ -13,7 +13,49 @@ from snek.app import Application def cli(): pass +@cli.command() +def export(): + app = Application(db_path="sqlite:///snek.db") + + async def fix_message(message): + message = { + "uid": message["uid"], + "user_uid": message["user_uid"], + "text": message["message"], + "sent": message["created_at"], + } + user = await app.services.user.get(uid=message["user_uid"]) + message["user"] = user and user["username"] or None + return (message["user"] or "") + ": " + (message["text"] or "") + async def run(): + + result = [] + for channel in app.db["channel"].find( + is_private=False, is_listed=True, tag="public" + ): + print(f"Dumping channel: {channel['label']}.") + result += [ + await fix_message(record) + for record in app.db["channel_message"].find( + channel_uid=channel["uid"], order_by="created_at" + ) + ] + print("Dump succesfull!") + print("Converting to json.") + print("Converting succesful, now writing to dump.txt") + with open("dump.txt", "w") as f: + f.write("\n\n".join(result)) + print("Dump written to dump.json") + asyncio.run(run()) + + +@cli.command() +def statistics(): + async def run(): + app = Application(db_path="sqlite:///snek.db") + app.services.statistics.database() + asyncio.run(run()) @cli.command() def maintenance():