Coverage for downtify/release_files.py: 85%

26 statements  

« prev     ^ index     » next       coverage.py v7.17.4, created at 2026-11-21 22:18 +0000

1"""Validate an on-disk track against a monitored release, not only its ID.""" 

2 

3from __future__ import annotations 

4 

5from pathlib import Path 

6from typing import Any 

7 

8from .file_naming import file_name_key 

9from .library_metadata import read_audio_metadata 

10 

11 

12def matches_release(path: Path, song: dict[str, Any]) -> bool: 

13 """Compare release tags and duration without modifying the file.""" 

14 if not path.is_file() or path.is_symlink(): 

15 return False 

16 actual = read_audio_metadata(path) 

17 if float(actual.get('duration') or 0) <= 0: 

18 return False 

19 for wanted, found in ( 

20 (song.get('name'), actual.get('title')), 

21 (song.get('album_name'), actual.get('album')), 

22 ): 

23 if not wanted or file_name_key(wanted) != file_name_key(found or ''): 

24 return False 

25 artists = song.get('artists') or [] 

26 if artists: 

27 found = {file_name_key(a) for a in actual.get('artists') or []} 

28 if file_name_key(artists[0]) not in found: 

29 return False 

30 number = int(song.get('track_number') or 0) 

31 if number and int(actual.get('track_number') or 0) != number: 

32 return False 

33 duration = float(song.get('duration') or 0) 

34 if duration and abs(float(actual.get('duration') or 0) - duration) > 3: 

35 return False 

36 return True