new working slimmed down Regulation script

This commit is contained in:
2026-05-01 20:12:46 -05:00
parent dad9999695
commit a84fc8fdc9
@@ -0,0 +1,170 @@
# YT Regulation Patreon
# Set URL and Location
$URL = "https://www.patreon.com/cw/TheRegulationPod"
$folderPath = '\\truenas3.kwyjibo.info\RegulationPodcast$'
$rejects = @()
$RejectString = $rejects -join '|'
Set-Location $folderPath
function Download-YtDlp {
param(
[string]$VideoUrl,
[string]$OutputPath,
[string]$RejectString,
[bool]$WithCookies
)
$ytDlpPath = "D:\yt-dlp\yt-dlp.exe"
$arguments = @(
$VideoUrl,
"--write-info-json",
"--write-thumbnail",
"--embed-metadata",
"--convert-thumbnails", "jpg",
"--no-progress",
"-t", "sleep",
"--rate-limit", "5M",
"-o", $OutputPath,
"-f", "bestvideo[ext=mp4]+bestaudio[ext=m4a]/best[ext=mp4]/best",
"-S", "vcodec:h264",
"--reject-title", $RejectString,
"--playlist-reverse",
"--download-archive", "ytdlp_archive.txt"
)
if ($WithCookies) {
$arguments += "--cookies-from-browser", "firefox"
}
$output = & $ytDlpPath @arguments 2>&1
$exitCode = $LASTEXITCODE
# Prefer actual error lines if present
$errorLines = $output | Where-Object { $_ -match '^\[error\]' }
$message = if ($exitCode -eq 0) {
$null
}
elseif ($errorLines) {
($errorLines -join "`n").Trim()
}
else {
# fallback if yt-dlp didn't tag it as [error]
($output -join "`n").Trim()
}
return [PSCustomObject]@{
ExitCode = $exitCode
Message = $message
}
}
function Parse-URLToIdentifier {
param(
[Parameter(Mandatory = $true)]
[string]$Url
)
$platform = ""
$name = ""
# 1. Determine the Platform
if ($Url -match "patreon\.com") {
$platform = "patreon"
# For Patreon, take the last segment of the URL path (e.g., TheRegulationPod)
$name = ($Url -split '/')[-1]
}
elseif ($Url -match "youtube.com") {
$platform = "youtube"
# For YouTube, extract everything after the "@" symbol
if ($Url -match '@(.*)') {
$name = $Matches[1]
}
else {
# Handle cases where @ might not be present (though unlikely for channel URLs)
$name = "UnknownChannel"
}
}
else {
Write-Error "Unsupported URL platform."
return $null
}
# Construct and return the final identifier
return "${platform}_${name}"
}
$archive_name = Parse-URLToIdentifier -Url $Url
# Download flat playlist
Write-Verbose -Message "Download flat playlist" -Verbose
$flat = D:\yt-dlp\yt-dlp.exe -j --flat-playlist $URL
$flat | Set-Content -Path "$archive_name.json"
# load json into memory, convert to psobject
$json = Get-Content -Path "$archive_name.json" | ConvertFrom-Json
# Load all .mp4 files into memory once at the start
Write-Verbose -Message "Gathering existing downloads" -Verbose
$mp4Files = Get-ChildItem -Path $folderPath -Recurse -Filter "*.mp4"
ForEach ($obj in $json) {
$TryWithCookies = $false
$videoUrl = $obj.url
$url_basename = $obj.webpage_url_basename
Write-Verbose -Message ("Start " + $url_basename) -Verbose
$outputPath = "Unsorted/Season %(upload_date>%y|Unknown)s/%(upload_date>%m|Unknown)s/%(release_date>%Y%m%d,upload_date>%Y%m%d)s - %(title).180B [%(extractor)s-%(id)s].%(ext)s"
$DownloadYTDLPSplat = @{
videoUrl = $videoUrl
outputPath = "Unsorted/Season %(upload_date>%y|Unknown)s/%(upload_date>%m|Unknown)s/%(release_date>%Y%m%d,upload_date>%Y%m%d)s - %(title).180B [%(extractor)s-%(id)s].%(ext)s"
}
$DownloadResult = Download-YtDlp @DownloadYTDLPSplat
switch ($DownloadResult.ExitCode) {
0 {
Write-Verbose -Message "Download successful"
}
{ $_ -ne 0 } {
If ($DownloadResult.ExitCode -ne 0) {
switch ($DownloadResult.Message) {
{ $_ -match 'No supported media found in this post' } {
Write-Verbose -Message 'No supported media found in this post' -Verbose
}
{ $_ -match 'do not have access' } {
Write-Verbose -Message 'do not have access' -Verbose
$TryWithCookies = $true
}
Default {
$errorLine = ($DownloadResult.Message -split "`n") | Where-Object { $_ -match '^ERROR:' } | Select-Object -First 1
Write-Verbose -Message $errorLine -Verbose
}
}
If ($TryWithCookies) {
$CookieResult = Download-YtDlp @DownloadYTDLPSplat
switch ($CookieResult.Message) {
{ $_ -match 'No supported media found in this post' } {
Write-Verbose -Message 'No supported media found in this post again.' -Verbose
}
{ $_ -match 'do not have access' } {
Write-Verbose -Message 'do not have access again' -Verbose
$TryWithCookies = $true
}
Default {
$errorLine = ($CookieResult.Message -split "`n") | Where-Object { $_ -match '^ERROR:' } | Select-Object -First 1
Write-Warning -Message "Still failed with cookies."
Write-Verbose -Message $errorLine -Verbose
}
}
}
}
}
}
Write-Verbose -Message ("End " + $url_basename) -Verbose
}