diff --git a/modules/videos/channel/test/test_models.py b/modules/videos/channel/test/test_models.py index c5a1b4c0..4edb9f0f 100644 --- a/modules/videos/channel/test/test_models.py +++ b/modules/videos/channel/test/test_models.py @@ -101,3 +101,20 @@ async def test_channel_download_relationships(test_session, download_channel): # Delete the Channel, and any relationships. download_channel.delete_with_videos() assert test_session.query(Download).count() == 0 + + +def test_channel_info_json(test_session, test_directory): + channel = Channel(name='new channel') + test_session.add(channel) + + with pytest.raises(FileNotFoundError) as e: + # Cannot get info json because directory is not defined. + channel.info_json_path + assert 'directory' in str(e) + + # Channel's directory is created if it does not exist. + channel.directory = test_directory / 'new channel' + assert channel.info_json_path == test_directory / 'new channel/new channel.info.json' + assert channel.directory.is_dir() + # info json file is not yet created. + assert not channel.info_json_path.exists() diff --git a/modules/videos/downloader.py b/modules/videos/downloader.py index 5881036e..8ce764bf 100755 --- a/modules/videos/downloader.py +++ b/modules/videos/downloader.py @@ -768,7 +768,7 @@ def update_channel_catalog(channel: Channel, info: dict): # Write the Channel's info to a JSON file. if channel.directory: - info_json_path = channel.directory / f'{channel.name}.info.json' + info_json_path = channel.info_json_path with info_json_path.open('wt') as fh: json.dump(info, fh, indent=2, sort_keys=True) logger.debug(f'Wrote channel info json to {info_json_path}') diff --git a/modules/videos/models.py b/modules/videos/models.py index aecc188f..7a926c3e 100644 --- a/modules/videos/models.py +++ b/modules/videos/models.py @@ -556,6 +556,15 @@ class Channel(ModelHelper, Base): def location(self) -> str: return f'/videos/channel/{self.id}/video' + @property + def info_json_path(self) -> pathlib.Path: + if self.directory: + if not self.directory.exists(): + self.directory.mkdir(parents=True) + return self.directory / f'{self.name}.info.json' + + raise FileNotFoundError(f'Cannot create Channel info json because directory is not defined: {self}') + def delete_with_videos(self): """Delete all Video records (but not video files) related to this Channel. Then delete the Channel.""" session = Session.object_session(self)