使用Python将本地html发送到wordpress

1、Xmlrpc方式实现

用到的库:

python-xmlrpc-wordpress

class Push2WordPressXmlRpc():
    def __init__(self,article_directory):
        self.article_directory = article_directory #文章目录
        self.wp = Client(config['wp_xmlrpc'],config['wp_admin'], config['wp_pwd']) #根据配置文件生成wp对象

    def push_aritlce(self,title,excerpt,content,customMeta,tags_list,catagory_list,thumbnail_id=0):
        """
        发布文章
        :param title: 文章名
        :param excerpt:预览
        :param content:文章内容
        :param customMeta:自定义标签
        :param tags_list:文章标签
        :param catagory_list:分类
        :param thumbnail_id:缩率图ID
        :return:
        """
        post = WordPressPost()
        post.title = title
        post.excerpt = excerpt
        post.content = content
        post.comment_status = 'closed'#open closed
        post.post_status = 'publish'  # 文章状态,不写默认是草稿,private表示私密的,draft表示草稿,publish表示发布


        post.terms_names = {
            'post_tag': tags_list,  # 文章所属标签,没有则自动创建
            'category': catagory_list  #文章所属分类,没有则自动创建
        }

        post.custom_fields = []   #自定义字段列表
        if thumbnail_id:
            post.thumbnail = thumbnail_id  # 缩略图的id

        for meta in customMeta:
              post.custom_fields.append({  #添加一个自定义字段
                'key': meta,
                'value': customMeta[meta]
                })

        try:
            post.id = self.wp.call(posts.NewPost(post))
        except:
            pass
        print('###发布文章:',title,'成功!')
        return post.id

2、图片处理

 def push_thumbnail(self,name):
        '''
        上传缩略图并返回ID
        :param name:图片名称
        :return:缩略图ID
        '''
        file_full_ename = self.article_directory + name  # 上传的缩略图文件路径

        type_suffix = file_full_ename.split('.')[-1]
        data = {
            'name': name,
            'type': 'image/' + type_suffix,  # mimetype
        }

        # read the binary file and let the XMLRPC library encode it into base64
        with open(file_full_ename, 'rb') as img:
            data['bits'] = xmlrpc_client.Binary(img.read())

        response = self.wp.call(media.UploadFile(data))

        attachment_id = response['id']
        return attachment_id

    def push_image(self,name):
        """
        上传单张图片到wordpress
        :param image_name:图片名称
        :return:返回图片地址url
        """
        try:

            file_full_ename = self.article_directory +"/"+ name  # 上传的图片文件路径

            type_suffix = file_full_ename.split('.')[-1]
            data = {
                'name': name,
                'type': 'image/' + type_suffix,  # mimetype
            }
            with open(file_full_ename, 'rb') as img:
                data['bits'] = xmlrpc_client.Binary(img.read())
            response = self.wp.call(media.UploadFile(data))
            image_url = response['url']
            # print('wordpress上传图片完成:' + image_url)


        except Exception as e:
            import traceback
            print(traceback.print_exc())

        return image_url


    def push_images(self,local_image_urls):
        """
        批量上传图片到wordpress
        :param local_image_urls:
        :return:返回字典:local_url:web_url
        """
        web_urls={}
        for local_image_url in local_image_urls:
            web_url = self.push_image(local_image_url)
            web_urls[local_image_url]=web_url

        return web_urls

3、发送源码

大T笔记所有文章均为本人原创,转载请您注明来源,并留下原文链接地址,是对我的尊重,也是对知识的尊重,谢谢!
大T笔记 » 使用Python将本地html发送到wordpress

大T笔记-我的个人互联网创业和自由职业之路

关于我 我的百宝箱
大T笔记-专注个人互联网创业和自由职业