-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathghost2hugo.py
54 lines (44 loc) · 1.33 KB
/
ghost2hugo.py
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
#!/usr/bin/python3
import sqlite3
import json
from datetime import datetime
conn = sqlite3.connect('ghost.db')
conn.row_factory = sqlite3.Row
c = conn.cursor()
c2 = conn.cursor()
for i in c.execute('''
SELECT id, title, meta_description as description, slug, markdown as text,
status as draft, page, meta_title, image,
DATE(published_at/1000, "unixepoch") as date,
DATE(created_at/1000, "unixepoch") as date2
FROM posts
'''):
g = {i.keys()[e]: tuple(i)[e] for e in range(len(i.keys()))}
t = (i['id'],)
g['tags'] = [e['name'] for e in c2.execute('''
SELECT t.name FROM posts_tags pt JOIN tags t ON pt.tag_id = t.id
WHERE pt.post_id=?''', t)]
if g['date'] == None:
g['date'] = g['date2']
if g['draft'] == 'published':
g['draft'] = False
else:
g['draft'] = True
g.pop('date2')
text = g.pop('text')
text = text.replace("# ", "#")
text = text.replace("#", "# ")
text = text.replace("# # # ", "### ")
text = text.replace("# # ", "## ")
text = text.replace("\# ", "\#")
if g['page'] == True:
page = 'page'
else:
page = 'post'
g['type'] = page
g.pop('page')
f = open("./content/%s.md" % (g['title']), "w")
f.write(json.dumps(g, sort_keys=True, indent=4, separators=(',', ': ')))
f.write('\n\n\n')
f.write(text)
f.close()