Drive-In: check the next two days' films each round and find re-filed ones again

Paul, 28 November 2026: Fellowship on the Lord of the Rings bill was booked under a number the
server had given up. One request checks the next 48 hours; a gone film is found again by name in
the same slot and length. Replay test with the server's real answer.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-30 07:35:55 +00:00
co-authored by Claude Opus 5.5
parent bca45f2e6a
commit 0d5c7fd154
4 changed files with 103 additions and 1 deletions
@@ -300,7 +300,8 @@ internal class ChannelScheduler(private val store: ScheduleStore,
fun topUpDriveIn(userId: String, session: EmbySampleSession? = null): ChannelPlan? {
val now = clock()
val existing = store.loadPlan(userId, DRIVE_IN_NUMBER)
val existing = session?.let { s -> runCatching { refileDriveIn(userId, s, now) }.onFailure { Log.w("OrbitSchedule", "channel=$DRIVE_IN_NUMBER refileAhead failed=${it.javaClass.simpleName}") }.getOrNull() }
?: store.loadPlan(userId, DRIVE_IN_NUMBER)
if (driveInBookedAhead(existing, now)) return existing
val failed = store.loadFailed(userId)
fun films(numbers: List<Int>) = numbers.flatMap { number -> withoutFailed(store.loadPool(userId, number)?.series.orEmpty().filter { it.isFilm }, failed) }.distinctBy { seriesKey(it.title) }
@@ -321,6 +322,31 @@ internal class ChannelScheduler(private val store: ScheduleStore,
return plan
}
/**
* The Drive-In's films booked for the next two days, checked against the server (Paul, 28 November 2026: Fellowship, on the
* Lord of the Rings bill, was booked under a number the server had since given up). One small request for the lot; each
* film that has gone is found again by name, as at tuning, and its slot keeps the copy found. Same film, same slot and
* length: nothing is moved. Returns the plan as saved.
*/
internal fun refileDriveIn(userId: String, session: EmbySampleSession, now: Long): ChannelPlan? = synchronized(PlanEdits.lock) {
val plan = store.loadPlan(userId, DRIVE_IN_NUMBER) ?: return@synchronized null
val ahead = plan.entries.filter { it.endMs > now && it.startMs < now + DRIVE_IN_REFILE_AHEAD_MS && it.media.item.type == "Movie" }
if (ahead.isEmpty()) return@synchronized plan
val present = session.presentIds(ahead.map { it.media.item.id })
val gone = ahead.filter { it.media.item.id !in present }
if (gone.isEmpty()) return@synchronized plan
var entries = plan.entries
var refiled = 0
for (entry in gone) {
val found = runCatching { session.refiledFilm(entry.media.item, DRIVE_IN_NUMBER) }.getOrNull() ?: continue
entries = refiledEntry(entries, entry, found) ?: continue
refiled++
}
Log.i("OrbitSchedule", "channel=$DRIVE_IN_NUMBER refileAhead checked=${ahead.size} gone=${gone.size} refiled=$refiled")
if (refiled == 0) return@synchronized plan
plan.copy(entries = entries).also { store.savePlan(userId, DRIVE_IN_NUMBER, it) }
}
/**
* The curated events' verified films, read at most once a day, and only when the Drive-In is about to book. A read that
* fails keeps the last verified list; with no session, the saved list is used as it is.
@@ -132,6 +132,20 @@ internal fun openBooked(session: EmbySampleSession, channel: SampleChannel, stor
}
}
/** How far ahead the Drive-In's booked films are checked against the server each round ([ChannelScheduler.refileDriveIn]). */
internal const val DRIVE_IN_REFILE_AHEAD_MS = 48 * 60 * 60_000L
/**
* [entries] with [entry]'s file swapped for [found], the same film found again under a new number: same slot, same booked
* length, its new provider ID and year kept. Null when the entry isn't there any more.
*/
internal fun refiledEntry(entries: List<ScheduleEntry>, entry: ScheduleEntry, found: ScheduledMedia): List<ScheduleEntry>? {
val at = entries.indexOfFirst { it.startMs == entry.startMs && it.media.item.id == entry.media.item.id }.takeIf { it >= 0 } ?: return null
val slot = entries[at].media.durationMs
val media = found.copy(durationMs = slot, item = found.item.copy(minutes = slot / 60_000, durationMs = slot))
return entries.toMutableList().also { it[at] = entries[at].copy(media = media, fileMs = null) }
}
/** Every change to a saved plan goes through this, so a top-up and a re-timing never write over each other. */
internal object PlanEdits {
val lock = Any()
@@ -0,0 +1,61 @@
package tv.orbit.classic
import org.json.JSONObject
import org.junit.Assert.assertEquals
import org.junit.Assert.assertNotNull
import org.junit.Rule
import org.junit.Test
import org.junit.rules.TemporaryFolder
/**
* The Drive-In's Lord of the Rings bill (Paul, 28 November 2026): Fellowship was booked under the number the server used before
* it re-filed its films, with no IMDb ID or year. Replays the server's real answer to Orbit's search for it.
*/
class DriveInRefileTest {
@get:Rule val folder = TemporaryFolder()
@org.junit.Before fun server() { EmbyServer.origin = "https://emby.example.com"; IdRepairs.resetForTest() }
@org.junit.After fun noServer() { EmbyServer.origin = "" }
@Test fun theBookedFilmIsFoundAgainByItsName() {
val search = javaClass.classLoader!!.getResourceAsStream("fellowship-search.json")!!.bufferedReader().readText()
val transport = EmbyTransport { r -> if ("IncludeItemTypes=Movie" in r.path && "SearchTerm=" in r.path) EmbyReply(200, search) else EmbyReply(500, "") }
val booked = SampleItem("The Lord of the Rings: The Fellowship of the Ring", "Movie", 228, "", "3750915", 228 * 60_000L)
val session = EmbySampleSession(transport, "fixture-token-private", "fixture-user", EmbySample(listOf(booked), 1))
val found = session.refiledFilm(booked, DRIVE_IN_NUMBER)
assertNotNull(found)
println("found=${found!!.item.id} source=${found.sourceId} ref=${found.item.ref}")
assertEquals("5504682", found.item.id)
}
@Test fun theDriveInChecksItsNextTwoDaysAndKeepsEachSlot() {
val search = javaClass.classLoader!!.getResourceAsStream("fellowship-search.json")!!.bufferedReader().readText()
val asked = mutableListOf<String>()
val transport = EmbyTransport { r ->
asked += r.path
when {
"SearchTerm=" in r.path -> EmbyReply(200, search)
"&Ids=" in r.path -> EmbyReply(200, JSONObject().put("Items", org.json.JSONArray().put(JSONObject().put("Id", "4282684"))).toString())
else -> EmbyReply(500, "")
}
}
val now = 1_795_000_000_000L
val fellowship = SampleItem("The Lord of the Rings: The Fellowship of the Ring", "Movie", 228, "", "3750915", 228 * 60_000L)
val batman = SampleItem("Batman Returns", "Movie", 126, "", "4282684", 126 * 60_000L)
val plan = ChannelPlan(listOf(
ScheduleEntry("3750915", EpisodeMark(1, 1), ScheduledMedia(fellowship, "mediasource_3750915", 13_698_368L), now + 3_600_000L),
ScheduleEntry("4282684", EpisodeMark(1, 1), ScheduledMedia(batman, "mediasource_4282684", 126 * 60_000L), now + 30 * 3_600_000L)), ScheduleCursor())
val store = ScheduleStore(folder.newFolder())
store.savePlan("fixture-user", DRIVE_IN_NUMBER, plan)
val session = EmbySampleSession(transport, "fixture-token-private", "fixture-user", EmbySample(listOf(fellowship, batman), 1))
val saved = ChannelScheduler(store, clock = { now }).refileDriveIn("fixture-user", session, now)!!
// Fellowship has its current copy, in the same slot and booked length; Batman Returns was still there and is untouched.
assertEquals("5504682", saved.entries[0].media.item.id)
assertEquals(plan.entries[0].startMs, saved.entries[0].startMs)
assertEquals(plan.entries[0].media.durationMs, saved.entries[0].media.durationMs)
assertEquals("imdb.tt0120737", saved.entries[0].media.item.ref)
assertEquals(plan.entries[1], saved.entries[1])
assertEquals(saved.entries.map { it.media.item.id }, store.loadPlan("fixture-user", DRIVE_IN_NUMBER)!!.entries.map { it.media.item.id })
// One request to check both, one search for the gone film.
assertEquals(2, asked.size)
}
}
File diff suppressed because one or more lines are too long