Ejemplo n.º 1
0
 def edit_posts(self, post_id, title, feature_img, staff, content, img_list,
                tag_list, categrory):
     post = WordPressPost()
     post.title = title
     if staff:
         content = staff + '<br>' + content
     post.content = content
     post.categrory = []
     post.categrory.append(categrory)
     if tag_list:
         post.terms_names = {
             'post_tag': tag_list,
             'category': post.categrory
         }
     else:
         post.terms_names = {'post_tag': '', 'category': post.categrory}
     #img_list设置为空,避免图片重复上传
     img_list = []
     if img_list:
         img_name = img_list[-1].split('/')[-1]
         filename = img_list[-1].replace('http://', '/www/wwwroot/')
         data = {'name': img_name, 'type': 'image/jpeg'}
         try:
             with open(filename, 'rb') as img:
                 data['bits'] = xmlrpc_client.Binary(img.read())
             response = self.wp.call(media.UploadFile(data))
             attachment_id = response['id']
             post.thumbnail = attachment_id
         except:
             print('最后一张图片不存在:', img_list[-1])
     #    for i in range(len(img_list)):
     #        img_name=img_list[i].split('/')[-1]
     #        filename = './'+img_name
     #上传的图片本地文件路径
     # prepare metadata
     #        data = {'name': 'picture.jpg','type': 'image/jpeg',}
     #        data['name']=img_name
     # read the binary file and let the XMLRPC library encode it into base64
     #        with open(filename,'rb') as img:
     #            data['bits'] = xmlrpc_client.Binary(img.read())
     #        response=self.wp.call(media.UploadFile(data))
     #        if i ==len(img_list)-1:
     #            attachment_id = response['id']
     #            post.thumbnail=attachment_id
     post.post_status = 'publish'
     self.wp.call(EditPost(post_id, post))
     print('正在修正[ID]:%s,[标题]:%s' % (post_id, post.title))
     if os.path.isfile(self.wp_log):
         with open(self.wp_log, 'a+') as f:
             f.writelines(str(post_id) + '\n')
     return post_id, len(post.content)
Ejemplo n.º 2
0
 def push_posts(self, title, feature_img, staff, content, img_list,
                tag_list, categrory):
     post = WordPressPost()
     post.title = title
     post.content = content
     post.categrory = []
     post.categrory.append(categrory)
     if tag_list:
         post.terms_names = {
             'post_tag': tag_list,
             'category': post.categrory
         }
     else:
         post.terms_names = {'post_tag': '', 'category': post.categrory}
     post.post_status = 'publish'
     #如果特色图片存在,那么加到图片列表最后一张
     if feature_img:
         img_list.append(feature_img)
     #上传图片到wp
     if img_list:
         for i in range(len(img_list)):
             img_name = img_list[i].split('/')[-1]
             filename = img_list[i]
             #上传的图片本地文件路径
             # prepare metadata
             data = {'name': img_name, 'type': 'image/jpeg'}
             #data['name']=img_name
             # read the binary file and let the XMLRPC library encode it into base64
             with open(filename, 'rb') as img:
                 data['bits'] = xmlrpc_client.Binary(img.read())
             response = self.wp.call(media.UploadFile(data))
             #取最后一张图片作为特色图片
             if i == len(img_list) - 1:
                 attachment_id = response['id']
                 post.thumbnail = attachment_id
         '''
         response == {
           'id': 6,
           'file': 'picture.jpg'
           'url': 'http://www.example.com/wp-content/uploads/2012/04/16/picture.jpg',
           'type': 'image/jpeg',
         }
         '''
     postid = self.wp.call(NewPost(post))
     print('正在发布[ID]:%s,[标题]:%s' % (postid, post.title))
     #  if os.path.isfile(self.wp_log):
     #  with open(self.wp_log,'a+') as f:
     #  f.writelines(str(postid)+'\n')
     return postid
