将Hexo的JSON转换为WordPress的WXR

This commit is contained in:
huangge1199 2024-07-18 15:23:17 +08:00
parent c58a9da126
commit d88d7e5715
2 changed files with 115 additions and 0 deletions

1
content.json Normal file

File diff suppressed because one or more lines are too long

114
hexo_to_wp.py Normal file
View File

@ -0,0 +1,114 @@
import json
import datetime
import xml.etree.ElementTree as ET
from xml.dom import minidom
def create_wxr_element():
root = ET.Element("rss", version="2.0",
attrib={
"xmlns:excerpt": "http://wordpress.org/export/1.2/excerpt/",
"xmlns:content": "http://purl.org/rss/1.0/modules/content/",
"xmlns:wfw": "http://wellformedweb.org/CommentAPI/",
"xmlns:dc": "http://purl.org/dc/elements/1.1/",
"xmlns:wp": "http://wordpress.org/export/1.2/"
})
channel = ET.SubElement(root, "channel")
title = ET.SubElement(channel, "title")
title.text = "Hexo to WordPress"
link = ET.SubElement(channel, "link")
link.text = ""
description = ET.SubElement(channel, "description")
description.text = ""
pubDate = ET.SubElement(channel, "pubDate")
pubDate.text = datetime.datetime.now().strftime('%a, %d %b %Y %H:%M:%S +0000')
language = ET.SubElement(channel, "language")
language.text = "en"
wxr_version = ET.SubElement(channel, "wp:wxr_version")
wxr_version.text = "1.2"
return root, channel
def add_post_to_channel(channel, post):
item = ET.SubElement(channel, "item")
title = ET.SubElement(item, "title")
title.text = post['title']
link = ET.SubElement(item, "link")
link.text = post['link']
pubDate = ET.SubElement(item, "pubDate")
pubDate.text = post['date']
dc_creator = ET.SubElement(item, "dc:creator")
dc_creator.text = 'admin'
guid = ET.SubElement(item, "guid", isPermaLink="false")
guid.text = post['link']
description = ET.SubElement(item, "description")
description.text = ""
content_encoded = ET.SubElement(item, "content:encoded")
content_encoded.text = post['content']
excerpt_encoded = ET.SubElement(item, "excerpt:encoded")
excerpt_encoded.text = ""
post_id = ET.SubElement(item, "wp:post_id")
post_id.text = post['path']
post_date = ET.SubElement(item, "wp:post_date")
post_date.text = post['date']
post_date_gmt = ET.SubElement(item, "wp:post_date_gmt")
post_date_gmt.text = post['date']
comment_status = ET.SubElement(item, "wp:comment_status")
comment_status.text = "open"
ping_status = ET.SubElement(item, "wp:ping_status")
ping_status.text = "open"
post_name = ET.SubElement(item, "wp:post_name")
post_name.text = post['slug']
status = ET.SubElement(item, "wp:status")
status.text = "publish"
post_parent = ET.SubElement(item, "wp:post_parent")
post_parent.text = "0"
menu_order = ET.SubElement(item, "wp:menu_order")
menu_order.text = "0"
post_type = ET.SubElement(item, "wp:post_type")
post_type.text = "post"
post_password = ET.SubElement(item, "wp:post_password")
post_password.text = ""
is_sticky = ET.SubElement(item, "wp:is_sticky")
is_sticky.text = "0"
# 添加分类和标签
for category in post.get('categories', []):
cat = ET.SubElement(item, "category", domain="category", nicename=category['name'])
cat.text = category['name']
for tag in post.get('tags', []):
tg = ET.SubElement(item, "category", domain="post_tag", nicename=tag['name'])
tg.text = tag['name']
def prettify(element):
"""Return a pretty-printed XML string for the Element."""
rough_string = ET.tostring(element, 'utf-8')
reparsed = minidom.parseString(rough_string)
return reparsed.toprettyxml(indent=" ")
def save_wxr_file(root, filename):
pretty_xml = prettify(root)
with open(filename, 'w', encoding='utf-8') as f:
f.write(pretty_xml)
def main():
with open('content.json', 'r', encoding='utf-8') as f:
hexo_data = json.load(f)
root, channel = create_wxr_element()
for post in hexo_data['posts']:
add_post_to_channel(channel, post)
save_wxr_file(root, 'hexo_to_wp.xml')
if __name__ == "__main__":
main()