11#!/usr/bin/python3
2+ import os
23
34from wordpress_xmlrpc import Client , WordPressPage
45from wordpress_xmlrpc .methods .posts import EditPost
89import ssl
910
1011
11- def update_wiki (url , username , password , _page_title , _page_id , _markdown ):
12- """
13- Update an existing wordpress page with generated markdown.
14- Assumes you have a markdown file with content you want published
15- to an existing wordpress page.
16- requires: python-wordpress-xmlrpc or python3-wordpress-xmlrpc
17- :param url: wordpress xmlrpc endpoint
18- :param username: wordpress username
19- :param password: wordpress password
20- :param _page_title: post page title
21- :param _page_id: post page id
22- :param _markdown: path to markdown file for upload
23- """
24- scheme = urlparse (url )[0 ]
25- if scheme == "https" :
26- ssl ._create_default_https_context = ssl ._create_unverified_context
12+ class Wiki :
13+ def __init__ (self , url , username , password ):
14+ """
15+ Wiki object initialization
2716
28- wp = Client (url , username , password )
17+ :param url: wordpress url
18+ :param username: wordpress username
19+ :param password: wordpress password
20+ """
21+ self .url = url
22+ self .username = username
23+ self .password = password
24+ self .endpoint = os .path .join (self .url , "xmlrpc.php" )
2925
30- # define pages variable
31- page = WordPressPage ()
32- page . title = _page_title
26+ scheme = urlparse ( self . url )[ 0 ]
27+ if scheme == "https" :
28+ ssl . _create_default_https_context = ssl . _create_unverified_context
3329
34- # page id can be found by viewing via wp-admin dashboard in URL
35- page .id = _page_id
30+ def update (self , _page_title , _page_id , _markdown ):
31+ """
32+ Update an existing wordpress page with generated markdown.
33+ Assumes you have a markdown file with content you want published
34+ to an existing wordpress page.
35+ :param _page_title: post page title
36+ :param _page_id: post page id
37+ :param _markdown: path to markdown file for upload
38+ """
3639
37- # set local content file to read handle info into a string
38- with open (_markdown , "r" ) as _file :
39- page .content = _file .read ()
40+ wp = Client (self .endpoint , self .username , self .password )
4041
41- # post new content to the page
42- wp .call (EditPost (page .id , page ))
42+ # define pages variable
43+ page = WordPressPage ()
44+ page .title = _page_title
45+
46+ # page id can be found by viewing via wp-admin dashboard in URL
47+ page .id = _page_id
48+
49+ # set local content file to read handle info into a string
50+ with open (_markdown , "r" ) as _file :
51+ page .content = _file .read ()
52+
53+ # post new content to the page
54+ wp .call (EditPost (page .id , page ))
4355
4456
4557if __name__ == "__main__" :
@@ -66,7 +78,7 @@ def update_wiki(url, username, password, _page_title, _page_id, _markdown):
6678 dest = "wpurl" ,
6779 type = str ,
6880 default = None ,
69- help = "Specify wordpress URL. e.g. http://wiki.example.com/xmlrpc.php " ,
81+ help = "Specify wordpress URL. e.g. http://wiki.example.com" ,
7082 required = True ,
7183 )
7284 parser .add_argument (
@@ -103,4 +115,5 @@ def update_wiki(url, username, password, _page_title, _page_id, _markdown):
103115 page_title = args .pagetitle
104116 page_id = args .pageid
105117
106- update_wiki (wp_url , wp_username , wp_password , page_title , page_id )
118+ wiki = Wiki (wp_url , wp_username , wp_password )
119+ wiki .update (page_title , page_id , markdown )
0 commit comments