commit b1fcd85d89996c66832f79522fb11cc67b6db25f parent 21d253319a823b879d7b3147d37df8cc5df021cc Author: JayVii <jayvii[AT]posteo[DOT]de> Date: Thu, 29 Feb 2024 20:33:34 +0100 Retry download and return error on failure Retries Video-Download up to three times. Returns a 404 error on failure. Diffstat:
M | index.php | | | 30 | ++++++++++++++++++++++-------- |
1 file changed, 22 insertions(+), 8 deletions(-)
diff --git a/index.php b/index.php @@ -129,7 +129,12 @@ if (!is_null($_GET["channel"])) { print_r($rss_xml); } else if (!is_null($_GET["video"])) { - if (!file_exists(basename($_GET["video"]) . ".opus")) { + $download_retry = 0; + while ( + !file_exists(basename($_GET["video"]) . ".opus") && + $download_retry <= 3 + ) { + $download_retry++; passthru( "yt-dlp " . "-x " . @@ -138,13 +143,22 @@ if (!is_null($_GET["channel"])) { "https://www.youtube.com/watch?v=" . basename($_GET["video"]) ); } - header("content-type: audio/ogg; codec=opus"); - header("content-length: " . filesize(basename($_GET["video"]) . ".opus")); - header( - "content-disposition: inline; filename=" . - basename($_GET["video"]) . ".opus" - ); - readfile(basename($_GET['video']) . ".opus"); + // If file has been downloaded properly, return to use + if (file_exists(basename($_GET["video"]) . ".opus")) { + header("content-type: audio/ogg; codec=opus"); + header( + "content-length: " . filesize(basename($_GET["video"]) . ".opus") + ); + header( + "content-disposition: inline; filename=" . + basename($_GET["video"]) . ".opus" + ); + readfile(basename($_GET['video']) . ".opus"); + } else { + // otherwise return error and exit + http_response_code(404); + die(); + } } else { echo "<html><head><title>yt2rss</title></head><body>"; echo "<h1>yt2rss</h1>";