Add day parts to the shared engine, on Vortex and After Dark (0.42.0 in testing)
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -9,8 +9,8 @@ android {
|
||||
applicationId = "tv.orbit.classic"
|
||||
minSdk = 26
|
||||
targetSdk = 36
|
||||
versionCode = 42
|
||||
versionName = "0.41.0-smart-vortex"
|
||||
versionCode = 43
|
||||
versionName = "0.42.0-day-parts"
|
||||
}
|
||||
buildFeatures { compose = true }
|
||||
compileOptions { sourceCompatibility = JavaVersion.VERSION_17; targetCompatibility = JavaVersion.VERSION_17 }
|
||||
|
||||
@@ -1,5 +1,10 @@
|
||||
package tv.orbit.classic
|
||||
|
||||
/** Local hours, wrapping past midnight, and the titles a channel may use then. No titles means everything in its pool. */
|
||||
internal data class DayPart(val fromHour: Int, val toHour: Int, val titles: List<String> = emptyList()) {
|
||||
fun covers(hour: Int) = if (fromHour <= toHour) hour in fromHour until toHour else hour >= fromHour || hour < toHour
|
||||
}
|
||||
|
||||
/** What makes a channel itself. The engine below knows nothing about any particular channel. */
|
||||
internal data class ChannelProgramming(val number: Int, val name: String, val searches: List<String>, val seriesCount: Int = 3,
|
||||
/** Set for a film channel: how long before a film may air again, as long as the pool allows it. */
|
||||
@@ -7,7 +12,8 @@ internal data class ChannelProgramming(val number: Int, val name: String, val se
|
||||
/** A show that finishes waits until every show has finished, so nothing repeats while others have new episodes. */
|
||||
val finishTogether: Boolean = false,
|
||||
/** Only the exact title counts, for channels whose short names could match something unrelated. */
|
||||
val exactTitles: Boolean = false) {
|
||||
val exactTitles: Boolean = false,
|
||||
val dayParts: List<DayPart> = emptyList()) {
|
||||
/** Changes when the list does, so the episode cache is read again. What's already committed stays. */
|
||||
val signature get() = "${searches.joinToString("|")}#$seriesCount"
|
||||
}
|
||||
@@ -16,9 +22,14 @@ internal val SmartChannels = listOf(
|
||||
ChannelProgramming(102, "After Dark", listOf("The Thing", "The Evil Dead", "Re-Animator", "Hellraiser", "Videodrome", "An American Werewolf in London",
|
||||
"Halloween", "Day of the Dead", "They Live", "Escape from New York", "The Fog", "Near Dark", "The Lost Boys", "Fright Night", "Society", "Tremors",
|
||||
"Basket Case", "Phantasm", "Repo Man", "Donnie Darko", "The Wicker Man", "Suspiria", "Eraserhead", "Blue Velvet", "From Dusk Till Dawn", "Scream",
|
||||
"Poltergeist", "The Howling", "Creepshow", "Prince of Darkness"), seriesCount = 40, filmRestMs = 48 * 60 * 60_000L),
|
||||
"Poltergeist", "The Howling", "Creepshow", "Prince of Darkness"), seriesCount = 40, filmRestMs = 48 * 60 * 60_000L,
|
||||
dayParts = listOf(DayPart(21, 5), DayPart(5, 21, listOf("The 'Burbs", "'Til Death Do Us Part", "Tremors", "The Lost Boys", "Fright Night", "Poltergeist", "Scream",
|
||||
"Escape from New York", "They Live", "Repo Man", "Donnie Darko", "From Dusk Till Dawn", "Creepshow", "Near Dark", "Halloween", "The Fog", "Blue Velvet",
|
||||
"The Wicker Man", "An American Werewolf in London", "Prince of Darkness")))),
|
||||
ChannelProgramming(103, "Vortex", listOf("Dragon Ball Z", "Batman Beyond", "Superman: The Animated Series", "Naruto", "Gundam Wing", "Yu Yu Hakusho", "Samurai Jack"),
|
||||
seriesCount = 6, finishTogether = true),
|
||||
seriesCount = 6, finishTogether = true,
|
||||
dayParts = listOf(DayPart(6, 18, listOf("Batman Beyond", "Superman: The Animated Series", "Samurai Jack")),
|
||||
DayPart(18, 6, listOf("Dragon Ball Z", "Naruto", "Yu Yu Hakusho", "Gundam Wing")))),
|
||||
ChannelProgramming(140, "Terra", listOf("The Blue Planet", "Blue Planet II", "Planet Earth", "Planet Earth II", "Planet Earth III", "Frozen Planet", "Frozen Planet II",
|
||||
"Life on Earth", "The Living Planet", "The Trials of Life", "Our Planet", "Our Planet II", "Dynasties", "Dynasties II", "Seven Worlds, One Planet", "A Perfect Planet",
|
||||
"Hostile Planet", "Africa", "Wild China", "South Pacific", "Great Barrier Reef", "The Hunt", "The Life of Mammals", "The Life of Birds", "Nature's Great Events", "Green Planet", "Prehistoric Planet"),
|
||||
@@ -53,6 +64,17 @@ internal const val SCHEDULE_KEEP_PAST_MS = 24 * 60 * 60_000L
|
||||
internal fun nextEpisode(series: PoolSeries, last: EpisodeMark?): PoolEpisode =
|
||||
last?.let { mark -> series.episodes.firstOrNull { it.mark > mark } } ?: series.episodes.first()
|
||||
|
||||
/**
|
||||
* What a programme starting at [startMs] may use, by the local hour then, so the clocks changing moves blocks with the wall clock.
|
||||
* A block with nothing in the pool leaves the whole pool, so a channel never goes off air for lack of a title.
|
||||
*/
|
||||
internal fun dayPartPool(pool: List<PoolSeries>, parts: List<DayPart>, startMs: Long, zone: java.time.ZoneId): List<PoolSeries> {
|
||||
val hour = java.time.Instant.ofEpochMilli(startMs).atZone(zone).hour
|
||||
val titles = parts.firstOrNull { it.covers(hour) }?.titles.orEmpty().map(::seriesKey).toSet()
|
||||
if (titles.isEmpty()) return pool
|
||||
return pool.filter { seriesKey(it.title) in titles }.ifEmpty { pool }
|
||||
}
|
||||
|
||||
/** The next show after [last] in the rota that [eligible] allows. */
|
||||
internal fun nextInTurn(pool: List<PoolSeries>, last: String?, eligible: (PoolSeries) -> Boolean): PoolSeries {
|
||||
val after = pool.indexOfFirst { it.id == last } + 1
|
||||
@@ -70,13 +92,14 @@ internal fun nextFilm(pool: List<PoolSeries>, shown: Map<String, Long>, startMs:
|
||||
}
|
||||
|
||||
/** Films by their rest period, back to back from [fromMs] until [untilMs] is covered. */
|
||||
internal fun extendFilmSchedule(pool: List<PoolSeries>, cursor: ScheduleCursor, fromMs: Long, untilMs: Long, restMs: Long): ChannelPlan {
|
||||
internal fun extendFilmSchedule(pool: List<PoolSeries>, cursor: ScheduleCursor, fromMs: Long, untilMs: Long, restMs: Long,
|
||||
dayParts: List<DayPart> = emptyList(), zone: java.time.ZoneId = java.time.ZoneId.systemDefault()): ChannelPlan {
|
||||
val shown = cursor.filmShown.toMutableMap()
|
||||
val entries = mutableListOf<ScheduleEntry>()
|
||||
var last = cursor.lastSeriesId
|
||||
var start = fromMs
|
||||
while (start < untilMs) {
|
||||
val film = nextFilm(pool, shown, start, restMs)
|
||||
val film = nextFilm(dayPartPool(pool, dayParts, start, zone), shown, start, restMs)
|
||||
val copy = film.episodes.first()
|
||||
val item = SampleItem(film.title, "Movie", copy.durationMs / 60_000, "", copy.id, copy.durationMs)
|
||||
entries += ScheduleEntry(film.id, copy.mark, ScheduledMedia(item, copy.sourceId, copy.durationMs), start)
|
||||
@@ -88,16 +111,19 @@ internal fun extendFilmSchedule(pool: List<PoolSeries>, cursor: ScheduleCursor,
|
||||
}
|
||||
|
||||
/** Shows take turns, one episode each, back to back from [fromMs] until [untilMs] is covered. */
|
||||
internal fun extendSchedule(pool: List<PoolSeries>, cursor: ScheduleCursor, fromMs: Long, untilMs: Long, filmRestMs: Long? = null, finishTogether: Boolean = false): ChannelPlan {
|
||||
internal fun extendSchedule(pool: List<PoolSeries>, cursor: ScheduleCursor, fromMs: Long, untilMs: Long, filmRestMs: Long? = null, finishTogether: Boolean = false,
|
||||
dayParts: List<DayPart> = emptyList(), zone: java.time.ZoneId = java.time.ZoneId.systemDefault()): ChannelPlan {
|
||||
require(pool.isNotEmpty() && pool.all { it.episodes.isNotEmpty() })
|
||||
if (filmRestMs != null) return extendFilmSchedule(pool, cursor, fromMs, untilMs, filmRestMs)
|
||||
if (filmRestMs != null) return extendFilmSchedule(pool, cursor, fromMs, untilMs, filmRestMs, dayParts, zone)
|
||||
val marks = cursor.marks.toMutableMap()
|
||||
val entries = mutableListOf<ScheduleEntry>()
|
||||
var last = cursor.lastSeriesId
|
||||
var start = fromMs
|
||||
while (start < untilMs) {
|
||||
if (finishTogether && pool.all { marks[it.id] == it.episodes.last().mark }) marks.clear()
|
||||
val series = nextInTurn(pool, last) { !finishTogether || marks[it.id] != it.episodes.last().mark }
|
||||
val available = dayPartPool(pool, dayParts, start, zone)
|
||||
// Shows only wait for others in the same block, or a daytime show could stand idle for an evening one.
|
||||
if (finishTogether && available.all { marks[it.id] == it.episodes.last().mark }) available.forEach { marks.remove(it.id) }
|
||||
val series = nextInTurn(pool, last) { it in available && (!finishTogether || marks[it.id] != it.episodes.last().mark) }
|
||||
val episode = nextEpisode(series, marks[series.id])
|
||||
val media = ScheduledMedia(scheduledItem(series.title, episode), episode.sourceId, episode.durationMs)
|
||||
entries += ScheduleEntry(series.id, episode.mark, media, start)
|
||||
@@ -128,12 +154,13 @@ internal fun airedCursor(base: ScheduleCursor, entries: List<ScheduleEntry>, now
|
||||
* Only ever appends. Programmes already committed stay as they are, apart from dropping old ones.
|
||||
* With no usable plan it starts from now at the aired position.
|
||||
*/
|
||||
internal fun topUpPlan(existing: ChannelPlan?, aired: ScheduleCursor, pool: List<PoolSeries>?, nowMs: Long, filmRestMs: Long? = null, finishTogether: Boolean = false): ChannelPlan? {
|
||||
internal fun topUpPlan(existing: ChannelPlan?, aired: ScheduleCursor, pool: List<PoolSeries>?, nowMs: Long, filmRestMs: Long? = null, finishTogether: Boolean = false,
|
||||
dayParts: List<DayPart> = emptyList(), zone: java.time.ZoneId = java.time.ZoneId.systemDefault()): ChannelPlan? {
|
||||
val kept = existing?.let { plan -> plan.copy(entries = plan.entries.dropWhile { it.endMs < nowMs - SCHEDULE_KEEP_PAST_MS }) }
|
||||
if (kept != null && kept.endMs >= nowMs + SCHEDULE_TOP_UP_MS) return kept
|
||||
if (pool.isNullOrEmpty()) return kept
|
||||
val from = if (kept != null && kept.endMs > nowMs) kept.endMs else nowMs
|
||||
val added = extendSchedule(pool, existing?.tail ?: aired, from, nowMs + SCHEDULE_AHEAD_MS, filmRestMs, finishTogether)
|
||||
val added = extendSchedule(pool, existing?.tail ?: aired, from, nowMs + SCHEDULE_AHEAD_MS, filmRestMs, finishTogether, dayParts, zone)
|
||||
return ChannelPlan(kept?.entries.orEmpty() + added.entries, added.tail)
|
||||
}
|
||||
|
||||
|
||||
@@ -53,7 +53,7 @@ internal class ChannelScheduler(private val store: ScheduleStore, private val cl
|
||||
val existing = store.loadPlan(userId, number)
|
||||
val progress = store.loadProgress(userId, number)
|
||||
val base = progress?.aired ?: ScheduleCursor()
|
||||
val plan = topUpPlan(existing, base, pool?.series, now, programming.filmRestMs, programming.finishTogether) ?: throw EmbyCheckException("${programming.name} has no episodes to schedule.")
|
||||
val plan = topUpPlan(existing, base, pool?.series, now, programming.filmRestMs, programming.finishTogether, programming.dayParts) ?: throw EmbyCheckException("${programming.name} has no episodes to schedule.")
|
||||
if (plan !== existing) store.savePlan(userId, number, plan)
|
||||
store.saveProgress(userId, number, ScheduleStore.SavedProgress(progress?.enabled ?: true, airedCursor(base, plan.entries, now)))
|
||||
session.rememberScheduledItems(plan.entries.map { it.media.item })
|
||||
|
||||
@@ -41,7 +41,7 @@ internal class HttpsEmbyTransport(internal val deviceId: String = UUID.randomUUI
|
||||
connection.connectTimeout = 10_000
|
||||
connection.readTimeout = request.readTimeoutMs ?: if (request.path == "/Sessions/Logout") 5_000 else 15_000
|
||||
connection.setRequestProperty("Accept", "application/json")
|
||||
connection.setRequestProperty("X-Emby-Authorization", "Emby Client=\"Orbit TV\", Device=\"Orbit TV\", DeviceId=\"$deviceId\", Version=\"0.41.0\"")
|
||||
connection.setRequestProperty("X-Emby-Authorization", "Emby Client=\"Orbit TV\", Device=\"Orbit TV\", DeviceId=\"$deviceId\", Version=\"0.42.0\"")
|
||||
request.token?.let { connection.setRequestProperty("X-Emby-Token", it) }
|
||||
if (request.method == "POST") {
|
||||
val bytes = request.body.orEmpty().toByteArray(Charsets.UTF_8)
|
||||
|
||||
@@ -33,7 +33,7 @@ internal class SecureMediaDataSource(private val session: EmbySampleSession) : B
|
||||
next.instanceFollowRedirects = false
|
||||
next.connectTimeout = 10_000
|
||||
next.readTimeout = 20_000
|
||||
next.setRequestProperty("User-Agent", "OrbitTV/0.41.0")
|
||||
next.setRequestProperty("User-Agent", "OrbitTV/0.42.0")
|
||||
next.setRequestProperty("Accept-Encoding", "identity")
|
||||
session.mediaHeaders(address).forEach { (key, value) -> next.setRequestProperty(key, value) }
|
||||
if (dataSpec.position != 0L || dataSpec.length != C.LENGTH_UNSET.toLong()) {
|
||||
|
||||
@@ -342,4 +342,67 @@ class ChannelScheduleTest {
|
||||
file.writeText(JSONObject(file.readText()).apply { remove("signature") }.toString())
|
||||
assertEquals("", old.loadPool(user, 140)!!.signature)
|
||||
}
|
||||
|
||||
private val london = java.time.ZoneId.of("Europe/London")
|
||||
private fun localHour(ms: Long) = java.time.Instant.ofEpochMilli(ms).atZone(london).hour
|
||||
private fun at(y: Int, m: Int, d: Int, h: Int) = java.time.ZonedDateTime.of(y, m, d, h, 0, 0, 0, london).toInstant().toEpochMilli()
|
||||
private val cartoons = show("Cartoon", 3, 40, 22)
|
||||
private val anime = show("Anime", 9, 33, 24)
|
||||
private val parts = listOf(DayPart(6, 18, listOf("Show Cartoon")), DayPart(18, 6, listOf("Show Anime")))
|
||||
|
||||
@Test fun aDayPartCoversItsHoursAcrossMidnight() {
|
||||
assertTrue(DayPart(18, 6).covers(23)); assertTrue(DayPart(18, 6).covers(5)); assertFalse(DayPart(18, 6).covers(6))
|
||||
assertTrue(DayPart(6, 18).covers(6)); assertFalse(DayPart(6, 18).covers(18))
|
||||
}
|
||||
|
||||
@Test fun programmesStartOnlyInTheirOwnBlock() {
|
||||
val start = at(2026, 11, 2, 7)
|
||||
extendSchedule(listOf(cartoons, anime), ScheduleCursor(), start, start + 7 * day, dayParts = parts, zone = london).entries.forEach {
|
||||
assertEquals(if (localHour(it.startMs) in 6 until 18) "Cartoon" else "Anime", it.seriesId)
|
||||
}
|
||||
}
|
||||
|
||||
@Test fun aProgrammeMayRunPastTheBlockButNothingOverlaps() {
|
||||
val start = at(2026, 11, 2, 7)
|
||||
val entries = extendSchedule(listOf(cartoons, anime), ScheduleCursor(), start, start + 2 * day, dayParts = parts, zone = london).entries
|
||||
assertTrue(entries.zipWithNext().all { (a, b) -> a.endMs == b.startMs })
|
||||
}
|
||||
|
||||
@Test fun blocksFollowTheWallClockWhenTheClocksChange() {
|
||||
for (start in listOf(at(2026, 10, 24, 12), at(2027, 3, 27, 12))) {
|
||||
extendSchedule(listOf(cartoons, anime), ScheduleCursor(), start, start + 3 * day, dayParts = parts, zone = london).entries.forEach {
|
||||
assertEquals(if (localHour(it.startMs) in 6 until 18) "Cartoon" else "Anime", it.seriesId)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test fun eachShowKeepsItsPlaceAcrossBlocks() {
|
||||
val start = at(2026, 11, 2, 7)
|
||||
val entries = extendSchedule(listOf(show("Cartoon", 9, 40, 22), anime), ScheduleCursor(), start, start + 7 * day, dayParts = parts, zone = london).entries
|
||||
entries.groupBy { it.seriesId }.values.forEach { shows -> assertEquals(shows.map { it.mark }.sorted(), shows.map { it.mark }) }
|
||||
}
|
||||
|
||||
@Test fun aBlockWithNoTitlesInThePoolUsesEverythingRatherThanGoingOffAir() {
|
||||
val noMatch = listOf(DayPart(0, 12, listOf("Not In The Pool")))
|
||||
assertEquals(2, dayPartPool(listOf(cartoons, anime), noMatch, at(2026, 11, 2, 7), london).size)
|
||||
assertEquals(2, dayPartPool(listOf(cartoons, anime), listOf(DayPart(0, 12)), at(2026, 11, 2, 7), london).size)
|
||||
}
|
||||
|
||||
@Test fun lateOnlyFilmsNeverStartBeforeTheLateBlock() {
|
||||
val day = (1..20).map { film("Day$it") }
|
||||
val late = (1..13).map { film("Late$it") }
|
||||
val filmParts = listOf(DayPart(21, 5), DayPart(5, 21, day.map { it.title }))
|
||||
val start = at(2026, 11, 2, 12)
|
||||
val entries = extendSchedule(day + late, ScheduleCursor(), start, start + 7 * this.day, rest, dayParts = filmParts, zone = london).entries
|
||||
entries.filter { it.seriesId.startsWith("Late") }.forEach { assertFalse(localHour(it.startMs) in 5 until 21) }
|
||||
assertTrue(entries.any { it.seriesId.startsWith("Late") })
|
||||
}
|
||||
|
||||
@Test fun finishedShowsOnlyWaitWithinTheirOwnBlock() {
|
||||
val short = show("s", 1, 1, 60); val other = show("o", 1, 200, 60)
|
||||
val noon = at(2026, 11, 2, 12)
|
||||
val entries = extendSchedule(listOf(short, other), ScheduleCursor(), noon, noon + 12 * 60 * minute, finishTogether = true,
|
||||
dayParts = listOf(DayPart(6, 18, listOf("Show s")), DayPart(18, 6, listOf("Show o"))), zone = london).entries
|
||||
assertEquals(List(6) { "s" }, entries.filter { localHour(it.startMs) in 6 until 18 }.map { it.seriesId })
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user