-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpublish_to_wp.py
191 lines (137 loc) · 4.25 KB
/
publish_to_wp.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
#!/usr/bin/env python
import xmlrpclib
import sys
import os
import mimetypes
import pprint
import re
import urlparse
import json
import hashlib
import codecs
#publish page user passwd http://localhost/wordpress/xmlrpc.php file
HOME = os.environ['HOME']
pp = pprint.PrettyPrinter(indent=4)
url = sys.argv[4]
user = sys.argv[2]
pw = sys.argv[3]
post_type = sys.argv[1]
title = 'new post'
slug = None
do_publish = False
REGISTRY_FILE = HOME+'/Library/Containers/com.onflapp.TextSer/Data/Library/Caches/com.onflapp.TextSer/'+re.sub(r'[:/]', '', url)+'_registry.json'
server = xmlrpclib.ServerProxy(url)
def log_DEBUG(log):
print(log)
def display_result(postid):
result = page_info(postid)
print (result['link'])
def page_info(postid):
result = server.wp.getPage(0, postid, user, pw)
#pp.pprint(result)
return result
def store_page(title, slug, body):
content = {}
content['title'] = title
content['description'] = body
content['post_type'] = 'page'
if (slug != None):
content['wp_slug'] = slug
postid = find_pageid(title)
if (postid == None):
result = server.metaWeblog.newPost(0, user, pw, content, do_publish)
if (result != None):
display_result(result)
else:
result = server.metaWeblog.editPost(postid, user, pw, content, do_publish)
if (result == True):
display_result(postid)
def store_post(title, slug, body):
content = {}
content['title'] = title
content['description'] = body
if (post_type == 'page'):
content['post_type'] = 'page'
if (slug != None):
content['wp_slug'] = slug
log_DEBUG('store post ' + title)
postid = find_postid(title)
if (postid == None):
result = server.metaWeblog.newPost(0, user, pw, content, False)
if (result != None):
display_result(result)
else:
result = server.metaWeblog.editPost(postid, user, pw, content, False)
if (result == True):
display_result(postid)
def find_postid(title):
result = server.metaWeblog.getRecentPosts(0, user, pw, 9999)
for it in result:
if (it['title'] == title):
return it['postid']
return None
def find_pageid(title):
result = server.wp.getPageList(0, user, pw)
for it in result:
if (it['page_title'] == title):
postinfo = page_info(it['page_id'])
if (postinfo['page_status'] != 'trash'):
return it['page_id']
return None
def upload_file(fname):
with open(fname, 'rb') as f:
data = f.read()
md5 = hashlib.md5(data).hexdigest()
bfile = os.path.basename(fname)
if (registry.get(bfile) == md5):
log_DEBUG('skipping ' + bfile + ', uploaded already')
return registry[bfile+'_url']
bits = xmlrpclib.Binary(data)
content = {}
content['bits'] = bits
content['type'] = mimetypes.guess_type(fname)[0]
content['name'] = os.path.basename(fname)
result = server.metaWeblog.newMediaObject(0, user, pw, content)
registry[bfile+'_url'] = result['url']
registry[bfile] = md5
print ("uploaded new file as " + result['url'])
return unicode(result['url'])
### start
print ("posting to " + url)
print ("posted images registry: " + REGISTRY_FILE)
print ("---")
if (os.path.isfile(REGISTRY_FILE)):
with codecs.open(REGISTRY_FILE, encoding='utf-8') as f:
registry = json.load(f)
else:
registry = {}
with codecs.open(sys.argv[5], encoding='utf-8') as f:
lines = f.readlines()
body = []
images = {}
for it in lines:
m = re.match(r'^%\s?file:\s*(.*)\n$', it)
if (m):
f = m.group(1)
a = upload_file(f)
images[os.path.basename(f)] = a
continue
m = re.match(r'^%\s?title:\s*(.*)\n$', it)
if (m):
title = m.group(1)
continue
m = re.match(r'^%\s?slug:\s*(.*)\n$', it)
if (m):
slug = m.group(1)
continue
for key in images.keys():
img = images[key]
it = it.replace('<img src="'+key+'"', '<img src="'+img+'"')
body.append(it)
if (post_type == 'page'):
store_page(title, slug, ''.join(body))
else:
store_post(title, slug, ''.join(body))
#### save registry
with open(REGISTRY_FILE, 'w') as f:
json.dump(registry, f)