-
Notifications
You must be signed in to change notification settings - Fork 0
/
yt-dlp_functions.ps1
172 lines (143 loc) · 5.7 KB
/
yt-dlp_functions.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
function Show-DownloadInfo {
$borderColor = 'DarkCyan'
$headerColor = 'Cyan'
$textColor = 'Yellow'
# Draw top border
Write-Host ("─" * ($Host.UI.RawUI.WindowSize.Width)) -ForegroundColor $borderColor
# Show ASCII art with padding
Write-Ascii $Extractor
Write-Host
# Create info block with consistent formatting
$infoBlock = @(
@{ Label = "MODE"; Value = $mode }
@{ Label = "URL"; Value = $url }
@{ Label = "OUTPUT DIR"; Value = $destination }
)
# Display info block with proper alignment
$labelWidth = ($infoBlock | ForEach-Object { $_.Label.Length } | Measure-Object -Maximum).Maximum + 2
foreach ($item in $infoBlock) {
$label = $item.Label.PadRight($labelWidth)
Write-Host " ┃ " -ForegroundColor $borderColor -NoNewline
Write-Host $label -ForegroundColor $headerColor -NoNewline
Write-Host " │ " -ForegroundColor $borderColor -NoNewline
Write-Host $item.Value -ForegroundColor $textColor
}
# Display output files
Write-Host " ┃ " -ForegroundColor $borderColor -NoNewline
Write-Host "FILES".PadRight($labelWidth) -ForegroundColor $headerColor -NoNewline
Write-Host " │ " -ForegroundColor $borderColor
foreach ($fileName in $OutputFiles) {
Write-Host " ┃ " -ForegroundColor $borderColor -NoNewline
Write-Host "".PadRight($labelWidth) -NoNewline
Write-Host " │ " -ForegroundColor $borderColor -NoNewline
Write-Host $fileName -ForegroundColor $textColor
}
# Draw separator
Write-Host ("─" * ($Host.UI.RawUI.WindowSize.Width)) -ForegroundColor $borderColor
# Display command parameters with alternating colors
Write-Host " COMMAND " -BackgroundColor DarkGreen -ForegroundColor White -NoNewline
Write-Host " " -NoNewline
for ($i = 0; $i -lt $DownloadParameters.Count; $i++) {
$bgColor = if ($i % 2 -eq 0) { 'DarkBlue' } else { 'DarkMagenta' }
Write-Host " $($DownloadParameters[$i]) " -ForegroundColor White -BackgroundColor $bgColor -NoNewline
Write-Host " " -NoNewline
}
Write-Host
# Draw bottom border
Write-Host ("─" * ($Host.UI.RawUI.WindowSize.Width)) -ForegroundColor $borderColor
}
function Get-OutputFileNames {
param( $OptionsObj, $InfoJSON, $OutTemplate )
$OutputFiles = @()
$outFileName = $InfoJSON | & $ytdlPath '--load-info-json' - -O $OutTemplate
if ($OptionsObj.CustomRange) {
foreach ($Item in $Options.Items) {
$Timestamp = ConvertTo-Seconds $Item
# Apply the replacements to the filename and add to the output list
$modifiedFileName = $outFileName -replace ':', ':' -replace '_NA', "_$Timestamp.0"
$OutputFiles += $modifiedFileName
}
}
else {
$OutputFiles += $outFileName
}
return $OutputFiles
}
function Get-OutputTemplate {
param( $InfoJson )
$ExtractorHashTable = @{
facebook = "%(uploader_id)s";
hotstar = "%(series)s- S%(season_number)sE%(episode_number)s- %(upload_date)s";
zee5 = "%(series)s- S%(season_number)sE%(episode_number)s- %(upload_date)s";
instagram = "%(channel)s- %(uploader)s - %(uploader_id)s";
tiktok = "%(uploader)s";
twitter = "%(uploader_id)s- %(uploader)s";
vimeo = "%(uploader_id)s- %(uploader)s";
youtube = "%(uploader_id)s- %(uploader)s";
XHamster = "%(uploader_id)s";
pornhub = "%(uploader_id)s- %(cast)s";
}
$InfoJSONFormatted = $InfoJSON | ConvertFrom-Json
$Extractor = $InfoJSONFormatted.extractor
$OutTemplate = "$($ExtractorHashTable[$extractor]) - (%(extractor)s)%(id)s - %(title)s$( $Options.customRange ? ' _%(section_start)s' : '' ) @[o][bQ].%(ext)s"
Return $OutTemplate
}
Function Get-Proxy() {
Return ( Get-ItemProperty -Path "Registry::HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings" )
}
function Get-InfoJson {
[CmdletBinding()]
param(
[Parameter(Mandatory)]
[string]$YtdlPath,
[Parameter(Mandatory)]
[string[]]$InfoJsonParameters
)
Write-Host "Getting .info.json ..."
$stopWatch = [System.Diagnostics.Stopwatch]::StartNew()
try {
$result = & $YtdlPath $InfoJsonParameters 2>&1
$stdout = $result | Where-Object { $_ -is [string] }
$stderr = $result | Where-Object { $_ -is [System.Management.Automation.ErrorRecord] }
$stopWatch.Stop()
Write-Host "Elapsed: $($stopWatch.Elapsed)" -ForegroundColor Cyan
if ($stdout) {
try {
$null = $stdout | ConvertFrom-Json
# Write-Host $stdout
return $stdout
}
catch {
Write-Warning "Failed to parse JSON: $_"
Write-Host $stdout
return $null
}
}
if ($stderr) {
Write-Warning "Error occurred: $stderr"
$MsgboxResult = [System.Windows.MessageBox]::Show("$stderr`n`nRetry?", 'Yt-ldp Error', 4, 32)
If ( $MsgboxResult -eq 'Yes') { Return Get-InfoJson $YtdlPath $InfoJsonParameters }
Else { Return $null }
}
if (-not $stdout -and -not $stderr) {
Write-Warning "No output or error received."
}
}
catch {
Write-Error "An unexpected error occurred: $_"
}
return $null
}
function exitTerminal {
param ( $pathToJson, $finalFilePath )
$response = infoJsonOperations $pathToJson $finalFilePath
if ( $response -eq 'Cancel' ) { exit }
}
function exitAndCloseTerminal {
if ( $Debug ) {
exit
}
else {
[Environment]::Exit(0)
}
}