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>
129 lines
4.7 KiB
Python
129 lines
4.7 KiB
Python
from datetime import date
|
|
|
|
import pytest
|
|
|
|
from ytstream import naming
|
|
|
|
|
|
class TestSanitise:
|
|
@pytest.mark.parametrize(
|
|
"raw, expected",
|
|
[
|
|
("Hermitcraft S11#11: Expanding Business", "Hermitcraft S11#11 Expanding Business"),
|
|
("A/B", "A B"),
|
|
('Say "hello" <now>', "Say hello now"),
|
|
("path\\to\\thing", "path to thing"),
|
|
("what? really * | yes", "what really yes"),
|
|
(" .leading and trailing. ", "leading and trailing"),
|
|
("line one\nline two", "line one line two"),
|
|
("tabs\tand\r\nnewlines", "tabs and newlines"),
|
|
],
|
|
)
|
|
def test_removes_illegal_characters(self, raw, expected):
|
|
assert naming.sanitize_component(raw) == expected
|
|
|
|
def test_empty_input_is_empty(self):
|
|
assert naming.sanitize_component("") == ""
|
|
assert naming.sanitize_component(None) == ""
|
|
|
|
def test_title_that_is_only_illegal_characters_collapses_to_empty(self):
|
|
assert naming.sanitize_component("///???") == ""
|
|
|
|
def test_truncates_to_120_characters(self):
|
|
long = "word " * 60
|
|
result = naming.sanitize_component(long)
|
|
assert len(result) <= naming.MAX_TITLE_LEN
|
|
|
|
def test_truncation_prefers_a_word_boundary(self):
|
|
text = "alpha bravo charlie delta echo foxtrot golf hotel india juliet " * 3
|
|
result = naming.sanitize_component(text)
|
|
assert not result.endswith(" ")
|
|
# Should not cut mid-word when a boundary is available late enough.
|
|
assert result == result.rstrip()
|
|
assert " " in result
|
|
|
|
def test_hard_cuts_when_no_late_word_boundary_exists(self):
|
|
text = "a" + "b" * 400
|
|
result = naming.sanitize_component(text)
|
|
assert len(result) == naming.MAX_TITLE_LEN
|
|
|
|
|
|
class TestEpisodeNumbering:
|
|
@pytest.mark.parametrize(
|
|
"day, ordinal, expected",
|
|
[
|
|
(date(2026, 8, 11), 0, 8110),
|
|
(date(2026, 8, 11), 1, 8111),
|
|
(date(2026, 8, 12), 0, 8120),
|
|
(date(2026, 1, 1), 0, 1010),
|
|
(date(2026, 12, 31), 0, 12310),
|
|
(date(2026, 12, 31), 9, 12319),
|
|
],
|
|
)
|
|
def test_episode_number(self, day, ordinal, expected):
|
|
assert naming.episode_number(day, ordinal) == expected
|
|
|
|
def test_ordinal_is_clamped_at_nine(self):
|
|
assert naming.episode_number(date(2026, 8, 11), 12) == 8119
|
|
|
|
def test_negative_ordinal_clamps_to_zero(self):
|
|
assert naming.episode_number(date(2026, 8, 11), -3) == 8110
|
|
|
|
def test_numbers_sort_chronologically_across_the_year(self):
|
|
days = [date(2026, 1, 1), date(2026, 6, 15), date(2026, 8, 11), date(2026, 12, 31)]
|
|
numbers = [naming.episode_number(day, 0) for day in days]
|
|
assert numbers == sorted(numbers)
|
|
|
|
def test_episode_range_covers_ten_slots(self):
|
|
low, high = naming.episode_range(date(2026, 8, 11))
|
|
assert (low, high) == (8110, 8119)
|
|
|
|
def test_season_is_the_upload_year(self):
|
|
assert naming.season_for(date(2026, 8, 11)) == 2026
|
|
|
|
|
|
class TestParseUploadDate:
|
|
def test_accepts_ytdlp_compact_form(self):
|
|
assert naming.parse_upload_date("20260811") == date(2026, 8, 11)
|
|
|
|
def test_accepts_iso_form(self):
|
|
assert naming.parse_upload_date("2026-08-11") == date(2026, 8, 11)
|
|
|
|
def test_accepts_iso_timestamp(self):
|
|
assert naming.parse_upload_date("2026-08-11T12:00:00+00:00") == date(2026, 8, 11)
|
|
|
|
def test_passes_through_a_date(self):
|
|
assert naming.parse_upload_date(date(2026, 8, 11)) == date(2026, 8, 11)
|
|
|
|
def test_rejects_nonsense(self):
|
|
with pytest.raises(ValueError):
|
|
naming.parse_upload_date("not a date")
|
|
|
|
|
|
class TestBasename:
|
|
def test_includes_video_id_for_uniqueness(self):
|
|
stem = naming.basename("clabretro", 2026, 8110, "A Title", "dQw4w9WgXcQ")
|
|
assert stem == "clabretro - S2026E8110 - A Title [dQw4w9WgXcQ]"
|
|
|
|
def test_episode_is_unpadded_to_match_the_nfo(self):
|
|
stem = naming.basename("c", 2026, 8110, "t", "id")
|
|
assert "S2026E8110" in stem
|
|
assert "E08110" not in stem
|
|
|
|
def test_falls_back_to_video_id_when_the_title_sanitises_away(self):
|
|
stem = naming.basename("c", 2026, 8110, "///", "dQw4w9WgXcQ")
|
|
assert stem.endswith("dQw4w9WgXcQ [dQw4w9WgXcQ]")
|
|
|
|
def test_two_videos_same_title_differ_by_id(self):
|
|
one = naming.basename("c", 2026, 8110, "Same", "aaaaaaaaaaa")
|
|
two = naming.basename("c", 2026, 8111, "Same", "bbbbbbbbbbb")
|
|
assert one != two
|
|
|
|
|
|
class TestChannelDirName:
|
|
def test_sanitises_the_title(self):
|
|
assert naming.channel_dir_name("Tom / Jerry", "UCabc") == "Tom Jerry"
|
|
|
|
def test_falls_back_to_channel_id_when_title_is_unusable(self):
|
|
assert naming.channel_dir_name("???", "UCabc") == "UCabc"
|