From ef6ed81d019e7c71ed7530a79e638ae5875e2bf8 Mon Sep 17 00:00:00 2001 From: baixl Date: Tue, 12 Feb 2019 16:43:43 +0800 Subject: [PATCH] =?UTF-8?q?download=20speed=20=E2=80=8B=E2=80=8Bup:skip=20?= =?UTF-8?q?sleep=20if=20download=20not=20start=20or=20media=20type=20not?= =?UTF-8?q?=20match?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- telegram_export/downloader.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/telegram_export/downloader.py b/telegram_export/downloader.py index bf41cc3..f1efe66 100755 --- a/telegram_export/downloader.py +++ b/telegram_export/downloader.py @@ -226,7 +226,7 @@ async def _download_media(self, media_id, context_id, sender_id, date, media_type = media_row[3].split('.') media_type, media_subtype = media_type[0], media_type[-1] if media_type not in ('photo', 'document'): - return # Only photos or documents are actually downloadable + return False # Only photos or documents are actually downloadable formatter = defaultdict( str, @@ -260,7 +260,7 @@ async def _download_media(self, media_id, context_id, sender_id, date, filename += '.{}{}'.format(media_id, ext) if os.path.isfile(filename): __log__.debug('Skipping already-existing file %s', filename) - return + return False __log__.debug('Downloading to %s', filename) os.makedirs(os.path.dirname(filename), exist_ok=True) @@ -301,16 +301,18 @@ def progress(saved, total): progress_callback=progress ) self._incomplete_download = None + return True async def _media_consumer(self, queue, bar): while self._running: start = time.time() media_id, context_id, sender_id, date = await queue.get() - await self._download_media(media_id, context_id, sender_id, + need_sleep = await self._download_media(media_id, context_id, sender_id, datetime.datetime.utcfromtimestamp(date), bar) queue.task_done() - await asyncio.sleep(max(MEDIA_DELAY - (time.time() - start), 0), + if need_sleep: + await asyncio.sleep(max(MEDIA_DELAY - (time.time() - start), 0), loop=self.loop) async def _user_consumer(self, queue, bar):