Keep projects distinct within each commercial break
This commit is contained in:
@@ -3,6 +3,8 @@
|
||||
import argparse
|
||||
import collections
|
||||
import fcntl
|
||||
import heapq
|
||||
from itertools import islice
|
||||
import json
|
||||
import os
|
||||
from pathlib import Path
|
||||
@@ -58,9 +60,41 @@ def rotation(clips, rng):
|
||||
yield clip
|
||||
|
||||
|
||||
def project(clip):
|
||||
return Path(clip["externalId"]).stem.split("-", 1)[0].casefold()
|
||||
|
||||
|
||||
def short_breaks(clips, count, rng):
|
||||
groups = collections.defaultdict(list)
|
||||
for clip in islice(rotation(clips, rng), count * 2):
|
||||
groups[project(clip)].append(clip)
|
||||
|
||||
pending = []
|
||||
for name, group in groups.items():
|
||||
rng.shuffle(group)
|
||||
heapq.heappush(pending, (-len(group), rng.random(), name))
|
||||
|
||||
breaks = []
|
||||
while len(pending) >= 2:
|
||||
pair = []
|
||||
chosen = [heapq.heappop(pending), heapq.heappop(pending)]
|
||||
for remaining, _, name in chosen:
|
||||
pair.append(groups[name].pop())
|
||||
if remaining < -1:
|
||||
heapq.heappush(pending, (remaining + 1, rng.random(), name))
|
||||
rng.shuffle(pair)
|
||||
breaks.append(pair)
|
||||
if pending:
|
||||
_, _, name = pending[0]
|
||||
breaks.extend([clip] for clip in groups[name][:count - len(breaks)])
|
||||
rng.shuffle(breaks)
|
||||
return iter(breaks)
|
||||
|
||||
|
||||
def build_lineup(episodes, short, long, filler):
|
||||
rng = random.Random("|".join(clip["uuid"] for clip in short + long))
|
||||
short_cycle, long_cycle = rotation(short, rng), rotation(long, rng)
|
||||
breaks = short_breaks(short, len(episodes) * 3, rng)
|
||||
long_cycle = rotation(long, rng)
|
||||
|
||||
def ad(clip, position):
|
||||
return {"type": "filler", "id": clip["uuid"], "duration": clip["duration"],
|
||||
@@ -73,7 +107,7 @@ def build_lineup(episodes, short, long, filler):
|
||||
lineup.append(ad(next(long_cycle), "pre"))
|
||||
for index, (start, stop) in enumerate(zip(points, points[1:])):
|
||||
if index:
|
||||
lineup.extend(ad(next(short_cycle), "mid") for _ in range(2))
|
||||
lineup.extend(ad(clip, "mid") for clip in next(breaks))
|
||||
lineup.append({"type": "content", "id": episode["id"],
|
||||
"duration": stop - start, "startOffsetMs": start})
|
||||
return lineup
|
||||
@@ -120,7 +154,8 @@ def refresh(apply_active):
|
||||
if not short or not long:
|
||||
return
|
||||
|
||||
signature = [[clip["uuid"], clip["duration"]] for clip in clips]
|
||||
signature = {"distinct_projects": [[clip["uuid"], clip["duration"], project(clip)]
|
||||
for clip in clips]}
|
||||
filler = next(item for item in api("/filler-lists") if item["name"] == "Commercials")
|
||||
filler_path = f'/filler-lists/{filler["id"]}'
|
||||
members = [{"type": "content", "id": clip["uuid"], "duration": clip["duration"],
|
||||
|
||||
Reference in New Issue
Block a user