|
|
|
|
@ -718,13 +718,26 @@ def post_to_wp(post_data, category, link, author, image_url, original_source, im |
|
|
|
|
""" |
|
|
|
|
import logging |
|
|
|
|
import requests |
|
|
|
|
from foodie_config import WP_CREDENTIALS, X_API_CREDENTIALS |
|
|
|
|
from foodie_config import X_API_CREDENTIALS # Removed WP_CREDENTIALS |
|
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__) |
|
|
|
|
wp_username = WP_CREDENTIALS["username"] |
|
|
|
|
wp_password = WP_CREDENTIALS["password"] |
|
|
|
|
|
|
|
|
|
endpoint = f"{WP_CREDENTIALS['url']}/wp-json/wp/v2/posts" |
|
|
|
|
# Extract WordPress credentials from author dictionary |
|
|
|
|
wp_url = author.get("url") |
|
|
|
|
wp_username = author.get("username") |
|
|
|
|
wp_password = author.get("password") |
|
|
|
|
|
|
|
|
|
if not all([wp_url, wp_username, wp_password]): |
|
|
|
|
logger.error(f"Missing WordPress credentials for author: {author.get('username', 'unknown')}") |
|
|
|
|
return None, None |
|
|
|
|
|
|
|
|
|
# Ensure wp_url ends with '/wp-json/wp/v2' |
|
|
|
|
if not wp_url.endswith('/wp-json/wp/v2'): |
|
|
|
|
wp_base_url = f"{wp_url.rstrip('/')}/wp-json/wp/v2" |
|
|
|
|
else: |
|
|
|
|
wp_base_url = wp_url |
|
|
|
|
|
|
|
|
|
endpoint = f"{wp_base_url}/posts" |
|
|
|
|
if post_id: |
|
|
|
|
endpoint += f"/{post_id}" |
|
|
|
|
|
|
|
|
|
@ -733,12 +746,20 @@ def post_to_wp(post_data, category, link, author, image_url, original_source, im |
|
|
|
|
"Content-Type": "application/json" |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
# Get or create category ID |
|
|
|
|
category_id = get_wp_category_id(category, wp_base_url, wp_username, wp_password) |
|
|
|
|
if not category_id: |
|
|
|
|
category_id = create_wp_category(category, wp_base_url, wp_username, wp_password) |
|
|
|
|
if not category_id: |
|
|
|
|
logger.warning(f"Failed to get or create category '{category}', using default") |
|
|
|
|
category_id = 1 # Fallback to default category |
|
|
|
|
|
|
|
|
|
payload = { |
|
|
|
|
"title": post_data["title"], |
|
|
|
|
"content": post_data["content"], |
|
|
|
|
"status": post_data["status"], |
|
|
|
|
"author": WP_CREDENTIALS["authors"].get(post_data["author"], 1), |
|
|
|
|
"categories": [category] |
|
|
|
|
"author": wp_username, # Use username directly |
|
|
|
|
"categories": [category_id] |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
try: |
|
|
|
|
@ -749,10 +770,10 @@ def post_to_wp(post_data, category, link, author, image_url, original_source, im |
|
|
|
|
logger.info(f"{'Updated' if post_id else 'Posted'} WordPress post: {post_data['title']} (ID: {post_id})") |
|
|
|
|
|
|
|
|
|
if image_url and not post_id: # Only upload image for new posts |
|
|
|
|
media_id = upload_image_to_wp(image_url, post_data["title"], image_source, uploader, page_url) |
|
|
|
|
media_id = upload_image_to_wp(image_url, post_data["title"], wp_base_url, wp_username, wp_password, image_source, uploader, page_url) |
|
|
|
|
if media_id: |
|
|
|
|
requests.post( |
|
|
|
|
f"{WP_CREDENTIALS['url']}/wp-json/wp/v2/posts/{post_id}", |
|
|
|
|
f"{wp_base_url}/posts/{post_id}", |
|
|
|
|
headers=headers, |
|
|
|
|
json={"featured_media": media_id} |
|
|
|
|
) |
|
|
|
|
|