From 44b8439b5b92bc5892f329fbca3f940196dedc70 Mon Sep 17 00:00:00 2001 From: Tolmachev Igor Date: Mon, 23 Sep 2024 22:45:35 +0300 Subject: Fix `Chat not found` error --- video2story/__init__.py | 2 +- video2story/uploader.py | 47 ++++++++++++++++++++++++----------------------- 2 files changed, 25 insertions(+), 24 deletions(-) diff --git a/video2story/__init__.py b/video2story/__init__.py index 2fcf2a0..5e04371 100644 --- a/video2story/__init__.py +++ b/video2story/__init__.py @@ -166,7 +166,7 @@ story_parser.add_argument( ) -def main(): +def main() -> None: args = parser.parse_args() if args.module == "video": if not (1 <= args.duration <= 60): diff --git a/video2story/uploader.py b/video2story/uploader.py index 6a2af42..34aebcc 100644 --- a/video2story/uploader.py +++ b/video2story/uploader.py @@ -1,7 +1,6 @@ from os import listdir from os.path import isdir, join from threading import Event -from time import sleep from telegram.client import Telegram @@ -29,6 +28,11 @@ def upload( telegram.login() me = int(telegram.call_method("getMe", block=True).update["id"]) # type:ignore + done = Event() + telegram.add_update_handler("updateSavedMessagesTopic", lambda *_: done.set()) + telegram.call_method("loadSavedMessagesTopics", {"limit": 1}, block=True) + done.wait() + if users is not None: user_ids = [] for u in users: @@ -38,7 +42,7 @@ def upload( try: user_ids.append( - telegram.call_method( + telegram.call_method( # type: ignore "searchPublicChat", {"username": u.strip().removeprefix("@")}, block=True, @@ -53,14 +57,27 @@ def upload( print("Input folder does not exists") exit(1) - queue = set() + queue: set[int] = set() done = Event() - for i in range(start, (end + 1) if end is not None else len(listdir(input_dir))): - print(f"Adding to the uploading queue. (VIDEO-ID: {i})") + def wait_uploading(file: dict[str, object]) -> None: + if ( + file["file"]["id"] in queue # type: ignore + and not file["file"]["remote"]["is_uploading_active"] # type:ignore + ): + queue.remove(file["file"]["id"]) # type: ignore + + print(f" - Story uploaded. {len(queue)} left.") + if len(queue) == 0: + done.set() + telegram.add_update_handler("updateFile", wait_uploading) + + print("Start uploading") + for i in range(start, (end + 1) if end is not None else len(listdir(input_dir))): + print(f" - Adding story to the uploading queue. (VIDEO-ID: {i})") queue.add( - telegram.call_method( + telegram.call_method( # type: ignore "sendStory", { "chat_id": me, @@ -102,22 +119,6 @@ def upload( block=True, ).update["content"]["video"]["video"]["id"] ) - sleep(0.5) - - print(f"Uploading. {len(queue)} left") - - def update(file: dict[str, object]) -> None: - if ( - file["file"]["id"] in queue - and not file["file"]["remote"]["is_uploading_active"] - ): - queue.remove(file["file"]["id"]) - - if len(queue) == 0: - print("Done") - done.set() - else: - print(f"Uploading. {len(queue)} left") - telegram.add_update_handler("updateFile", update) done.wait() + print("Done") -- cgit v1.2.3