social media crossposting tool. 3rd time's the charm
mastodon
misskey
crossposting
bluesky
1import queue
2import threading
3from pathlib import Path
4from time import sleep
5from typing import Callable
6
7import env
8from database.migrations import DatabaseMigrator
9from util.util import LOGGER
10
11
12def main() -> None:
13 data = Path(env.DATA_DIR)
14
15 if not data.exists():
16 data.mkdir(parents=True)
17
18 settings = data.joinpath("settings.json")
19 database = data.joinpath("db.sqlite")
20
21 if not settings.exists():
22 LOGGER.info("First launch detected! Creating %s and exiting!", settings)
23 return
24
25 LOGGER.info("Loading settings...")
26 # TODO
27
28 migrator = DatabaseMigrator(database, Path(env.MIGRATIONS_DIR))
29 try:
30 migrator.migrate()
31 except Exception:
32 LOGGER.exception("Failed to migrate database!")
33 finally:
34 migrator.close()
35
36 LOGGER.info("Starting task worker...")
37
38 def worker(task_queue: queue.Queue[Callable[[], None] | None]):
39 while True:
40 task = task_queue.get()
41 if task is None:
42 break
43
44 try:
45 task()
46 except Exception:
47 LOGGER.exception("Exception in worker thread!")
48 finally:
49 task_queue.task_done()
50
51 task_queue: queue.Queue[Callable[[], None] | None] = queue.Queue()
52 thread = threading.Thread(target=worker, args=(task_queue,), daemon=True)
53 thread.start()
54
55 LOGGER.info("Connecting to %s...", 'TODO') # TODO
56 try:
57 task_queue.put(lambda: print("hi"))
58 sleep(10) # TODO
59 except KeyboardInterrupt:
60 LOGGER.info("Stopping...")
61
62 task_queue.join()
63 task_queue.put(None)
64 thread.join()
65
66if __name__ == "__main__":
67 main()