forked from org2blog/org2blog-importers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wp_to_org2blog.py
303 lines (228 loc) · 7.93 KB
/
wp_to_org2blog.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
#!/usr/bin/env python3
"""wp_to_org2blog.py: Convert wordpress.xml to org2blog posts.
To create and export of Posts and nothing else:
- Log in to your WordPress site
- Dashboard, Tools, Export
- Posts
- Categories: All
- Authors: All
- Date range start, end: "Start Date", "End Date"
- Returns every post
- Status: All
- Direct download and add.
"""
__author__ = "Puneeth Chaganti"
__copyright__ = "Copyright 2011"
__license__ = "GPLv3"
__version__ = "0.7"
__email__ = "[email protected]"
import os
import re
import argparse
import logging
from time import strptime, strftime
from xml.dom import minidom
from xml.parsers.expat import ExpatError
from subprocess import Popen, PIPE, SubprocessError
from shlex import split
from urllib.parse import unquote
SUBTREE_TEMPLATE = u"""\
%(stars)s %(title)s %(tags)s
%(space)s :PROPERTIES:
%(space)s :POSTID: %(id)s
%(space)s :POST_DATE: %(date)s
%(space)s :CATEGORY: %(categories)s
%(space)s :END:
%(space)s %(text)s
"""
BUFFER_TEMPLATE = u"""\
#+POSTID: %(id)s
#+DATE: %(date)s
#+OPTIONS: toc:nil num:nil todo:nil pri:nil tags:nil ^:nil TeX:nil
#+CATEGORY: %(categories)s
#+TAGS: %(tags)s
#+TITLE: %(title)s
%(text)s
"""
def html_to_org(html):
"""Converts a html snippet to an org-snippet."""
command = "pandoc -r html -t org --wrap=none -"
args = split(command)
proc = Popen(args, stdin=PIPE, stdout=PIPE, stderr=PIPE)
output, error = proc.communicate(html)
if (proc.returncode != 0) or (error != b""):
errormessage = """%s exited with return code %s and error %s when parsing:
%s"""
raise SubprocessError(errormessage % (args[0], proc.returncode, error, html))
return output
def get_first_child_data(node, tag_name):
"""Try to retrieve the data contained at a node's tag's firstChild."""
try:
return node.getElementsByTagName(tag_name)[0].firstChild.data
except (AttributeError, IndexError):
return None
def node_to_post(node):
"""Takes an XML node from the export and converts it to a dict"""
# key: (XML) tag_name
key_map = {
"title": "title",
"link": "link",
"date": "pubDate",
"author": "dc:creator",
"id": "wp:post_id",
"text": "content:encoded",
"post_name": "wp:post_name",
}
post = dict()
for key, tag_name in key_map.items():
post[key] = get_first_child_data(node, tag_name)
try:
if int(post["id"]) % 10 == 0:
logging.getLogger().info("Processing post #%s", post["id"])
except ValueError:
logging.getLogger().warning("Processing post #%s", post["id"])
if post["text"] is not None:
post["text"] = post["text"].replace("\r\n", "\n")
post["text"] = post["text"].replace("\n", "#$NEWLINE-MARKER$#")
post["text"] = html_to_org(post["text"].encode("utf8")).decode("utf8")
post["text"] = post["text"].replace("#$NEWLINE-MARKER$#", "\n")
else:
post["text"] = ""
# Get the tags and categories
post["tags"] = list()
post["categories"] = list()
for element in node.getElementsByTagName("category"):
domain, name = element.getAttribute("domain"), element.getAttribute("nicename")
if name and domain and ("tag" in domain or "category" in domain):
name = element.firstChild.data
domain = "tags" if "tag" in domain else "categories"
post[domain].append(name)
return post
def xml_to_list(infile):
"""Return a list containing all the posts from the infile.
Each post is a dictionary.
"""
try:
dom = minidom.parse(infile)
except ExpatError as err:
logging.getLogger().error("Error while parsing %s: %s", infile, err)
raise
blog = list() # list that will contain all posts
for node in dom.getElementsByTagName("item"):
post = node_to_post(node)
for domain in ["tags", "categories"]:
post[domain] = sorted(set(post[domain]))
# FIXME - wp:attachment_url could be use to download attachments
blog.append(post)
return blog
def link_to_file(link, post_name=None):
"""Gets filename from wordpress url."""
name = None
check_for_letters = re.compile("[a-z]+", re.IGNORECASE)
try:
if (post_name != "") and (post_name is not None):
if check_for_letters.match(post_name) is not None:
name = post_name
else:
name = None
if name is None:
if "?p=" in link:
name = link.split("?p=")[-1]
else:
name = link.split("/")[-2]
name = "%s.org" % unquote(name)
# occasionally, some encoding errors may seep through
except TypeError:
logging.getLogger().warning(
"Error getting file link for %s, %s", link, post_name
)
return name
def parse_date(date, date_format):
"""Change wp date format to a different format."""
if date is not None:
date = date.split("+")[0].strip()
date = strptime(date, "%a, %d %b %Y %H:%M:%S")
date = strftime(date_format, date)
return date
def blog_to_org(blog_list, name, level, separate_buffer, prefix):
"""Converts a blog-list into an org file."""
space = " " * level
stars = "*" * level
tag_sep = ", "
cat_sep = ", "
template_parts = dict()
if separate_buffer:
template = BUFFER_TEMPLATE
else:
template = SUBTREE_TEMPLATE
tag_sep = ":"
org_file = open("%s.org" % name, "wb")
for post in blog_list:
post["tags"] = tag_sep.join(post["tags"])
if tag_sep == ":":
post["tags"] = ":" + post["tags"] + ":"
post["categories"] = cat_sep.join(post["categories"])
date_wp_fmt = post["date"]
post["date"] = parse_date(date_wp_fmt, "[%Y-%m-%d %a %H:%M]")
if not separate_buffer:
post["text"] = post["text"].replace("\n", "\n %s" % space)
template_parts.update(**post)
template_parts.update(space=space)
template_parts.update(stars=stars)
post_output = template % template_parts
if separate_buffer:
if prefix:
file_name = "%s-%s" % (
parse_date(date_wp_fmt, "%Y-%m-%d"),
link_to_file(post["link"], post["post_name"]),
)
else:
file_name = link_to_file(post["link"], post["post_name"])
if not os.path.exists(name):
os.mkdir(name)
else:
org_file = open(os.path.join(name, file_name), "wb")
org_file.write(post_output.encode("utf8"))
org_file.close()
else:
org_file.write(post_output.encode("utf8"))
org_file.close()
def main():
parser = argparse.ArgumentParser(
description="Convert wordpress.xml to org2blog posts."
)
parser.add_argument("in_file", help="the input xml file exported from WP")
parser.add_argument(
"--buffer",
action="store_true",
help="enable to obtain a separate file for each post",
)
parser.add_argument(
"--prefix-date",
action="store_true",
help="prefix a date to the post files, when --buffer",
)
parser.add_argument(
"-l",
"--level",
type=int,
default=1,
help="level of the subtree when exporting to SUBTREE",
)
parser.add_argument(
"-o",
"--out-file",
default="org-posts",
help="file or directory name for output",
)
args = parser.parse_args()
logging_format = "%(message)s"
logging.basicConfig(format=logging_format, level=logging.INFO)
logger = logging.getLogger()
logger.info("Parsing xml ...")
blog_list = xml_to_list(args.in_file)
logger.info("Writing posts...")
blog_to_org(blog_list, args.out_file, args.level, args.buffer, args.prefix_date)
logger.info("Done!")
if __name__ == "__main__":
main()