From b9d79d54407d05cc47ca6d676599e40338e93402 Mon Sep 17 00:00:00 2001 From: Roland Date: Sun, 13 Aug 2023 10:55:10 -0600 Subject: [PATCH] Downloads can be re-created and their settings are preserved. --- modules/videos/common.py | 23 +++++++++++++++++------ modules/videos/downloader.py | 2 +- modules/videos/test/test_common.py | 5 ++++- wrolpi/downloader.py | 3 ++- wrolpi/status.py | 3 +++ wrolpi/test/test_downloader.py | 22 ++++++++++++++++++++++ 6 files changed, 49 insertions(+), 9 deletions(-) diff --git a/modules/videos/common.py b/modules/videos/common.py index 646031b2..207d919d 100644 --- a/modules/videos/common.py +++ b/modules/videos/common.py @@ -136,15 +136,24 @@ def ffmpeg_poster(video_path: Path, poster_path: Path, seconds: int): def ffmpeg_video_complete(video_path: Path, seconds: int = None) -> bool: - """Checks if video file is complete by taking screenshot from the end of the video.""" - seconds = seconds or get_video_duration(video_path) - 5 + """Checks if video file is complete by taking screenshot from the end of the video. + + @raise FileNotFoundError: raised if the video file does not exist. + """ + if not video_path.is_file(): + raise FileNotFoundError(f'Video file not found: {video_path}') + with tempfile.NamedTemporaryFile(suffix='.jpg', delete=False) as fh: path = Path(fh.name) path.unlink() try: + seconds = seconds or get_video_duration(video_path) - 5 ffmpeg_poster(video_path, path, seconds) return True - except Exception: + except subprocess.CalledProcessError: + return False + except TypeError: + # "N/A" duration. return False finally: if path.is_file(): @@ -234,7 +243,7 @@ async def ffprobe_json(video_path: Union[Path, str]) -> dict: return content -def get_video_duration(video_path: Path) -> int: +def get_video_duration(video_path: Path) -> Optional[int]: """Get the duration of a video in seconds. Do this using ffprobe.""" if not isinstance(video_path, Path): video_path = Path(video_path) @@ -252,8 +261,10 @@ def get_video_duration(video_path: Path) -> int: logger.warning(f'FFPROBE failed to get duration with stdout: {e.stdout.decode()}') logger.warning(f'FFPROBE failed to get duration with stderr: {e.stderr.decode()}') raise - stdout = proc.stdout.decode() - duration = int(Decimal(stdout.strip())) + stdout = proc.stdout.decode().strip() + if stdout == 'N/A': + return None + duration = int(Decimal(stdout)) return duration diff --git a/modules/videos/downloader.py b/modules/videos/downloader.py index a207a0f7..3df14ba7 100755 --- a/modules/videos/downloader.py +++ b/modules/videos/downloader.py @@ -325,7 +325,7 @@ class VideoDownloader(Downloader, ABC): if name not in existing_names: video.add_tag(name) - # Check that video is valid. + # Check that video has both audio and video streams. await video.get_ffprobe_json() if not video.get_streams_by_codec_type('video'): return DownloadResult( diff --git a/modules/videos/test/test_common.py b/modules/videos/test/test_common.py index 01d492a1..f59266ab 100644 --- a/modules/videos/test/test_common.py +++ b/modules/videos/test/test_common.py @@ -21,7 +21,7 @@ def test_get_absolute_media_path(): assert str(path).endswith('videos') -def test_get_video_duration(test_directory): +def test_get_video_duration(test_directory, corrupted_video_file): """ Video duration can be retrieved from the video file. """ @@ -36,6 +36,9 @@ def test_get_video_duration(test_directory): empty_file.touch() get_video_duration(empty_file) + with pytest.raises(subprocess.CalledProcessError): + get_video_duration(corrupted_video_file) + def test_convert_image(): """ diff --git a/wrolpi/downloader.py b/wrolpi/downloader.py index 05187c43..2d31341f 100644 --- a/wrolpi/downloader.py +++ b/wrolpi/downloader.py @@ -516,6 +516,7 @@ class DownloadManager: download = Download(url=url, status='new') session.add(download) session.flush() + print(f'{download.settings=}') download.manager = self return download @@ -545,7 +546,7 @@ class DownloadManager: download.renew(reset_attempts=reset_attempts) download.downloader = downloader_name download.sub_downloader = sub_downloader_name - download.settings = settings + download.settings = settings if settings is not None else download.settings downloads.append(download) try: diff --git a/wrolpi/status.py b/wrolpi/status.py index d55f0290..591988a9 100755 --- a/wrolpi/status.py +++ b/wrolpi/status.py @@ -115,6 +115,9 @@ def get_cpu_info_psutil() -> CPUInfo: high_temperature = int(temp.get(name)[0].high or 0) critical_temperature = int(temp.get(name)[0].critical or 0) + if high_temperature and high_temperature == critical_temperature: + high_temperature = critical_temperature - 25 + info = CPUInfo( cores=psutil.cpu_count(logical=True), cur_frequency=int(cpu_freq.current), diff --git a/wrolpi/test/test_downloader.py b/wrolpi/test/test_downloader.py index e2365ef6..9cdfa8be 100644 --- a/wrolpi/test/test_downloader.py +++ b/wrolpi/test/test_downloader.py @@ -84,6 +84,28 @@ async def test_create_downloads(test_session, test_download_manager, test_downlo assert_download_urls({'https://example.com/1', 'https://example.com/2'}) +@pytest.mark.asyncio +async def test_recreate_download(test_session, test_download_manager, test_downloader, tag_factory): + """Settings are preserved when a Download is restarted.""" + tag = tag_factory() + settings = {'tag_names': [tag.name, ]} + + # Create download with settings initially. + test_download_manager.create_downloads(['https://example.com/1', ], test_downloader.name, settings=settings) + download: Download = test_session.query(Download).one() + assert download.settings == settings + + # Restarting download does not remove settings. + test_download_manager.create_downloads(['https://example.com/1', ], test_downloader.name, settings=None) + download: Download = test_session.query(Download).one() + assert download.settings == settings + + # Settings can be overwritten when not None. + test_download_manager.create_downloads(['https://example.com/1', ], test_downloader.name, settings=dict()) + download: Download = test_session.query(Download).one() + assert download.settings == dict() + + def test_downloader_must_have_name(): """ A Downloader class must have a name.