mastoposter-oss_images/mastoposter/__main__.py

71 lines
1.9 KiB
Python
Raw Normal View History

2022-08-24 08:09:41 +03:00
#!/usr/bin/env python3
from asyncio import run
from configparser import ConfigParser
from mastoposter import execute_integrations, load_integrations_from
2022-08-24 08:28:18 +03:00
from mastoposter.sources import websocket_source
2022-08-26 14:32:55 +03:00
from typing import AsyncGenerator, Callable, List
2022-08-24 08:28:18 +03:00
from mastoposter.integrations.base import BaseIntegration
from mastoposter.types import Status
2022-08-24 08:09:41 +03:00
async def listen(
source: Callable[..., AsyncGenerator[Status, None]],
drains: List[BaseIntegration],
user: str,
/,
**kwargs,
):
async for status in source(**kwargs):
if status.account.id != user:
continue
# TODO: add option/filter to handle that
if status.visibility in ("direct", "private"):
2022-08-24 08:09:41 +03:00
continue
# TODO: find a better way to handle threads
2022-08-24 08:09:41 +03:00
if (
status.in_reply_to_account_id is not None
and status.in_reply_to_account_id != user
):
continue
await execute_integrations(status, drains)
2022-08-24 08:09:41 +03:00
def main(config_path: str):
conf = ConfigParser()
conf.read(config_path)
for section in conf.sections():
_remove = set()
for k, v in conf[section].items():
normalized_key = k.replace(" ", "_").replace("-", "_")
if k == normalized_key:
continue
conf[section][normalized_key] = v
_remove.add(k)
for k in _remove:
del conf[section][k]
modules = load_integrations_from(conf)
2022-08-24 08:09:41 +03:00
url = "wss://{}/api/v1/streaming".format(conf["main"]["instance"])
run(
listen(
websocket_source,
modules,
conf["main"]["user"],
url=url,
reconnect=conf["main"].getboolean("auto_reconnect", fallback=False),
2022-08-24 08:09:41 +03:00
list=conf["main"]["list"],
access_token=conf["main"]["token"],
)
)
if __name__ == "__main__":
from sys import argv
main(argv[1])