diff --git a/VERSION b/VERSION index 7c1886b..2cfabea 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -0.0.10 +0.0.11 diff --git a/webhook_server.py b/webhook_server.py index ac4c4ca..0e8c4be 100644 --- a/webhook_server.py +++ b/webhook_server.py @@ -689,6 +689,49 @@ def _pick_earliest(cands: List[Tuple[str, str]]) -> Optional[Tuple[str, str]]: except Exception: 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]]: """ Decide 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]: ensure_movie() 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") def from_moviefile_dateadded() -> Tuple[Optional[str], str]: