From ab1de9a86f6ea08c98c64e4880f5e3b7f339a3e2 Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Sat, 3 Apr 2021 22:53:33 +0200 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Create=20basic=20main?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- poetry.lock | 14 +++++++- pyproject.toml | 1 + royalnet_telethon/__main__.py | 67 +++++++++++++++++++++++++++++++++++ 3 files changed, 81 insertions(+), 1 deletion(-) create mode 100644 royalnet_telethon/__main__.py diff --git a/poetry.lock b/poetry.lock index eb35e28f..d4b2ec3f 100644 --- a/poetry.lock +++ b/poetry.lock @@ -63,6 +63,14 @@ category = "dev" optional = false python-versions = ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*, !=3.4.*" +[[package]] +name = "click" +version = "7.1.2" +description = "Composable command line interface toolkit" +category = "main" +optional = false +python-versions = ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*, !=3.4.*" + [[package]] name = "colorama" version = "0.4.4" @@ -501,7 +509,7 @@ brotli = ["brotlipy (>=0.6.0)"] [metadata] lock-version = "1.1" python-versions = "^3.8" -content-hash = "3920fb7d7d5d93ec0f5b54dc23777eb80a016ca3fa3c3c5fce14711928204f3f" +content-hash = "bcc3b7e2bfc0ff9a9b9c044e613a4f2dd99047173dd9b66e0e13c39d08e6a9d9" [metadata.files] alabaster = [ @@ -532,6 +540,10 @@ chardet = [ {file = "chardet-4.0.0-py2.py3-none-any.whl", hash = "sha256:f864054d66fd9118f2e67044ac8981a54775ec5b67aed0441892edb553d21da5"}, {file = "chardet-4.0.0.tar.gz", hash = "sha256:0d6f53a15db4120f2b08c94f11e7d93d2c911ee118b6b30a04ec3ee8310179fa"}, ] +click = [ + {file = "click-7.1.2-py2.py3-none-any.whl", hash = "sha256:dacca89f4bfadd5de3d7489b7c8a566eee0d3676333fbb50030263894c38c0dc"}, + {file = "click-7.1.2.tar.gz", hash = "sha256:d2b5255c7c6349bc1bd1e59e08cd12acbbd63ce649f2588755783aa94dfb6b1a"}, +] colorama = [ {file = "colorama-0.4.4-py2.py3-none-any.whl", hash = "sha256:9f47eda37229f68eee03b24b9748937c7dc3868f906e8ba69fbcbdd3bc5dc3e2"}, {file = "colorama-0.4.4.tar.gz", hash = "sha256:5941b2b48a20143d2267e95b1c2a7603ce057ee39fd88e7329b0c292aa16869b"}, diff --git a/pyproject.toml b/pyproject.toml index 05f58011..fc147431 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -21,6 +21,7 @@ classifiers = [ python = "^3.8" royalnet = "^6.1.2" Telethon = "^1.21.1" +click = "^7.1.2" [tool.poetry.dev-dependencies] pytest = "^6.1.1" diff --git a/royalnet_telethon/__main__.py b/royalnet_telethon/__main__.py new file mode 100644 index 00000000..9dcbf23f --- /dev/null +++ b/royalnet_telethon/__main__.py @@ -0,0 +1,67 @@ +# Special imports +from __future__ import annotations +import royalnet.royaltyping as t + +# External imports +import logging +import importlib +import click +import asyncio +import royalnet.engineer as engi + +# Internal imports +from .pda import TelethonPDA + +# Special global objects +log = logging.getLogger(__name__) + + +# Code +@click.command() +@click.option("-p", "--pack", "packs", multiple=True) +@click.option("-i", "--api-id", "api_id", required=True, type=int) +@click.option("-h", "--api-hash", "api_hash", required=True) +@click.option("-t", "--token", "token", required=True) +@click.option("-u", "--username", "username", required=True) +def main(packs: t.List[str], token: str, api_id: int, api_hash: str, username: str): + log.debug("Creating PDA...") + pda = TelethonPDA(tg_api_id=api_id, tg_api_hash=api_hash, bot_username=username) + + for pack in packs: + log.debug(f"Importing module: {pack!r}") + try: + pack = importlib.import_module(pack) + except ImportError as e: + log.error(f"Skipping {pack!r}: {e!r}") + continue + + for attribute in dir(pack): + log.debug(f"Getting attribute: {attribute!r}") + value = pack.__getattribute__(attribute) + log.debug(f"Attribute is: {value!r}") + + if isinstance(value, engi.PartialCommand): + log.debug(f"Attribute is a PartialCommand, registering it as: {value.f.__name__!r}") + pda.register_partial(part=value, names=[value.f.__name__]) + + elif isinstance(value, engi.Conversation): + log.debug(f"Attribute is a Conversation, registering it...") + pda.register_conversation(conv=value) + + log.debug("Getting event loop...") + loop = asyncio.get_event_loop() + log.debug(f"Event loop is: {loop!r}") + + log.debug("Running the PDA until interrupted...") + try: + loop.run_until_complete(pda.run(bot_token=token)) + except KeyboardInterrupt: + log.debug("Got an interrupt, shutting down...") + exit(0) + + log.fatal("PDA stopped unexpectedly, shutting down...") + exit(1) + + +if __name__ == "__main__": + main()