Need help my python Gurus

googleagent59

Senior Member
Joined
Aug 6, 2010
Messages
977
Reaction score
205
import csv
from wordpress_xmlrpc import Client, WordPressPost
from wordpress_xmlrpc.methods.posts import NewPost
import openai
# OpenAI API key
openai.api_key = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
# WordPress credentials
wordpress_url = "https://xxxxx.com/xmlrpc.php"
wordpress_username = "xxxxx"
wordpress_password = "xxxxx"
def generate_article(title):
# Generate article content using OpenAI API
prompt = f"Write an article about {title}."
response = openai.Completion.create(
engine="davinci",
prompt=prompt,
max_tokens=500, # Adjust this limit as needed
temperature=0.7, # Adjust this for creativity
)
article_content = response.choices[0].text.strip()
return article_content
def post_to_wordpress(title, content):
# Connect to WordPress
wp = Client(wordpress_url, wordpress_username, wordpress_password)
# Create a new WordPress post
post = WordPressPost()
post.title = title
post.content = content
post.post_status = "publish" # You can change this to "draft" or "pending" if needed
# Publish the post
post_id = wp.call(NewPost(post))
# Read titles from the CSV file
with open('titles.csv', 'r') as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
title = row['title']
article_content = generate_article(title)
post_to_wordpress(title, article_content)
print(f"Posted: {title}")
 
Maybe " prompt = f"Write an article about {title}." "
 
Are you getting any errors? Anything happening?

I would try removing '/xmlrpc.php' from the wp url. Just use 'https://xxxxx.com'
 
import csv
from wordpress_xmlrpc import Client, WordPressPost
from wordpress_xmlrpc.methods.posts import NewPost
import openai

# Function to generate an article using OpenAI API
def generate_article(title):
prompt = f"Write an article about {title}."
response = openai.Completion.create(
engine="davinci",
prompt=prompt,
max_tokens=500,
temperature=0.7,
)
article_content = response.choices[0].text.strip()
return article_content

# Function to post the article to WordPress
def post_to_wordpress(title, content, wp):
post = WordPressPost()
post.title = title
post.content = content
post.post_status = "publish" # Change this to "draft" or "pending" if needed
post_id = wp.call(NewPost(post))
print(f"Posted: {title}")

# Main script
if __name__ == "__main__":
# Set OpenAI API key
openai.api_key = "your_openai_api_key_here"

# WordPress credentials
wordpress_url = "your_wordpress_url_here"
wordpress_username = "your_username_here"
wordpress_password = "your_password_here"

# Connect to WordPress
wp = Client(wordpress_url, wordpress_username, wordpress_password)

# Read titles from the CSV file
with open('titles.csv', 'r') as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
title = row['title']
article_content = generate_article(title)
post_to_wordpress(title, article_content, wp)
 
Back
Top