ytdl-sub/tests/e2e/youtube/test_playlist.py
Jesse Bannon 309f259464
[FEATURE] Ability to migrate download archive files (#751)
With the recent push to 'beautify subscriptions' (see https://github.com/jmbannon/ytdl-sub/releases/tag/2023.10.02), we need the ability to change subscription names from their legacy form:
```
rick_a:
  preset:
    - "tv_show"
  overrides:
    tv_show_name: "Rick A"
    url: "https://www.youtube.com/channel/UCuAXFkgsw1L7xaCfnd5JJOw"
```
into:
```
tv_show:
  "Rick A": "https://www.youtube.com/channel/UCuAXFkgsw1L7xaCfnd5JJOw"
```

This however has implications from ytdl-sub's legacy download archive naming. By default, we write archives to `.ytdl-sub-{subscription_name}-download-archive.json`. If we change the subscription name, the archive will not be found, causing a complete redownload. This new feature gives us the ability to migrate download archives to a new naming schema.

# Migrating to Beautified Subscriptions
## Step 0
BACK UP ALL CONFIG + SUBSCRIPTION FILES!!!!! If something goes wrong, restore your backup and try again and/or ask for help.

## Step 1
Since we know we'll be changing our `subscripion_name` to the value of `tv_show_name`, we can use that in our newly migrated download archive name by setting this within the `tv_show` preset (or whatever your 'base' preset is).
```
presets:
  tv_show:
    output_options:
      migrated_download_archive_name: ".{tv_show_name_sanitized}-download-archive.json" 
```

## Step 2
Perform a download as usual, via `ytdl-sub sub ...`. This will load the old archive, and save it into the new archive. You should see `MIGRATION DETECTED` within the logs. Ensure it completes successfully.

Perform another download invocation and ensure you see the `MIGRATION SUCCESSFUL` within the logs.

## Step 3
Now we can set:
```
presets:
  tv_show:
    output_options:
      # rename migrated_download_archive_name to just download_archive_name
      download_archive_name: ".{tv_show_name_sanitized}-download-archive.json" 

  overrides:
    tv_show_name: "{subscription_name}"
    url: "{subscription_value}"
```
Our download archives now default to our new format, and we set `tv_show_name` + `url` to use subscription values by default.

## Step 4
We can now beautify our subscription.yaml file to:
```
tv_show:
  "Rick A": "https://www.youtube.com/channel/UCuAXFkgsw1L7xaCfnd5JJOw"
```
2023-10-03 16:08:14 -07:00

197 lines
7.1 KiB
Python

from pathlib import Path
from typing import Dict
import pytest
from conftest import assert_logs
from e2e.conftest import mock_run_from_cli
from expected_download import assert_expected_downloads
from expected_transaction_log import assert_transaction_log_matches
from mergedeep import mergedeep
from ytdl_sub.config.config_file import ConfigFile
from ytdl_sub.downloaders.ytdlp import YTDLP
from ytdl_sub.subscriptions.subscription import Subscription
@pytest.fixture
def playlist_preset_dict(output_directory):
return {
"preset": [
"jellyfin_tv_show_collection",
"season_by_collection__episode_by_year_month_day",
"collection_season_1",
],
"format": "worst[ext=mp4]", # download the worst format so it is fast
"output_directory_nfo_tags": {
"nfo_name": "tvshow.nfo",
"nfo_root": "test",
"tags": {
"playlist_title": "{playlist_title}",
"playlist_uploader": "{playlist_uploader}",
"playlist_description": "{playlist_description}",
},
},
"nfo_tags": {
"tags": {
"playlist_index": "{playlist_index}",
"playlist_count": "{playlist_count}",
}
},
"subtitles": {
"subtitles_name": "{episode_file_path}.{lang}.{subtitles_ext}",
"allow_auto_generated_subtitles": True,
},
"overrides": {
"tv_show_name": "JMC",
"tv_show_directory": output_directory,
"collection_season_1_url": "https://youtube.com/playlist?list=PL5BC0FC26BECA5A35",
"collection_season_1_name": "JMC - Season 1",
},
}
class TestPlaylist:
"""
Downloads my old minecraft youtube channel, pretends they are music videos. Ensure the above
files exist and have the expected md5 file hashes.
"""
@classmethod
def _ensure_subscription_migrates(
cls,
config: ConfigFile,
subscription_name: str,
subscription_dict: Dict,
output_directory: Path,
):
# Ensure download archive migrates
mergedeep.merge(
subscription_dict,
{
"output_options": {
"migrated_download_archive_name": ".ytdl-sub-{tv_show_name_sanitized}-download-archive.json"
}
},
)
migrated_subscription = Subscription.from_dict(
config=config,
preset_name=subscription_name,
preset_dict=subscription_dict,
)
transaction_log = migrated_subscription.download()
assert_transaction_log_matches(
output_directory=output_directory,
transaction_log=transaction_log,
transaction_log_summary_file_name="youtube/test_playlist_archive_migrated.txt",
)
assert_expected_downloads(
output_directory=output_directory,
dry_run=False,
expected_download_summary_file_name="youtube/test_playlist_archive_migrated.json",
)
# Ensure no changes after migration
transaction_log = migrated_subscription.download()
assert transaction_log.is_empty
assert_expected_downloads(
output_directory=output_directory,
dry_run=False,
expected_download_summary_file_name="youtube/test_playlist_archive_migrated.json",
)
@pytest.mark.parametrize("dry_run", [True, False])
def test_playlist_download(
self,
music_video_config,
playlist_preset_dict,
output_directory,
dry_run,
):
playlist_subscription = Subscription.from_dict(
config=music_video_config,
preset_name="music_video_playlist_test",
preset_dict=playlist_preset_dict,
)
transaction_log = playlist_subscription.download(dry_run=dry_run)
assert_transaction_log_matches(
output_directory=output_directory,
transaction_log=transaction_log,
transaction_log_summary_file_name="youtube/test_playlist.txt",
)
assert_expected_downloads(
output_directory=output_directory,
dry_run=dry_run,
expected_download_summary_file_name="youtube/test_playlist.json",
)
# Ensure another invocation will hit ExistingVideoReached
if not dry_run:
with assert_logs(
logger=YTDLP.logger,
expected_message="ExistingVideoReached, stopping additional downloads",
log_level="debug",
):
transaction_log = playlist_subscription.download()
assert transaction_log.is_empty
assert_expected_downloads(
output_directory=output_directory,
dry_run=dry_run,
expected_download_summary_file_name="youtube/test_playlist.json",
)
self._ensure_subscription_migrates(
config=music_video_config,
subscription_name="music_video_playlist_test",
subscription_dict=playlist_preset_dict,
output_directory=output_directory,
)
@pytest.mark.parametrize("dry_run", [True, False])
def test_playlist_download_from_cli_sub(
self,
preset_dict_to_subscription_yaml_generator,
music_video_config_for_cli,
playlist_preset_dict,
output_directory,
dry_run,
):
with preset_dict_to_subscription_yaml_generator(
subscription_name="music_video_playlist_test", preset_dict=playlist_preset_dict
) as subscription_path:
args = "--dry-run " if dry_run else ""
args += f"--config {music_video_config_for_cli} "
args += f"sub {subscription_path}"
subscription_transaction_log = mock_run_from_cli(args=args)
assert len(subscription_transaction_log) == 1
transaction_log = subscription_transaction_log[0][1]
assert_transaction_log_matches(
output_directory=output_directory,
transaction_log=transaction_log,
transaction_log_summary_file_name="youtube/test_playlist.txt",
)
assert_expected_downloads(
output_directory=output_directory,
dry_run=dry_run,
expected_download_summary_file_name="youtube/test_playlist.json",
)
if not dry_run:
# Ensure another invocation will hit ExistingVideoReached
with assert_logs(
logger=YTDLP.logger,
expected_message="ExistingVideoReached, stopping additional downloads",
log_level="debug",
):
transaction_log = mock_run_from_cli(args=args)[0][1]
assert transaction_log.is_empty
assert_expected_downloads(
output_directory=output_directory,
dry_run=dry_run,
expected_download_summary_file_name="youtube/test_playlist.json",
)