add(cloudron) hacky install of yt-dlp

This commit is contained in:
SansGuidon 2025-04-28 11:05:47 +00:00
parent 86b743457b
commit 64a2d28957

View File

@ -0,0 +1,118 @@
set -euo pipefail
read -rsp "Set password for admin (HTTP basic auth) user: " pass1; echo
read -rsp "Confirm password: " pass2; echo
[ "$pass1" != "$pass2" ] && echo "Passwords do not match." && exit 1
read -rp "Enter downloads directory [/app/data/downloads]: " downloads_dir
downloads_dir=${downloads_dir:-/app/data/downloads}
mkdir -p /app/data/public/bin "$downloads_dir"
/bin/rm -f /app/data/public/bin/yt-dlp
curl -sSL https://github.com/yt-dlp/yt-dlp/releases/latest/download/yt-dlp -o /app/data/public/bin/yt-dlp
chmod +x /app/data/public/bin/yt-dlp
if command -v htpasswd >/dev/null 2>&1; then
htpasswd -cb /app/data/.htpasswd admin "$pass1"
else
echo "admin:$(openssl passwd -apr1 "$pass1")" > /app/data/.htpasswd
fi
cat > /app/data/public/.htaccess <<'EOF'
AuthType Basic
AuthName "Restricted Area"
AuthUserFile /app/data/.htpasswd
Require valid-user
EOF
set +u
{
echo "[settings]"
echo "downloads_dir=$downloads_dir"
} > /app/data/config.ini
set -u
cat > /app/data/public/index.php <<'EOF'
<?php
$config = parse_ini_file("/app/data/config.ini");
$downloads_dir = $config["downloads_dir"];
$cookies_path = "/app/data/cookies.txt";
$cookies_option = file_exists($cookies_path) ? "--cookies $cookies_path" : "";
if ($_SERVER["REQUEST_METHOD"] === "POST") {
if (isset($_POST["clearcookies"])) {
@unlink($cookies_path);
echo "<p>Cookies cleared. <a href='/'>Go back</a></p>";
exit;
}
if (isset($_FILES["cookies"])) {
if ($_FILES["cookies"]["error"] === UPLOAD_ERR_OK && is_uploaded_file($_FILES["cookies"]["tmp_name"])) {
move_uploaded_file($_FILES["cookies"]["tmp_name"], $cookies_path);
echo "<p>Cookies uploaded successfully. <a href='/'>Go back</a></p>";
exit;
} else {
echo "<p>Cookie upload failed. <a href='/'>Go back</a></p>";
exit;
}
}
if (!empty($_POST["urls"])) {
$urls = array_filter(array_map("trim", explode("\n", $_POST["urls"])));
foreach ($urls as $url) {
$escaped_url = escapeshellarg($url);
$command = "/app/data/public/bin/yt-dlp -o " . escapeshellarg("$downloads_dir/%(title)s.%(ext)s") . " $cookies_option $escaped_url >> /tmp/yt-dlp.log 2>&1 &";
shell_exec($command);
}
echo "<p>Downloads started. <a href='/'>Go back</a> or <a href='/log.php'>View Log</a>.</p>";
exit;
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>yt-dlp Downloader</title></head>
<body>
<h1>yt-dlp Downloader</h1>
<form method="post">
<textarea name="urls" rows="10" cols="80" placeholder="One URL per line"></textarea><br><br>
<button type="submit">Download</button>
</form>
<h2>Cookies Management</h2>
<?php if (!file_exists("/app/data/cookies.txt")): ?>
<form method="post" enctype="multipart/form-data">
<input type="file" name="cookies" accept=".txt" required><br><br>
<button type="submit">Upload Cookies.txt</button>
</form>
<?php else: ?>
<form method="post">
<button type="submit" name="clearcookies" value="1">Clear Cookies.txt</button>
</form>
<?php endif; ?>
<p><small>Need help with cookies? <a href="https://github.com/yt-dlp/yt-dlp/wiki/FAQ#how-do-i-pass-cookies-to-yt-dlp" target="_blank">Learn how to export your cookies.txt</a>.</small></p>
<p><a href="/log.php" target="_blank">View Download Log</a></p>
<h2>Latest Downloads</h2>
<ul>
<?php
$lines = @file("/tmp/yt-dlp.log", FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES) ?: [];
$success = array_filter($lines, function($line) {
return preg_match("/Destination:/", $line);
});
$success = array_reverse($success);
$success = array_slice($success, 0, 5);
foreach ($success as $entry) {
echo "<li>" . htmlspecialchars($entry) . "</li>";
}
?>
</ul>
</body>
</html>
EOF
cat > /app/data/public/log.php <<'EOF'
<?php
header("Refresh: 3");
echo "<pre>";
system("tail -n 100 /tmp/yt-dlp.log 2>&1");
echo "</pre>";
?>
EOF
chown -R www-data:www-data /app/data/public
echo "Done. Access via your LAMP app URL (protected)."