-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathwordpress2.py
195 lines (173 loc) · 6.66 KB
/
wordpress2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#! /usr/bin/env python
# -*- coding: utf-8 -*-
#======================================================================
#
# wordpress2.py - wordpress api
#
# Created by skywind on 2019/05/04
# Last Modified: 2019/05/04 23:24:18
#
#======================================================================
from __future__ import print_function, unicode_literals
import sys
import wordpress_xmlrpc
#----------------------------------------------------------------------
# WordPress
#----------------------------------------------------------------------
class WordPress (object):
def __init__ (self, url, username, password):
self._url = self.__url_normalize(url)
self._rpc = self.__parse_url(self._url)
self._username = username
self._password = password
self._client = wordpress_xmlrpc.Client(self._rpc,
self._username, self._password)
def __url_normalize (self, url):
if not url.startswith('https://'):
if not url.startswith('http://'):
url = 'http://' + url
return url
def __parse_url (self, url):
if url.endswith('/xmlrpc.php'):
return url
if not url.endswith('/'):
url = url + '/'
return url + 'xmlrpc.php'
# post['id']: integer uid of the post
# post['content']: string content
# post['title']: string title
# post['status']: string of draft, private, publish
# post['category']: list
# post['tag']: list
# post['comment']: open/closed
# post['date']: datetime object in UTC
def __convert_post (self, post):
newpost = wordpress_xmlrpc.WordPressPost()
if post:
if 'id' in post:
newpost.id = post['id']
if 'title' in post:
newpost.title = post['title']
newpost.content = post.get('content', '')
newpost.post_status = 'draft'
if post.get('status'):
newpost.post_status = post['status']
cats = post.get('category')
tags = post.get('tag')
if cats or tags:
newpost.terms_names = {}
if cats:
newpost.terms_names['category'] = cats
if tags:
newpost.terms_names['post_tag'] = tags
if post.get('comment'):
newpost.comment_status = post['comment']
if post.get('date'):
newpost.date = post['date']
if post.get('date_modifed'):
newpost.date_modified = post['date_modified']
if post.get('slug'):
newpost.slug = post['slug']
else:
newpost.post_status = 'draft'
newpost.comment_status = 'open'
newpost.content = ''
return newpost
def post_new (self, post = None):
newpost = self.__convert_post(post)
action = wordpress_xmlrpc.methods.posts.NewPost(newpost)
pid = self._client.call(action)
return pid
def post_edit (self, post):
if 'id' not in post:
raise ValueError('missing id in post')
newpost = self.__convert_post(post)
pid = post['id']
action = wordpress_xmlrpc.methods.posts.EditPost(pid, newpost)
return self._client.call(action)
def post_get (self, pid):
action = wordpress_xmlrpc.methods.posts.GetPost(pid)
return self._client.call(action)
# keys of query: number, offset, orderby, order(ASC/DESC), post_type
# and post_status
# returns list of WordPressPost instances
def post_list (self, query):
action = wordpress_xmlrpc.methods.posts.GetPosts(query)
return self._client.call(action)
# returns { 'id':xx, 'file':xx, 'url':xx, 'type':xx }
def media_upload (self, source, name, mime = None):
if isinstance(source, str):
content = open(source, 'rb').read()
if hasattr(source, 'read'):
content = source.read()
else:
content = source
if mime is None:
if content[:3] == b'\xff\xd8\xff':
mime = 'image/jpeg'
elif content[:5] == b'\x89PNG\x0d':
mime = 'image/png'
elif content[:2] == b'\x42\x4d':
mime = 'image/x-bmp'
elif content[:5] == b'GIF89':
mime = 'image/gif'
elif content[:2] == b'\x50\x4b':
mime = 'application/zip'
elif content[:3] == b'\x37\x7a\xbc':
mime = 'application/x-7z-compressed'
data = {}
data['name'] = name
data['bits'] = wordpress_xmlrpc.compat.xmlrpc_client.Binary(content)
if mime:
data['type'] = mime
action = wordpress_xmlrpc.methods.media.UploadFile(data)
response = self._client.call(action)
return response
def media_get (self, attachment_id):
action = wordpress_xmlrpc.methods.media.GetMediaItem(attachment_id)
return self._client.call(action)
# keys of query: number, offset, parent_id, mime_type
# returns list of WordPressMedia instances
def media_list (self, query):
action = wordpress_xmlrpc.methods.media.GetMediaLibrary(query)
return self._client.call(action)
#----------------------------------------------------------------------
# testing suit
#----------------------------------------------------------------------
if __name__ == '__main__':
def test1():
wp = WordPress('localhost/web/blog', 'skywind', '678900')
print(wp._rpc)
return 0
def test2():
wp = WordPress('localhost/web/blog', 'skywind', '678900')
pid = wp.post_new()
post = {}
import ascmini
post['id'] = pid
post['content'] = 'Now is: ' + ascmini.timestamp()
post['title'] = 'robot'
post['status'] = 'publish'
post['category'] = ['life']
post['tag'] = ['ai', 'game']
print(wp.post_edit(post))
return 0
def test3():
wp = WordPress('localhost/web/blog', 'skywind', '678900')
post = wp.post_get(40)
print(post)
print(post.link)
print(post.content)
print(post.id)
return 0
def test4():
wp = WordPress('localhost/web/blog', 'skywind', '678900')
query = {}
query['offset'] = 0
for post in wp.post_list(query):
print(post)
print(post.link)
print(post.date)
print(type(post.date), post.date.timestamp())
return 0
test4()