-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
move media downloading to another class and another async task
- Loading branch information
Showing
8 changed files
with
235 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import asyncio | ||
from asyncio import sleep | ||
from os.path import relpath | ||
from typing import Union, Optional | ||
|
||
from pyrogram import Client | ||
from pyrogram.errors import RPCError | ||
|
||
from .export_config import ExportConfig | ||
from .progress_print import ProgressPrint | ||
|
||
|
||
class MediaExporter: | ||
def __init__(self, client: Client, config: ExportConfig, media_dict: dict, progress: ProgressPrint): | ||
self.client = client | ||
self.config = config | ||
self.output = media_dict | ||
self.task = None | ||
self.queue: list[tuple[str, str, Union[str, int]]] = [] | ||
self.ids: set[Union[str, int]] = set() | ||
self.all_ids: set[Union[str, int]] = set() | ||
self.progress = progress | ||
|
||
self._running = False | ||
|
||
def add(self, file_id: str, download_dir: str, out_id: Union[str, int]) -> None: | ||
if out_id in self.all_ids: return | ||
self.queue.append((file_id, download_dir, out_id)) | ||
self.ids.add(out_id) | ||
self.all_ids.add(out_id) | ||
self._status() | ||
|
||
async def _download(self, file_id: str, download_dir: str, out_id: Union[str, int]) -> None: | ||
try: | ||
path = await self.client.download_media(file_id, file_name=download_dir) | ||
except RPCError: | ||
return | ||
path = relpath(path, self.config.output_dir.absolute()) | ||
self.output[out_id] = path | ||
|
||
def _status(self, status: str=None) -> None: | ||
with self.progress.update(): | ||
self.progress.media_status = status or self.progress.media_status | ||
self.progress.media_queue = len(self.queue) | ||
|
||
async def _task(self) -> None: | ||
while self._running: | ||
if not self.queue: | ||
self._status("Idle...") | ||
await sleep(.1) | ||
continue | ||
self._status("Downloading...") | ||
await self._download(*self.queue[0]) | ||
_, _, task_id = self.queue.pop(0) | ||
self.ids.discard(task_id) | ||
|
||
self._status("Stopped...") | ||
|
||
async def run(self) -> None: | ||
self._running = True | ||
self.task = asyncio.get_event_loop().create_task(self._task()) | ||
|
||
async def stop(self) -> None: | ||
await self.wait() | ||
self._running = False | ||
|
||
async def wait(self, messages: Optional[list[int]]=None) -> None: | ||
messages = set(messages) if messages is not None else None | ||
while self._running and self.queue: | ||
if messages is not None and not messages.intersection(self.ids): | ||
break | ||
await sleep(.1) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
from asyncio import sleep, get_event_loop | ||
|
||
from pyrogram import Client | ||
from pyrogram.types import Message as PyroMessage | ||
|
||
from .progress_print import ProgressPrint | ||
|
||
|
||
class Preloader: | ||
def __init__(self, client: Client, progress: ProgressPrint, media_cb): | ||
self.client = client | ||
self.progress = progress | ||
self.finished = False | ||
self.messages: list[PyroMessage] = [] | ||
self.messages_loaded = 0 | ||
self.media_cb = media_cb | ||
|
||
self._task = None | ||
self._pyro_args = () | ||
self._pyro_kwargs = {} | ||
|
||
def __call__(self, *pyrogram_args, **pyrogram_kwargs): | ||
self._pyro_args = pyrogram_args | ||
self._pyro_kwargs = pyrogram_kwargs | ||
return self | ||
|
||
def __aiter__(self): | ||
return self | ||
|
||
async def _preload(self) -> None: | ||
async for message in self.client.get_chat_history(*self._pyro_args, **self._pyro_kwargs): | ||
self.messages.append(message) | ||
self.messages_loaded += 1 | ||
|
||
if message.media and self.media_cb: | ||
await self.media_cb(message) | ||
|
||
with self.progress.update(): | ||
self.progress.status = "Preloading messages and media..." | ||
self.progress.messages_loaded = self.messages_loaded | ||
|
||
self.finished = True | ||
|
||
async def __anext__(self) -> PyroMessage: | ||
if self._task is None: self._task = get_event_loop().create_task(self._preload()) | ||
|
||
while not self.finished and not self.messages: | ||
await sleep(.01) | ||
|
||
if self.finished and not self.messages: | ||
raise StopAsyncIteration | ||
|
||
return self.messages.pop(0) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.