fix
This commit is contained in:
+36
-15
@@ -718,46 +718,67 @@ 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}"
|
||||
|
||||
|
||||
headers = {
|
||||
"Authorization": "Basic " + base64.b64encode(f"{wp_username}:{wp_password}".encode()).decode(),
|
||||
"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:
|
||||
response = requests.post(endpoint, headers=headers, json=payload)
|
||||
response.raise_for_status()
|
||||
post_id = response.json().get("id")
|
||||
post_url = response.json().get("link")
|
||||
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}
|
||||
)
|
||||
logger.info(f"Set featured image (Media ID: {media_id}) for post {post_id}")
|
||||
|
||||
|
||||
if should_post_tweet and post_url:
|
||||
credentials = X_API_CREDENTIALS.get(post_data["author"])
|
||||
if credentials:
|
||||
@@ -766,7 +787,7 @@ def post_to_wp(post_data, category, link, author, image_url, original_source, im
|
||||
logger.info(f"Successfully tweeted for post: {post_data['title']}")
|
||||
else:
|
||||
logger.warning(f"Failed to tweet for post: {post_data['title']}")
|
||||
|
||||
|
||||
return post_id, post_url
|
||||
except requests.exceptions.RequestException as e:
|
||||
logger.error(f"Failed to {'update' if post_id else 'post'} WordPress post: {post_data['title']}: {e}", exc_info=True)
|
||||
|
||||
Reference in New Issue
Block a user