Downloads can be re-created and their settings are preserved.

This commit is contained in:
Roland 2023-08-13 10:55:10 -06:00
parent 1e4644e15c
commit b9d79d5440
No known key found for this signature in database
GPG key ID: 9D6DBD3ACF11AB93
6 changed files with 49 additions and 9 deletions

View file

@ -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

View file

@ -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(

View file

@ -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():
"""

View file

@ -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:

View file

@ -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),

View file

@ -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.