2022-08-24 08:09:41 +03:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
from asyncio import run
|
|
|
|
from configparser import ConfigParser
|
2022-08-26 18:37:36 +03:00
|
|
|
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
|
2022-08-26 02:03:06 +03:00
|
|
|
|
|
|
|
# TODO: add option/filter to handle that
|
|
|
|
if status.visibility in ("direct", "private"):
|
2022-08-24 08:09:41 +03:00
|
|
|
continue
|
2022-08-26 02:03:06 +03:00
|
|
|
|
|
|
|
# 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
|
2022-08-26 02:03:06 +03:00
|
|
|
|
2022-08-26 18:37:36 +03:00
|
|
|
await execute_integrations(status, drains)
|
2022-08-24 08:09:41 +03:00
|
|
|
|
|
|
|
|
|
|
|
def main(config_path: str):
|
|
|
|
conf = ConfigParser()
|
|
|
|
conf.read(config_path)
|
|
|
|
|
2022-08-26 02:03:06 +03:00
|
|
|
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]
|
|
|
|
|
2022-08-26 18:37:36 +03:00
|
|
|
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,
|
2022-08-27 14:27:42 +03:00
|
|
|
reconnect=conf["main"].getboolean("auto_reconnect", 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])
|