This commit is contained in:
Jesse Bannon 2023-02-28 13:57:54 -08:00
parent ad0b56002c
commit c2fab59b3e
6 changed files with 55 additions and 38 deletions

View file

@ -439,7 +439,7 @@ class Downloader(DownloadArchiver, Generic[DownloaderOptionsT], ABC):
if path.endswith(".info.json")
]
for info_json_file in info_json_files:
os.remove(info_json_file)
FileHandler.delete(info_json_file)
def _extract_entry_info_with_retry(self, entry: Entry) -> Entry:
download_entry_dict = self.extract_info_with_retry(

View file

@ -140,24 +140,26 @@ def set_ffmpeg_metadata_chapters(
metadata_file.write("\n".join(lines))
metadata_file.flush()
FFMPEG.run(
[
"-i",
file_path,
"-i",
metadata_file.name,
"-map",
"0",
"-map_chapters",
"1",
"-bitexact", # for reproducibility
"-codec",
"copy",
tmp_file_path,
]
)
FileHandler.move(tmp_file_path, file_path)
os.remove(metadata_file.name)
try:
FFMPEG.run(
[
"-i",
file_path,
"-i",
metadata_file.name,
"-map",
"0",
"-map_chapters",
"1",
"-bitexact", # for reproducibility
"-codec",
"copy",
tmp_file_path,
]
)
FileHandler.move(tmp_file_path, file_path)
finally:
FileHandler.delete(metadata_file.name)
def add_ffmpeg_metadata_key_values(file_path: str, key_values: Dict[str, str]) -> None:

View file

@ -67,17 +67,18 @@ def convert_url_thumbnail(thumbnail_url: str, output_thumbnail_path: str) -> Opt
with tempfile.NamedTemporaryFile(delete=False) as thumbnail:
thumbnail.write(file.read())
os.makedirs(os.path.dirname(output_thumbnail_path), exist_ok=True)
try:
os.makedirs(os.path.dirname(output_thumbnail_path), exist_ok=True)
tmp_output_path = FFMPEG.tmp_file_path(
relative_file_path=thumbnail.name, extension="jpg"
)
FFMPEG.run(["-bitexact", "-i", thumbnail.name, tmp_output_path])
tmp_output_path = FFMPEG.tmp_file_path(
relative_file_path=thumbnail.name, extension="jpg"
)
FFMPEG.run(["-bitexact", "-i", thumbnail.name, tmp_output_path])
# Have FileHandler handle the move to a potential cross-device
FileHandler.move(tmp_output_path, output_thumbnail_path)
FileHandler.delete(tmp_output_path)
os.remove(thumbnail.name)
# Have FileHandler handle the move to a potential cross-device
FileHandler.move(tmp_output_path, output_thumbnail_path)
finally:
FileHandler.delete(tmp_output_path)
FileHandler.delete(thumbnail.name)
return True

View file

@ -1,6 +1,7 @@
import contextlib
import json
import logging
import os
import tempfile
from pathlib import Path
from typing import Any
@ -11,6 +12,7 @@ from unittest.mock import patch
import pytest
from ytdl_sub.utils.file_handler import FileHandler
from ytdl_sub.utils.logger import Logger
@ -84,9 +86,12 @@ def preset_dict_to_subscription_yaml_generator() -> Callable:
@contextlib.contextmanager
def _preset_dict_to_subscription_yaml_generator(subscription_name: str, preset_dict: Dict):
subscription_dict = {subscription_name: preset_dict}
with tempfile.NamedTemporaryFile(suffix=".yaml") as tmp_file:
with tempfile.NamedTemporaryFile(suffix=".yaml", delete=False) as tmp_file:
tmp_file.write(json.dumps(subscription_dict).encode("utf-8"))
tmp_file.flush()
try:
yield tmp_file.name
finally:
FileHandler.delete(tmp_file.name)
return _preset_dict_to_subscription_yaml_generator

View file

@ -15,7 +15,7 @@ from ytdl_sub.cli.main import main
from ytdl_sub.config.config_file import ConfigFile
from ytdl_sub.subscriptions.subscription import Subscription
from ytdl_sub.subscriptions.subscription_download import SubscriptionDownload
from ytdl_sub.utils.file_handler import FileHandlerTransactionLog
from ytdl_sub.utils.file_handler import FileHandlerTransactionLog, FileHandler
from ytdl_sub.utils.logger import Logger
from ytdl_sub.utils.yaml import load_yaml
@ -67,10 +67,13 @@ def music_video_config(music_video_config_path, working_directory) -> ConfigFile
@pytest.fixture()
def music_video_config_for_cli(music_video_config) -> str:
with tempfile.NamedTemporaryFile(suffix=".yaml") as tmp_file:
with tempfile.NamedTemporaryFile(suffix=".yaml", delete=False) as tmp_file:
tmp_file.write(json.dumps(music_video_config._value).encode("utf-8"))
tmp_file.flush()
try:
yield tmp_file.name
finally:
FileHandler.delete(tmp_file.name)
@pytest.fixture()
@ -98,10 +101,13 @@ def timestamps_file_path():
"00:01:01 Part 5\n",
]
with tempfile.NamedTemporaryFile(mode="w", encoding="utf-8", suffix=".txt") as tmp:
with tempfile.NamedTemporaryFile(mode="w", encoding="utf-8", suffix=".txt", delete=False) as tmp:
tmp.writelines(timestamps)
tmp.seek(0)
try:
yield tmp.name
finally:
FileHandler.delete(tmp.name)
def mock_run_from_cli(args: str) -> List[Tuple[Subscription, FileHandlerTransactionLog]]:

View file

@ -6,6 +6,7 @@ import pytest
from ytdl_sub.utils.exceptions import FileNotFoundException
from ytdl_sub.utils.exceptions import InvalidYamlException
from ytdl_sub.utils.file_handler import FileHandler
from ytdl_sub.utils.yaml import load_yaml
@ -28,8 +29,10 @@ def bad_yaml_file_path(bad_yaml) -> str:
tmp_file.write(bad_yaml.encode("utf-8"))
tmp_file.flush()
yield tmp_file.name
os.remove(tmp_file.name)
try:
yield tmp_file.name
finally:
FileHandler.delete(tmp_file.name)
def test_load_yaml_file_not_found():