diff --git a/modules/videos/channel/lib.py b/modules/videos/channel/lib.py index c22cdf4a..76a56132 100644 --- a/modules/videos/channel/lib.py +++ b/modules/videos/channel/lib.py @@ -56,7 +56,9 @@ COD = Union[dict, Channel] def get_channel(session: Session, *, channel_id: int = None, source_id: str = None, url: str = None, directory: str = None, name: str = None, return_dict: bool = True) -> COD: """ - Attempt to find a Channel using the provided params. The params are in order of reliability. + Attempt to find a Channel using the provided params. The params are in order of reliability, except that + directory beats source_id/url: a Video's files must live in their Channel's directory, so the directory a file + is in reflects the user's intent and beats yt-dlp metadata. Raises UnknownChannel if no channel is found. """ @@ -65,18 +67,6 @@ def get_channel(session: Session, *, channel_id: int = None, source_id: str = No # Always eagerly load Collection to ensure name/directory/tag_id are available if channel_id: channel = session.query(Channel).options(joinedload(Channel.collection)).filter_by(id=channel_id).one_or_none() - if not channel and source_id: - channel = session.query(Channel).options(joinedload(Channel.collection)).filter_by( - source_id=source_id).one_or_none() - if not channel and url: - channel = session.query(Channel).options(joinedload(Channel.collection)).filter_by(url=url).one_or_none() - if not channel and url: - # Try to extract YouTube channel ID from the URL and match by source_id. - from modules.videos.common import get_youtube_channel_id - yt_channel_id = get_youtube_channel_id(url) - if yt_channel_id: - channel = session.query(Channel).options(joinedload(Channel.collection)).filter_by( - source_id=yt_channel_id).one_or_none() if not channel and directory: directory = Path(directory) if not directory.is_absolute(): @@ -93,6 +83,18 @@ def get_channel(session: Session, *, channel_id: int = None, source_id: str = No if channel: break current = current.parent + if not channel and source_id: + channel = session.query(Channel).options(joinedload(Channel.collection)).filter_by( + source_id=source_id).one_or_none() + if not channel and url: + channel = session.query(Channel).options(joinedload(Channel.collection)).filter_by(url=url).one_or_none() + if not channel and url: + # Try to extract YouTube channel ID from the URL and match by source_id. + from modules.videos.common import get_youtube_channel_id + yt_channel_id = get_youtube_channel_id(url) + if yt_channel_id: + channel = session.query(Channel).options(joinedload(Channel.collection)).filter_by( + source_id=yt_channel_id).one_or_none() if not channel and name: channel = session.query(Channel).join(Collection).filter(Collection.name == name).one_or_none() diff --git a/modules/videos/channel/test/test_lib.py b/modules/videos/channel/test/test_lib.py index d8e15dfb..4797f380 100644 --- a/modules/videos/channel/test/test_lib.py +++ b/modules/videos/channel/test/test_lib.py @@ -65,6 +65,43 @@ def test_get_channel(test_session, test_directory, channel_factory): assert lib.get_channel(test_session, **p) +def test_get_channel_directory_beats_source_id(test_session, test_directory, channel_factory): + """A Channel owning the directory wins over a Channel matching source_id/url. + + A Video's files must live in their Channel's directory, so the directory a file is in reflects the user's + intent and beats yt-dlp metadata.""" + source_id_channel = channel_factory(source_id='the source id', url='https://example.com/source-channel') + directory_channel = channel_factory(directory=test_directory / 'directory channel') + + # Both source_id and directory match different Channels; the directory's Channel wins. + channel = lib.get_channel( + test_session, + source_id='the source id', + url='https://example.com/source-channel', + directory=str(test_directory / 'directory channel'), + return_dict=False, + ) + assert channel == directory_channel + + # A subdirectory of the Channel's directory also wins. + channel = lib.get_channel( + test_session, + source_id='the source id', + directory=str(test_directory / 'directory channel/2024'), + return_dict=False, + ) + assert channel == directory_channel + + # source_id still matches when the directory does not belong to any Channel. + channel = lib.get_channel( + test_session, + source_id='the source id', + directory=str(test_directory / 'not a channel directory'), + return_dict=False, + ) + assert channel == source_id_channel + + @pytest.mark.asyncio async def test_channels_no_url(async_client, test_session, test_directory, test_channels_config): """Test that a Channel's URL is coerced to None if it is empty.""" diff --git a/modules/videos/test/test_lib.py b/modules/videos/test/test_lib.py index cdd2070d..b4b1f0d4 100644 --- a/modules/videos/test/test_lib.py +++ b/modules/videos/test/test_lib.py @@ -126,6 +126,33 @@ def test_validate_video(test_session, test_directory, video_factory, image_bytes assert {i.suffix for i in vid2.file_group.my_paths()} == {'.jpg', '.png', '.mp4'} +def test_validate_video_directory_beats_info_json_channel(test_session, test_directory, channel_factory, + video_factory): + """A Video in a Channel's directory belongs to that Channel, even when its info json names another + existing Channel. + + Replicates a user report: videos placed in Channel B's directory did not appear on Channel B's page + because their info json named Channel A (which exists in the library), and the source_id match + was preferred over the directory.""" + channel_a = channel_factory(source_id='channel_a_id', url='https://example.com/channel_a') + channel_b = channel_factory(directory=test_directory / 'videos/ChannelB') + + video = video_factory( + with_video_file=channel_b.directory / 'The Title.mp4', + with_info_json={ + 'channel_id': 'channel_a_id', + 'channel_url': 'https://example.com/channel_a', + 'duration': 5, + 'epoch': 12345678, + 'title': 'The Title', + }, + ) + test_session.commit() + + assert video.channel_id == channel_b.id, \ + f'Video in ChannelB directory should belong to ChannelB, got channel_id={video.channel_id}' + + @pytest.mark.asyncio async def test_get_statistics(test_session, video_factory, channel_factory): # Can get statistics in empty DB.