From 0d5c7fd1549d2c53d09c65f0fce44c76a69ab92a Mon Sep 17 00:00:00 2001 From: Pauljd Date: Mon, 30 Nov 2026 07:35:55 +0000 Subject: [PATCH] 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 --- .../java/tv/orbit/classic/ChannelScheduler.kt | 28 ++++++++- .../java/tv/orbit/classic/ProgrammeLengths.kt | 14 +++++ .../tv/orbit/classic/DriveInRefileTest.kt | 61 +++++++++++++++++++ .../src/test/resources/fellowship-search.json | 1 + 4 files changed, 103 insertions(+), 1 deletion(-) create mode 100644 android/app/src/test/java/tv/orbit/classic/DriveInRefileTest.kt create mode 100644 android/app/src/test/resources/fellowship-search.json diff --git a/android/app/src/main/java/tv/orbit/classic/ChannelScheduler.kt b/android/app/src/main/java/tv/orbit/classic/ChannelScheduler.kt index 6d25e87c..4dadd890 100644 --- a/android/app/src/main/java/tv/orbit/classic/ChannelScheduler.kt +++ b/android/app/src/main/java/tv/orbit/classic/ChannelScheduler.kt @@ -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) = 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. diff --git a/android/app/src/main/java/tv/orbit/classic/ProgrammeLengths.kt b/android/app/src/main/java/tv/orbit/classic/ProgrammeLengths.kt index 8dae19af..4be45cc7 100644 --- a/android/app/src/main/java/tv/orbit/classic/ProgrammeLengths.kt +++ b/android/app/src/main/java/tv/orbit/classic/ProgrammeLengths.kt @@ -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, entry: ScheduleEntry, found: ScheduledMedia): List? { + 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() diff --git a/android/app/src/test/java/tv/orbit/classic/DriveInRefileTest.kt b/android/app/src/test/java/tv/orbit/classic/DriveInRefileTest.kt new file mode 100644 index 00000000..624847ff --- /dev/null +++ b/android/app/src/test/java/tv/orbit/classic/DriveInRefileTest.kt @@ -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() + 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) + } +} diff --git a/android/app/src/test/resources/fellowship-search.json b/android/app/src/test/resources/fellowship-search.json new file mode 100644 index 00000000..da32b62f --- /dev/null +++ b/android/app/src/test/resources/fellowship-search.json @@ -0,0 +1 @@ +{"Items": [{"Name": "The Lord of the Rings: The Fellowship of the Ring", "Id": "5439819", "Container": "mkv", "MediaSources": [{"Chapters": [], "Protocol": "File", "Id": "mediasource_5439819", "Type": "Default", "Container": "mkv", "Size": 40196239080, "Name": "The Lord of the Rings The Fellowship of the Ring (2001) [imdb-tt0120737][tmdb-120][Bluray-2160p][HDR][HDR10][TrueHD Atmos 7.1][x265]-4K", "IsRemote": false, "HasMixedProtocols": false, "RunTimeTicks": 136983680000, "SupportsTranscoding": true, "SupportsDirectStream": true, "SupportsDirectPlay": true, "IsInfiniteStream": false, "RequiresOpening": false, "RequiresClosing": false, "RequiresLooping": false, "SupportsProbing": true, "MediaStreams": [{"Codec": "hevc", "Language": "eng", "ColorTransfer": "smpte2084", "ColorPrimaries": "bt2020", "ColorSpace": "bt2020nc", "TimeBase": "1/1000", "Title": "The.Lord.Of.The.Rings.The.Fellowship.Of.The.Ring.2001.Extended.Edition.REPACK.2160p.UHD.BluRay.TrueHD.7.1.HDR.x265-CtrlHD", "VideoRange": "HDR 10", "DisplayTitle": "4K HDR 10 HEVC", "DisplayLanguage": "English", "IsInterlaced": false, "BitRate": 23475052, "BitDepth": 10, "RefFrames": 1, "IsDefault": true, "IsForced": false, "IsHearingImpaired": false, "Height": 1608, "Width": 3840, "AverageFrameRate": 23.976025, "RealFrameRate": 23.976025, "Profile": "Main 10", "Type": "Video", "AspectRatio": "160:67", "Index": 0, "IsExternal": false, "IsTextSubtitleStream": false, "SupportsExternalStream": false, "Protocol": "File", "PixelFormat": "yuv420p10le", "Level": 150, "IsAnamorphic": false, "ExtendedVideoType": "Hdr10", "ExtendedVideoSubType": "Hdr10", "ExtendedVideoSubTypeDescription": "HDR 10", "AttachmentSize": 0}, {"Codec": "truehd", "Language": "eng", "TimeBase": "1/1000", "DisplayTitle": "English TRUEHD 7.1 (Default)", "DisplayLanguage": "English", "IsInterlaced": false, "ChannelLayout": "7.1", "BitDepth": 24, "Channels": 8, "SampleRate": 48000, "IsDefault": true, "IsForced": false, "IsHearingImpaired": false, "Type": "Audio", "Index": 1, "IsExternal": false, "IsTextSubtitleStream": false, "SupportsExternalStream": false, "Protocol": "File", "ExtendedVideoType": "None", "ExtendedVideoSubType": "None", "ExtendedVideoSubTypeDescription": "None", "AttachmentSize": 0}, {"Codec": "ac3", "Language": "eng", "TimeBase": "1/1000", "DisplayTitle": "English AC3 5.1", "DisplayLanguage": "English", "IsInterlaced": false, "ChannelLayout": "5.1", "BitRate": 640000, "Channels": 6, "SampleRate": 48000, "IsDefault": false, "IsForced": false, "IsHearingImpaired": false, "Type": "Audio", "Index": 2, "IsExternal": false, "IsTextSubtitleStream": false, "SupportsExternalStream": false, "Protocol": "File", "ExtendedVideoType": "None", "ExtendedVideoSubType": "None", "ExtendedVideoSubTypeDescription": "None", "AttachmentSize": 0}, {"Codec": "subrip", "Language": "eng", "TimeBase": "1/1000", "DisplayTitle": "English (SUBRIP)", "DisplayLanguage": "English", "IsInterlaced": false, "IsDefault": false, "IsForced": false, "IsHearingImpaired": false, "Type": "Subtitle", "Index": 3, "IsExternal": false, "IsTextSubtitleStream": true, "SupportsExternalStream": true, "Protocol": "File", "ExtendedVideoType": "None", "ExtendedVideoSubType": "None", "ExtendedVideoSubTypeDescription": "None", "AttachmentSize": 0, "SubtitleLocationType": "InternalStream"}, {"Codec": "subrip", "Language": "eng", "TimeBase": "1/1000", "Title": "SDH", "DisplayTitle": "English (SUBRIP)", "DisplayLanguage": "English", "IsInterlaced": false, "IsDefault": false, "IsForced": false, "IsHearingImpaired": false, "Type": "Subtitle", "Index": 4, "IsExternal": false, "IsTextSubtitleStream": true, "SupportsExternalStream": true, "Protocol": "File", "ExtendedVideoType": "None", "ExtendedVideoSubType": "None", "ExtendedVideoSubTypeDescription": "None", "AttachmentSize": 0, "SubtitleLocationType": "InternalStream"}], "Formats": [], "Bitrate": 23475052, "RequiredHttpHeaders": {}, "AddApiKeyToDirectStreamUrl": false, "ReadAtNativeFramerate": false, "DefaultAudioStreamIndex": 1, "ItemId": "5439819", "MimeType": "video/x-matroska"}], "RunTimeTicks": 136983680000, "Size": 40196239080, "Bitrate": 23475052, "ProductionYear": 2001, "ProviderIds": {"Tmdb": "120", "Imdb": "tt0120737", "Tvdb": "107", "eidr": "10.5240/E030-107B-C08A-BF93-AC79-B"}, "IsFolder": false, "Type": "Movie", "MediaType": "Video"}, {"Name": "The Lord of the Rings: The Fellowship of the Ring", "Id": "5504682", "Container": "mp4", "MediaSources": [{"Chapters": [], "Protocol": "File", "Id": "mediasource_5504682", "Type": "Default", "Container": "mp4", "Size": 14643366967, "Name": "The Lord of the Rings The Fellowship of the Ring (2001) [imdb-tt0120737][tmdb-120][Bluray-1080p][AC3 5.1][x264]", "IsRemote": false, "HasMixedProtocols": false, "RunTimeTicks": 136917750000, "SupportsTranscoding": true, "SupportsDirectStream": true, "SupportsDirectPlay": true, "IsInfiniteStream": false, "RequiresOpening": false, "RequiresClosing": false, "RequiresLooping": false, "SupportsProbing": true, "MediaStreams": [{"Codec": "h264", "CodecTag": "avc1", "Language": "und", "ColorTransfer": "bt709", "ColorPrimaries": "bt709", "ColorSpace": "bt709", "TimeBase": "1/24000", "VideoRange": "SDR", "DisplayTitle": "1080p H264", "NalLengthSize": "4", "IsInterlaced": false, "BitRate": 7914172, "BitDepth": 8, "RefFrames": 1, "IsDefault": true, "IsForced": false, "IsHearingImpaired": false, "Height": 800, "Width": 1920, "AverageFrameRate": 23.976025, "RealFrameRate": 23.976025, "Profile": "High", "Type": "Video", "AspectRatio": "2.40:1", "Index": 0, "IsExternal": false, "IsTextSubtitleStream": false, "SupportsExternalStream": false, "Protocol": "File", "PixelFormat": "yuv420p", "Level": 41, "IsAnamorphic": false, "ExtendedVideoType": "None", "ExtendedVideoSubType": "None", "ExtendedVideoSubTypeDescription": "None", "AttachmentSize": 0}, {"Codec": "ac3", "CodecTag": "ac-3", "Language": "eng", "TimeBase": "1/48000", "DisplayTitle": "English AC3 5.1 (Default)", "DisplayLanguage": "English", "IsInterlaced": false, "ChannelLayout": "5.1", "BitRate": 640000, "Channels": 6, "SampleRate": 48000, "IsDefault": true, "IsForced": false, "IsHearingImpaired": false, "Type": "Audio", "Index": 1, "IsExternal": false, "IsTextSubtitleStream": false, "SupportsExternalStream": false, "Protocol": "File", "ExtendedVideoType": "None", "ExtendedVideoSubType": "None", "ExtendedVideoSubTypeDescription": "None", "AttachmentSize": 0}], "Formats": [], "Bitrate": 8556007, "RequiredHttpHeaders": {}, "AddApiKeyToDirectStreamUrl": false, "ReadAtNativeFramerate": false, "DefaultAudioStreamIndex": 1, "ItemId": "5504682", "MimeType": "video/mp4"}], "RunTimeTicks": 136917750000, "Size": 14643366967, "Bitrate": 8556007, "ProductionYear": 2001, "ProviderIds": {"Tmdb": "120", "Imdb": "tt0120737", "Tvdb": "107", "eidr": "10.5240/E030-107B-C08A-BF93-AC79-B"}, "IsFolder": false, "Type": "Movie", "MediaType": "Video"}], "TotalRecordCount": 2} \ No newline at end of file