之前网站由Flarum搭建,由于种种原因网站宕机,且一直没有维护。 幸好数据库还是保持下来, 最后经过考虑还是迁移到Typecho。

由于这个程序保存数据库格式不一样,就使用了工具和代码辅助迁移。

数据导出

通过navicat将原Flarum文章数据,与新安装好的Typecho文章数据导出为json
ppGX2X8.png

得到两个文件 flarum_posts.json,typecho_contents.json

数据转化

import json
import re
import time

# 导入原始数据
flarum_data = json.load(open("flarum_posts.json", 'r+', encoding="utf8"))

# print(type(data["RECORDS"]))

raw_data = flarum_data["RECORDS"]
# print(raw_data[0]["content"])

# 删除flarum中的tag
clean = re.compile('<.*?>')

# 获取目标文件模板
typecho_data = json.load(open("typecho_contents.json", 'r+', encoding="utf8"))
# print()
typecho_contents_demo = typecho_data["RECORDS"][1]
typecho_data["RECORDS"] = []

# 开始转换
cid = 7
for flarum_i in raw_data:
    # 不能使用 =
    temp_dome = typecho_contents_demo.copy()

    temp_dome["cid"] = cid
    temp_dome["slug"] = cid

    temp_content = re.sub(clean, '', flarum_i["content"]).replace("'", '"')
    if len(temp_content) < 150:
        print(temp_content)
        continue
    temp_dome["title"] = temp_content.split("\n", 1)[0].replace("#", "").strip().replace("  ", "").replace("*","").replace("&lt;","<").replace("&gt;",">")
    temp_dome["text"] = "<!--markdown-->" + temp_content

    # 时间
    temp_time = time.strptime(flarum_i["created_at"], "%d/%m/%Y %H:%M:%S")
    temp_dome["created"] = int(time.mktime(temp_time))
    temp_dome["modified"] = temp_dome["created"]

    typecho_data["RECORDS"].append(temp_dome)
    # print(typecho_contents_demo)

    cid += 1

filename = 'result.json'
with open(filename, 'w', encoding='utf8') as f:
    json.dump(typecho_data, f, ensure_ascii=False)

(代码比较粗糙,泛用性比较低,不能完全完成迁移操作,仅仅实现辅助作用。)

数据恢复

运行上面的代码,会得到result.json,通过navicat 导入数据。

文章目录