From 9182969d2b550c2a23bc754e84dd4c1fa34c23fc Mon Sep 17 00:00:00 2001 From: Jumpstile Date: Sat, 14 Nov 2026 17:57:04 -0500 Subject: [PATCH 1/2] feat: build event guides automatically for high-confidence event channels (#175, event slice 3) Channels the detector rates high get a guide built from their own provider programmes (real titles and times, channel name in gaps). Any parse problem, missing time or error falls back to the normal provider guide; no channel is dropped. Saved user choices win. Frozen contracts are untouched. Co-Authored-By: Claude Sonnet 5.5 Claude-Session: https://claude.ai/code/session_01NzHAckAcs78Lu3rv8Gqgrf --- schemas/guided-setup-summary.schema.json | 25 +++++- scripts/Build-My-Lineup.ps1 | 37 +++++++-- .../Add-ChannelForgePlaceholderGuide.ps1 | 20 ++--- .../Get-ChannelForgeEventGuidePlan.ps1 | 45 +++++++++++ .../Get-ChannelForgeEventProgrammeBlocks.ps1 | 44 ++++++++++ .../Private/Get-ChannelForgeGuideWindow.ps1 | 16 ++++ .../Read-ChannelForgeEventGuideProgramme.ps1 | 37 +++++++++ tests/unit/BuildMyLineup.Tests.ps1 | 54 +++++++++++++ tests/unit/EventGuidePlan.Tests.ps1 | 81 +++++++++++++++++++ 9 files changed, 342 insertions(+), 17 deletions(-) create mode 100644 src/ChannelForge/Private/Get-ChannelForgeEventGuidePlan.ps1 create mode 100644 src/ChannelForge/Private/Get-ChannelForgeEventProgrammeBlocks.ps1 create mode 100644 src/ChannelForge/Private/Get-ChannelForgeGuideWindow.ps1 create mode 100644 src/ChannelForge/Private/Read-ChannelForgeEventGuideProgramme.ps1 create mode 100644 tests/unit/EventGuidePlan.Tests.ps1 diff --git a/schemas/guided-setup-summary.schema.json b/schemas/guided-setup-summary.schema.json index 5cbd362..2f49e7d 100644 --- a/schemas/guided-setup-summary.schema.json +++ b/schemas/guided-setup-summary.schema.json @@ -86,14 +86,37 @@ "KeepWithoutGuide", "OwnPlaceholder", "BindTrueMatch", - "ShareOnPurpose" + "ShareOnPurpose", + "EventGuide" ] } } } + }, + "EventGuide": { + "type": "object", + "additionalProperties": false, + "required": ["Applied", "Fallback"], + "properties": { + "Applied": { "$ref": "#/$defs/eventGuideRows" }, + "Fallback": { "$ref": "#/$defs/eventGuideRows" } + } } }, "$defs": { + "eventGuideRows": { + "type": "array", + "items": { + "type": "object", + "additionalProperties": false, + "required": ["Key", "Name", "Reason"], + "properties": { + "Key": { "type": "string" }, + "Name": { "type": "string" }, + "Reason": { "type": "string" } + } + } + }, "eventPatternPreview": { "type": "object", "additionalProperties": false, diff --git a/scripts/Build-My-Lineup.ps1 b/scripts/Build-My-Lineup.ps1 index 7f8faa8..0d1e31a 100644 --- a/scripts/Build-My-Lineup.ps1 +++ b/scripts/Build-My-Lineup.ps1 @@ -101,6 +101,7 @@ function Write-WorkflowReport { "Decisions file: $($Report.DecisionsFile)" } if ($Report.NoGuideCount -gt 0) { "$($Report.NoGuideCount) channels have no guide. Run again and choose OwnPlaceholder to give them a name-only guide." } + foreach ($eventRow in @($Report.EventGuide.Applied) + @($Report.EventGuide.Fallback)) { "Event channel '$($eventRow.Name)': $($eventRow.Reason)" } if ($Report.EventPatternPreview.Enabled) { "Event-pattern preview: $($Report.EventPatternPreview.Status)" "Event-pattern inference ran: $($Report.EventPatternPreview.InferenceRan.ToString().ToLowerInvariant())" @@ -729,15 +730,20 @@ try { $decisions = @(Invoke-ChannelForgePrivate -Name 'Read-ChannelForgeChannelDecisions' -Arguments @{ Path = $decisionsPath }) $strict = $AmbiguousAction -eq 'Cancel' $interactive = [string]::IsNullOrWhiteSpace($AmbiguousAction) -and -not ([Console]::IsInputRedirected -or -not [Environment]::UserInteractive) + $entryStreamKey = { + param($entry) + $slice = $entry.EntryOutputSlice + $entryLines = [Text.Encoding]::UTF8.GetString($candidateM3UBytes, [int]$slice.ByteOffset, [int]$slice.ByteLength) -split "`n" + $streamUrl = @($entryLines | Where-Object { $_ -and -not $_.StartsWith('#') }) | Select-Object -Last 1 + [string](Invoke-ChannelForgePrivate -Name 'Get-ChannelForgeChannelKey' -Arguments @{ Url = [string]$streamUrl } | Select-Object -Last 1) + } $held = @(foreach ($binding in @($m3uBindings | Where-Object { [string]$_.Status -eq 'ReviewNeeded' })) { $entry = @($manifest.Entries | Where-Object { [string]$_.EntryId -ceq [string]$binding.EntryId }) | Select-Object -First 1 if ($null -eq $entry) { continue } - $slice = $entry.EntryOutputSlice - $entryLines = [Text.Encoding]::UTF8.GetString($candidateM3UBytes, [int]$slice.ByteOffset, [int]$slice.ByteLength) -split "`n" - $streamUrl = @($entryLines | Where-Object { $_ -and -not $_.StartsWith('#') }) | Select-Object -Last 1 + $streamKey = & $entryStreamKey $entry [pscustomobject]@{ EntryId = [string]$entry.EntryId - Key = [string](Invoke-ChannelForgePrivate -Name 'Get-ChannelForgeChannelKey' -Arguments @{ Url = [string]$streamUrl } | Select-Object -Last 1) + Key = $streamKey Name = [string]$entry.DisplayName GroupId = [string]$binding.M3URawId } @@ -774,6 +780,21 @@ try { if ($outcome.Choice -ne 'ShareOnPurpose') { $tvgIdByEntry[$outcome.EntryId] = $outcome.Guide } } $ownChannels = @($choices.Outcomes | Where-Object { $_.Choice -eq 'OwnPlaceholder' } | ForEach-Object { [pscustomobject]@{ Id = $_.Guide; Name = $_.Name } }) + # Event channels (event-channel/v2, slice 3): channels rated high get an event guide from their own provider + # guide. A saved user choice wins. Any problem leaves every channel on its normal guide. + $eventGuideByEntry = @{}; $eventApplied = @(); $eventFallback = @() + if ($null -ne $candidateXMLTVBytes) { + try { + $guideIdByEntry = @{} + foreach ($binding in @($m3uBindings | Where-Object { [string]$_.Status -in 'ExactBound', 'ReviewNeeded' })) { $guideIdByEntry[[string]$binding.EntryId] = [string]$binding.M3URawId } + $eventEntries = @($manifest.Entries | ForEach-Object { [pscustomobject]@{ EntryId = [string]$_.EntryId; Key = (& $entryStreamKey $_); Name = [string]$_.DisplayName; GuideId = [string]$guideIdByEntry[[string]$_.EntryId] } }) + $eventPlan = Invoke-ChannelForgePrivate -Name 'Get-ChannelForgeEventGuidePlan' -Arguments @{ XmltvBytes = $candidateXMLTVBytes; Entry = $eventEntries; ExcludedKey = [string[]]@($decisions | ForEach-Object { [string]$_.key }) } | Select-Object -Last 1 + $eventApplied = @($eventPlan.Applied); $eventFallback = @($eventPlan.Fallback) + $ownChannels = @($ownChannels) + @($eventPlan.Channel) + foreach ($eventChannel in @($eventPlan.Channel)) { foreach ($eventEntryId in @($eventChannel.EntryIds)) { $eventGuideByEntry[$eventEntryId] = $eventChannel.Id; $tvgIdByEntry[$eventEntryId] = $eventChannel.Id } } + } + catch { Write-Warning 'The event channel guide could not be built, so every channel keeps its normal guide.' } + } if ($ownChannels.Count -gt 0) { $candidateXMLTVBytes = [byte[]](Invoke-ChannelForgePrivate -Name 'Add-ChannelForgePlaceholderGuide' -Arguments @{ XmltvBytes = $candidateXMLTVBytes; Channel = $ownChannels; TimeZone = [System.TimeZoneInfo]::Local } | Select-Object -Last 1) @@ -785,8 +806,8 @@ try { $report.ChannelGuides = @($manifest.Entries | ForEach-Object { $id = [string]$_.EntryId $outcome = $outcomeByEntry[$id] - $guideValue = if ($null -ne $outcome) { $outcome.Guide } elseif ($null -ne $candidateXMLTVBytes -and $exactEntryIds.ContainsKey($id)) { $exactEntryIds[$id] } else { $null } - [ordered]@{ name = [string]$_.DisplayName; guide = $guideValue; choice = if ($null -ne $outcome) { $outcome.Choice } elseif ($null -ne $guideValue) { 'ExactMatch' } else { 'NoMatch' } } + $guideValue = if ($eventGuideByEntry.ContainsKey($id)) { $eventGuideByEntry[$id] } elseif ($null -ne $outcome) { $outcome.Guide } elseif ($null -ne $candidateXMLTVBytes -and $exactEntryIds.ContainsKey($id)) { $exactEntryIds[$id] } else { $null } + [ordered]@{ name = [string]$_.DisplayName; guide = $guideValue; choice = if ($eventGuideByEntry.ContainsKey($id)) { 'EventGuide' } elseif ($null -ne $outcome) { $outcome.Choice } elseif ($null -ne $guideValue) { 'ExactMatch' } else { 'NoMatch' } } }) $report.CollisionGroups = @($choices.Outcomes | Group-Object -Property GroupId -CaseSensitive | ForEach-Object { $actions = [ordered]@{} @@ -798,7 +819,9 @@ try { SameStreamChannels = @($_.Group | Group-Object -Property Key | Where-Object { $_.Count -gt 1 } | ForEach-Object { @($_.Group | ForEach-Object Name) -join ' = ' }) } }) - $report.NoGuideCount = @($choices.Outcomes | Where-Object { $_.Choice -eq 'KeepWithoutGuide' }).Count + $report.NoGuideCount = @($choices.Outcomes | Where-Object { $_.Choice -eq 'KeepWithoutGuide' -and -not $eventGuideByEntry.ContainsKey($_.EntryId) }).Count + $report.EventGuide = [ordered]@{ Applied = @($eventApplied); Fallback = @($eventFallback) } + foreach ($eventRow in @($eventApplied) + @($eventFallback)) { Write-Host "Event channel '$($eventRow.Name)': $($eventRow.Reason)" -ForegroundColor Yellow } $report.DecisionsFile = 'state/channel-decisions.json' if ($report.CollisionGroups.Count -gt 0) { Write-Host "Guide collisions: $($report.CollisionGroups.Count) guide ID(s) are shared by several channels." -ForegroundColor Yellow diff --git a/src/ChannelForge/Private/Add-ChannelForgePlaceholderGuide.ps1 b/src/ChannelForge/Private/Add-ChannelForgePlaceholderGuide.ps1 index 92355c5..1eaa2a1 100644 --- a/src/ChannelForge/Private/Add-ChannelForgePlaceholderGuide.ps1 +++ b/src/ChannelForge/Private/Add-ChannelForgePlaceholderGuide.ps1 @@ -12,14 +12,10 @@ function Add-ChannelForgePlaceholderGuide { $utf8 = [System.Text.UTF8Encoding]::new($false, $true) $text = $utf8.GetString($XmltvBytes) - $stamp = [regex]::Matches($text, '$name`n") - foreach ($block in $blocks) { - [void]$programmeXml.Append(" $name`n") + # A channel with its own Blocks (Start, Stop, Title as UTC datetime) gets those; the rest get the day blocks. + $source = $blocks + if ($item.PSObject.Properties['Blocks'] -and @($item.Blocks).Count -gt 0) { + $source = @($item.Blocks | ForEach-Object { , @($_.Start, $_.Stop, [System.Security.SecurityElement]::Escape([string]$_.Title)) }) + } + foreach ($block in $source) { + $title = if ($block.Count -gt 2) { $block[2] } else { $name } + [void]$programmeXml.Append(" $title`n") } } diff --git a/src/ChannelForge/Private/Get-ChannelForgeEventGuidePlan.ps1 b/src/ChannelForge/Private/Get-ChannelForgeEventGuidePlan.ps1 new file mode 100644 index 0000000..1cedafb --- /dev/null +++ b/src/ChannelForge/Private/Get-ChannelForgeEventGuidePlan.ps1 @@ -0,0 +1,45 @@ +function Get-ChannelForgeEventGuidePlan { + # event-channel/v2, slice 3: the automatic apply rule. Only channels the detector rates high get an + # event guide, built from their own provider guide programs. Everything else keeps its normal guide. + # Entry: EntryId, Key, Name, GuideId (provider guide id, may be empty). ExcludedKey: channels with a + # saved user choice, which wins. Returns Channel (for Add-ChannelForgePlaceholderGuide: Id, Name, + # Blocks, plus Key and EntryIds), Applied and Fallback rows (Key, Name, Reason). Pure; no channel dropped. + [CmdletBinding()] + param( + [Parameter(Mandatory)][byte[]]$XmltvBytes, + [Parameter(Mandatory)][AllowEmptyCollection()][object[]]$Entry, + [AllowEmptyCollection()][string[]]$ExcludedKey = @() + ) + + $text = [System.Text.UTF8Encoding]::new($false, $true).GetString($XmltvBytes) + $window = Get-ChannelForgeGuideWindow -Text $text + $programmes = Read-ChannelForgeEventGuideProgramme -Text $text -GuideId @($Entry | ForEach-Object { [string]$_.GuideId } | Where-Object { $_ }) + $byKey = [ordered]@{} + foreach ($item in $Entry) { if (-not $byKey.Contains([string]$item.Key)) { $byKey[[string]$item.Key] = [System.Collections.Generic.List[object]]::new() }; $byKey[[string]$item.Key].Add($item) } + + $detectorInput = foreach ($group in $byKey.Values) { + $rows = if ($group[0].GuideId -and $programmes.ContainsKey([string]$group[0].GuideId)) { @($programmes[[string]$group[0].GuideId] | Where-Object Valid) } else { @() } + [pscustomobject]@{ + Key = [string]$group[0].Key; DisplayName = [string]$group[0].Name; GroupTitle = ''; Category = $null + Programmes = @($rows | ForEach-Object { [pscustomobject]@{ Title = $_.Title; Start = $_.Start; Stop = $(if ($null -ne $_.Stop) { $_.Stop } else { $_.Start }) } }) + } + } + $channels = [System.Collections.Generic.List[object]]::new() + $applied = [System.Collections.Generic.List[object]]::new() + $fallback = [System.Collections.Generic.List[object]]::new() + foreach ($rated in @(Get-ChannelForgeEventChannelCandidate -Channel @($detectorInput) | Where-Object { $_.Confidence -ceq 'high' })) { + $group = $byKey[$rated.Key] + $reason = $null + if ($ExcludedKey -contains $rated.Key) { $reason = 'you saved a guide choice for this channel' } + else { + $guideRows = @() + if ($group[0].GuideId -and $programmes.ContainsKey([string]$group[0].GuideId)) { $guideRows = @($programmes[[string]$group[0].GuideId]) } + $built = Get-ChannelForgeEventProgrammeBlocks -Programme $guideRows -ChannelName $rated.DisplayName -WindowStart $window.Start -WindowEnd $window.End + $reason = $built.Reason + } + if ($reason) { [void]$fallback.Add([pscustomobject]@{ Key = $rated.Key; Name = $rated.DisplayName; Reason = "normal guide kept: $reason" }); continue } + [void]$channels.Add([pscustomobject]@{ Id = 'ChannelForge-' + $rated.Key.Substring(0, 32); Name = $rated.DisplayName; Blocks = $built.Blocks; Key = $rated.Key; EntryIds = @($group | ForEach-Object { [string]$_.EntryId }) }) + [void]$applied.Add([pscustomobject]@{ Key = $rated.Key; Name = $rated.DisplayName; Reason = "event guide applied: $($rated.Reasons -join '; ')" }) + } + [pscustomobject]@{ Channel = @($channels.ToArray()); Applied = @($applied.ToArray()); Fallback = @($fallback.ToArray()) } +} diff --git a/src/ChannelForge/Private/Get-ChannelForgeEventProgrammeBlocks.ps1 b/src/ChannelForge/Private/Get-ChannelForgeEventProgrammeBlocks.ps1 new file mode 100644 index 0000000..f017b56 --- /dev/null +++ b/src/ChannelForge/Private/Get-ChannelForgeEventProgrammeBlocks.ps1 @@ -0,0 +1,44 @@ +function Get-ChannelForgeEventProgrammeBlocks { + # event-channel/v2, slice 3. Turns one channel's provider programs into ordered guide blocks with + # no overlap and no hole across the window: real programs keep their title and times, every gap + # shows the channel name (not provider filler). A title and a start are the minimum. A missing + # stop runs to the next real program, or DefaultMinutes. Returns Blocks (Start, Stop, Title as UTC + # datetime) or, when the channel must keep its normal guide, Reason. Pure. + [CmdletBinding()] + param( + [Parameter(Mandatory)][AllowEmptyCollection()][object[]]$Programme, + [Parameter(Mandatory)][string]$ChannelName, + [Parameter(Mandatory)][datetime]$WindowStart, + [Parameter(Mandatory)][datetime]$WindowEnd, + [int]$DefaultMinutes = 180 + ) + + if ($Programme.Count -eq 0) { return [pscustomobject]@{ Blocks = @(); Reason = 'the provider guide has no programs for this channel' } } + if (@($Programme | Where-Object { -not $_.Valid }).Count -gt 0) { return [pscustomobject]@{ Blocks = @(); Reason = 'a program time in the provider guide could not be read' } } + + $fillers = Get-ChannelForgeEventFillerTitle + $nameKey = ConvertTo-ChannelForgeEventTitleKey -Text $ChannelName + $real = @($Programme | Where-Object { + $key = ConvertTo-ChannelForgeEventTitleKey -Text ([string]$_.Title) + $key -and $fillers -notcontains $key -and $key -cne $nameKey + } | Sort-Object { $_.Start.UtcDateTime } | Group-Object { $_.Start.UtcDateTime } | ForEach-Object { $_.Group[0] }) + + $name = ConvertTo-ChannelForgeGuideSafeText -Value $ChannelName -MaximumLength 200 + $blocks = [System.Collections.Generic.List[object]]::new() + $cursor = $WindowStart + for ($i = 0; $i -lt $real.Count; $i++) { + $start = [datetime]$real[$i].Start.UtcDateTime + if ($start -lt $cursor) { $start = $cursor } + $next = if ($i + 1 -lt $real.Count) { [datetime]$real[$i + 1].Start.UtcDateTime } else { $WindowEnd } + $stop = if ($null -ne $real[$i].Stop) { [datetime]$real[$i].Stop.UtcDateTime } elseif ($i + 1 -lt $real.Count) { $next } else { $start.AddMinutes($DefaultMinutes) } + if ($stop -gt $next) { $stop = $next } + if ($stop -gt $WindowEnd) { $stop = $WindowEnd } + if ($stop -le $start) { continue } + if ($start -gt $cursor) { [void]$blocks.Add([pscustomobject]@{ Start = $cursor; Stop = $start; Title = $name }) } + $title = ConvertTo-ChannelForgeGuideSafeText -Value $real[$i].Title -MaximumLength 200 + [void]$blocks.Add([pscustomobject]@{ Start = $start; Stop = $stop; Title = $(if ($title) { $title } else { $name }) }) + $cursor = $stop + } + if ($WindowEnd -gt $cursor) { [void]$blocks.Add([pscustomobject]@{ Start = $cursor; Stop = $WindowEnd; Title = $name }) } + [pscustomobject]@{ Blocks = @($blocks.ToArray()); Reason = $null } +} diff --git a/src/ChannelForge/Private/Get-ChannelForgeGuideWindow.ps1 b/src/ChannelForge/Private/Get-ChannelForgeGuideWindow.ps1 new file mode 100644 index 0000000..cf6a200 --- /dev/null +++ b/src/ChannelForge/Private/Get-ChannelForgeGuideWindow.ps1 @@ -0,0 +1,16 @@ +function Get-ChannelForgeGuideWindow { + # Earliest start and latest stop (UTC) of the programs in an XMLTV text. + [CmdletBinding()] + param([Parameter(Mandatory)][string]$Text) + + $stamp = [regex]::Matches($Text, ' rows (Title, Start, Stop, Valid). Times are UTC datetimeoffset. A missing stop is + # allowed (Stop is null, Valid stays true). A start or stop that cannot be read makes Valid false. + [CmdletBinding()] + param( + [Parameter(Mandatory)][string]$Text, + [Parameter(Mandatory)][string[]]$GuideId + ) + + $wanted = [System.Collections.Generic.HashSet[string]]::new([string[]]$GuideId, [System.StringComparer]::Ordinal) + $culture = [System.Globalization.CultureInfo]::InvariantCulture + $readTime = { + param([string]$Value) + $parsed = [datetimeoffset]::MinValue + if ($Value -match '^\d{14} \+0000$' -and [datetimeoffset]::TryParseExact($Value, "yyyyMMddHHmmss '+0000'", $culture, [System.Globalization.DateTimeStyles]::AssumeUniversal, [ref]$parsed)) { return $parsed } + return $null + } + $result = @{} + foreach ($match in [regex]::Matches($Text, ']*)>(.*?)', [System.Text.RegularExpressions.RegexOptions]::Singleline)) { + $attribute = @{} + foreach ($pair in [regex]::Matches($match.Groups[1].Value, '(\w+)="([^"]*)"')) { $attribute[$pair.Groups[1].Value] = [System.Net.WebUtility]::HtmlDecode($pair.Groups[2].Value) } + # The merged guide names a channel ':|:'; the playlist tvg-id is the raw id. + $channelId = [regex]::Replace([string]$attribute['channel'], '^\d+:[^|]*\|\d+:', '') + if (-not $wanted.Contains($channelId)) { continue } + $start = & $readTime ([string]$attribute['start']) + $stop = & $readTime ([string]$attribute['stop']) + $title = [regex]::Match($match.Groups[2].Value, ']*>(.*?)', [System.Text.RegularExpressions.RegexOptions]::Singleline) + if (-not $result.ContainsKey($channelId)) { $result[$channelId] = [System.Collections.Generic.List[object]]::new() } + $result[$channelId].Add([pscustomobject]@{ + Title = $(if ($title.Success) { [System.Net.WebUtility]::HtmlDecode($title.Groups[1].Value) } else { '' }) + Start = $start; Stop = $stop + Valid = ($null -ne $start -and ($attribute.ContainsKey('stop') -eq ($null -ne $stop))) + }) + } + return $result +} diff --git a/tests/unit/BuildMyLineup.Tests.ps1 b/tests/unit/BuildMyLineup.Tests.ps1 index 4c1aa42..091deb6 100644 --- a/tests/unit/BuildMyLineup.Tests.ps1 +++ b/tests/unit/BuildMyLineup.Tests.ps1 @@ -628,3 +628,57 @@ Describe 'Real ETV playlist regression' { ([regex]::Matches($m3u, '#EXTINF')).Count | Should -Be 482 } } + +Describe 'Event channel guide in the accepted lineup' { + BeforeAll { + function New-EventFixture { + param([string]$Name, [string]$BigFightStart) + $root = New-GuidedSetupRoot -Name $Name + $playlist = Join-Path $TestDrive "$Name.m3u" + $guide = Join-Path $TestDrive "$Name.xml" + "#EXTM3U`n#EXTINF:-1 tvg-id=`"ppv1.us`" group-title=`"PPV`",PPV 1`nhttps://example.invalid/live/ppv1`n#EXTINF:-1 tvg-id=`"news.us`" group-title=`"News`",News Channel`nhttps://example.invalid/live/news`n" | Set-Content -LiteralPath $playlist -Encoding utf8 + @" + + + PPV 1 + News Channel + No Event + Big Fight Night + No Event + News + +"@ | Set-Content -LiteralPath $guide -Encoding utf8 + [pscustomobject]@{ Root = $root; Playlist = $playlist; Guide = $guide } + } + } + + It 'publishes the event title under the channel own guide id and reports the channel' { + $fixture = New-EventFixture -Name 'event-apply' -BigFightStart '20261115180000 +0000' + $run = Invoke-NoKeyboardBuild -Fixture $fixture + $run.ExitCode | Should -Be 0 + $summary = Read-GuidedSetupSummary -Root $fixture.Root + $ownId = 'ChannelForge-' + (Get-TestChannelKey -Url 'https://example.invalid/live/ppv1').Substring(0, 32) + $ppv = Get-TestGuide -Summary $summary -Name 'PPV 1' + $ppv.guide | Should -Be $ownId + $ppv.choice | Should -Be 'EventGuide' + @($summary.EventGuide.Applied).Count | Should -Be 1 + (Get-TestGuide -Summary $summary -Name 'News Channel').guide | Should -Be 'news.us' + $xml = [xml](Get-Content -LiteralPath (Join-Path $fixture.Root 'output\guided-setup\accepted\guide.xml') -Raw) + @($xml.tv.programme | Where-Object { $_.channel -eq $ownId } | ForEach-Object { $_.title }) | Should -Contain 'Big Fight Night' + (Get-Content -LiteralPath (Join-Path $fixture.Root 'output\guided-setup\accepted\lineup.m3u') -Raw) | Should -Match ([regex]::Escape("tvg-id=`"$ownId`"")) + } + + It 'keeps the normal guide and reports why when the provider guide has no programs for the channel' { + # An unreadable time is already refused by the guide reader before this step, so the reachable fallback is a channel with no programs. + $fixture = New-EventFixture -Name 'event-fallback' -BigFightStart '20261115180000 +0000' + $text = (Get-Content -LiteralPath $fixture.Guide -Raw) -replace '(?m)^.*channel="ppv1\.us">.*\r?\n', '' + Set-Content -LiteralPath $fixture.Guide -Value $text -Encoding utf8 + $run = Invoke-NoKeyboardBuild -Fixture $fixture + $run.ExitCode | Should -Be 0 + $summary = Read-GuidedSetupSummary -Root $fixture.Root + (Get-TestGuide -Summary $summary -Name 'PPV 1').guide | Should -Be 'ppv1.us' + @($summary.EventGuide.Applied).Count | Should -Be 0 + $summary.EventGuide.Fallback[0].Reason | Should -BeLike '*no programs*' + $run.Output | Should -Not -BeLike '*could not be built*' + } +} diff --git a/tests/unit/EventGuidePlan.Tests.ps1 b/tests/unit/EventGuidePlan.Tests.ps1 new file mode 100644 index 0000000..0b786ff --- /dev/null +++ b/tests/unit/EventGuidePlan.Tests.ps1 @@ -0,0 +1,81 @@ +BeforeAll { + $script:RepoRoot = Split-Path -Parent (Split-Path -Parent $PSScriptRoot) + Import-Module (Join-Path $script:RepoRoot 'src\ChannelForge\ChannelForge.psd1') -Global -Force + + function New-Xmltv { + param([hashtable]$Programmes) + $body = foreach ($id in $Programmes.Keys) { + foreach ($row in $Programmes[$id]) { "<programme start=`"$($row[0])`" stop=`"$($row[1])`" channel=`"$id`"><title>$($row[2])" } + } + [Text.Encoding]::UTF8.GetBytes("$($body -join '')") + } + function New-Entry { + param([string]$Name, [int]$Number, [string]$GuideId) + [pscustomobject]@{ EntryId = "e$Number"; Key = ('{0:x64}' -f $Number); Name = $Name; GuideId = $GuideId } + } + # Runs the plan, then writes the guide the way the build does. Returns the plan and the programmes per channel id. + function Invoke-Plan { + param([byte[]]$Xmltv, [object[]]$Entry, [string[]]$Excluded = @()) + InModuleScope ChannelForge -Parameters @{ X = $Xmltv; E = $Entry; Ex = $Excluded } { + $plan = Get-ChannelForgeEventGuidePlan -XmltvBytes $X -Entry $E -ExcludedKey $Ex + $bytes = if ($plan.Channel.Count -gt 0) { [byte[]](Add-ChannelForgePlaceholderGuide -XmltvBytes $X -Channel $plan.Channel -TimeZone ([TimeZoneInfo]::Utc) | Select-Object -Last 1) } else { $X } + $doc = [xml][Text.Encoding]::UTF8.GetString($bytes) + $rows = @{} + foreach ($p in @($doc.tv.programme)) { + $rows[$p.channel] += @([pscustomobject]@{ Start = "$($p.start)".PadRight(14).Substring(8, 4); Stop = "$($p.stop)".PadRight(14).Substring(8, 4); Title = $p.title }) + } + [pscustomobject]@{ Plan = $plan; Rows = $rows } + } + } + $script:Day = '20261115' + $script:Provider = @{ + ppv1 = @(@("$($script:Day)000000 +0000", "$($script:Day)180000 +0000", 'No Event'), @("$($script:Day)180000 +0000", "$($script:Day)210000 +0000", 'Big Fight Night'), @("$($script:Day)210000 +0000", "$($script:Day)235900 +0000", 'No Event')) + news = @(,@("$($script:Day)000000 +0000", "$($script:Day)235900 +0000", 'News')) + } +} + +Describe 'Event guide for high channels' { + It 'gives a high channel its own guide with the real title and times, and the channel name in the gaps' { + $out = Invoke-Plan -Xmltv (New-Xmltv $script:Provider) -Entry @((New-Entry 'PPV 1' 1 'ppv1'), (New-Entry 'News' 2 'news')) + $out.Plan.Channel.Count | Should -Be 1 + $id = $out.Plan.Channel[0].Id + $id | Should -Be ('ChannelForge-' + ('{0:x64}' -f 1).Substring(0, 32)) + $rows = $out.Rows[$id] + @($rows | ForEach-Object { "$($_.Start)-$($_.Stop) $($_.Title)" }) | Should -Be @('0000-1800 PPV 1', '1800-2100 Big Fight Night', '2100-2359 PPV 1') + $out.Plan.Applied[0].Reason | Should -BeLike 'event guide applied*' + } + + It 'shows the channel name for the whole window when the channel has no event' { + $xmltv = New-Xmltv @{ ppv1 = $script:Provider.ppv1[0], $script:Provider.ppv1[2]; news = $script:Provider.news } + $out = Invoke-Plan -Xmltv $xmltv -Entry @((New-Entry 'PPV 1' 1 'ppv1'), (New-Entry 'News' 2 'news')) + @($out.Rows[$out.Plan.Channel[0].Id] | ForEach-Object { $_.Title } | Select-Object -Unique) | Should -Be @('PPV 1') + } + + It 'leaves a medium channel on its normal guide' { + $real = @{ fight = @(@("$($script:Day)000000 +0000", "$($script:Day)080000 +0000", 'Bout A'), @("$($script:Day)080000 +0000", "$($script:Day)160000 +0000", 'Bout B'), @("$($script:Day)160000 +0000", "$($script:Day)235900 +0000", 'Bout C')) } + $out = Invoke-Plan -Xmltv (New-Xmltv $real) -Entry @(New-Entry 'Fight Network' 3 'fight') + $out.Plan.Channel.Count | Should -Be 0 + $out.Plan.Fallback.Count | Should -Be 0 + } + + It 'falls back to the normal guide, and reports why, when a program time cannot be read' { + $broken = @{ ppv1 = @(,@('garbage', "$($script:Day)180000 +0000", 'Big Fight Night')); news = $script:Provider.news } + $out = Invoke-Plan -Xmltv (New-Xmltv $broken) -Entry @((New-Entry 'PPV 1' 1 'ppv1'), (New-Entry 'News' 2 'news')) + $out.Plan.Channel.Count | Should -Be 0 + $out.Plan.Fallback[0].Name | Should -Be 'PPV 1' + $out.Plan.Fallback[0].Reason | Should -BeLike '*could not be read*' + $out.Rows['ppv1'].Count | Should -Be 1 + } + + It 'falls back when the provider guide has no programs for the channel' { + $out = Invoke-Plan -Xmltv (New-Xmltv @{ news = $script:Provider.news }) -Entry @((New-Entry 'PPV 1' 1 'missing'), (New-Entry 'News' 2 'news')) + $out.Plan.Channel.Count | Should -Be 0 + $out.Plan.Fallback[0].Reason | Should -BeLike '*no programs*' + } + + It 'keeps a channel with a saved guide choice on that choice' { + $out = Invoke-Plan -Xmltv (New-Xmltv $script:Provider) -Entry @(New-Entry 'PPV 1' 1 'ppv1') -Excluded @('{0:x64}' -f 1) + $out.Plan.Channel.Count | Should -Be 0 + $out.Plan.Fallback[0].Reason | Should -BeLike '*saved a guide choice*' + } +} From 808e74d59cb65fc7ac972cdd1bdcbf8b0715f0fc Mon Sep 17 00:00:00 2001 From: Jumpstile Date: Sat, 14 Nov 2026 18:27:18 -0500 Subject: [PATCH 2/2] fix: read any XMLTV time offset in event guides and treat To Be Advised titles as filler (#175) Real EPG Ripper check: 46% of programmes use offsets other than +0000, which made four PPV channels fall back to the normal guide. The reader now applies the offset and uses the UTC instant. Titles such as Next PPV Event is To Be Advised are filler, not events. Co-Authored-By: Claude Sonnet 5.5 Claude-Session: https://claude.ai/code/session_01NzHAckAcs78Lu3rv8Gqgrf --- .../Get-ChannelForgeEventChannelCandidate.ps1 | 12 ++++++++-- .../Get-ChannelForgeEventProgrammeBlocks.ps1 | 4 ++-- .../Get-ChannelForgeEventTimeAgreement.ps1 | 4 ++-- .../Read-ChannelForgeEventGuideProgramme.ps1 | 2 +- tests/unit/EventGuidePlan.Tests.ps1 | 24 +++++++++++++++++++ 5 files changed, 39 insertions(+), 7 deletions(-) diff --git a/src/ChannelForge/Private/Get-ChannelForgeEventChannelCandidate.ps1 b/src/ChannelForge/Private/Get-ChannelForgeEventChannelCandidate.ps1 index f2b69d4..3087f34 100644 --- a/src/ChannelForge/Private/Get-ChannelForgeEventChannelCandidate.ps1 +++ b/src/ChannelForge/Private/Get-ChannelForgeEventChannelCandidate.ps1 @@ -7,7 +7,7 @@ function Get-ChannelForgeEventChannelCandidate { [CmdletBinding()] param([Parameter(Mandatory)][AllowEmptyCollection()][object[]]$Channel) - $fillers = Get-ChannelForgeEventFillerTitle + # Filler titles: Test-ChannelForgeEventFillerKey $ppvWords = @([pscustomobject]@{ Key = 'ppv'; Aliases = @('ppv', 'pay per view') }) $eventWords = @([pscustomobject]@{ Key = 'event'; Aliases = @('ppv', 'pay per view', 'event', 'events', 'ufc', 'wwe', 'aew', 'boxing', 'fight', 'wrestling', 'live event') }) $sportsKeys = @(Get-ChannelForgeOneGuideCategoryRegistry | Where-Object { $_.Group -ceq 'sports' } | ForEach-Object { [string]$_.Key }) @@ -33,7 +33,7 @@ function Get-ChannelForgeEventChannelCandidate { # A title that echoes the channel name (equal, or one starts with the other, shorter side 4+ chars) is a placeholder. $short = [math]::Min($title.Length, $name.Length) $echo = $short -ge 4 -and ($name.StartsWith($title, [StringComparison]::Ordinal) -or $title.StartsWith($name, [StringComparison]::Ordinal)) - if ($fillers -contains $title -or $title -ceq $name -or $echo) { $placeholder += $hours } + if ((Test-ChannelForgeEventFillerKey -Key $title) -or $title -ceq $name -or $echo) { $placeholder += $hours } else { $realTitles[$title] = $true; if ($hours -ge 6) { $long += $hours } } } $share = if ($total -gt 0) { $placeholder / $total } else { 0.0 } @@ -76,6 +76,14 @@ function Get-ChannelForgeEventFillerTitle { 'pay per view', 'ppv', 'event', 'events') } +function Test-ChannelForgeEventFillerKey { + # True when a title key (ConvertTo-ChannelForgeEventTitleKey) is provider filler: an exact entry of + # Get-ChannelForgeEventFillerTitle, or a title containing "to be advised/announced/determined". + [CmdletBinding()] + param([string]$Key) + ((Get-ChannelForgeEventFillerTitle) -contains $Key) -or ($Key -match '\bto be (advised|announced|determined)\b') +} + function ConvertTo-ChannelForgeEventTitleKey { # Lowercase, punctuation to single spaces. The one title comparison rule for event channels. [CmdletBinding()] diff --git a/src/ChannelForge/Private/Get-ChannelForgeEventProgrammeBlocks.ps1 b/src/ChannelForge/Private/Get-ChannelForgeEventProgrammeBlocks.ps1 index f017b56..db079ce 100644 --- a/src/ChannelForge/Private/Get-ChannelForgeEventProgrammeBlocks.ps1 +++ b/src/ChannelForge/Private/Get-ChannelForgeEventProgrammeBlocks.ps1 @@ -16,11 +16,11 @@ function Get-ChannelForgeEventProgrammeBlocks { if ($Programme.Count -eq 0) { return [pscustomobject]@{ Blocks = @(); Reason = 'the provider guide has no programs for this channel' } } if (@($Programme | Where-Object { -not $_.Valid }).Count -gt 0) { return [pscustomobject]@{ Blocks = @(); Reason = 'a program time in the provider guide could not be read' } } - $fillers = Get-ChannelForgeEventFillerTitle + # Filler titles: Test-ChannelForgeEventFillerKey $nameKey = ConvertTo-ChannelForgeEventTitleKey -Text $ChannelName $real = @($Programme | Where-Object { $key = ConvertTo-ChannelForgeEventTitleKey -Text ([string]$_.Title) - $key -and $fillers -notcontains $key -and $key -cne $nameKey + $key -and -not (Test-ChannelForgeEventFillerKey -Key $key) -and $key -cne $nameKey } | Sort-Object { $_.Start.UtcDateTime } | Group-Object { $_.Start.UtcDateTime } | ForEach-Object { $_.Group[0] }) $name = ConvertTo-ChannelForgeGuideSafeText -Value $ChannelName -MaximumLength 200 diff --git a/src/ChannelForge/Private/Get-ChannelForgeEventTimeAgreement.ps1 b/src/ChannelForge/Private/Get-ChannelForgeEventTimeAgreement.ps1 index ca44236..646e512 100644 --- a/src/ChannelForge/Private/Get-ChannelForgeEventTimeAgreement.ps1 +++ b/src/ChannelForge/Private/Get-ChannelForgeEventTimeAgreement.ps1 @@ -12,7 +12,7 @@ function Get-ChannelForgeEventTimeAgreement { [int]$WindowMinutes = 15 ) - $fillers = Get-ChannelForgeEventFillerTitle + # Filler titles: Test-ChannelForgeEventFillerKey $rated = @{} foreach ($row in $Candidate) { $rated[[string]$row.Key] = $true } $entries = [System.Collections.Generic.List[object]]::new() @@ -25,7 +25,7 @@ function Get-ChannelForgeEventTimeAgreement { $start = [datetimeoffset]::MinValue $hasStart = $programme.Start -is [datetimeoffset] -or [datetimeoffset]::TryParse([string]$programme.Start, [System.Globalization.CultureInfo]::InvariantCulture, [System.Globalization.DateTimeStyles]::None, [ref]$start) if ($programme.Start -is [datetimeoffset]) { $start = $programme.Start } - if (-not $hasStart -or $start -eq [datetimeoffset]::MinValue -or -not $titleKey -or $fillers -contains $titleKey -or $titleKey -ceq $name) { $skipped++; continue } + if (-not $hasStart -or $start -eq [datetimeoffset]::MinValue -or -not $titleKey -or (Test-ChannelForgeEventFillerKey -Key $titleKey) -or $titleKey -ceq $name) { $skipped++; continue } $entries.Add([pscustomobject]@{ TitleKey = $titleKey; Title = ([string]$programme.Title).Trim(); ChannelKey = [string]$item.Key; DisplayName = [string]$item.DisplayName; Start = $start }) } } diff --git a/src/ChannelForge/Private/Read-ChannelForgeEventGuideProgramme.ps1 b/src/ChannelForge/Private/Read-ChannelForgeEventGuideProgramme.ps1 index 8371abe..36a167f 100644 --- a/src/ChannelForge/Private/Read-ChannelForgeEventGuideProgramme.ps1 +++ b/src/ChannelForge/Private/Read-ChannelForgeEventGuideProgramme.ps1 @@ -13,7 +13,7 @@ function Read-ChannelForgeEventGuideProgramme { $readTime = { param([string]$Value) $parsed = [datetimeoffset]::MinValue - if ($Value -match '^\d{14} \+0000$' -and [datetimeoffset]::TryParseExact($Value, "yyyyMMddHHmmss '+0000'", $culture, [System.Globalization.DateTimeStyles]::AssumeUniversal, [ref]$parsed)) { return $parsed } + if ($Value -match '^(\d{14}) ([+-]\d{2})(\d{2})$' -and [datetimeoffset]::TryParseExact(('{0} {1}:{2}' -f $Matches[1], $Matches[2], $Matches[3]), 'yyyyMMddHHmmss zzz', $culture, [System.Globalization.DateTimeStyles]::None, [ref]$parsed)) { return $parsed.ToUniversalTime() } return $null } $result = @{} diff --git a/tests/unit/EventGuidePlan.Tests.ps1 b/tests/unit/EventGuidePlan.Tests.ps1 index 0b786ff..d1a271e 100644 --- a/tests/unit/EventGuidePlan.Tests.ps1 +++ b/tests/unit/EventGuidePlan.Tests.ps1 @@ -78,4 +78,28 @@ Describe 'Event guide for high channels' { $out.Plan.Channel.Count | Should -Be 0 $out.Plan.Fallback[0].Reason | Should -BeLike '*saved a guide choice*' } + + It 'applies the time offset: -0500, +0100 and +0530 all give the same real instants' -ForEach @( + @{ Zone = '-0500'; From = '20261115130000'; To = '20261115160000' } + @{ Zone = '+0100'; From = '20261115190000'; To = '20261115220000' } + @{ Zone = '+0530'; From = '20261115233000'; To = '20261116023000' } + ) { + $offset = @{ ppv1 = @(,@("$From $Zone", "$To $Zone", 'Big Fight Night')); news = $script:Provider.news } + $out = Invoke-Plan -Xmltv (New-Xmltv $offset) -Entry @((New-Entry 'PPV 1' 1 'ppv1'), (New-Entry 'News' 2 'news')) + $out.Plan.Fallback.Count | Should -Be 0 + @($out.Rows[$out.Plan.Channel[0].Id] | ForEach-Object { "$($_.Start)-$($_.Stop) $($_.Title)" }) | Should -Be @('0000-1800 PPV 1', '1800-2100 Big Fight Night', '2100-2359 PPV 1') + } + + It 'falls back when the offset is not a real offset' { + $bad = @{ ppv1 = @(,@("$($script:Day)180000 +9999", "$($script:Day)210000 +9999", 'Big Fight Night')); news = $script:Provider.news } + $out = Invoke-Plan -Xmltv (New-Xmltv $bad) -Entry @((New-Entry 'PPV 1' 1 'ppv1'), (New-Entry 'News' 2 'news')) + $out.Plan.Channel.Count | Should -Be 0 + $out.Plan.Fallback[0].Reason | Should -BeLike '*could not be read*' + } + + It 'shows the channel name, not provider filler, for a To Be Advised title' { + $tba = @{ ppv1 = @(,@("$($script:Day)000000 +0000", "$($script:Day)235900 +0000", 'Next PPV Event is To Be Advised')); news = $script:Provider.news } + $out = Invoke-Plan -Xmltv (New-Xmltv $tba) -Entry @((New-Entry 'PPV 1' 1 'ppv1'), (New-Entry 'News' 2 'news')) + @($out.Rows[$out.Plan.Channel[0].Id] | ForEach-Object { $_.Title } | Select-Object -Unique) | Should -Be @('PPV 1') + } }