forked from arty-name/livejournal-export
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexport.py
executable file
·352 lines (254 loc) · 9.77 KB
/
export.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
#!/usr/bin/env python3
import json
import sys
import os
import subprocess
import re
import html2text
import markdown
from bs4 import BeautifulSoup
from datetime import datetime
from operator import itemgetter
from livejournaldl import LiveJournalDL
TAG = re.compile(r'\[!\[(.*?)\]\(http:\/\/utx.ambience.ru\/img\/.*?\)\]\(.*?\)')
USER = re.compile(r'<lj user="?(.*?)"?>')
TAGLESS_NEWLINES = re.compile('(?<!>)\n')
NEWLINES = re.compile('(\s*\n){3,}')
# Special Markdown characters to escape.
# Not included because I don't think they'll cause an issue:
# '{', '}', '.', '!', '(', ')'
MDCHARS = ['*', '_', '[', ']', '#', '+', '-']
SLUGS = {}
def fix_user_links(json):
""" replace user links with usernames """
if 'subject' in json:
json['subject'] = USER.sub(r'\1', json['subject'])
if 'body' in json and json['body']:
json['body'] = USER.sub(r'\1', json['body'])
def json_to_html(json):
if json['body']:
b = TAGLESS_NEWLINES.sub('<br>\n', json['body'])
else:
b = ''
return """<!doctype html>
<meta charset="utf-8">
<title>{subject}</title>
<article>
<h1>{subject}</h1>
{body}
</article>
""".format(
subject=json['subject'] or json['date'],
body=b
)
def get_slug(json):
slug = json['subject']
if not len(slug):
slug = json['id']
if '<' in slug or '&' in slug:
slug = BeautifulSoup('<p>{0}</p>'.format(slug)).text
slug = re.compile(r'\W+').sub('-', slug)
slug = re.compile(r'^-|-$').sub('', slug)
if slug in SLUGS:
slug += (len(slug) and '-' or '') + json['id']
SLUGS[slug] = True
return slug
def json_to_markdown(json):
if json['body']:
body = TAGLESS_NEWLINES.sub('<br>', json['body'])
else:
body = ''
h = html2text.HTML2Text()
h.body_width = 0
h.unicode_snob = True
body = h.handle(body)
body = NEWLINES.sub('\n\n', body)
# read UTX tags
tags = TAG.findall(body)
json['tags'] = len(tags) and '\ntags: {0}'.format(', '.join(tags)) or ''
# remove UTX tags from text
json['body'] = TAG.sub('', body).strip()
json['slug'] = get_slug(json)
json['subject'] = json['subject'] or json['date']
return """id: {id}
title: {subject}
slug: {slug}
date: {date}{tags}
{body}
""".format(**json)
# Converts to markdown formatted for Day One
def json_to_dayone(json):
if json['body']:
body = TAGLESS_NEWLINES.sub('<br>', json['body'])
else:
body = ''
h = html2text.HTML2Text()
h.body_width = 0
h.unicode_snob = True
body = h.handle(body)
body = NEWLINES.sub('\n\n', body)
# read UTX tags
tags = TAG.findall(body)
json['tags'] = len(tags) and '\ntags: {0}'.format(', '.join(tags)) or ''
# remove UTX tags from text
json['body'] = TAG.sub('', body).strip()
json['slug'] = get_slug(json)
json['subject'] = json['subject'] or json['date']
return """# {subject}
{body}
""".format(**json)
def group_comments_by_post(comments):
posts = {}
for comment in comments:
post_id = comment['jitemid']
if post_id not in posts:
posts[post_id] = {}
post = posts[post_id]
post[comment['id']] = comment
return posts
def nest_comments(comments):
post = []
for comment in comments.values():
fix_user_links(comment)
if 'parentid' not in comment:
post.append(comment)
else:
comments[comment['parentid']]['children'].append(comment)
return post
def comment_to_li(comment):
if 'state' in comment and comment['state'] == 'D':
return ''
html = '<h3>{0}: {1}</h3>'.format(comment.get('author', 'anonym'), comment.get('subject', ''))
html += '\n<a id="comment-{0}"></a>'.format(comment['id'])
if 'body' in comment:
html += '\n' + markdown.markdown(TAGLESS_NEWLINES.sub('<br>\n', comment['body']))
if len(comment['children']) > 0:
html += '\n' + comments_to_html(comment['children'])
subject_class = 'subject' in comment and ' class=subject' or ''
return '<li{0}>{1}\n</li>'.format(subject_class, html)
def comments_to_html(comments):
return '<ul>\n{0}\n</ul>'.format('\n'.join(map(comment_to_li, sorted(comments, key=itemgetter('id')))))
# Format comments for Day One export
def comment_to_dayone(comment):
if 'state' in comment and comment['state'] == 'D':
return ''
md = ''
if 'author' in comment:
md += '> **[{0}](https://{0}.livejournal.com/)**\n'.format(comment['author'])
else:
md += '> _anonymous_\n'
if 'subject' in comment:
md += '> **{0}**\n'.format(comment['subject'])
md += '> \n'
if 'body' in comment:
b = comment['body']
# Escape markdown special characters
for c in MDCHARS:
if c in b:
b = b.replace(c, "\\" + c)
md += '> ' + '> '.join(b.splitlines(True)) + '\n'
# If this comment has children, process them recursively
if len(comment['children']) > 0:
md += '>' + '>'.join(comments_to_dayone(comment['children']).splitlines(True)) + '\n'
return md
# Format comments for Day One export
def comments_to_dayone(comments):
return '\n{0}'.format('\n'.join(map(comment_to_dayone, sorted(comments, key=itemgetter('id')))))
def save_as_json(id, json_post, post_comments):
json_data = {'id': id, 'post': json_post, 'comments': post_comments}
with open('posts-json/{0}.json'.format(id), 'w', encoding='utf-8') as f:
f.write(json.dumps(json_data, ensure_ascii=False, indent=2))
def save_as_html(id, subfolder, json_post, post_comments_html):
os.makedirs('posts-html/{0}'.format(subfolder), exist_ok=True)
with open('posts-html/{0}/{1}.html'.format(subfolder, id), 'w', encoding='utf-8') as f:
f.writelines(json_to_html(json_post))
if post_comments_html:
f.write('\n<h2>Comments</h2>\n' + post_comments_html)
def save_as_markdown(id, subfolder, json_post, post_comments_html):
os.makedirs('posts-markdown/{0}'.format(subfolder), exist_ok=True)
with open('posts-markdown/{0}/{1}.md'.format(subfolder, id), 'w', encoding='utf-8') as f:
f.write(json_to_markdown(json_post))
if post_comments_html:
with open('comments-markdown/{0}.md'.format(json_post['slug']), 'w', encoding='utf-8') as f:
f.write(post_comments_html)
# Added by Erick for Day One format
def save_as_dayone(id, subfolder, json_post, post_comments):
os.makedirs('posts-dayone/{0}'.format(subfolder), exist_ok=True)
with open('posts-dayone/{0}/{1}.md'.format(subfolder, id), 'w', encoding='utf-8') as f:
# Write out to text file
f.write(json_to_dayone(json_post))
if post_comments:
f.write('\n\n')
f.write(post_comments)
# Send to Day One
tags = []
if config.get('DAYONE_TAGS'):
tags += config.get('DAYONE_TAGS').split(' ')
if json_post.get('mood'):
tags.append(json_post.get('mood'))
cmd = [
config['DAYONE_CMD'],
'--date="{0}"'.format(json_post['eventtime']),
'--journal', config['DAYONE_JOURNAL']
]
if tags:
cmd.append('--tags')
cmd += tags
cmd.append('--')
cmd.append('new')
with open('posts-dayone/{0}/{1}.md'.format(subfolder, id), 'r', encoding='utf-8') as f:
out = subprocess.check_output(cmd, stdin=f)
print('Day One: ' + out.decode().strip())
def load_from_json(file):
try:
f = open(file, 'r', encoding='utf-8')
except IOError as err:
print("\nError opening file: {0}".format(err))
sys.exit('Failed to find data. Exiting.')
else:
print('Loading from file: ' + file + "\n")
with f:
return json.load(f)
def combine(all_posts, all_comments):
posts_comments = group_comments_by_post(all_comments)
for json_post in all_posts:
id = json_post['id']
jitemid = int(id) >> 8
date = datetime.strptime(json_post['date'], '%Y-%m-%d %H:%M:%S')
subfolder = '{0.year}-{0.month:02d}'.format(date)
post_comments = jitemid in posts_comments and nest_comments(posts_comments[jitemid]) or None
post_comments_html = post_comments and comments_to_html(post_comments) or ''
post_comments_dayone = post_comments and comments_to_dayone(post_comments) or ''
fix_user_links(json_post)
if config['EXPORT_JSON']:
save_as_json(id, json_post, post_comments)
if config['EXPORT_HTML']:
os.makedirs('posts-html', exist_ok=True)
save_as_html(id, subfolder, json_post, post_comments_html)
if config['EXPORT_MARKDOWN']:
os.makedirs('posts-markdown', exist_ok=True)
os.makedirs('comments-markdown', exist_ok=True)
save_as_markdown(id, subfolder, json_post, post_comments_html)
if config['EXPORT_DAYONE']:
os.makedirs('posts-dayone', exist_ok=True)
save_as_dayone(id, subfolder, json_post, post_comments_dayone)
if __name__ == '__main__':
# Load config from file
with open('config.json', 'r', encoding='utf-8') as f:
config = json.load(f)
if config['GET_POSTS'] or config['GET_COMMENTS']:
# Log in to LiveJournal
lj = LiveJournalDL()
if lj.login(config['USERNAME'], config['PASSWORD']):
print('\nLogged into LiveJournal as ' + config['USERNAME'] + "...\n")
else:
sys.exit('Logging into LiveJournal failed. Check username and password in config.json.')
if config['GET_POSTS']:
all_posts = lj.download_posts(config['BEGIN_YEAR'], config['END_YEAR'])
else:
all_posts = load_from_json('posts-json/all.json')
if config['GET_COMMENTS']:
all_comments = lj.download_comments()
else:
all_comments = load_from_json('comments-json/all.json')
combine(all_posts, all_comments)