Find a re-filed film by its channel's entry, so a shared name like Halloween still matches

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-17 13:29:20 +00:00
co-authored by Claude Opus 5.5
parent 33e1571591
commit 563a64589a
4 changed files with 37 additions and 6 deletions
@@ -1176,3 +1176,15 @@ internal fun presentationOf(entry: ScheduleEntry, parts: List<DayPart>, zone: ja
}
return matches.singleOrNull()?.key.orEmpty()
}
/**
* The entry on [channelNumber]'s own lists that a booked film [title] came from, as "Halloween (1978)", or null when the
* lists hold none, or more than one film of that name. A booked film keeps only the library's name, which can belong to
* several films; the entry says which.
*/
internal fun listedFilmEntry(channelNumber: Int, title: String): String? {
val programming = SmartChannels.firstOrNull { it.number == channelNumber } ?: return null
val key = seriesKey(title)
return (programming.searches + programming.dayParts.flatMap { it.titles })
.filter { seriesKey(FilmIdentity.of(it).title) == key }.distinct().singleOrNull()
}
@@ -507,7 +507,7 @@ internal class EmbySampleSession(private val transport: EmbyTransport, private v
*/
/** Films already looked at for a lighter copy this session, so a heavy film is searched for at most once. */
private val lighterChecked = java.util.Collections.synchronizedSet(HashSet<String>())
fun lighterFilm(item: SampleItem): ScheduledMedia? = if (item.type != "Movie" || !lighterChecked.add(item.id)) null else refiledFilm(item)
fun lighterFilm(item: SampleItem, channelNumber: Int? = null): ScheduledMedia? = if (item.type != "Movie" || !lighterChecked.add(item.id)) null else refiledFilm(item, channelNumber)
/**
* Which of [ids] the server still has (15 November 2026: a rescan gave some booked programmes new numbers). Fifty at a
@@ -524,9 +524,12 @@ internal class EmbySampleSession(private val transport: EmbyTransport, private v
return found
}
fun refiledFilm(item: SampleItem): ScheduledMedia? {
fun refiledFilm(item: SampleItem, channelNumber: Int? = null): ScheduledMedia? {
if (item.type != "Movie") return null
val film = findFilm(transport, userId, ::currentToken, item.title, BoxDecoders::canDecode).film ?: return null
// The booked title has no year (17 November 2026: "Halloween" on After Dark matched the 1978, 2007 and 2018 films, so
// it was never found again). The channel's own list entry carries the year and IMDb ID, so it's searched by that.
val entry = channelNumber?.let { listedFilmEntry(it, item.title) } ?: item.title
val film = findFilm(transport, userId, ::currentToken, entry, BoxDecoders::canDecode).film ?: return null
if (film.id == item.id) return null
val copy = film.episodes.first()
val found = SampleItem(film.title, "Movie", copy.durationMs / 60_000, "", copy.id, copy.durationMs)
@@ -108,7 +108,7 @@ internal fun openBooked(session: EmbySampleSession, channel: SampleChannel, stor
val plan = session.prepareForTuning(show.media.item, show.media.sourceId, ahead) { Thread.sleep(2_000) }
// A film booked as a heavy copy (13 November 2026: 4K remuxes drifting out of sync) swaps to a lighter copy of the same
// film when the library has one: one search, once per film, and the schedule is updated so it stays swapped.
if (live && plan.heavy) runCatching { session.lighterFilm(show.media.item) }.getOrNull()?.let { lighter ->
if (live && plan.heavy) runCatching { session.lighterFilm(show.media.item, channel.number) }.getOrNull()?.let { lighter ->
android.util.Log.i("OrbitSchedule", "channel=${channel.number} lighterCopy old=${show.media.item.id} new=${lighter.item.id}")
PlanEdits.replace(channel, store, session.accountId, show.startMs, show.media.item.id, lighter)
val booked = show.copy(media = lighter.copy(durationMs = show.media.durationMs), fileMs = null)
@@ -118,7 +118,7 @@ internal fun openBooked(session: EmbySampleSession, channel: SampleChannel, stor
}
catch (failure: EmbyCheckException) {
if (failure.httpStatus != 404 && failure.httpStatus != 500) throw failure
val found = session.refiledFilm(show.media.item) ?: session.refiledEpisode(show.media.item) ?: throw failure
val found = session.refiledFilm(show.media.item, channel.number) ?: session.refiledEpisode(show.media.item) ?: throw failure
android.util.Log.i("OrbitSchedule", "channel=${channel.number} refiled old=${show.media.item.id} new=${found.item.id}")
if (live) {
PlanEdits.replace(channel, store, session.accountId, show.startMs, show.media.item.id, found)
@@ -176,7 +176,7 @@ internal object PlanEdits {
val gone = ahead.filter { it.media.item.id !in present }
var refiled = 0
for (entry in gone) {
val found = runCatching { session.refiledFilm(entry.media.item) ?: session.refiledEpisode(entry.media.item) }.getOrNull() ?: continue
val found = runCatching { session.refiledFilm(entry.media.item, channel.number) ?: session.refiledEpisode(entry.media.item) }.getOrNull() ?: continue
replace(channel, store, session.accountId, entry.startMs, entry.media.item.id, found); refiled++
}
android.util.Log.i("OrbitSchedule", "channel=${channel.number} recheck checked=${ahead.size} gone=${gone.size} refiled=$refiled")
@@ -37,6 +37,22 @@ class RefiledTest {
assertTrue(asked.none { it.startsWith("/Shows/other1") })
}
/** 17 November 2026: "Halloween" on After Dark was booked without its year, and three films share that name. */
@Test fun aFilmSharingItsNameIsFoundByTheChannelsOwnEntry() {
fun film(id: String, year: Int, imdb: String) = JSONObject().put("Id", id).put("Name", "Halloween").put("Type", "Movie").put("ProductionYear", year)
.put("ProviderIds", JSONObject().put("Imdb", imdb)).put("MediaSources", source("src$id"))
val transport = EmbyTransport { r ->
if ("IncludeItemTypes=Movie" in r.path) EmbyReply(200, JSONObject().put("TotalRecordCount", 3).put("Items", JSONArray()
.put(film("h2018", 2018, "tt1502407")).put(film("h1978", 1978, "tt0077651")).put(film("h2007", 2007, "tt0373883"))).toString())
else EmbyReply(404, "")
}
val booked = SampleItem("Halloween", "Movie", 91, "", "old1978", 91 * 60_000L)
val session = EmbySampleSession(transport, "fixture-token-private", "fixture-user", EmbySample(listOf(booked), 1))
assertEquals("Halloween (1978)", listedFilmEntry(AFTER_DARK_NUMBER, "Halloween"))
assertNull(session.refiledFilm(booked))
assertEquals("h1978", session.refiledFilm(booked, AFTER_DARK_NUMBER)!!.item.id)
}
@Test fun nothingWhenTheEpisodeIsntThere() {
val transport = EmbyTransport { r ->
when {