Skip to content

Commit

Permalink
fix: Prevent _get_related_files from returning an empty list
Browse files Browse the repository at this point in the history
  • Loading branch information
bradenhilton committed Feb 15, 2025
1 parent 7be898b commit edae289
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 10 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ jobs:
fail-fast: false
matrix:
os: [ubuntu-22.04, windows-2022]
python-version: ["3.9", "3.10", "3.11", "3.12", "3.13", "pypy3.9"]
python-version: ["3.9", "3.10", "3.11", "3.12", "3.13", "pypy3.10"]
steps:
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683

Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "hatchling.build"

[project]
name = "yt-dlp-fixupmtime"
version = "1.1.1"
version = "1.1.2"
description = "A yt-dlp postprocessor plugin to set the mtime of all files to a given datetime value by key"
requires-python = ">=3.9"
license = { file = "LICENSE" }
Expand Down
19 changes: 12 additions & 7 deletions test/test_fixup_mtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,23 @@
class TestFixupMtimePP(unittest.TestCase):
def setUp(self):
self.pp = FixupMtimePP()
self.info = {"filepath": "test äةُ한汉漢なナอัй.mp4", "upload_date": "20120101"}

# Create a temporary directory of files, set their initial mtime
file_path = Path(self.info["filepath"])
self.temp_dir = tempfile.mkdtemp()
self.files = [
Path(self.temp_dir, filename) for filename in ["test.mp4", "test.jpg", "test.info.json", "test2.mp4"]
file_path,
file_path.with_suffix(""),
file_path.with_suffix(".jpg"),
file_path.with_suffix(".info.json"),
Path("test2.mp4"),
]
self.initial_mtime = 1234567890.0
for path in self.files:
path.touch()
os.utime(path, (self.initial_mtime, self.initial_mtime))

self.info = {"filepath": self.files[0], "upload_date": "20120101"}

def tearDown(self):
shutil.rmtree(self.temp_dir)

Expand Down Expand Up @@ -70,7 +74,7 @@ def test__strptime_or_none_with_valid_string_and_valid_format_code(self):

def test__get_related_files(self):
filepath = self.info.get("filepath")
expected = [path for path in self.files if path.name in ["test.mp4", "test.jpg", "test.info.json"]]
expected = list(filter(lambda fpath: not fpath.name.startswith("test2"), self.files))
self.assertCountEqual(self.pp._get_related_files(filepath), expected) # noqa: PT009

def test__get_mtime_with_existing_mtime(self):
Expand Down Expand Up @@ -102,9 +106,10 @@ def test_run(self):
self.pp._mtime_format = "%Y%m%d"
expected = datetime(year=2012, month=1, day=1, tzinfo=timezone.utc).timestamp()
self.pp.run(self.info)
for path in self.files[:3]:
assert os.path.getmtime(path) == expected
assert os.path.getmtime(self.files[3]) != expected
for file in list(filter(lambda fpath: not fpath.name.startswith("test2"), self.files)):
assert file.stat().st_mtime == expected
for file in list(filter(lambda fpath: fpath.name.startswith("test2"), self.files)):
assert file.stat().st_mtime != expected


if __name__ == "__main__":
Expand Down
12 changes: 11 additions & 1 deletion yt_dlp_plugins/postprocessor/fixup_mtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
if TYPE_CHECKING:
from collections.abc import Iterable


from yt_dlp.postprocessor.common import PostProcessor
from yt_dlp.utils import date_formats

Expand All @@ -34,7 +35,16 @@ def _strptime_or_none(self, timestamp: float | str | None, format_code: str) ->
def _get_related_files(self, filepath: Path | str) -> list[Path]:
if isinstance(filepath, str):
filepath = Path(filepath)
return [Path(path) for path in filepath.parent.glob(f"{glob.escape(filepath.stem)}.*") if path.is_file()]
directory = filepath.parent
base_name = glob.escape(filepath.stem)
files_with_extension = directory.glob(f"{base_name}.*")
files = [fpath for fpath in files_with_extension if fpath.is_file()]
file_no_extension = directory / base_name
if file_no_extension.exists() and file_no_extension.is_file():
files.append(file_no_extension)
if filepath not in files:
files.insert(0, filepath)
return files

def _get_mtime(self, filepath: Path, info: dict, mtime_key: str) -> float | int | str | None:
return os.path.getmtime(filepath) if mtime_key == "mtime" else info.get(mtime_key)
Expand Down

0 comments on commit edae289

Please sign in to comment.