Skip to content

Instantly share code, notes, and snippets.

@qqAys
Created June 7, 2024 07:05
Show Gist options
  • Save qqAys/1f881da977947ce7d5026cfb0a11efe9 to your computer and use it in GitHub Desktop.
Save qqAys/1f881da977947ce7d5026cfb0a11efe9 to your computer and use it in GitHub Desktop.
typecho2mkdocs
import pymysql
from pathlib import Path
result_dir = Path("./typecho_export")
result_dir.mkdir(parents=True, exist_ok=True)
conn = pymysql.Connect(host='example.com', user='user', passwd='passwd', db='typecho')
cursor = conn.cursor()
sql = """
select title,
slug,
tu.screenName,
ifnull(from_unixtime(tc.created), 'null') as 'created',
ifnull(from_unixtime(tc.modified), 'null') as 'modified',
text
from typecho_contents tc
left join typecho_users tu on tc.authorId = tu.uid
where type = 'post'
and authorId = 1;"""
cursor.execute(sql)
result = cursor.fetchall()
for i in result:
title, slug, author, created, modified, content = i
title: str = title.replace(" ", "").replace("!", "!")
content: str = content.replace("<!--more-->", "<!-- more -->").replace("<!--markdown-->", "")
print(title, slug, author, created, modified)
with open(Path(result_dir, f'{title}.md'), mode='w') as md:
text = f"""---
comments: true
date:
created: {created}
updated: {modified}
authors:
- {author}
slug: {slug}
---
# {title}
{content}"""
md.write(text)
cursor.close()
conn.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment