2022-08-24 08:09:41 +03:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
from asyncio import run
|
|
|
|
from configparser import ConfigParser
|
2022-08-26 14:32:55 +03:00
|
|
|
from mastoposter.integrations import DiscordIntegration, TelegramIntegration
|
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-24 08:09:41 +03:00
|
|
|
for drain in drains:
|
|
|
|
await drain.post(status)
|
|
|
|
|
|
|
|
|
|
|
|
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]
|
|
|
|
|
|
|
|
modules: List[BaseIntegration] = []
|
2022-08-24 08:09:41 +03:00
|
|
|
for module_name in conf.get("main", "modules").split():
|
|
|
|
module = conf[f"module/{module_name}"]
|
|
|
|
if module["type"] == "telegram":
|
|
|
|
modules.append(
|
|
|
|
TelegramIntegration(
|
2022-08-26 02:03:06 +03:00
|
|
|
token=module["token"],
|
|
|
|
chat_id=module["chat"],
|
|
|
|
show_post_link=module.getboolean("show_post_link", fallback=True),
|
|
|
|
show_boost_from=module.getboolean("show_boost_from", fallback=True),
|
2022-08-24 08:09:41 +03:00
|
|
|
)
|
|
|
|
)
|
2022-08-26 02:03:06 +03:00
|
|
|
elif module["type"] == "discord":
|
|
|
|
modules.append(DiscordIntegration(webhook=module["webhook"]))
|
2022-08-24 08:09:41 +03:00
|
|
|
else:
|
|
|
|
raise ValueError("Invalid module type %r" % module["type"])
|
|
|
|
|
|
|
|
url = "wss://{}/api/v1/streaming".format(conf["main"]["instance"])
|
|
|
|
run(
|
|
|
|
listen(
|
|
|
|
websocket_source,
|
|
|
|
modules,
|
|
|
|
conf["main"]["user"],
|
|
|
|
url=url,
|
|
|
|
list=conf["main"]["list"],
|
|
|
|
access_token=conf["main"]["token"],
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
from sys import argv
|
|
|
|
|
|
|
|
main(argv[1])
|