Ejemplo n.º 3
0
    def push_posts(self, title, content, categrory, tag, img_list):
        post = WordPressPost()
        post.title = title
        post.content = content
        post.categrory = []
        post.categrory.append(categrory)

        if tag:
            post.tag = []
            post.tag.append(tag)
            post.terms_names = {
                'post_tag': post.tag,
                'category': post.categrory
            }
        else:
            post.terms_names = {'post_tag': '', 'category': post.categrory}
        post.post_status = 'publish'
        if img_list:
            for i in range(len(img_list)):
                img_name = img_list[i].split('/')[-1]
                filename = './' + img_name
                #上传的图片本地文件路径
                # prepare metadata
                data = {
                    'name': 'picture.jpg',
                    'type': 'image/jpeg',
                }
                data['name'] = img_name
                # read the binary file and let the XMLRPC library encode it into base64
                with open(filename, 'rb') as img:
                    data['bits'] = xmlrpc_client.Binary(img.read())
                response = self.wp.call(media.UploadFile(data))
                if i == len(img_list) - 1:
                    attachment_id = response['id']
                    post.thumbnail = attachment_id
            '''
            response == {
              'id': 6,
              'file': 'picture.jpg'
              'url': 'http://www.example.com/wp-content/uploads/2012/04/16/picture.jpg',
              'type': 'image/jpeg',
            }
            '''
        postid = self.wp.call(NewPost(post))
        print('正在发布[ID]:%s,[标题]:%s' % (postid, post.title))
        return postid, len(post.content)
Ejemplo n.º 4
0
 def edit_post(self, post_id, title, content, categrory, post_tag,
               img_list):
     post = WordPressPost()
     post.title = title
     post.content = content
     post.categrory = []
     post.categrory.append(categrory)
     if post_tag is not None:
         post.terms_names = {
             'post_tag': post_tag,
             'category': post.categrory
         }
     else:
         post.terms_names = {'post_tag': '', 'category': post.categrory}
     post.post_status = 'publish'
     self.wp.call(EditPost(post_id, post))
     print('正在修正[ID]:%s,[标题]:%s' % (post_id, post.title))
     return post_id, len(post.content)
Ejemplo n.º 5
0
    def editPost(self, postID, sourceUrl, title, featureImg, content, category,
                 tags, customField):
        post = WordPressPost()
        post.title = title  #标题
        post.content = content  #内容
        #post.post_status = 'publish'
        #文章状态,不写默认是草稿,private表示私密的,draft表示草稿,publish表示发布
        imgList = []  #图片列表
        if customField != {}:
            try:
                if customField["imgList"]:
                    imgList = customField["imgList"]
                else:
                    imgList = str(imgList)
            except KeyError:
                print("customField中没有imgList这个图片列表")

        if not category:
            category = [
                'other',
            ]
        post.categrory = category

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

        #自定义字段列表
        post.custom_fields = []
        #添加自定义字段内容
        if postID == '0':
            for key in customField:
                post.custom_fields.append({
                    'key': key,
                    'value': customField[key]
                })
                #print('post.custom_fields',type(post.custom_fields),post.custom_fields)
        else:
            for n in range(len(post.custom_fields)):
                for key in customField:
                    if post.custom_fields[n][
                            'key'] == key and post.custom_fields[n][
                                'value'] != customField[key]:
                        post.custom_fields[n]['value'] = customField[key]

        #如果特色图片存在,上传特色图片
        if featureImg:
            img_name = featureImg.split('/')[-1]
            filename = featureImg
            data = {'name': img_name, 'type': 'image/jpeg'}
            with open(filename, 'rb') as img:
                data['bits'] = xmlrpc_client.Binary(img.read())
            response = self.wp.call(media.UploadFile(data))
            attachment_id = response['id']
            post.thumbnail = attachment_id
        if postID == '0':
            postID = self.wp.call(NewPost(post))
            print('正在发布[ID]:%s,[标题]:%s' % (postID, post.title))
        else:
            self.wp.call(EditPost(postID, post))
            print('正在修正[ID]:%s,[标题]:%s' % (postID, post.title))
        return postID