From 65d0cc5b4d21ad24c584bb6bf9308500992fd400 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anna=20=E2=80=9CCyberTailor=E2=80=9D?= Date: Wed, 10 May 2023 07:28:21 +0500 Subject: [PATCH 1/6] Added argument parser Just for config file path. Defaults to environment variable "MASTOPOSTER_CONFIG_FILE". --- mastoposter/__main__.py | 36 ++++++++++++++++++++++++++++-------- 1 file changed, 28 insertions(+), 8 deletions(-) diff --git a/mastoposter/__main__.py b/mastoposter/__main__.py index 195b61a..6fd395b 100644 --- a/mastoposter/__main__.py +++ b/mastoposter/__main__.py @@ -13,6 +13,7 @@ but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. """ +from argparse import ArgumentParser from asyncio import run from configparser import ConfigParser, ExtendedInterpolation from logging import ( @@ -23,14 +24,21 @@ from logging import ( getLevelName, getLogger, ) -from sys import argv, stdout -from mastoposter import execute_integrations, load_integrations_from -from mastoposter.integrations import FilteredIntegration -from mastoposter.sources import websocket_source +from os import getenv +from sys import stdout from typing import AsyncGenerator, Callable, List -from mastoposter.types import Account, Status + from httpx import Client, HTTPTransport +from mastoposter import ( + execute_integrations, + load_integrations_from, + __version__, + __description__ +) +from mastoposter.integrations import FilteredIntegration +from mastoposter.sources import websocket_source +from mastoposter.types import Account, Status from mastoposter.utils import normalize_config @@ -95,10 +103,22 @@ async def listen( await execute_integrations(status, drains) -def main(config_path: str = argv[1]): - conf = ConfigParser(interpolation=ExtendedInterpolation()) - conf.read(config_path) +def main(): + parser = ArgumentParser( + prog="mastoposter", + description=__description__ + ) + parser.add_argument("config", nargs="?", + default=getenv("MASTOPOSTER_CONFIG_FILE") + ) + parser.add_argument("-v", action="version", version=__version__) + args = parser.parse_args() + if not args.config: + raise RuntimeError("No config file. Aborting") + + conf = ConfigParser(interpolation=ExtendedInterpolation()) + conf.read(args.config) init_logger(getLevelName(conf["main"].get("loglevel", "INFO"))) normalize_config(conf) From 9d8c8252a40807906b647664ddcde0bdc97cb02d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anna=20=E2=80=9CCyberTailor=E2=80=9D?= Date: Wed, 10 May 2023 07:13:18 +0500 Subject: [PATCH 2/6] Updated boilerplate config There were some mistakes. --- config.ini | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/config.ini b/config.ini index f383747..230c539 100644 --- a/config.ini +++ b/config.ini @@ -1,8 +1,3 @@ -[DEFAULT] -# Number of retries in case request fails. Applies globally -# Can be changed on per-module basis -http-retries = 5 - [main] # This is a list of output modules. Each module should be defined in section, # named "module/MODULENAME". Space-separated list of strings. @@ -12,7 +7,7 @@ modules = telegram instance = mastodon.example.org # Mastodon user token. -# Required permissions: read:statuses read:lists +# Required permissions: read:accounts read:statuses read:lists # You can get your token by creating application in # ${instance}/settings/applications token = blahblah @@ -38,6 +33,10 @@ list = 1 auto-reconnect = yes reconnect-delay = 1.0 +# Number of retries in case request fails. Applies globally +# Can be changed on per-module basis +http-retries = 5 + # Example Telegram integration. You can use it as a template [module/telegram] type = telegram @@ -109,7 +108,7 @@ webhook = url ;type = content ;# Mode of the filter. ;# "regexp" requires "regexp" property and should contain... A RegExp -;# "hashtag" should contain space-separated list of tags +;# "tag" should contain space-separated list of tags ;mode = regexp ;# Regular expression pattern to be matched ;regexp = ^x-no-repost From cc817f992464cbb067531d43d73b39debf86676b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anna=20=E2=80=9CCyberTailor=E2=80=9D?= Date: Wed, 10 May 2023 08:50:52 +0500 Subject: [PATCH 3/6] Fixed misplaces ul/ol in node_to_plaintext --- mastoposter/utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mastoposter/utils.py b/mastoposter/utils.py index f46d789..3cab198 100644 --- a/mastoposter/utils.py +++ b/mastoposter/utils.py @@ -264,7 +264,7 @@ def node_to_plaintext(el: PageElement) -> str: " \u2022 %s" % li.replace("\n", "\n ").strip() for li in children ) - if el.name == "ol" + if el.name == "ul" else ( "%d. %s" % (i, li.replace("\n", "\n ").strip()) for i, li in enumerate(children) From f6dbac76e41926339ef5eb3b065a8c2c9f194b07 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anna=20=E2=80=9CCyberTailor=E2=80=9D?= Date: Wed, 10 May 2023 06:48:56 +0500 Subject: [PATCH 4/6] Added OpenRC service --- contrib/mastoposter.initd | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 contrib/mastoposter.initd diff --git a/contrib/mastoposter.initd b/contrib/mastoposter.initd new file mode 100644 index 0000000..a35730f --- /dev/null +++ b/contrib/mastoposter.initd @@ -0,0 +1,26 @@ +#!/sbin/openrc-run +# mastoposter - configurable reposter from Mastodon-compatible Fediverse servers +# Copyright (C) 2022-2023 hatkidchan +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. + +# shellcheck shell=sh + +command="/usr/bin/mastoposter" +command_args="${MASTOPOSTER_CONFIG_FILE:=/etc/mastoposter/config.ini}" +command_background=1 +pidfile="/run/${RC_SVCNAME}.pid" +output_log="/var/log/${RC_SVCNAME}.log" +error_log="${output_log}" + +depend() { + need net +} From 19cdde7ef4bdc0f56dd123a8822b539ff100164b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anna=20=E2=80=9CCyberTailor=E2=80=9D?= Date: Wed, 10 May 2023 06:49:39 +0500 Subject: [PATCH 5/6] Added SystemD service --- contrib/mastoposter.service | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 contrib/mastoposter.service diff --git a/contrib/mastoposter.service b/contrib/mastoposter.service new file mode 100644 index 0000000..028cb77 --- /dev/null +++ b/contrib/mastoposter.service @@ -0,0 +1,28 @@ +# mastoposter - configurable reposter from Mastodon-compatible Fediverse servers +# Copyright (C) 2022-2023 hatkidchan +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. + +[Unit] +Description=Configurable reposter from Mastodon-compatible Fediverse servers +After=network.target +Wants=network-online.target + +[Service] +Type=simple +ExecStart=/usr/bin/mastoposter /etc/mastoposter/config.ini +Restart=always +RestartSec=5 +StandardOutput=journal +StandardError=inherit + +[Install] +WantedBy=multi-user.target From 43963144c424a4cf23d2fca8b1ea201774551a78 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anna=20=E2=80=9CCyberTailor=E2=80=9D?= Date: Wed, 10 May 2023 06:50:14 +0500 Subject: [PATCH 6/6] Include 'contrib' in sdist --- pyproject.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyproject.toml b/pyproject.toml index 8cfac9f..cd08aa1 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -34,4 +34,4 @@ Source = "https://github.com/hatkidchan/mastoposter" mastoposter = "mastoposter.__main__:main" [tool.flit.sdist] -include = ["config.ini"] +include = ["config.ini", "contrib/*"]