Phases 1-4 of plan.md §13. Forked from youtube-automate as planned rather than
written from scratch: naming, NFO, auth, the admin UI, settings and the DB layer
came across largely unchanged, download.py is gone, and the pieces that only make
sense for a streaming library are new.
api.py YouTube Data API v3 client. The whole metadata path.
strm.py Materialising: a .strm, an .nfo and a thumbnail. Replaces the
330-line download.py, because the job is writing a URL to a file.
subsync.py The subscription mirror, most of which is refusals.
reap.py Retention, rewritten around the 30-day window and min_keep_videos.
discovery.py RSS polling plus an API-backed, resumable, bounded backfill.
proxy/ The verified PoC, moved in with a systemd unit.
330 tests, all passing, no network and no yt-dlp in any of them. The suite leans
towards the failure paths, because that is where this design can actually hurt
someone: a 403 that looks like an unsubscribe, a video that ages out and comes
back, a title that never arrives. tests/test_proxy.py replaces the two standalone
scripts under proxy/ and now drives the real make_handler(mgr, ...) rather than the
PoC's make_handler(path, done), so routing and video-id validation are covered too.
Ran it end to end against the live API and it found three real bugs.
The first was mine and the tests caught it: strm.remove pruned empty directories
up to the media root, so a channel directory whose tvshow.nfo happened to be
missing would be deleted along with the season. It only looked safe because
tvshow.nfo normally stops the walk. The prune boundary is now the channel
directory explicitly.
The other two only showed up against real data, and they compounded. The backfill
inserted rows with no title and left the RSS poll to fill them in — but RSS returns
15 entries, which for Pitch Side spans 23 days against a 30-day window, so five of
twenty episodes were named after their video ids. Worse, strm.materialise wrote
that fallback back to the database as the title, which made the row look titled and
permanently disabled the repair path. Both fixed: playlistItems.list now requests
snippet as well as contentDetails, which costs the same single quota unit and
carries the title alongside the exact publish date, and the fallback is used for the
filename without being persisted. A title that does arrive late now also removes the
badly-named files and re-queues, so the episode is rewritten rather than keeping its
video-id name forever. Verified against the live API: all twenty Pitch Side episodes
now carry real titles.
Measured on the real account: 119 subscriptions queued for approval and none added
on the first sync, then a two-channel run backfilled and materialised 26 episodes in
under seven seconds.
Two deliberate departures from plan.md, both recorded there:
min_keep_videos defaults to 5 rather than being left as an open question. Without
it 52 of 117 measured channels are empty Jellyfin series that flicker in and out as
their single video crosses the retention line, and the plan already recommended it.
The Jellyfin refresh is a bare /Library/Refresh with a comment explaining why it
must stay that way. A normal scan makes zero media probes; FullRefresh does probe,
and at 400 episodes that is 400 cold starts.
Not yet done: no systemd units are installed (needs root — deploy/deploy.sh), the
admin UI has no routes for sources or the approval queue yet, and nothing has been
pointed at the real media root.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
95 lines
3.4 KiB
Python
95 lines
3.4 KiB
Python
import xml.etree.ElementTree as ET
|
|
|
|
from ytstream import nfo
|
|
|
|
HOSTILE = (
|
|
"Ampersands & angle <brackets> and \"quotes\"\n"
|
|
"control chars: \x00\x07\x1b\n"
|
|
"emoji 🎬 and em-dash — and links https://example.com/?a=1&b=2"
|
|
)
|
|
|
|
|
|
class TestEpisodeNfo:
|
|
def build(self, **overrides):
|
|
kwargs = dict(
|
|
title="Video Title",
|
|
show_title="Some Channel",
|
|
season=2026,
|
|
episode=8110,
|
|
plot="A plot.",
|
|
aired="2026-08-11",
|
|
duration_seconds=762,
|
|
video_id="dQw4w9WgXcQ",
|
|
)
|
|
kwargs.update(overrides)
|
|
return nfo.episode_nfo(**kwargs)
|
|
|
|
def test_is_well_formed_xml(self):
|
|
root = ET.fromstring(self.build())
|
|
assert root.tag == "episodedetails"
|
|
|
|
def test_hostile_description_still_parses(self):
|
|
root = ET.fromstring(self.build(plot=HOSTILE))
|
|
plot = root.findtext("plot")
|
|
assert "&" in plot and "<brackets>" in plot
|
|
assert "🎬" in plot
|
|
|
|
def test_control_characters_are_stripped(self):
|
|
plot = ET.fromstring(self.build(plot=HOSTILE)).findtext("plot")
|
|
for bad in ("\x00", "\x07", "\x1b"):
|
|
assert bad not in plot
|
|
|
|
def test_newlines_are_preserved(self):
|
|
plot = ET.fromstring(self.build(plot="one\ntwo")).findtext("plot")
|
|
assert plot == "one\ntwo"
|
|
|
|
def test_runtime_is_rounded_minutes(self):
|
|
assert ET.fromstring(self.build(duration_seconds=762)).findtext("runtime") == "13"
|
|
|
|
def test_short_video_still_gets_at_least_one_minute(self):
|
|
assert ET.fromstring(self.build(duration_seconds=20)).findtext("runtime") == "1"
|
|
|
|
def test_runtime_omitted_when_duration_unknown(self):
|
|
assert ET.fromstring(self.build(duration_seconds=None)).find("runtime") is None
|
|
|
|
def test_unique_id_marks_youtube_as_default(self):
|
|
unique = ET.fromstring(self.build()).find("uniqueid")
|
|
assert unique.get("type") == "youtube"
|
|
assert unique.get("default") == "true"
|
|
assert unique.text == "dQw4w9WgXcQ"
|
|
|
|
def test_season_and_episode_are_present(self):
|
|
root = ET.fromstring(self.build())
|
|
assert root.findtext("season") == "2026"
|
|
assert root.findtext("episode") == "8110"
|
|
|
|
def test_title_keeps_characters_that_the_filename_strips(self):
|
|
root = ET.fromstring(self.build(title="Hermitcraft S11#11: Expanding Business"))
|
|
assert root.findtext("title") == "Hermitcraft S11#11: Expanding Business"
|
|
|
|
def test_empty_plot_does_not_break(self):
|
|
assert ET.fromstring(self.build(plot=None)).find("plot") is not None
|
|
|
|
|
|
class TestTvshowNfo:
|
|
def test_well_formed_and_carries_channel_id(self):
|
|
root = ET.fromstring(nfo.tvshow_nfo("clabretro", HOSTILE, "UCabc123"))
|
|
assert root.tag == "tvshow"
|
|
assert root.findtext("title") == "clabretro"
|
|
assert root.findtext("studio") == "YouTube"
|
|
assert root.find("uniqueid").text == "UCabc123"
|
|
|
|
|
|
class TestWrite:
|
|
def test_write_is_atomic_and_leaves_no_temp_file(self, tmp_path):
|
|
target = tmp_path / "sub" / "tvshow.nfo"
|
|
nfo.write(target, b"<tvshow/>")
|
|
assert target.read_bytes() == b"<tvshow/>"
|
|
assert list(tmp_path.rglob("*.tmp")) == []
|
|
|
|
def test_overwrites_existing(self, tmp_path):
|
|
target = tmp_path / "tvshow.nfo"
|
|
nfo.write(target, b"<a/>")
|
|
nfo.write(target, b"<b/>")
|
|
assert target.read_bytes() == b"<b/>"
|