-
Notifications
You must be signed in to change notification settings - Fork 0
/
convert_old_blog.py
71 lines (61 loc) · 1.92 KB
/
convert_old_blog.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import os
import re
import sqlite3
import dateutil.parser
DIR = '/home/rrader/workspace/itkpi.pp.ua/content'
FORMAT = """---
title: "{title}"
date: {date}
draft: false
author: {author}
email: {author_mail}
---
<div class="image-wrapper">
<img src="{image}" class="post-image full-img">
</div>
{content}
{tag_names}
"""
c = sqlite3.connect('/home/rrader/workspace/itkpi.pp.ua/journey.db')
cur = c.cursor()
for row in cur.execute(
"select title, slug, markdown, published_at, published_by, image, id from posts where status = 'published'"
):
path = DIR + '/{}.md'.format(row[1])
if not os.path.exists(path):
continue
with open(path, 'w') as f:
cont = row[2].decode()
cont = re.sub(r'\n(\#+)', r'\n\1 ', cont)
image = ''
if row[5]:
image = row[5].decode()
if '[DRAFT]' in row[0].decode() or '[TODO]' in row[0].decode():
continue
cur2 = c.cursor()
user = tuple(cur2.execute(
"select name, email from users where id = {}".format(row[4])
))
author = user[0][0].decode()
author_mail = user[0][1].decode()
cur3 = c.cursor()
tags_ids = [
str(row[0])
for row in
cur3.execute(
"select tag_id from posts_tags where post_id = {}".format(row[6])
)
]
tag_names = [
row[0].decode() for row in
cur3.execute(
"select name from tags where id in ({})".format(','.join(tags_ids))
)
]
if tag_names:
tag_names = 'Tags: ' + ', '.join(tag_names)
else:
tag_names = ''
f.write(FORMAT.format(title=row[0].decode().replace('"', '\\"'), date=dateutil.parser.parse(row[3]).isoformat(),
content=cont, image=image, author=author, author_mail=author_mail, tag_names=tag_names))
print(path, 'saved')