Skip to content

Instantly share code, notes, and snippets.

@Lusitaniae
Created May 2, 2020 10:24
Show Gist options
  • Save Lusitaniae/d15e573317bdfa3f09fd71d990735146 to your computer and use it in GitHub Desktop.
Save Lusitaniae/d15e573317bdfa3f09fd71d990735146 to your computer and use it in GitHub Desktop.
Convert WordPress dump into Hugo content yml files
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import xmltodict
import time
import hashlib
total_count = 0
m = hashlib.md5()
with open('wp.xml', 'rt', encoding="utf-8") as fd:
cfg = xmltodict.parse(fd.read())
for item in cfg['rss']['channel']['item']:
try:
for comment in item['wp:comment']:
if comment['wp:comment_approved'] == "1":
total_count += 1
try:
date_time = comment['wp:comment_date_gmt']
pattern = '%Y-%m-%d %H:%M:%S'
epoch = int(time.mktime(time.strptime(date_time, pattern)))
file = "comment-" + str(epoch) + ".yml"
except Exception as e:
print(e)
pass
try:
title = item['title']
except Exception as e:
print(e)
pass
try:
id = comment['wp:comment_id']
except Exception as e:
print(e)
pass
try:
name = comment['wp:comment_author']
except Exception as e:
print(e)
pass
try:
reply_to = ""
if comment['wp:comment_parent'] != '0':
reply_to = comment['wp:comment_parent']
except Exception as e:
print(e)
pass
try:
temp_email = comment['wp:comment_author_email']
temp_email = temp_email.strip()
temp_email = temp_email.lower()
temp_email = temp_email.encode('utf-8')
m.update(temp_email)
email = m.hexdigest()
except Exception as e:
print(e)
pass
try:
body = comment['wp:comment_content'].replace('\n', '\n ')
except Exception as e:
print(e)
pass
try:
date = comment['wp:comment_date_gmt']
except Exception as e:
print(e)
pass
content = """---
_id: %s
post_title: %s
name: %s
reply_to: %s
email: %s
date: %s
body: |
%s
""" % (id, title, name, reply_to, email, date, body)
print("creating " + file)
with open ('tmp/'+file, 'w', encoding="utf-8") as f: f.write (content)
except Exception as e:
print(e)
pass
print("Total Count: " + str(total_count))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment