Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 5cc9ad6

Browse files
committedJul 7, 2015
Added support for exporting comments.
1 parent 1511bfc commit 5cc9ad6

File tree

1 file changed

+73
-0
lines changed

1 file changed

+73
-0
lines changed
 

‎nikola/plugins/command/import_wordpress.py

+73
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,13 @@ class CommandImportWordpress(Command, ImportMixin):
131131
'type': bool,
132132
'help': "Export categories as categories, instead of treating them as tags",
133133
},
134+
{
135+
'name': 'export_comments',
136+
'long': 'export-comments',
137+
'default': False,
138+
'type': bool,
139+
'help': "Export comments as .wpcomment files",
140+
},
134141
]
135142
all_tags = set([])
136143

@@ -160,6 +167,7 @@ def _read_options(self, options, args):
160167
self.import_empty_items = options.get('include_empty_items', False)
161168

162169
self.export_categories_as_categories = options.get('export_categories_as_categories', False)
170+
self.read_comments = options.get('read_comments', False)
163171

164172
self.auth = None
165173
if options.get('download_auth') is not None:
@@ -478,6 +486,59 @@ def transform_content(self, content, post_format):
478486
else:
479487
return None
480488

489+
def _extract_comment(self, comment, wordpress_namespace):
490+
id = int(get_text_tag(comment, "{{{0}}}comment_id".format(wordpress_namespace), None))
491+
author = get_text_tag(comment, "{{{0}}}comment_author".format(wordpress_namespace), None)
492+
author_email = get_text_tag(comment, "{{{0}}}comment_author_email".format(wordpress_namespace), None)
493+
author_url = get_text_tag(comment, "{{{0}}}comment_author_url".format(wordpress_namespace), None)
494+
author_IP = get_text_tag(comment, "{{{0}}}comment_author_IP".format(wordpress_namespace), None)
495+
# date = get_text_tag(comment, "{{{0}}}comment_date".format(wordpress_namespace), None)
496+
date_gmt = get_text_tag(comment, "{{{0}}}comment_date_gmt".format(wordpress_namespace), None)
497+
content = get_text_tag(comment, "{{{0}}}comment_content".format(wordpress_namespace), None)
498+
approved = get_text_tag(comment, "{{{0}}}comment_approved".format(wordpress_namespace), '0')
499+
if approved == '0':
500+
approved = 'hold'
501+
elif approved == '1':
502+
approved = 'approved'
503+
elif approved == 'spam' or approved == 'trash':
504+
pass
505+
else:
506+
LOGGER.warn("Unknown comment approved status: " + str(approved))
507+
parent = int(get_text_tag(comment, "{{{0}}}comment_parent".format(wordpress_namespace), 0))
508+
if parent == 0:
509+
parent = None
510+
user_id = int(get_text_tag(comment, "{{{0}}}comment_user_id".format(wordpress_namespace), 0))
511+
if user_id == 0:
512+
user_id = None
513+
514+
if approved == 'trash' or approved == 'spam':
515+
return None
516+
517+
return {"id": id, "status": str(approved), "approved": approved == "approved",
518+
"author": author, "email": author_email, "url": author_url, "ip": author_IP,
519+
"date": date_gmt, "content": content, "parent": parent, "user_id": user_id}
520+
521+
def _write_comment(self, filename, comment):
522+
def write_header_line(fd, header_field, header_content):
523+
if header_content is None:
524+
return
525+
header_content = str(header_content).replace('\n', ' ')
526+
line = '.. ' + header_field + ': ' + header_content + '\n'
527+
fd.write(line.encode('utf8'))
528+
529+
with open(filename, "wb+") as fd:
530+
write_header_line(fd, "id", comment["id"])
531+
write_header_line(fd, "status", comment["status"])
532+
write_header_line(fd, "approved", comment["approved"])
533+
write_header_line(fd, "author", comment["author"])
534+
write_header_line(fd, "author_email", comment["email"])
535+
write_header_line(fd, "author_url", comment["url"])
536+
write_header_line(fd, "author_IP", comment["ip"])
537+
write_header_line(fd, "date_utc", comment["date"])
538+
write_header_line(fd, "parent_id", comment["parent"])
539+
write_header_line(fd, "wordpress_user_id", comment["user_id"])
540+
fd.write(('\n' + comment['content']).encode('utf8'))
541+
481542
def _create_metadata(self, status, excerpt, tags, categories, post_name=None):
482543
other_meta = {'wp-status': status}
483544
if excerpt is not None:
@@ -648,6 +709,18 @@ def import_item(self, item, wordpress_namespace, out_folder=None):
648709
os.path.join(self.output_folder,
649710
out_folder, out_content_filename),
650711
content)
712+
713+
if self.read_comments:
714+
comments = []
715+
for tag in item.findall('{{{0}}}comment'.format(wordpress_namespace)):
716+
comment = self._extract_comment(tag, wordpress_namespace)
717+
if comment is not None:
718+
comments.append(comment)
719+
720+
for comment in comments:
721+
comment_filename = out_folder_slug[1] + "." + str(comment['id']) + ".wpcomment"
722+
self._write_comment(os.path.join(self.output_folder, out_folder_slug[0], comment_filename), comment)
723+
651724
return (out_folder, slug)
652725
else:
653726
LOGGER.warn(('Not going to import "{0}" because it seems to contain'

0 commit comments

Comments
 (0)
Please sign in to comment.