WordPressのアーカイブxmlをHugo用に変換するPythonスクリプト
まずは準備。
code:convertWordPressToHugo.py
# -*- coding: utf-8 -*-
以下は教えてもらった方法をそのまま書いた
code:convertWordPressToHugo.py
import xml.etree.ElementTree as ET
tree = ET.parse("rstylesample.xml")
root = tree.getroot()
# 全ての子要素を取得
for child in root:
print(child)
<Element 'channel' at 0x10b55fae0>が返ってきた
code:convertWordPressToHugo.py
for child in root:
for childchild in child:
print(childchild)
以下が返ってきた。
<Element 'channel' at 0x109af9a90>
<Element 'title' at 0x109af9ae0>
<Element 'link' at 0x109af9b30>
<Element 'description' at 0x109af9bd0>
<Element 'pubDate' at 0x109af9c20>
<Element 'language' at 0x109af9cc0>
<Element 'generator' at 0x109afc3b0>
<Element 'item' at 0x109afc450>
<Element 'item' at 0x109b2df90>
<Element 'item' at 0x109b346d0>
<Element 'item' at 0x109b39a90>
<Element 'item' at 0x109b41040>
<Element 'item' at 0x109b456d0>
<Element 'item' at 0x109b4adb0>
上の方はR-styleのサイトのデータ。
その下に並んでいるitemが記事リスト。
2023/2/4
タグがitemであるかどうかを知りたい
code:convertWordPressToHugo.py
print(childchild.tag)
以下が返ってきた
title
link
description
pubDate
language
generator
item
item
item
item
item
item
item
code:convertWordPressToHugo.py
if childchild.tag == "item":
print(childchild)
code:old_convertWordPressToHugo.py
for topic in childchild:
if (topic.tag == "title"):
print(topic.text)
さらに中身を探索
code:old_2convertWordPressToHugo.py
for topic in childchild:
print(topic.tag)
if (topic.tag == "title"):
print(topic.text)
if (topic.tag == "pubDate"):
print(topic.text)
if (topic.tag == "content:encoded"):
print(topic.text)
print(topic.text)
code:old_3convertWordPressToHugo.py
if childchild.tag == "item":
temphead = []
tempbody = ""
temphead.append(sectionline)
for topic in childchild:
if (topic.tag == "title"):
temphead.append("title: " +topic.text)
if (topic.tag == "pubDate"):
temphead.append("date: " +topic.text)
tempbody = topic.text
else:
temphead.append("draft: false")
temphead.append(sectionline)
temp = "\n".join(temphead) + "\n\n" + tempbody
with open(targetFilePath,mode="w") as f:
f.write(temp)
print("ファイルを作成しました")
code:convertWordPressToHugo.py
if childchild.tag == "item":
temphead = []
tempbody = ""
tempCat = []
tempTag = []
temphead.append(sectionline)
for topic in childchild:
if (topic.tag == "title"):
temphead.append("title : " +topic.text)
if (topic.tag == "pubDate"):
temphead.append("date : " +topic.text)
if (topic.tag == "category" and topic.attrib"domain" == "category"): tempCat.append('"' + urllib.parse.unquote(topic.attrib"nicename") + '"' ) if (topic.tag == "category" and topic.attrib"domain" == "post_tag"): tempTag.append('"' + urllib.parse.unquote(topic.attrib"nicename") + '"') if (topic.tag == "link"):
tempbody = topic.text
else:
temphead.append("categories : +",".join(tempCat) + "")
temphead.append("tags : +",".join(tempTag) + "")
temphead.append("draft : false")
temphead.append("author : 倉下忠憲")
temphead.append(sectionline)
temp = "\n".join(temphead) + "\n\n" + tempbody
with open(targetFilePath,mode="w") as f:
f.write(temp)
print("ファイルを作成しました")
概ねの完成版
code:fixconvertWordPressToHugo.py
# -*- coding: utf-8 -*-
import xml.etree.ElementTree as ET
import urllib.parse
tree = ET.parse("r-style.xml")
root = tree.getroot()
# 全ての子要素を取得
for child in root:
pass
for child in root:
for childchild in child:
if childchild.tag == "item":
temphead = []
tempbody = ""
tempCat = []
tempTag = []
temphead.append(sectionline)
for topic in childchild:
if (topic.tag == "title"):
if topic.text is not None:
artcleTitle = topic.text.replace("/","/")
else:
artcleTitle = "無題"
temphead.append("title : " +artcleTitle)
if (topic.tag == "pubDate"):
if topic.text is not None:
temphead.append("date : " +topic.text)
else:
temphead.append("date : " +"Wed, 01 Mar 2000 00:00:00 +0000")
if (topic.tag == "category" and topic.attrib"domain" == "category"): tempCat.append('"' + urllib.parse.unquote(topic.attrib"nicename") + '"' ) if (topic.tag == "category" and topic.attrib"domain" == "post_tag"): tempTag.append('"' + urllib.parse.unquote(topic.attrib"nicename") + '"') if (topic.tag == "link"):
if topic.text is not None:
tempbody = topic.text
else:
tempbody = "中身なし"
else:
temphead.append("categories : +",".join(tempCat) + "")
temphead.append("tags : +",".join(tempTag) + "")
temphead.append("draft : false")
temphead.append("author : 倉下忠憲")
temphead.append(sectionline)
temp = "\n".join(temphead) + "\n\n" + tempbody
with open("contents/" + targetFilePath,mode="w") as f:
f.write(temp)
print("ファイルを作成しました")