-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStagearr.ps1
More file actions
521 lines (434 loc) · 18.5 KB
/
Stagearr.ps1
File metadata and controls
521 lines (434 loc) · 18.5 KB
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
#Requires -Version 5.1
<#
.SYNOPSIS
Stagearr vNext - qBittorrent Post-Processing Automation
.DESCRIPTION
Handles post-processing of completed torrent downloads:
- Unrar archives to staging
- MP4→MKV remux
- Subtitle extraction/stripping from MKV
- OpenSubtitles download
- Subtitle cleanup via SubtitleEdit
- Import to Radarr/Sonarr/Medusa
- Email notifications
Designed for qBittorrent's "Run external program on torrent completion" hook.
Uses a file-backed queue for reliability across reboots.
.PARAMETER DownloadPath
Path to the downloaded torrent (file or folder).
.PARAMETER DownloadLabel
Torrent label/category (e.g., TV, Movie).
Empty becomes "NoLabel". "NoProcess" skips processing entirely.
.PARAMETER TorrentHash
Torrent hash (used for Radarr/Sonarr import matching).
.PARAMETER NoCleanup
Skip staging folder cleanup after processing.
.PARAMETER NoMail
Skip email notification for this job.
.PARAMETER Force
Force re-run of a job even if it already exists (completed or failed).
.PARAMETER Wait
Wait for the lock if another instance is currently processing.
Without this flag, the worker exits immediately if the lock is held
(the job is still queued and will be picked up by the active worker).
Useful for manual runs when you want to see console output.
.PARAMETER Status
Show queue status and exit.
.PARAMETER SyncConfig
Compare config.toml against config-sample.toml and offer to
apply missing or remove deprecated settings.
.PARAMETER Setup
Run interactive setup wizard to create or edit config.toml.
.PARAMETER Update
Check for updates and apply if available. Auto mode applies
immediately; notify/off mode prompts for confirmation.
.PARAMETER ConfigPath
Path to config.toml (default: same directory as script).
.EXAMPLE
# qBittorrent completion hook:
Stagearr.ps1 -DownloadPath "%F" -DownloadLabel "%L" -TorrentHash "%I"
.EXAMPLE
# Manual processing:
Stagearr.ps1 -DownloadPath "C:\Downloads\Movie.2024" -DownloadLabel "Movie"
.EXAMPLE
# Check queue status:
Stagearr.ps1 -Status
.EXAMPLE
# Check for missing/extra settings:
Stagearr.ps1 -SyncConfig
.EXAMPLE
# Interactive setup wizard:
Stagearr.ps1 -Setup
.EXAMPLE
# Check for updates:
Stagearr.ps1 -Update
.EXAMPLE
# Interactively re-run a recent job:
Stagearr.ps1 -Rerun
.EXAMPLE
# Show last 20 jobs for re-run:
Stagearr.ps1 -Rerun -RerunLimit 20
.EXAMPLE
# Re-run a previously completed/failed job:
Stagearr.ps1 -DownloadPath "C:\Downloads\Movie.2024" -DownloadLabel "Movie" -Force
.NOTES
Version: 2.0.3
Requires: PowerShell 5.1 or 7.x
External Tools: WinRAR, MKVToolNix, SubtitleEdit
#>
[CmdletBinding(DefaultParameterSetName = 'Enqueue')]
param(
[Parameter(ParameterSetName = 'Enqueue', Position = 0)]
[string]$DownloadPath,
[Parameter(ParameterSetName = 'Enqueue')]
[string]$DownloadLabel = '',
[Parameter(ParameterSetName = 'Enqueue')]
[string]$TorrentHash = '',
[Parameter(ParameterSetName = 'Enqueue')]
[switch]$NoCleanup,
[Parameter(ParameterSetName = 'Enqueue')]
[Alias('NoMail')]
[switch]$SkipEmail,
[Parameter(ParameterSetName = 'Enqueue')]
[switch]$Force,
[Parameter(ParameterSetName = 'Enqueue')]
[switch]$Wait,
[Parameter(ParameterSetName = 'Status')]
[switch]$Status,
[Parameter(ParameterSetName = 'SyncConfig')]
[switch]$SyncConfig,
[Parameter(ParameterSetName = 'Setup')]
[switch]$Setup,
[Parameter(ParameterSetName = 'Update')]
[switch]$Update,
[Parameter(ParameterSetName = 'Rerun')]
[switch]$Rerun,
[Parameter(ParameterSetName = 'Rerun', Position = 0)]
[int]$RerunLimit = 10,
[Parameter()]
[string]$ConfigPath
)
# Determine script root and module path
$ScriptRoot = $PSScriptRoot
if ([string]::IsNullOrWhiteSpace($ScriptRoot)) {
$ScriptRoot = Split-Path -Parent $MyInvocation.MyCommand.Path
}
# Import the core module
$ModulePath = Join-Path -Path $ScriptRoot -ChildPath 'Modules\Stagearr.Core\Stagearr.Core.psd1'
if (-not (Test-Path -LiteralPath $ModulePath)) {
Write-Host "ERROR: Module not found at: $ModulePath" -ForegroundColor Red
exit 1
}
try {
# Suppress module loading verbose messages (redirect verbose stream to null)
$null = Import-Module $ModulePath -Force -DisableNameChecking -ErrorAction Stop -Verbose:$false 4>&1
} catch {
Write-Host "ERROR: Failed to import module: $_" -ForegroundColor Red
exit 1
}
$StagearrVersion = (Get-Module -Name 'Stagearr.Core').Version.ToString()
# Determine config path
if ([string]::IsNullOrWhiteSpace($ConfigPath)) {
$ConfigPath = Join-Path -Path $ScriptRoot -ChildPath 'config.toml'
}
# Handle SyncConfig mode BEFORE loading config (config may be invalid)
if ($PSCmdlet.ParameterSetName -eq 'SyncConfig') {
# Initialize console renderer with defaults (no config loaded yet)
Initialize-SAConsoleRenderer -UseColors $true
# Display banner
Write-SABanner -Title "Stagearr" -Version $StagearrVersion
Write-SAPhaseHeader -Title "Configuration Sync"
# Check if config.toml exists
if (-not (Test-Path -LiteralPath $ConfigPath)) {
Write-SAOutcome -Level Error -Label "Config" -Text "Not found: $ConfigPath"
Write-SAProgress -Label "Hint" -Text "Run: .\Stagearr.ps1 -Setup"
exit 1
}
$samplePath = Join-Path $ScriptRoot 'config-sample.toml'
if (-not (Test-Path -LiteralPath $samplePath)) {
Write-SAOutcome -Level Error -Label "Sample" -Text "Not found: $samplePath"
exit 1
}
Invoke-SAConfigSync -ConfigPath $ConfigPath -SamplePath $samplePath
exit 0
}
# Handle Setup mode BEFORE loading config
if ($PSCmdlet.ParameterSetName -eq 'Setup') {
Initialize-SAConsoleRenderer -UseColors $true
Write-SABanner -Title "Stagearr" -Version $StagearrVersion
Write-SAPhaseHeader -Title "Setup Wizard"
$samplePath = Join-Path $ScriptRoot 'config-sample.toml'
if (-not (Test-Path -LiteralPath $samplePath)) {
Write-SAOutcome -Level Error -Label "Sample" -Text "Not found: $samplePath"
exit 1
}
Invoke-SASetup -ConfigPath $ConfigPath -SamplePath $samplePath
exit 0
}
# Load configuration (for all other modes)
if (-not (Test-Path -LiteralPath $ConfigPath)) {
Write-Host "ERROR: Configuration file not found: $ConfigPath" -ForegroundColor Red
Write-Host "Run '.\Stagearr.ps1 -Setup' to create one, or copy config-sample.toml to config.toml" -ForegroundColor Yellow
exit 1
}
try {
$Config = Read-SAConfig -Path $ConfigPath
} catch {
Write-Host "ERROR: Failed to load configuration: $_" -ForegroundColor Red
exit 1
}
# Store script root in config for reference
$Config['_scriptRoot'] = $ScriptRoot
$Config['_configPath'] = $ConfigPath
# Check for updates (respects interval and mode config) — skip if -Update handles it
if ($PSCmdlet.ParameterSetName -ne 'Update') {
Invoke-SAUpdateCheck -Config $Config -LocalVersion $StagearrVersion -ScriptRoot $ScriptRoot
}
# Initialize console renderer
Initialize-SAConsoleRenderer -UseColors $Config.logging.consoleColors
# Display banner
Write-SABanner -Title "Stagearr" -Version $StagearrVersion
# Handle different modes
switch ($PSCmdlet.ParameterSetName) {
'Update' {
Write-SAPhaseHeader -Title "Update Check"
Invoke-SAInteractiveUpdate -Config $Config -LocalVersion $StagearrVersion -ScriptRoot $ScriptRoot
exit 0
}
'Status' {
# Show queue status
Write-SAPhaseHeader -Title "Queue Status"
# Use @() to ensure arrays even with single item
$pending = @(Get-SAJobs -QueueRoot $Config.paths.queueRoot -State 'pending')
$running = @(Get-SAJobs -QueueRoot $Config.paths.queueRoot -State 'running')
$completed = @(Get-SAJobs -QueueRoot $Config.paths.queueRoot -State 'completed' -Limit 10)
$failed = @(Get-SAJobs -QueueRoot $Config.paths.queueRoot -State 'failed' -Limit 10)
Write-SAKeyValue -Key "Pending" -Value $pending.Count
Write-SAKeyValue -Key "Running" -Value $running.Count
Write-SAKeyValue -Key "Completed" -Value "$($completed.Count) (last 10)"
Write-SAKeyValue -Key "Failed" -Value "$($failed.Count) (last 10)"
# Show lock status (compact)
$lockHeld = Test-SAGlobalLock -QueueRoot $Config.paths.queueRoot
if ($lockHeld) {
$lockInfo = Get-SAGlobalLockInfo -QueueRoot $Config.paths.queueRoot
if ($lockInfo) {
$lockSince = $lockInfo.startedAt.ToString('HH:mm:ss')
Write-SAKeyValue -Key "Lock" -Value "Held (PID $($lockInfo.pid), since $lockSince)"
} else {
Write-SAKeyValue -Key "Lock" -Value "Held"
}
} else {
Write-SAKeyValue -Key "Lock" -Value "Available"
}
# Show running jobs with details
if ($running.Count -gt 0) {
Write-SAPhaseHeader -Title "Running"
foreach ($job in $running) {
$name = Split-Path -Path $job.input.downloadPath -Leaf
Write-SAKeyValue -Key "Title" -Value $name
Write-SAKeyValue -Key "Label" -Value $job.input.downloadLabel
# Calculate elapsed time
$elapsedTimestamp = if ($job.startedAt) { $job.startedAt } else { $job.updatedAt }
if ($elapsedTimestamp) {
try {
$startTime = [datetime]::Parse(
$elapsedTimestamp,
[System.Globalization.CultureInfo]::InvariantCulture,
[System.Globalization.DateTimeStyles]::RoundtripKind
)
$elapsed = (Get-Date) - $startTime.ToLocalTime()
if ($elapsed.TotalHours -ge 1) {
$elapsedStr = '{0}h {1}m' -f [int]$elapsed.TotalHours, $elapsed.Minutes
} elseif ($elapsed.TotalMinutes -ge 1) {
$elapsedStr = '{0}m {1}s' -f [int]$elapsed.TotalMinutes, $elapsed.Seconds
} else {
$elapsedStr = '{0}s' -f [int]$elapsed.TotalSeconds
}
Write-SAKeyValue -Key "Elapsed" -Value $elapsedStr
} catch {
# Skip elapsed if timestamp can't be parsed
}
}
# Show progress if available
if ($job.progress) {
if ($job.progress.phase) {
Write-SAKeyValue -Key "Phase" -Value $job.progress.phase
}
if ($job.progress.activity) {
Write-SAKeyValue -Key "Activity" -Value $job.progress.activity
}
}
}
}
# Show pending jobs
if ($pending.Count -gt 0) {
Write-SAPhaseHeader -Title "Pending"
foreach ($job in $pending) {
$name = Split-Path -Path $job.input.downloadPath -Leaf
Write-SAKeyValue -Key $job.id.Substring(0, 8) -Value "$($job.input.downloadLabel): $name"
}
}
# Show completed jobs with result, duration, and time
if ($completed.Count -gt 0) {
Write-SAPhaseHeader -Title "Completed (Recent)"
foreach ($job in $completed) {
$name = Split-Path -Path $job.input.downloadPath -Leaf
if ($name.Length -gt 60) { $name = $name.Substring(0, 57) + '...' }
$label = $job.input.downloadLabel
# Get duration and completion time from result
$duration = ''
$finishTime = ''
if ($job.result) {
if ($job.result.duration) {
$duration = $job.result.duration
}
if ($job.result.completedAt) {
try {
$completedDt = [datetime]::Parse(
$job.result.completedAt,
[System.Globalization.CultureInfo]::InvariantCulture,
[System.Globalization.DateTimeStyles]::RoundtripKind
)
$finishTime = $completedDt.ToLocalTime().ToString('HH:mm')
} catch {
$finishTime = ''
}
}
}
# Fall back to updatedAt if no completedAt
if ([string]::IsNullOrWhiteSpace($finishTime) -and $job.updatedAt) {
try {
$updatedDt = [datetime]::Parse(
$job.updatedAt,
[System.Globalization.CultureInfo]::InvariantCulture,
[System.Globalization.DateTimeStyles]::RoundtripKind
)
$finishTime = $updatedDt.ToLocalTime().ToString('HH:mm')
} catch { }
}
$parts = @($label)
if ($duration) { $parts += $duration }
if ($finishTime) { $parts += $finishTime }
$detail = $parts -join ' '
Write-SAOutcome -Level Success -Text "$name $detail"
}
}
# Show failed jobs
if ($failed.Count -gt 0) {
Write-SAPhaseHeader -Title "Failed (Recent)"
foreach ($job in $failed) {
$name = Split-Path -Path $job.input.downloadPath -Leaf
if ($name.Length -gt 60) { $name = $name.Substring(0, 57) + '...' }
$label = $job.input.downloadLabel
$finishTime = ''
if ($job.result -and $job.result.completedAt) {
try {
$completedDt = [datetime]::Parse(
$job.result.completedAt,
[System.Globalization.CultureInfo]::InvariantCulture,
[System.Globalization.DateTimeStyles]::RoundtripKind
)
$finishTime = $completedDt.ToLocalTime().ToString('HH:mm')
} catch { }
}
if ([string]::IsNullOrWhiteSpace($finishTime) -and $job.updatedAt) {
try {
$updatedDt = [datetime]::Parse(
$job.updatedAt,
[System.Globalization.CultureInfo]::InvariantCulture,
[System.Globalization.DateTimeStyles]::RoundtripKind
)
$finishTime = $updatedDt.ToLocalTime().ToString('HH:mm')
} catch { }
}
$parts = @($label)
if ($finishTime) { $parts += $finishTime }
$detail = $parts -join ' '
Write-SAOutcome -Level Error -Text "$name $detail"
if ($job.lastError) {
Write-SAProgress -Label "Error" -Text $job.lastError -Indent 2
}
}
}
exit 0
}
'Rerun' {
Write-SAPhaseHeader -Title "Re-run Job"
Invoke-SARerun -QueueRoot $Config.paths.queueRoot -Config $Config -Limit $RerunLimit -ProcessJob {
param($Context, $Job)
Invoke-SAJobProcessing -Context $Context -Job $Job
}
exit 0
}
'Enqueue' {
# Enqueue a new job
# Validate download path
if ([string]::IsNullOrWhiteSpace($DownloadPath)) {
Write-SAOutcome -Level Error -Text "DownloadPath is required"
Write-Host ""
Write-Host "Usage: Stagearr.ps1 -DownloadPath <path> [-DownloadLabel <label>] [-TorrentHash <hash>]"
Write-Host ""
exit 1
}
# Check if path exists
if (-not (Test-Path -LiteralPath $DownloadPath)) {
Write-SAOutcome -Level Error -Label "Path" -Text "Not found: $DownloadPath"
exit 1
}
# Normalize label
if ([string]::IsNullOrWhiteSpace($DownloadLabel)) {
$DownloadLabel = 'NoLabel'
}
# Check for skip label
if ($DownloadLabel -eq $Config.labels.skip) {
Write-SAProgress -Label "Skip" -Text "Label is '$($Config.labels.skip)', not processing"
exit 0
}
# Check if label should skip email
if ($DownloadLabel -eq 'NoMail') {
$SkipEmail = $true
}
if ($Wait) {
# Defer job creation until after lock acquisition to prevent
# the background worker from stealing it during the wait period
$jobParams = @{
QueueRoot = $Config.paths.queueRoot
DownloadPath = $DownloadPath
DownloadLabel = $DownloadLabel
TorrentHash = $TorrentHash
DownloadRoot = $Config.paths.downloadRoot
NoCleanup = $NoCleanup
NoMail = $SkipEmail
Force = $Force
}
Start-SAWorker -QueueRoot $Config.paths.queueRoot -Config $Config -Wait `
-Verbose:($VerbosePreference -eq 'Continue') `
-DeferredJobParams $jobParams `
-ProcessJob {
param($Context, $Job)
Invoke-SAJobProcessing -Context $Context -Job $Job
}
} else {
# Add job immediately so background worker picks it up
$job = Add-SAJob -QueueRoot $Config.paths.queueRoot `
-DownloadPath $DownloadPath `
-DownloadLabel $DownloadLabel `
-TorrentHash $TorrentHash `
-DownloadRoot $Config.paths.downloadRoot `
-NoCleanup:$NoCleanup `
-NoMail:$SkipEmail `
-Force:$Force
if ($null -eq $job) {
Write-SAProgress -Label "Queue" -Text "Job already exists or was skipped"
Write-SAProgress -Label "Hint" -Text "Use -Force to re-run"
exit 0
}
Start-SAWorker -QueueRoot $Config.paths.queueRoot -Config $Config `
-Verbose:($VerbosePreference -eq 'Continue') `
-ProcessJob {
param($Context, $Job)
Invoke-SAJobProcessing -Context $Context -Job $Job
}
}
exit 0
}
}