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}")