diff --git a/VERSION b/VERSION index 2cfabea..8cbf02c 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -0.0.11 +0.0.12 diff --git a/webhook_server.py b/webhook_server.py index 0e8c4be..db04ca8 100644 --- a/webhook_server.py +++ b/webhook_server.py @@ -690,7 +690,7 @@ def _pick_earliest(cands: List[Tuple[str, str]]) -> Optional[Tuple[str, str]]: 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""" + """Pick digital release date closest to cinema date, with TMDB preferred over Radarr as tiebreaker""" if not cands: return None if not cinema_date: @@ -699,23 +699,37 @@ def _pick_digital_closest_to_cinema(cands: List[Tuple[str, str]], cinema_date: O 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): + # Source preference: TMDB > Jellyseerr > OMDb > Radarr + source_priority = { + "tmdb:digital": 0, + "jellyseerr:digital": 1, + "omdb:dvd": 2, + "radarr:digitalRelease": 3 + } + + # Sort candidates by cinema proximity first, then by source preference + def sort_key(candidate): try: cand_dt = datetime.fromisoformat(candidate[0].replace("Z", "+00:00")) - days_diff = (cand_dt - cinema_dt).days + days_diff = abs((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) + # Primary sort: distance from cinema (within 5 years preferred) + if days_diff <= 1825: # 5 years * 365 days + distance_score = days_diff else: - # For dates far from cinema release, heavily penalize them - return abs(days_diff) + 10000 + # Heavily penalize dates far from cinema release + distance_score = days_diff + 10000 + + # Secondary sort: source preference (lower number = higher preference) + source_score = source_priority.get(candidate[1], 99) + + # Return tuple for sorting: (distance, source_priority) + return (distance_score, source_score) except Exception: - return 999999 # Invalid dates go to the end + return (999999, 99) # Invalid dates/sources go to the end - sorted_cands = sorted(cands, key=distance_key) + sorted_cands = sorted(cands, key=sort_key) selected = sorted_cands[0] # Log the decision for debugging @@ -723,6 +737,11 @@ def _pick_digital_closest_to_cinema(cands: List[Tuple[str, str]], cinema_date: O 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}") + + # Show other candidates if multiple exist + if len(cands) > 1: + other_cands = [f"{c[1]}={c[0]}" for c in sorted_cands[1:3]] # Show top 2 alternatives + _log("DEBUG", f"Other candidates considered: {other_cands}") except Exception: pass