diff --git a/VERSION b/VERSION index a14da29..a6e7bcb 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -2.0.16 +2.0.17 diff --git a/api/routes.py b/api/routes.py index 7f9921c..fc3c1ca 100644 --- a/api/routes.py +++ b/api/routes.py @@ -117,16 +117,36 @@ async def sonarr_webhook(request: Request, background_tasks: BackgroundTasks, de series_id = series_results[0].get("id") print(f"DEBUG: Found series ID {series_id} for rename lookup") - # Get recent history for episodefilerenamed events (last 24 hours) + # Get recent history for the series and filter for rename events from datetime import datetime, timedelta since_date = (datetime.utcnow() - timedelta(hours=1)).strftime('%Y-%m-%dT%H:%M:%SZ') - history_url = f"{config.sonarr_url}/api/v3/history?seriesId={series_id}&eventType=episodeFileRenamed&since={since_date}&pageSize=10" + history_url = f"{config.sonarr_url}/api/v3/history?seriesId={series_id}&sortKey=date&sortDir=desc&page=1&pageSize=50" print(f"DEBUG: Checking recent rename history: {history_url}") history_response = requests.get(history_url, headers={"X-Api-Key": os.environ.get("SONARR_API_KEY", "")}, timeout=10) if history_response.status_code == 200: history_data = history_response.json() - recent_renames = history_data.get("records", []) + all_records = history_data.get("records", []) + print(f"DEBUG: Got {len(all_records)} total history records") + + # Filter for recent rename events + since_timestamp = datetime.utcnow() - timedelta(hours=1) + recent_renames = [] + + for record in all_records: + event_type = record.get("eventType", "") + date_str = record.get("date", "") + + if event_type == "episodeFileRenamed" and date_str: + try: + event_time = datetime.strptime(date_str.replace('Z', '+00:00'), '%Y-%m-%dT%H:%M:%S.%f%z') + event_time_utc = event_time.utctimetuple() + if datetime(*event_time_utc[:6]) > since_timestamp: + recent_renames.append(record) + except: + # If datetime parsing fails, include it anyway + recent_renames.append(record) + print(f"DEBUG: Found {len(recent_renames)} recent rename events") if recent_renames: