Feature: Update existing posts instead of skipping
- Added update_post() method to WordPress API client - Added _put() method for HTTP PUT requests - Modified create_post() to call update_post() when duplicate is found - Existing posts now get updated with latest content, tags, categories, dates, etc. - Prevents manual deletion and re-creation workflow - Added excerpt as explicit parameter to create_post() - Debug output shows 'Aktualisiere...' message when updating Example: Re-running import on existing post now updates all fields including newly added author tags
This commit is contained in:
parent
aa50145633
commit
fb9720fb2a
1 changed files with 100 additions and 3 deletions
103
wordpress_api.py
103
wordpress_api.py
|
|
@ -46,6 +46,13 @@ class WordPressAPI:
|
|||
response.raise_for_status()
|
||||
return response
|
||||
|
||||
def _put(self, endpoint: str, data: Optional[Dict] = None) -> requests.Response:
|
||||
"""PUT-Request an WordPress API (für Updates)"""
|
||||
url = urljoin(self.api_base, endpoint)
|
||||
response = self.session.put(url, json=data)
|
||||
response.raise_for_status()
|
||||
return response
|
||||
|
||||
def check_post_exists(self, title: str) -> Optional[int]:
|
||||
"""
|
||||
Prüft, ob ein Beitrag mit dem Titel bereits existiert
|
||||
|
|
@ -184,6 +191,7 @@ class WordPressAPI:
|
|||
featured_media: Optional[int] = None,
|
||||
categories: Optional[List[int]] = None,
|
||||
tags: Optional[List[int]] = None,
|
||||
excerpt: Optional[str] = None,
|
||||
check_duplicate: bool = True,
|
||||
**kwargs) -> Optional[int]:
|
||||
"""
|
||||
|
|
@ -202,12 +210,22 @@ class WordPressAPI:
|
|||
Returns:
|
||||
Post-ID des erstellten Beitrags, oder None bei Fehler
|
||||
"""
|
||||
# Duplikatsprüfung
|
||||
# Duplikatsprüfung - bei Duplikat Update durchführen
|
||||
if check_duplicate:
|
||||
existing_id = self.check_post_exists(title)
|
||||
if existing_id:
|
||||
print(f"Beitrag '{title}' existiert bereits (ID: {existing_id})")
|
||||
return existing_id
|
||||
print(f"Beitrag '{title}' existiert bereits (ID: {existing_id}) - Aktualisiere...")
|
||||
return self.update_post(
|
||||
post_id=existing_id,
|
||||
title=title,
|
||||
content=content,
|
||||
status=status,
|
||||
featured_media=featured_media,
|
||||
categories=categories,
|
||||
tags=tags,
|
||||
excerpt=excerpt,
|
||||
**kwargs
|
||||
)
|
||||
|
||||
# Post-Daten zusammenstellen
|
||||
post_data = {
|
||||
|
|
@ -217,6 +235,8 @@ class WordPressAPI:
|
|||
**kwargs
|
||||
}
|
||||
|
||||
if excerpt:
|
||||
post_data['excerpt'] = excerpt
|
||||
if featured_media:
|
||||
post_data['featured_media'] = featured_media
|
||||
if categories:
|
||||
|
|
@ -257,6 +277,83 @@ class WordPressAPI:
|
|||
print(f"Details: {e.response.text}")
|
||||
return None
|
||||
|
||||
def update_post(self, post_id: int, title: Optional[str] = None,
|
||||
content: Optional[str] = None, status: Optional[str] = None,
|
||||
featured_media: Optional[int] = None,
|
||||
categories: Optional[List[int]] = None,
|
||||
tags: Optional[List[int]] = None,
|
||||
excerpt: Optional[str] = None,
|
||||
**kwargs) -> Optional[int]:
|
||||
"""
|
||||
Aktualisiert einen existierenden WordPress-Beitrag
|
||||
|
||||
Args:
|
||||
post_id: ID des zu aktualisierenden Beitrags
|
||||
title: Neuer Titel (optional)
|
||||
content: Neuer Inhalt (optional)
|
||||
status: Neuer Status (optional)
|
||||
featured_media: ID des Beitragsbilds (optional)
|
||||
categories: Liste der Kategorie-IDs (optional)
|
||||
tags: Liste der Tag-IDs (optional)
|
||||
excerpt: Auszug (optional)
|
||||
**kwargs: Weitere WordPress-Post-Felder
|
||||
|
||||
Returns:
|
||||
Post-ID des aktualisierten Beitrags, oder None bei Fehler
|
||||
"""
|
||||
# Post-Daten zusammenstellen (nur Felder die gesetzt sind)
|
||||
post_data = {**kwargs}
|
||||
|
||||
if title is not None:
|
||||
post_data['title'] = title
|
||||
if content is not None:
|
||||
post_data['content'] = content
|
||||
if status is not None:
|
||||
post_data['status'] = status
|
||||
if excerpt is not None:
|
||||
post_data['excerpt'] = excerpt
|
||||
if featured_media is not None:
|
||||
post_data['featured_media'] = featured_media
|
||||
if categories is not None:
|
||||
post_data['categories'] = categories
|
||||
if tags is not None:
|
||||
post_data['tags'] = tags
|
||||
|
||||
# Debug: Zeige was aktualisiert wird
|
||||
print(f"Aktualisiere Beitrag (ID: {post_id}):")
|
||||
if title:
|
||||
print(f" - Titel: {title}")
|
||||
if status:
|
||||
print(f" - Status: {status}")
|
||||
if tags:
|
||||
print(f" - Tags: {tags}")
|
||||
if categories:
|
||||
print(f" - Kategorien: {categories}")
|
||||
if 'date' in post_data:
|
||||
print(f" - Datum: {post_data['date']}")
|
||||
if 'date_gmt' in post_data:
|
||||
print(f" - Datum GMT: {post_data['date_gmt']}")
|
||||
|
||||
# Beitrag aktualisieren
|
||||
try:
|
||||
response = self._put(f'posts/{post_id}', data=post_data)
|
||||
post = response.json()
|
||||
print(f"✅ Beitrag aktualisiert (ID: {post_id}, Status: {post.get('status')})")
|
||||
|
||||
# Debug: Zeige was WordPress zurückgibt
|
||||
if 'tags' in post and post['tags']:
|
||||
print(f" WordPress-Tags: {post['tags']}")
|
||||
if 'date' in post:
|
||||
print(f" WordPress-Datum: {post['date']}")
|
||||
|
||||
return post_id
|
||||
|
||||
except requests.exceptions.RequestException as e:
|
||||
print(f"Fehler beim Aktualisieren des Beitrags: {e}")
|
||||
if hasattr(e.response, 'text'):
|
||||
print(f"Details: {e.response.text}")
|
||||
return None
|
||||
|
||||
def get_categories(self, search: Optional[str] = None) -> List[Dict[str, Any]]:
|
||||
"""
|
||||
Holt alle verfügbaren Kategorien oder sucht nach einer bestimmten Kategorie
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue