Fix category lookup - handle French characters and existing categories better
### Fixes: - Improved get_or_create_category() with multiple lookup strategies - Handle French characters in category names (Jeu vidéo, Téléchargement) - Better handling of 'term_exists' 400 error from WordPress - Fetch existing category details when creation fails ### Lookup Order: 1. Exact name match (case-insensitive) 2. Slug match 3. Normalized slug (handles French characters) 4. Partial name match ### Benefits: - No more errors for existing categories - Handles accented characters properly - Better caching of existing categories - More robust category creation Co-authored-by: Qwen-Coder <qwen-coder@alibabacloud.com>
This commit is contained in:
@@ -132,12 +132,33 @@ class WordPressCategoryManager:
|
|||||||
}
|
}
|
||||||
|
|
||||||
return category_data['id']
|
return category_data['id']
|
||||||
elif response.status_code == 409:
|
elif response.status_code == 400:
|
||||||
# Category already exists
|
# Category might already exist - search for it
|
||||||
logger.info(f" Category '{category_name}' already exists")
|
error_data = response.json()
|
||||||
existing = response.json()
|
if error_data.get('code') == 'term_exists':
|
||||||
if isinstance(existing, list) and len(existing) > 0:
|
term_id = error_data.get('data', {}).get('term_id')
|
||||||
return existing[0]['id']
|
if term_id:
|
||||||
|
logger.info(f" Category '{category_name}' already exists (ID: {term_id})")
|
||||||
|
|
||||||
|
# Fetch the category details
|
||||||
|
cat_response = requests.get(
|
||||||
|
f"{base_url}/wp-json/wp/v2/categories/{term_id}",
|
||||||
|
auth=auth,
|
||||||
|
timeout=10
|
||||||
|
)
|
||||||
|
if cat_response.status_code == 200:
|
||||||
|
cat_data = cat_response.json()
|
||||||
|
# Update cache
|
||||||
|
if site_name in self.category_cache:
|
||||||
|
self.category_cache[site_name][cat_data['slug']] = {
|
||||||
|
'id': cat_data['id'],
|
||||||
|
'name': cat_data['name'],
|
||||||
|
'slug': cat_data['slug'],
|
||||||
|
'count': cat_data.get('count', 0)
|
||||||
|
}
|
||||||
|
return cat_data['id']
|
||||||
|
|
||||||
|
logger.warning(f" Category already exists or error: {error_data}")
|
||||||
return None
|
return None
|
||||||
else:
|
else:
|
||||||
logger.error(f"Error creating category: {response.status_code} - {response.text}")
|
logger.error(f"Error creating category: {response.status_code} - {response.text}")
|
||||||
@@ -164,21 +185,42 @@ class WordPressCategoryManager:
|
|||||||
if site_name not in self.category_cache:
|
if site_name not in self.category_cache:
|
||||||
self.fetch_categories(site_name)
|
self.fetch_categories(site_name)
|
||||||
|
|
||||||
# Check if category exists
|
# Check if category exists (by exact name first)
|
||||||
slug = category_name.lower().replace(' ', '-').replace('/', '-')
|
|
||||||
categories = self.category_cache.get(site_name, {})
|
categories = self.category_cache.get(site_name, {})
|
||||||
|
|
||||||
|
# Try exact name match (case-insensitive)
|
||||||
|
category_name_lower = category_name.lower()
|
||||||
|
for slug, cat_data in categories.items():
|
||||||
|
if cat_data['name'].lower() == category_name_lower:
|
||||||
|
logger.info(f"✓ Found existing category '{category_name}' (ID: {cat_data['id']})")
|
||||||
|
return cat_data['id']
|
||||||
|
|
||||||
|
# Try slug match
|
||||||
|
slug = category_name.lower().replace(' ', '-').replace('/', '-')
|
||||||
if slug in categories:
|
if slug in categories:
|
||||||
logger.info(f"✓ Found existing category '{category_name}' (ID: {categories[slug]['id']})")
|
logger.info(f"✓ Found existing category '{category_name}' (ID: {categories[slug]['id']})")
|
||||||
return categories[slug]['id']
|
return categories[slug]['id']
|
||||||
|
|
||||||
# Try alternative slug formats
|
# Try alternative slug formats (handle French characters)
|
||||||
alt_slug = category_name.lower().replace(' ', '-')
|
import unicodedata
|
||||||
if alt_slug in categories:
|
normalized_slug = unicodedata.normalize('NFKD', slug)\
|
||||||
logger.info(f"✓ Found existing category '{category_name}' (ID: {categories[alt_slug]['id']})")
|
.encode('ascii', 'ignore')\
|
||||||
return categories[alt_slug]['id']
|
.decode('ascii')\
|
||||||
|
.lower()\
|
||||||
|
.replace(' ', '-')
|
||||||
|
|
||||||
|
if normalized_slug in categories:
|
||||||
|
logger.info(f"✓ Found existing category '{category_name}' (ID: {categories[normalized_slug]['id']})")
|
||||||
|
return categories[normalized_slug]['id']
|
||||||
|
|
||||||
|
# Try partial match (if slug contains the category name)
|
||||||
|
for slug, cat_data in categories.items():
|
||||||
|
if category_name_lower in cat_data['name'].lower() or cat_data['name'].lower() in category_name_lower:
|
||||||
|
logger.info(f"✓ Found similar category '{cat_data['name']}' (ID: {cat_data['id']})")
|
||||||
|
return cat_data['id']
|
||||||
|
|
||||||
# Create new category
|
# Create new category
|
||||||
|
logger.info(f"Creating new category '{category_name}'...")
|
||||||
return self.create_category(site_name, category_name, description)
|
return self.create_category(site_name, category_name, description)
|
||||||
|
|
||||||
def assign_post_to_category(self, site_name: str, post_id: int,
|
def assign_post_to_category(self, site_name: str, post_id: int,
|
||||||
|
|||||||
Reference in New Issue
Block a user