Skip to content

Commit cf190ca

Browse files
committed
feat: improve download path handling and error reporting in yt-download.ps1
You can now force a DownloadsPath in config.ps1
1 parent d73adc1 commit cf190ca

1 file changed

Lines changed: 50 additions & 22 deletions

File tree

powershell/yt-download.ps1

Lines changed: 50 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,34 @@ $ScriptVersion = "2.4.0"
2323
* ℹ                  DEFAULT VARIABLES
2424
===========================================================================#>
2525

26-
27-
2826
# Don’t edit this part unless you know what you do.
2927
# Get default downloads dir for each platform
30-
if ($PSVersionTable.Platform -eq "Win32NT") {
31-
New-Variable -Name UserShellFolders -Value "HKCU:\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders" -Option Constant
32-
New-Variable -Name downloadsKey -Value "{374DE290-123F-4565-9164-39C4925E467B}" -Option Constant
33-
New-Variable -Name downloadsPath -Value ((Get-ItemProperty -Path $UserShellFolders -Name $downloadsKey).$downloadsKey) -Option Constant
34-
}
35-
elseif ($PSVersionTable.Platform -eq "Unix") {
36-
New-Variable -Name downloadsPath -Value (Join-Path -Path $HOME -ChildPath "Downloads") -Option Constant
28+
if (-not $DownloadsPath) {
29+
if ($PSVersionTable.Platform -eq "Win32NT") {
30+
New-Variable -Name UserShellFolders -Value "HKCU:\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders" -Option Constant
31+
New-Variable -Name downloadsKey -Value "{374DE290-123F-4565-9164-39C4925E467B}" -Option Constant
32+
New-Variable -Name DownloadsPath -Value ((Get-ItemProperty -Path $UserShellFolders -Name $downloadsKey).$downloadsKey) -Option Constant
33+
}
34+
elseif ($PSVersionTable.Platform -eq "Unix") {
35+
New-Variable -Name DownloadsPath -Value (Join-Path -Path $HOME -ChildPath "Downloads") -Option Constant
36+
}
37+
else {
38+
Write-Host "ERROR`n" -ForegroundColor Red
39+
Write-Host "`tUnknown OS :`n`tCan not get the default Download directory." -ForegroundColor Red
40+
Write-Host "`n`tOpen an issue. You can try to set the DownloadsPath in your `"config.ps1`" file.`nEXIT" -ForegroundColor Red
41+
exit 1
42+
}
3743
}
3844
else {
39-
TerminateWithError -errorMessage "Unknown OS"
45+
# Check if the DownloadsPath set in config.ps1 exists
46+
if (-not (Test-Path -Path $DownloadsPath -PathType Container)) {
47+
Write-Host "ERROR`n" -ForegroundColor Red
48+
Write-Host "`t`"$DownloadsPath`" :`n`tdoesn’t exist." -ForegroundColor Red
49+
Write-Host "`n`tYou set a custom download path which is invalid in your `"config.ps1`" file.`nEXIT" -ForegroundColor Red
50+
Write-Host "`tProvide a valid path or" -ForegroundColor Red
51+
Write-Host "`ttry to comment the line to use default download dir for user.`nEXIT" -ForegroundColor Red
52+
exit 1
53+
}
4054
}
4155

4256
#===========================================================================
@@ -80,7 +94,7 @@ foreach ($VarName in $Defaults.Keys) {
8094
}
8195
#===========================================================================
8296

83-
New-Variable -Name directory -Value (Join-Path -Path "$downloadsPath" -ChildPath "$DownloadFolderName") -Option Constant
97+
New-Variable -Name FullDownloadDir -Value (Join-Path -Path "$DownloadsPath" -ChildPath "$DownloadFolderName") -Option Constant
8498

8599
# This is the command triggered by the protocol
86100
# It open the Windows Terminal with the profile 'PowerShell 7' and this script with the given url
@@ -119,7 +133,7 @@ function Show-Help {
119133
Write-Host "`tUse this to open wiki at `"$repo`"`n"
120134
Write-Host "-install" -ForegroundColor Magenta
121135
Write-Host "`tUse this to register the custom protocol `"$protocol`://`" in the registry that will run this script with the parameter -url when called`n"
122-
Write-Host "`tThe downloads directory will be `"$directory`" and must exist. Edit this script to customize.`n"
136+
Write-Host "`tThe downloads directory will be `"$FullDownloadDir`" and must exist. Edit this script to customize.`n"
123137
Write-Host "-uninstall" -ForegroundColor Magenta
124138
Write-Host "`tUse this to unregister the custom protocol `"$protocol`://`" from the registry`n"
125139
Write-Host "-url" -ForegroundColor Magenta
@@ -147,7 +161,7 @@ function Show-Help {
147161
Write-Host "Edit this script to customize those paths."
148162
Write-Host "`"/`" as separator works also in Windows.`n"
149163
Write-Host "directory" -ForegroundColor Magenta
150-
Write-Host "`t$directory`n"
164+
Write-Host "`t$FullDownloadDir`n"
151165
Write-Host "UI_Path" -ForegroundColor Magenta
152166
Write-Host "`t$UI_Path`n"
153167
Write-Host "UseBrowserCookies (ignore \"myCookies\" if true)" -ForegroundColor Magenta
@@ -425,17 +439,31 @@ if ($url -and -not $install -and -not $uninstall) {
425439
===========================================================================#>
426440
if ($parameters.ContainsKey('dldir')) {
427441
$global:DL_DIR = $($parameters['dldir'])
428-
}
429-
else {
430-
$global:DL_DIR = $directory
431-
}
432442

433-
# Test if download dir exists
434-
if (Test-Path -Path $DL_DIR -PathType Container) {
435-
Write-Host "- Download file in (unless if handled by YDL-UI.exe): $DL_DIR" -ForegroundColor Green
443+
if (Test-Path -Path $DL_DIR -PathType Container) {
444+
Write-Host "- Download file in (unless if handled by YDL-UI.exe): $DL_DIR" -ForegroundColor Green
445+
}
446+
else {
447+
TerminateWithError -errorMessage "The DOWNLOAD_DIR set in yt-dlp userscript : `"$DL_DIR`" doesn’t exist."
448+
}
436449
}
437450
else {
438-
TerminateWithError -errorMessage "The $DL_DIR doesn’t exist."
451+
$global:DL_DIR = $FullDownloadDir
452+
if (Test-Path -Path $DL_DIR -PathType Container) {
453+
Write-Host "- Download file in (unless if handled by YDL-UI.exe): $DL_DIR" -ForegroundColor Green
454+
}
455+
else {
456+
Write-Host "`"$DL_DIR`" doesn’t exist. Let’s try to create it." -ForegroundColor Yellow
457+
458+
try {
459+
# We use -Force to avoid errors if the folder already exists (optional if necessary)
460+
$NewFolder = New-Item -Path $FullDownloadDir -ItemType Directory -ErrorAction Stop
461+
Write-Host "Success: Folder created at `"$($NewFolder.FullName)`"" -ForegroundColor Green
462+
}
463+
catch {
464+
TerminateWithError -errorMessage "Failed to create the folder `"$DownloadFolderName`" in `"$DownloadsPath`"." -exception $_.Exception
465+
}
466+
}
439467
}
440468

441469
$output = ""
@@ -618,7 +646,7 @@ if ($url -and -not $install -and -not $uninstall) {
618646
$optionsString = $optionsString -replace $pattern, $substitution
619647
}
620648

621-
if ($myCookies) {
649+
if (($myCookies) -and (-not $UseBrowserCookies)) {
622650
$pattern = '--cookies\s+(.+?\.txt)'
623651
$substitution = '--cookies "$1"'
624652
$optionsString = $optionsString -replace $pattern, $substitution

0 commit comments

Comments
 (0)