最近在GitHub上找到一个写Typecho转Hexo的大佬,可惜我是最新版的Python,运行直接报错,修改一个如同大洪水一样又报错很多。
于是在他的基础上,用python3的包来替换原来的一些写的方法。
代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
|
import os import re import pymysql import arrow from flask import Flask import urllib import codecs
host = '' port = 3306 db = '' user = '' password = ''
def main(): conn = pymysql.connect(host=host, port=port, db=db, user=user, password=password) cursor = conn.cursor(pymysql.cursors.DictCursor) cursor.execute("select type, slug, name from typecho_metas") for cate in cursor.fetchall(): path = 'data/分类/%s' % urllib.parse.unquote(cate['slug']) if not os.path.exists(path): os.makedirs(path) f = codecs.open('%s/index.md' % path, 'w', "utf-8") f.write("title: %s\n" % urllib.parse.unquote(cate['slug'])) f.write("date: %s\n" % arrow.now().format('YYYY-MM-DD HH:mm:ss')) if cate['type'] == 'category': f.write('type: "categories"\n') elif cate['type'] == 'tags': f.write('type: "tags"\n') f.write("comments: false\n") f.write("---\n") f.close()
cursor.execute("select cid, title, slug, text, created from typecho_contents where type='post'") for e in cursor.fetchall(): title = re.sub('[\/:*?"<>|]','-',e['title'].encode('raw_unicode_escape').decode("unicode-escape")) content = str(e['text'].replace('', '')) tags = [] category = "" cursor.execute( "select type, name, slug from `typecho_relationships` ts, typecho_metas tm where tm.mid = ts.mid and ts.cid = %s", e['cid']) for m in cursor.fetchall(): if m['type'] == 'tag': tags.append(m['name']) if m['type'] == 'category': category = urllib.parse.unquote(m['slug']) path = 'data/文章/' if not os.path.exists(path): os.makedirs(path) f = codecs.open('%s%s.md' % (path, title), 'w', "utf-8") f.write("---\n") f.write("title: %s\n" % title) f.write("date: %s\n" % arrow.get(e['created']).format('YYYY-MM-DD HH:mm:ss')) f.write("categories: %s\n" % category) f.write("tags: [%s]\n" % ','.join(tags)) f.write("---\n") f.write(content) f.close() conn.close()
if __name__ == "__main__": main()
|
复制粘贴,填写你的数据库信息即可
由诺依阁提供Hexo转Typecho软件支持