From d37ed659cdb0b3dcfd9fd3afb4be6f5cb39a6e35 Mon Sep 17 00:00:00 2001 From: blank X Date: Sat, 10 Apr 2021 13:23:05 +0700 Subject: [PATCH] Actually do stuff wow such commit messages --- autoytarchive/workers.py | 39 +++++++++++++++++++++++++++++++-------- 1 file changed, 31 insertions(+), 8 deletions(-) diff --git a/autoytarchive/workers.py b/autoytarchive/workers.py index c150b95..a3be17c 100644 --- a/autoytarchive/workers.py +++ b/autoytarchive/workers.py @@ -119,7 +119,6 @@ async def _video_worker(): video_json, start_time, first_try_live = await video_queue.get() late_to_queue = (Decimal(time.time()) - Decimal(start_time)) > 5 is_late = first_try_live or late_to_queue - command = ['ffmpeg', '-y'] tempdir_obj = tempfile.TemporaryDirectory(dir='.') try: tempdir = tempdir_obj.name @@ -136,14 +135,37 @@ async def _video_worker(): video_json = tmp break await asyncio.sleep(wait_time) - if video_json.get('requested_formats'): - for i in video_json['requested_formats']: - command.extend(('-i', i['url'])) - else: - command.extend(('-i', video_json['url'])) + async def construct_command(): + nonlocal video_json + command = ['ffmpeg', '-y'] + is_manifest = False + if video_json.get('requested_formats'): + for i in video_json['requested_formats']: + if urlparse(i['url']).netloc == 'manifest.googlevideo.com': + is_manifest = True + command.extend(('-i', i['url'])) + else: + is_manifest = urlparse(video_json['url']).netloc == 'manifest.googlevideo.com' + command.extend(('-i', video_json['url'])) + if is_manifest: + await asyncio.sleep(video_json['duration'] + 30) + wait_time = 30 + for i in range(5): + try: + tmp = await client.loop.run_in_executor(None, ytdl.extract_info, video_json['id']) + except BaseException as e: + e = str(e) + if '429' in e or 'too many request' in e.lower(): + wait_time = (i + 1) * 60 * 60 + else: + video_json = tmp + break + await asyncio.sleep(wait_time) + return await construct_command() + command.extend(('-c', 'copy', video_filename)) + return command video_filename = os.path.join(tempdir, video_json['id'] + '.mkv') - command.extend(('-c', 'copy', video_filename)) - proc = await asyncio.create_subprocess_exec(*command) + proc = await asyncio.create_subprocess_exec(*(await construct_command())) text = 'New video' if is_late: text += ' (is late)' @@ -196,6 +218,7 @@ async def _video_worker(): video_json = tmp break await asyncio.sleep(wait_time) + proc = await asyncio.create_subprocess_exec(*(await construct_command())) except BaseException: tempdir_obj.cleanup() raise