From e389893e14bbb7eeafde83c9f2134fc040b4571d Mon Sep 17 00:00:00 2001 From: SBCrumb Date: Mon, 8 Sep 2025 15:14:32 -0400 Subject: [PATCH] use date earliest --- clients/radarr_client.py | 34 +++++++++++++++++++++------------- 1 file changed, 21 insertions(+), 13 deletions(-) diff --git a/clients/radarr_client.py b/clients/radarr_client.py index 9fbc115..8f2bb7d 100644 --- a/clients/radarr_client.py +++ b/clients/radarr_client.py @@ -235,12 +235,13 @@ class RadarrClient: total_processed = 0 while page <= 20: # Safety limit + # Get history in chronological order data = self._get("/api/v3/history", { - "movieId": str(movie_id), # Ensure it's a string - "page": page, + "movieId": str(movie_id), + "page": page, "pageSize": page_size, - "sortKey": "date", - "sortDirection": "ascending" # Start from oldest + "sortKey": "date", + "sortDirection": "ascending" }) if not data: @@ -272,18 +273,22 @@ class RadarrClient: except Exception: pass - # Only process import events (type 3) - if event_type != self.EVENT_TYPE_IMPORTED: + # Track the first grab event we find + if event_type == self.EVENT_TYPE_GRABBED and not first_grab: + if event.get("date"): + try: + first_grab = datetime.fromisoformat(event["date"].replace("Z", "+00:00")).astimezone(timezone.utc).isoformat(timespec="seconds") + _log("DEBUG", f"Found first grab event at {first_grab}") + except Exception: + pass continue - imported_path = None + + # Only process import events try: data = json.loads(event.get("data", "{}")) imported_path = data.get("importedPath", "").lower() - except (json.JSONDecodeError, AttributeError): - pass - - if not imported_path: - continue + if not imported_path: + continue movie_imdb = (movie_info.get("imdbId", "") or "").lower() movie_title = (movie_info.get("title", "") or "").lower() @@ -405,7 +410,10 @@ class RadarrClient: Returns: (date_iso, source_description) """ - # Try history first + # Try history first - we want the earliest event regardless of type + import_date = self.earliest_import_event_optimized(movie_id) + + # Try history second import_date = self.earliest_import_event_optimized(movie_id) if import_date: return import_date, "radarr:history.import"