diff options
| author | Tolmachev Igor <me@igorek.dev> | 2024-09-23 22:45:35 +0300 |
|---|---|---|
| committer | Tolmachev Igor <me@igorek.dev> | 2024-09-23 22:45:35 +0300 |
| commit | 44b8439b5b92bc5892f329fbca3f940196dedc70 (patch) | |
| tree | 80119b17ef40b7261afde41bcc1ce59618fb3249 | |
| parent | 84e5548599a0145daeade340d2afd90ad32a614d (diff) | |
| download | video2story-44b8439b5b92bc5892f329fbca3f940196dedc70.tar.gz video2story-44b8439b5b92bc5892f329fbca3f940196dedc70.zip | |
Fix `Chat not found` error
| -rw-r--r-- | video2story/__init__.py | 2 | ||||
| -rw-r--r-- | 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( | |||
| 166 | ) | 166 | ) |
| 167 | 167 | ||
| 168 | 168 | ||
| 169 | def main(): | 169 | def main() -> None: |
| 170 | args = parser.parse_args() | 170 | args = parser.parse_args() |
| 171 | if args.module == "video": | 171 | if args.module == "video": |
| 172 | if not (1 <= args.duration <= 60): | 172 | 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 @@ | |||
| 1 | from os import listdir | 1 | from os import listdir |
| 2 | from os.path import isdir, join | 2 | from os.path import isdir, join |
| 3 | from threading import Event | 3 | from threading import Event |
| 4 | from time import sleep | ||
| 5 | 4 | ||
| 6 | from telegram.client import Telegram | 5 | from telegram.client import Telegram |
| 7 | 6 | ||
| @@ -29,6 +28,11 @@ def upload( | |||
| 29 | telegram.login() | 28 | telegram.login() |
| 30 | me = int(telegram.call_method("getMe", block=True).update["id"]) # type:ignore | 29 | me = int(telegram.call_method("getMe", block=True).update["id"]) # type:ignore |
| 31 | 30 | ||
| 31 | done = Event() | ||
| 32 | telegram.add_update_handler("updateSavedMessagesTopic", lambda *_: done.set()) | ||
| 33 | telegram.call_method("loadSavedMessagesTopics", {"limit": 1}, block=True) | ||
| 34 | done.wait() | ||
| 35 | |||
| 32 | if users is not None: | 36 | if users is not None: |
| 33 | user_ids = [] | 37 | user_ids = [] |
| 34 | for u in users: | 38 | for u in users: |
| @@ -38,7 +42,7 @@ def upload( | |||
| 38 | 42 | ||
| 39 | try: | 43 | try: |
| 40 | user_ids.append( | 44 | user_ids.append( |
| 41 | telegram.call_method( | 45 | telegram.call_method( # type: ignore |
| 42 | "searchPublicChat", | 46 | "searchPublicChat", |
| 43 | {"username": u.strip().removeprefix("@")}, | 47 | {"username": u.strip().removeprefix("@")}, |
| 44 | block=True, | 48 | block=True, |
| @@ -53,14 +57,27 @@ def upload( | |||
| 53 | print("Input folder does not exists") | 57 | print("Input folder does not exists") |
| 54 | exit(1) | 58 | exit(1) |
| 55 | 59 | ||
| 56 | queue = set() | 60 | queue: set[int] = set() |
| 57 | done = Event() | 61 | done = Event() |
| 58 | 62 | ||
| 59 | for i in range(start, (end + 1) if end is not None else len(listdir(input_dir))): | 63 | def wait_uploading(file: dict[str, object]) -> None: |
| 60 | print(f"Adding to the uploading queue. (VIDEO-ID: {i})") | 64 | if ( |
| 65 | file["file"]["id"] in queue # type: ignore | ||
| 66 | and not file["file"]["remote"]["is_uploading_active"] # type:ignore | ||
| 67 | ): | ||
| 68 | queue.remove(file["file"]["id"]) # type: ignore | ||
| 69 | |||
| 70 | print(f" - Story uploaded. {len(queue)} left.") | ||
| 71 | if len(queue) == 0: | ||
| 72 | done.set() | ||
| 61 | 73 | ||
| 74 | telegram.add_update_handler("updateFile", wait_uploading) | ||
| 75 | |||
| 76 | print("Start uploading") | ||
| 77 | for i in range(start, (end + 1) if end is not None else len(listdir(input_dir))): | ||
| 78 | print(f" - Adding story to the uploading queue. (VIDEO-ID: {i})") | ||
| 62 | queue.add( | 79 | queue.add( |
| 63 | telegram.call_method( | 80 | telegram.call_method( # type: ignore |
| 64 | "sendStory", | 81 | "sendStory", |
| 65 | { | 82 | { |
| 66 | "chat_id": me, | 83 | "chat_id": me, |
| @@ -102,22 +119,6 @@ def upload( | |||
| 102 | block=True, | 119 | block=True, |
| 103 | ).update["content"]["video"]["video"]["id"] | 120 | ).update["content"]["video"]["video"]["id"] |
| 104 | ) | 121 | ) |
| 105 | sleep(0.5) | ||
| 106 | |||
| 107 | print(f"Uploading. {len(queue)} left") | ||
| 108 | |||
| 109 | def update(file: dict[str, object]) -> None: | ||
| 110 | if ( | ||
| 111 | file["file"]["id"] in queue | ||
| 112 | and not file["file"]["remote"]["is_uploading_active"] | ||
| 113 | ): | ||
| 114 | queue.remove(file["file"]["id"]) | ||
| 115 | |||
| 116 | if len(queue) == 0: | ||
| 117 | print("Done") | ||
| 118 | done.set() | ||
| 119 | else: | ||
| 120 | print(f"Uploading. {len(queue)} left") | ||
| 121 | 122 | ||
| 122 | telegram.add_update_handler("updateFile", update) | ||
| 123 | done.wait() | 123 | done.wait() |
| 124 | print("Done") | ||
