File size: 939 Bytes
53e65b7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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