From 34cd56327355f05174060b194e754ded834b0f9b Mon Sep 17 00:00:00 2001 From: Sam Chau Date: Sat, 11 Jan 2025 02:37:56 +1030 Subject: [PATCH] fix: prevent duplicate format entries when syncing profile format IDs --- backend/app/importarr/profile.py | 39 ++++++++++++++++---------------- 1 file changed, 20 insertions(+), 19 deletions(-) diff --git a/backend/app/importarr/profile.py b/backend/app/importarr/profile.py index 2516038..1360fba 100644 --- a/backend/app/importarr/profile.py +++ b/backend/app/importarr/profile.py @@ -189,29 +189,33 @@ def sync_format_ids(profile_data: Dict, format_id_map: Dict[str, int]) -> Dict: if 'formatItems' not in profile_data: profile_data['formatItems'] = [] - existing_format_names = { - item['name'] - for item in profile_data['formatItems'] - } + # Create a set to track format names we've already processed + processed_formats = set() synced_items = [] - for item in profile_data['formatItems']: - if item['name'] in format_id_map: - synced_items.append({ - 'format': format_id_map[item['name']], - 'name': item['name'], - 'score': item['score'] - }) - else: - logger.warning(f"Custom format not found in arr: {item['name']}") + # First process existing items + for item in profile_data.get('formatItems', []): + if item['name'] not in processed_formats: + if item['name'] in format_id_map: + synced_items.append({ + 'format': format_id_map[item['name']], + 'name': item['name'], + 'score': item['score'] + }) + processed_formats.add(item['name']) + else: + logger.warning( + f"Custom format not found in arr: {item['name']}") + # Only add formats that haven't been processed yet for format_name, format_id in format_id_map.items(): - if format_name not in existing_format_names: + if format_name not in processed_formats: synced_items.append({ 'format': format_id, 'name': format_name, - 'score': 0 + 'score': 0 # Default score for new formats }) + processed_formats.add(format_name) profile_data['formatItems'] = synced_items return profile_data @@ -326,16 +330,13 @@ def _import_and_score_formats(formats: List[str], score_copy['name'] = format_names[i] modified_scores.append(score_copy) + # Only append once with the modified scores profile_data['custom_formats'].extend(modified_scores) except Exception as e: logger.error(f"Error importing {feature_name} formats: {str(e)}") return - if 'custom_formats' not in profile_data: - profile_data['custom_formats'] = [] - profile_data['custom_formats'].extend(scores) - def process_profile(profile_data: Dict, existing_names: Dict[str, int], base_url: str, api_key: str) -> Dict: