mirror of
https://github.com/unshackle-dl/unshackle.git
synced 2026-12-05 07:25:08 +00:00
feat(dl): add --max-downloads to stop a run after N downloaded titles
This commit is contained in:
@@ -92,7 +92,7 @@ def run(monkeypatch, tmp_path):
|
||||
module, "load_tracks", lambda service, title, **k: setattr(title, "tracks", service.get_tracks(title))
|
||||
)
|
||||
monkeypatch.setattr(module.tags, "tag_file", lambda *a, **k: None)
|
||||
monkeypatch.setattr(module.MediaInfo, "parse", lambda *a, **k: SimpleNamespace())
|
||||
monkeypatch.setattr(module.MediaInfo, "parse", lambda *a, **k: SimpleNamespace(general_tracks=[]))
|
||||
monkeypatch.setattr(
|
||||
Episode,
|
||||
"get_filename",
|
||||
@@ -290,3 +290,75 @@ def test_wall_clock_step_back_prints_no_negative_elapsed_time(run, monkeypatch):
|
||||
elapsed = re.findall(r"(?:finished|downloaded|titles) in (-?[\dhms]+)", captured.get())
|
||||
assert len(elapsed) == 3
|
||||
assert all(re.fullmatch(r"0m\d+s", value) for value in elapsed), elapsed
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def get_tracks_calls(monkeypatch):
|
||||
calls = []
|
||||
get_tracks = EXAMPLE.get_tracks
|
||||
|
||||
def counted(self, title):
|
||||
calls.append(title.number)
|
||||
return get_tracks(self, title)
|
||||
|
||||
monkeypatch.setattr(EXAMPLE, "get_tracks", counted)
|
||||
return calls
|
||||
|
||||
|
||||
def test_max_downloads_counts_only_downloaded_titles_and_leaves_the_rest_untouched(run, get_tracks_calls):
|
||||
run(count=1)
|
||||
get_tracks_calls.clear()
|
||||
result = run(count=4, max_downloads=2)
|
||||
assert get_tracks_calls == [1, 2, 3]
|
||||
assert [path.stem for path in result.completed_files] == ["Example.S01E02", "Example.S01E03"]
|
||||
assert [entry["title"] for entry in result.already_present] == ["Example S01E01"]
|
||||
assert not result.skipped_titles
|
||||
get_tracks_calls.clear()
|
||||
following = run(count=4, max_downloads=2)
|
||||
assert get_tracks_calls == [1, 2, 3, 4]
|
||||
assert [path.stem for path in following.completed_files] == ["Example.S01E04"]
|
||||
|
||||
|
||||
@pytest.mark.parametrize("flags", [{}, {"skip_dl": True}, {"no_mux": True}])
|
||||
def test_max_downloads_without_the_archive_takes_the_first_titles(run, get_tracks_calls, flags):
|
||||
run(count=3, max_downloads=2, skip_existing=False, **flags)
|
||||
assert get_tracks_calls == [1, 2]
|
||||
|
||||
|
||||
def test_a_title_skipped_for_a_missing_track_does_not_count(monkeypatch, run):
|
||||
get_tracks = EXAMPLE.get_tracks
|
||||
monkeypatch.setattr(
|
||||
EXAMPLE, "get_tracks", lambda self, title: get_tracks(self, title) if title.number > 1 else Tracks()
|
||||
)
|
||||
result = run(count=3, max_downloads=2, skip_missing=True)
|
||||
assert [entry["title"] for entry in result.skipped_titles] == ["Example S01E01"]
|
||||
assert len(result.completed_files) == 2
|
||||
|
||||
|
||||
def test_a_title_that_published_no_file_does_not_count(monkeypatch, run, get_tracks_calls):
|
||||
import unshackle.commands.dl as module
|
||||
|
||||
download = module.download_tracks_in_passes
|
||||
|
||||
def lose_first_title(tracks, *a, **k):
|
||||
download(tracks, *a, **k)
|
||||
for track in tracks:
|
||||
if track.id.endswith("-1"):
|
||||
track.path.unlink()
|
||||
|
||||
monkeypatch.setattr(module, "download_tracks_in_passes", lose_first_title)
|
||||
result = run(count=3, max_downloads=2, no_mux=True)
|
||||
assert get_tracks_calls == [1, 2, 3]
|
||||
assert len(result.completed_files) == 4
|
||||
|
||||
|
||||
def test_a_season_cut_short_by_max_downloads_gets_no_season_event(monkeypatch, run):
|
||||
import unshackle.commands.dl as module
|
||||
|
||||
events = []
|
||||
monkeypatch.setattr(module, "dispatch", lambda status, scope, *a, **k: events.append(scope))
|
||||
run(count=3, max_downloads=2)
|
||||
assert events == ["file", "file", "run"]
|
||||
events.clear()
|
||||
run(count=3, max_downloads=3, skip_existing=False)
|
||||
assert events == ["file", "file", "file", "season", "run"]
|
||||
|
||||
Reference in New Issue
Block a user