Skip to content

Commit 0a143d3

Browse files
committed
feat(ps1): use external config.ps1
1 parent 9ceca70 commit 0a143d3

1 file changed

Lines changed: 45 additions & 26 deletions

File tree

powershell/yt-download.ps1

Lines changed: 45 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@ $ErrorActionPreference = 'Stop'
1919
* ℹ                  DEFAULT VARIABLES
2020
===========================================================================#>
2121

22-
# This folder name must exist as a child in the user downloads directory defined by the OS
23-
New-Variable -Name DownloadFolderName -Value "yt-dlp" -Option Constant
22+
2423

2524
# Don’t edit this part unless you know what you do.
2625
# Get default downloads dir for each platform
@@ -37,27 +36,47 @@ else {
3736
}
3837

3938
#===========================================================================
40-
# you can edit those values
41-
New-Variable -Name UI_Path -Value "D:/Programmes/Internet/youtube-dl/YDL-UI_Portable/YDL-UI.exe" -Option Constant
42-
New-Variable -Name UseBrowser -Value $true -Option Constant # true is recommended
43-
# Must be the name of a yt-dlp supported browser and optionnaly the name of the profile directory containing the cookies you need
44-
New-Variable -Name browserCookies -Value "firefox:oo49eawy.Youtube" -Option Constant
45-
New-Variable -Name myCookies -Value "D:/pathTo/cookies.txt" # it is recommended to not use such a file for security reason.
39+
# import user config
40+
try {
41+
$ConfigPath = Join-Path $PSScriptRoot "config.ps1"
42+
if (Test-Path $ConfigPath) {
43+
. $ConfigPath
44+
}
45+
else {
46+
TerminateWithError -errorMessage "`"config.ps1`" is missing."
47+
}
48+
}
49+
catch {
50+
TerminateWithError -errorMessage "`"config.ps1`" is missing or can not be loaded."
51+
}
4652

47-
New-Variable -Name directory -Value (Join-Path -Path "$downloadsPath" -ChildPath "$DownloadFolderName") -Option Constant
48-
New-Variable -Name templateNameChannel -Value "%(uploader|)s%(uploader& - )s%(title).70s.%(ext)s" -Option Constant
49-
New-Variable -Name templateNameTitle -Value "%(title).70s.%(ext)s" -Option Constant
50-
# if useTitle is true then use $templateNameTitle else $templateNameChannel
51-
New-Variable -Name useTitle -Value $true -Option Constant
52-
# defaut quality for video (make it compatible to upload on 𝕏)
53-
New-Variable -Name videoQuality -Value "bestvideo*[vcodec^=avc1]+bestaudio[acodec^=mp4a]/bestvideo*+bestaudio/best"
54-
# Videos will use this container
55-
New-Variable -Name videoContainer -Value "mp4" -Option Constant
56-
# set URLs for which the script will detect as audio
57-
New-Variable -Name autoAudio -Value @("https://music.youtube.com/watch?v=") -Option Constant
58-
# set default js runtime required in the new yt-dlp versions. Just comment if using Deno.
59-
New-Variable -Name jsRuntime -Value "bun" -Option Constant
53+
$Defaults = @{
54+
DownloadFolderName = "yt-dlp"
55+
UI_Path = ""
56+
UseBrowserCookies = $true
57+
browserCookies = "firefox"
58+
myCookies = ""
59+
templateNameChannel = "%(uploader)s - %(title)s.%(ext)s"
60+
templateNameTitle = "%(title)s.%(ext)s"
61+
useTitle = $true
62+
videoQuality = "best"
63+
videoContainer = "mp4"
64+
autoAudio = @("https://music.youtube.com/watch?v=")
65+
jsRuntime = "bun"
66+
}
67+
68+
foreach ($VarName in $Defaults.Keys) {
69+
if (-not (Get-Variable -Name $VarName -ErrorAction SilentlyContinue)) {
70+
# Required variable is missing in config.ps1, let’s create it here with the default value
71+
New-Variable -Name $VarName -Value $Defaults[$VarName] -Option Constant -Confirm:$false
72+
Write-Host
73+
Write-Warning "Variable '$VarName' is missing in your `"config.ps1`". We will use the fallback value: `"$($Defaults[$VarName])`""
74+
Write-Host
75+
}
76+
}
77+
#===========================================================================
6078

79+
New-Variable -Name directory -Value (Join-Path -Path "$downloadsPath" -ChildPath "$DownloadFolderName") -Option Constant
6180

6281
# This is the command triggered by the protocol
6382
# It open the Windows Terminal with the profile 'PowerShell 7' and this script with the given url
@@ -115,8 +134,8 @@ function Show-Help {
115134
Write-Host "`t$directory`n"
116135
Write-Host "UI_Path" -ForegroundColor Magenta
117136
Write-Host "`t$UI_Path`n"
118-
Write-Host "UseBrowser (ignore \"myCookies\" if true)" -ForegroundColor Magenta
119-
Write-Host "`t$UseBrowser`n"
137+
Write-Host "UseBrowserCookies (ignore \"myCookies\" if true)" -ForegroundColor Magenta
138+
Write-Host "`t$UseBrowserCookies`n"
120139
Write-Host "browserCookies" -ForegroundColor Magenta
121140
Write-Host "`t$browserCookies`n"
122141
Write-Host "myCookies" -ForegroundColor Magenta
@@ -200,7 +219,7 @@ function Test-FFmpegInstallation {
200219
<#*==========================================================================
201220
* ℹ           GET PARAMETERS
202221
===========================================================================#>
203-
Clear-Host
222+
# Clear-Host
204223

205224
<#*==========================================================================
206225
* ℹ HANDLE HELP PARAMETER
@@ -451,7 +470,7 @@ if ($url -and -not $install -and -not $uninstall) {
451470
<#*==========================================================================
452471
* ℹ TEST COOKIES
453472
===========================================================================#>
454-
if (-not $UseBrowser) {
473+
if (-not $UseBrowserCookies) {
455474
# Test cookie path
456475
if (Test-Path -Path $myCookies) {
457476
Write-Host "`- Use user cookies from file" -ForegroundColor Green
@@ -473,7 +492,7 @@ if ($url -and -not $install -and -not $uninstall) {
473492
$pattern = '-o\s+(.+?)\s+http'
474493
$substitution = '-o "$1" http'
475494

476-
if ($UseBrowser) {
495+
if ($UseBrowserCookies) {
477496
$options += @("--cookies-from-browser", "$browserCookies")
478497
}
479498
elseif ($myCookies) {

0 commit comments

Comments
 (0)