Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import urllib
- from wordpress_xmlrpc import Client, WordPressPost
- from wordpress_xmlrpc.methods import posts
- import xmlrpclib
- from wordpress_xmlrpc.compat import xmlrpc_client
- from wordpress_xmlrpc.methods import media, posts
- import os
- import glob
- class Custom_WP_XMLRPC:
- def post_article(self,wpUrl,wpUserName,wpPassword,articleTitle, articleCategories, articleContent, articleTags, articlePhotoUrl):
- self.path=os.getcwd()+"\00000001.jpg"
- self.articlePhotoUrl=articlePhotoUrl
- self.wpUrl=wpUrl
- self.wpUserName=wpUserName
- self.wpPassword=wpPassword
- #Download File
- f = open(self.path,'wb')
- f.write(urllib.urlopen(self.articlePhotoUrl).read())
- f.close()
- #Upload to WordPress
- client = Client(self.wpUrl,self.wpUserName,self.wpPassword)
- filename = self.path
- # prepare metadata
- data = {'name': 'picture.jpg','type': 'image/jpg',}
- # 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 = client.call(media.UploadFile(data))
- attachment_id = response['id']
- #Post
- post = WordPressPost()
- post.title = articleTitle
- post.content = articleContent
- post.terms_names = { 'post_tag': articleTags,'category': articleCategories}
- post.post_status = 'publish'
- post.thumbnail = attachment_id
- post.id = client.call(posts.NewPost(post))
- print 'Post Successfully posted. Its Id is: ',post.id
- #########################################
- # POST & Wp Credentials Detail #
- #########################################
- #Url of Image on the internet
- wpUrl='http://127.0.0.1/xmlrpc.php'
- wpUserName='username'
- wpPassword='password'
- f = open('title.txt','r')
- articleTitle=f.read()
- f = open('post.txt','r')
- articleContent=f.read()
- f = open('url.txt','r')
- articlePhotoUrl=f.read()
- articleTags=['news']
- articleCategories=['news']
- #########################################
- # Creating Class object & calling the xml rpc custom post Function
- #########################################
- xmlrpc_object = Custom_WP_XMLRPC()
- #On Post submission this function will print the post id
- xmlrpc_object.post_article(wpUrl,wpUserName,wpPassword,articleTitle, articleCategories, articleContent, articleTags, articlePhotoUrl)
- Traceback (most recent call last):
- File "post-to-wordpress.py", line 73, in <module>
- xmlrpc_object.post_article(wpUrl,wpUserName,wpPassword,articleTitle, articleCategories, articleContent, articleTags, articlePhotoUrl)
- File "post-to-wordpress.py", line 22, in post_article
- client = Client(self.wpUrl,self.wpUserName,self.wpPassword)
- File "build/bdist.linux-x86_64/egg/wordpress_xmlrpc/base.py", line 24, in __init__
- File "/usr/lib64/python2.7/xmlrpclib.py", line 1243, in __call__
- return self.__send(self.__name, args)
- File "/usr/lib64/python2.7/xmlrpclib.py", line 1602, in __request
- verbose=self.__verbose
- File "/usr/lib64/python2.7/xmlrpclib.py", line 1283, in request
- return self.single_request(host, handler, request_body, verbose)
- File "/usr/lib64/python2.7/xmlrpclib.py", line 1316, in single_request
- return self.parse_response(response)
- File "/usr/lib64/python2.7/xmlrpclib.py", line 1493, in parse_response
- return u.close()
- File "/usr/lib64/python2.7/xmlrpclib.py", line 800, in close
- raise Fault(**self._stack[0])
- xmlrpclib.Fault: <Fault -32700: 'parse error. not well formed'>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement