ifix dates

This commit is contained in:
2025-09-07 13:11:35 -04:00
parent edf292211e
commit 92dc1c24de
2 changed files with 49 additions and 2 deletions
+1 -1
View File
@@ -1 +1 @@
0.0.10 0.0.11
+48 -1
View File
@@ -689,6 +689,49 @@ def _pick_earliest(cands: List[Tuple[str, str]]) -> Optional[Tuple[str, str]]:
except Exception: except Exception:
return cands[0] return cands[0]
def _pick_digital_closest_to_cinema(cands: List[Tuple[str, str]], cinema_date: Optional[str]) -> Optional[Tuple[str, str]]:
"""Pick digital release date closest to cinema date, preferring earlier over later dates"""
if not cands:
return None
if not cinema_date:
return _pick_earliest(cands) # Fall back to earliest if no cinema date
try:
cinema_dt = datetime.fromisoformat(cinema_date.replace("Z", "+00:00"))
# Sort candidates by how close they are to cinema date, preferring earlier dates
def distance_key(candidate):
try:
cand_dt = datetime.fromisoformat(candidate[0].replace("Z", "+00:00"))
days_diff = (cand_dt - cinema_dt).days
# Strongly prefer dates within 5 years of cinema release
if abs(days_diff) <= 1825: # 5 years * 365 days
return abs(days_diff)
else:
# For dates far from cinema release, heavily penalize them
return abs(days_diff) + 10000
except Exception:
return 999999 # Invalid dates go to the end
sorted_cands = sorted(cands, key=distance_key)
selected = sorted_cands[0]
# Log the decision for debugging
try:
selected_dt = datetime.fromisoformat(selected[0].replace("Z", "+00:00"))
days_diff = (selected_dt - cinema_dt).days
_log("DEBUG", f"Selected digital date {selected[0]} ({selected[1]}) - {days_diff} days from cinema date {cinema_date}")
except Exception:
pass
return selected
except Exception:
_log("WARNING", "Failed to parse cinema date for digital selection, using earliest")
return _pick_earliest(cands)
def _decide_movie_date(imdb_id: str, movie_dir: Path, poll_external: bool) -> Tuple[Optional[str], str, Optional[str]]: def _decide_movie_date(imdb_id: str, movie_dir: Path, poll_external: bool) -> Tuple[Optional[str], str, Optional[str]]:
""" """
Decide <dateadded> and supporting 'released' (premiered/year) for NFO. Decide <dateadded> and supporting 'released' (premiered/year) for NFO.
@@ -777,7 +820,11 @@ def _decide_movie_date(imdb_id: str, movie_dir: Path, poll_external: bool) -> Tu
def from_external_digital_bundle() -> Tuple[Optional[str], str]: def from_external_digital_bundle() -> Tuple[Optional[str], str]:
ensure_movie() ensure_movie()
cands = _collect_digital_candidates(imdb_id, movie_obj) cands = _collect_digital_candidates(imdb_id, movie_obj)
picked = _pick_earliest(cands) # Use cinema date to pick the most appropriate digital release
cinema_date = None
if movie_obj:
cinema_date = movie_obj.get("inCinemas") or movie_obj.get("theatricalRelease")
picked = _pick_digital_closest_to_cinema(cands, cinema_date)
return picked if picked else (None, "digital:none") return picked if picked else (None, "digital:none")
def from_moviefile_dateadded() -> Tuple[Optional[str], str]: def from_moviefile_dateadded() -> Tuple[Optional[str], str]: