Skip to content

Commit 1f0ba56

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

1 file changed

Lines changed: 75 additions & 26 deletions

File tree

powershell/yt-download.ps1

Lines changed: 75 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,16 @@ param(
33
[switch]$uninstall,
44
[switch]$help,
55
[switch]$man,
6+
[switch]$version,
67
[string]$url,
78
[switch]$debug
89
)
910

1011
# Stop the script if an error occurs.
1112
$ErrorActionPreference = 'Stop'
13+
14+
$ScriptVersion = "2.3.0"
15+
1216
<#*==========================================================================
1317
* ℹ PARAMETERS
1418
@@ -19,8 +23,7 @@ $ErrorActionPreference = 'Stop'
1923
* ℹ                  DEFAULT VARIABLES
2024
===========================================================================#>
2125

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
26+
2427

2528
# Don’t edit this part unless you know what you do.
2629
# Get default downloads dir for each platform
@@ -37,27 +40,47 @@ else {
3740
}
3841

3942
#===========================================================================
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.
43+
# import user config
44+
try {
45+
$ConfigPath = Join-Path $PSScriptRoot "config.ps1"
46+
if (Test-Path $ConfigPath) {
47+
. $ConfigPath
48+
}
49+
else {
50+
TerminateWithError -errorMessage "`"config.ps1`" is missing."
51+
}
52+
}
53+
catch {
54+
TerminateWithError -errorMessage "`"config.ps1`" is missing or can not be loaded."
55+
}
4656

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
57+
$Defaults = @{
58+
DownloadFolderName = "yt-dlp"
59+
UI_Path = ""
60+
UseBrowserCookies = $true
61+
browserCookies = "firefox"
62+
myCookies = ""
63+
templateNameChannel = "%(uploader)s - %(title)s.%(ext)s"
64+
templateNameTitle = "%(title)s.%(ext)s"
65+
useTitle = $true
66+
videoQuality = "best"
67+
videoContainer = "mp4"
68+
autoAudio = @("https://music.youtube.com/watch?v=")
69+
jsRuntime = "bun"
70+
}
6071

72+
foreach ($VarName in $Defaults.Keys) {
73+
if (-not (Get-Variable -Name $VarName -ErrorAction SilentlyContinue)) {
74+
# Required variable is missing in config.ps1, let’s create it here with the default value
75+
New-Variable -Name $VarName -Value $Defaults[$VarName] -Option Constant -Confirm:$false
76+
Write-Host
77+
Write-Warning "Variable '$VarName' is missing in your `"config.ps1`". We will use the fallback value: `"$($Defaults[$VarName])`""
78+
Write-Host
79+
}
80+
}
81+
#===========================================================================
82+
83+
New-Variable -Name directory -Value (Join-Path -Path "$downloadsPath" -ChildPath "$DownloadFolderName") -Option Constant
6184

6285
# This is the command triggered by the protocol
6386
# It open the Windows Terminal with the profile 'PowerShell 7' and this script with the given url
@@ -74,8 +97,19 @@ New-Variable -Name repo -Value "https://github.com/Fred-Vatin/run-yt-dlp-from-br
7497
<#*==========================================================================
7598
* ℹ                  FUNCTIONS
7699
===========================================================================#>
100+
function Show-Version {
101+
Write-Host ""
102+
Write-Host "yt-download" -ForegroundColor Yellow
103+
Write-Host "Version : " -NoNewline
104+
Write-Host "$ScriptVersion" -ForegroundColor Green
105+
Write-Host ""
106+
}
107+
108+
77109
# Display help message with specified formatting
78110
function Show-Help {
111+
Show-Version
112+
Write-Host ""
79113
Write-Host "`tPARAMETERS" -ForegroundColor Magenta
80114
Write-Host "========================`n" -ForegroundColor Magenta
81115
Write-Host "-help" -ForegroundColor Magenta
@@ -115,8 +149,8 @@ function Show-Help {
115149
Write-Host "`t$directory`n"
116150
Write-Host "UI_Path" -ForegroundColor Magenta
117151
Write-Host "`t$UI_Path`n"
118-
Write-Host "UseBrowser (ignore \"myCookies\" if true)" -ForegroundColor Magenta
119-
Write-Host "`t$UseBrowser`n"
152+
Write-Host "UseBrowserCookies (ignore \"myCookies\" if true)" -ForegroundColor Magenta
153+
Write-Host "`t$UseBrowserCookies`n"
120154
Write-Host "browserCookies" -ForegroundColor Magenta
121155
Write-Host "`t$browserCookies`n"
122156
Write-Host "myCookies" -ForegroundColor Magenta
@@ -200,7 +234,16 @@ function Test-FFmpegInstallation {
200234
<#*==========================================================================
201235
* ℹ           GET PARAMETERS
202236
===========================================================================#>
203-
Clear-Host
237+
if ($version) {
238+
Show-Version
239+
Write-Host "Source : " -NoNewline
240+
Write-Host "https://github.com/Fred-Vatin/run-yt-dlp-from-browser" -ForegroundColor Cyan
241+
Write-Host ""
242+
Write-Host "Run with the " -NoNewline
243+
Write-Host "-help " -ForegroundColor Magenta -NoNewline
244+
Write-Host "parameter for more info"
245+
exit
246+
}
204247

205248
<#*==========================================================================
206249
* ℹ HANDLE HELP PARAMETER
@@ -223,6 +266,8 @@ if ($man) {
223266
===========================================================================#>
224267
# Handle -url parameter if no other parameters are specified
225268
if ($url -and -not $install -and -not $uninstall) {
269+
Show-Version
270+
226271
Write-Host "`nScript called with argument " -NoNewline
227272
Write-Host "-url" -ForegroundColor Magenta
228273
Write-Host "`t$url`n"
@@ -451,7 +496,7 @@ if ($url -and -not $install -and -not $uninstall) {
451496
<#*==========================================================================
452497
* ℹ TEST COOKIES
453498
===========================================================================#>
454-
if (-not $UseBrowser) {
499+
if (-not $UseBrowserCookies) {
455500
# Test cookie path
456501
if (Test-Path -Path $myCookies) {
457502
Write-Host "`- Use user cookies from file" -ForegroundColor Green
@@ -473,7 +518,7 @@ if ($url -and -not $install -and -not $uninstall) {
473518
$pattern = '-o\s+(.+?)\s+http'
474519
$substitution = '-o "$1" http'
475520

476-
if ($UseBrowser) {
521+
if ($UseBrowserCookies) {
477522
$options += @("--cookies-from-browser", "$browserCookies")
478523
}
479524
elseif ($myCookies) {
@@ -542,6 +587,8 @@ if (-not (Test-AdminPrivileges)) {
542587
$ytdlKey = "Registry::HKEY_CLASSES_ROOT\$protocol"
543588

544589
if ($uninstall) {
590+
Show-Version
591+
545592
WriteTitle "UNINSTALL"
546593
Write-Host "Uninstalling ytdl protocol handler..."
547594

@@ -566,6 +613,8 @@ if ($uninstall) {
566613
===========================================================================#>
567614

568615
if ($install) {
616+
Show-Version
617+
569618
WriteTitle "INSTALL"
570619
Write-Host "Installing ytdl protocol handler...`n"
571620

0 commit comments

Comments
 (0)