Advertisement
Guest User

Untitled

a guest
Jul 27th, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.49 KB | None | 0 0
  1. import urllib
  2. from wordpress_xmlrpc import Client, WordPressPost
  3. from wordpress_xmlrpc.methods import posts
  4. import xmlrpclib
  5. from wordpress_xmlrpc.compat import xmlrpc_client
  6. from wordpress_xmlrpc.methods import media, posts
  7. import os
  8. import glob
  9.  
  10. class Custom_WP_XMLRPC:
  11. def post_article(self,wpUrl,wpUserName,wpPassword,articleTitle, articleCategories, articleContent, articleTags, articlePhotoUrl):
  12. self.path=os.getcwd()+"\00000001.jpg"
  13. self.articlePhotoUrl=articlePhotoUrl
  14. self.wpUrl=wpUrl
  15. self.wpUserName=wpUserName
  16. self.wpPassword=wpPassword
  17. #Download File
  18. f = open(self.path,'wb')
  19. f.write(urllib.urlopen(self.articlePhotoUrl).read())
  20. f.close()
  21. #Upload to WordPress
  22. client = Client(self.wpUrl,self.wpUserName,self.wpPassword)
  23. filename = self.path
  24. # prepare metadata
  25. data = {'name': 'picture.jpg','type': 'image/jpg',}
  26.  
  27. # read the binary file and let the XMLRPC library encode it into base64
  28. with open(filename, 'rb') as img:
  29. data['bits'] = xmlrpc_client.Binary(img.read())
  30. response = client.call(media.UploadFile(data))
  31. attachment_id = response['id']
  32. #Post
  33. post = WordPressPost()
  34. post.title = articleTitle
  35. post.content = articleContent
  36. post.terms_names = { 'post_tag': articleTags,'category': articleCategories}
  37. post.post_status = 'publish'
  38. post.thumbnail = attachment_id
  39. post.id = client.call(posts.NewPost(post))
  40. print 'Post Successfully posted. Its Id is: ',post.id
  41.  
  42.  
  43.  
  44.  
  45. #########################################
  46. # POST & Wp Credentials Detail #
  47. #########################################
  48.  
  49. #Url of Image on the internet
  50.  
  51. wpUrl='http://127.0.0.1/xmlrpc.php'
  52. wpUserName='username'
  53. wpPassword='password'
  54.  
  55. f = open('title.txt','r')
  56. articleTitle=f.read()
  57.  
  58. f = open('post.txt','r')
  59. articleContent=f.read()
  60.  
  61. f = open('url.txt','r')
  62. articlePhotoUrl=f.read()
  63.  
  64. articleTags=['news']
  65.  
  66. articleCategories=['news']
  67.  
  68. #########################################
  69. # Creating Class object & calling the xml rpc custom post Function
  70. #########################################
  71. xmlrpc_object = Custom_WP_XMLRPC()
  72. #On Post submission this function will print the post id
  73. xmlrpc_object.post_article(wpUrl,wpUserName,wpPassword,articleTitle, articleCategories, articleContent, articleTags, articlePhotoUrl)
  74.  
  75. Traceback (most recent call last):
  76. File "post-to-wordpress.py", line 73, in <module>
  77. xmlrpc_object.post_article(wpUrl,wpUserName,wpPassword,articleTitle, articleCategories, articleContent, articleTags, articlePhotoUrl)
  78. File "post-to-wordpress.py", line 22, in post_article
  79. client = Client(self.wpUrl,self.wpUserName,self.wpPassword)
  80. File "build/bdist.linux-x86_64/egg/wordpress_xmlrpc/base.py", line 24, in __init__
  81. File "/usr/lib64/python2.7/xmlrpclib.py", line 1243, in __call__
  82. return self.__send(self.__name, args)
  83. File "/usr/lib64/python2.7/xmlrpclib.py", line 1602, in __request
  84. verbose=self.__verbose
  85. File "/usr/lib64/python2.7/xmlrpclib.py", line 1283, in request
  86. return self.single_request(host, handler, request_body, verbose)
  87. File "/usr/lib64/python2.7/xmlrpclib.py", line 1316, in single_request
  88. return self.parse_response(response)
  89. File "/usr/lib64/python2.7/xmlrpclib.py", line 1493, in parse_response
  90. return u.close()
  91. File "/usr/lib64/python2.7/xmlrpclib.py", line 800, in close
  92. raise Fault(**self._stack[0])
  93. xmlrpclib.Fault: <Fault -32700: 'parse error. not well formed'>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement