automatedblogpostcreater / wordpress_handler.py
Pamudu13's picture
Upload 16 files
53e65b7 verified
raw
history blame contribute delete
939 Bytes
from wordpress_xmlrpc import Client, WordPressPost
from wordpress_xmlrpc.methods.posts import NewPost
from typing import Dict
class WordPressHandler:
def __init__(self, url: str, username: str, password: str):
self.client = Client(url, username, password)
def publish_post(self, content: str, metadata: Dict, image_url: str) -> int:
post = WordPressPost()
post.title = metadata['title']
post.content = f'<img src="{image_url}" alt="Cover Image">\n{content}'
post.slug = metadata['slug']
post.post_status = 'draft'
post.terms_names = {
'category': ['Your Category'],
'post_tag': ['Your Tags']
}
post.custom_fields = []
post.custom_fields.append({
'key': '_yoast_wpseo_metadesc',
'value': metadata['meta_description']
})
post_id = self.client.call(NewPost(post))
return post_id