Created
April 18, 2016 13:00
-
-
Save zhu327/836c40e09ca924f4c38d277bdb1aa845 to your computer and use it in GitHub Desktop.
从blog迁移到hugo脚本
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# coding: utf-8 | |
import MySQLdb | |
connect = MySQLdb.connect(user='root', passwd='', db='blog', host='127.0.0.1', port=3306) | |
cursor = connect.cursor() | |
cursor.execute('SELECT title, content, tags, created FROM blogs') | |
result = cursor.fetchall() | |
with open('template.md.bak', 'r') as f: | |
template = f.read() | |
for log in result: | |
data = { | |
'title': log[0], | |
'tags': '", "'.join(log[2].split(',')), | |
'date': log[3].strftime('%Y-%m-%dT%H:%M:%S+08:00') | |
} | |
content = log[1].split('\n') | |
count = index = 0 | |
for i, line in enumerate(content): | |
count += line.count('```') | |
if i >= 9 and (count % 2 == 0): | |
index = i+1 | |
break | |
content.insert(index, '<!--more-->') | |
data['content'] = '\n'.join(content) | |
with open('{}-{}.md'.format(log[3].strftime('%Y-%m-%d'), data['title']), 'w') as f: | |
f.write(template.format(**data)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment