-
Notifications
You must be signed in to change notification settings - Fork 4
/
tg2md.py
311 lines (228 loc) · 8.6 KB
/
tg2md.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
#!/usr/bin/env python
# parse.py - converts telegram json to jekyll md.
# Copyright (c) 2020, Lev Brekalov
# TODO summary:
# - replies
# - single/muliple tags
# - forwarded posts
# - custom post header
# - multiple photos in one post
import os
import logging
import argparse
import json
from datetime import datetime
def print_default_post_header(post_title, post_date, post_tag):
'''
returns default post header
'''
# TODO: handle post tag/tags
# TODO: support for custom header
post_header = '---\n'\
'title: {title}\n'\
'date: {date}\n'\
'tags: {tag}\n'\
'layout: post\n'\
'---\n'.format(title=post_title, date=post_date, tag=post_tag)
return post_header
def print_custom_post_header(post_header_file, *args):
'''
now unusable (i dunno how it may work)
'''
with post_header_file as f:
post_header_content = read(post_header_file)
for arg in args:
pass
return post_header_content
def parse_post_photo(post, photo_dir):
'''
converts photo tag to markdown image link
'''
post_photo_src = os.path.basename(post['photo'])
post_photo_src = os.path.join(photo_dir, post_photo_src)
post_photo = '![image]({src})\n\n'.format(src=post_photo_src)
return post_photo
def parse_post_photo_as_file(post, media_dir):
'''
converts file tag with thumbnail to image and a link
'''
# links to files are currently broken, because these files are
# going to `files` directory, not `photos`.
# need to track down any files with thumbnails and then to move them
# to a photos directory.
post_photo_file_src = os.path.basename(post['file'])
post_photo_file_src = os.path.join(media_dir, post_photo_file_src)
post_photo_thumbnail_src = os.path.basename(post['thumbnail'])
post_photo_thumbnail_src = os.path.join(media_dir,
post_photo_thumbnail_src)
post_photo_as_file = '![image]({thumb})\n[full size]({file})\n\n'\
.format(thumb=post_photo_thumbnail_src, file=post_photo_file_src)
return post_photo_as_file
def text_format(string, fmt):
'''
wraps string in markdown-styled formatting
'''
if fmt in ('*', '**', '***', '`', '```'):
output = '{fmt}{txt}{fmt}'
elif fmt == '```':
output = '{fmt}\n{txt}\n{fmt}'
else:
output = '<{fmt}>{txt}</{fmt}>'
output = output.format(fmt=fmt, txt=string.strip())
output += '\n' * string.split('\n').count('') * string.endswith('\n')
return output
def text_link_format(text, link):
'''
formats links
'''
# convert telegram links to anchors
# this implies that telegram links are pointing to the same channel
if link.startswith('https://t.me/c/'):
link = '#' + link.split('/')[-1]
link_fmt = '[{text}]({href})'
link_fmt = link_fmt.format(text=text.strip(), href=link)
link_fmt += '\n' * text.count('\n') * text.endswith('\n')
return link_fmt
def parse_text_object(obj):
'''
detects type of text object and wraps it in corresponding formatting
'''
obj_type = obj['type']
obj_text = obj['text']
if obj_type == 'hashtag':
post_tag = obj_text
return post_tag
elif obj_type == 'text_link':
return text_link_format(obj_text, obj['href'])
elif obj_type == 'link' or obj_type == 'email':
link = obj_text.strip()
link = 'https://' * (obj_type == 'link') * \
(1 - link.startswith('https://')) + link
post_link = '<{href}>'.format(href=link)
return post_link
elif obj_type == 'phone':
return obj_text
elif obj_type == 'italic':
return text_format(obj_text, '*')
elif obj_type == 'bold':
return text_format(obj_text, '**')
elif obj_type == 'code':
return text_format(obj_text, '`')
elif obj_type == 'pre':
return text_format(obj_text, '```')
elif obj_type == 'underline':
return text_format(obj_text, 'u')
elif obj_type == 'strikethrough':
return text_format(obj_text, 's')
def parse_post_text(post):
# TODO: handle reply-to
post_raw_text = post['text']
post_parsed_text = ''
if type(post_raw_text) == str:
return str(post_raw_text)
else:
for obj in post_raw_text:
if type(obj) == str:
post_parsed_text += obj
else:
post_parsed_text += str(parse_text_object(obj))
return post_parsed_text
def parse_post_media(post, media_dir):
'''
wraps media files into html tags
'''
post_media_file = os.path.basename(post['file'])
post_media_ext = post_media_file.split(".")[-1]
post_media_src = os.path.join(media_dir, post_media_file)
# audiofiles can be presented as audioplayers and other media types
# could be left as just links to them
# ???
post_media = '\n<audio controls>\n\
<source src="{src}" type="{mime_type}">\n\
</audio>'.format(src=post_media_src, mime_type=post['mime_type'])
return post_media
def parse_post_file(post, media_dir):
'''
wrap files into link tags
'''
post_file_src = os.path.basename(post['file'])
post_file_ext = post_file_src.split('.')[-1]
post_file_name = post_file_src.removesuffix('.' + post_file_ext)
post_file = f'\n\n[{post_file_name}]({post_file_src})\n\n'
return post_file
def parse_post(post, photo_dir, media_dir):
'''
converts post object to formatted text
'''
post_output = ''
# optional image
# TODO: handle multiple photos in one post (maybe by comparing timestamps)
if 'photo' in post:
post_output += str(parse_post_photo(post, photo_dir))
if all(['file' in post, 'thumbnail' in post]):
post_output += str(parse_post_photo_as_file(post, media_dir))
# post text
post_output += str(parse_post_text(post))
# optional media
if 'media_type' in post:
post_output += str(parse_post_media(post, media_dir))
elif 'file' in post and not 'thumbnail' in post:
post_output += str(parse_post_file(post, media_dir))
return post_output
def main():
parser = argparse.ArgumentParser(
usage='%(prog)s [options] json_file',
description='Convert exported Telegram channel data json to \
bunch of markdown posts ready to use with jekyll')
parser.add_argument(
'json', metavar='json_file',
help='result.json file from telegram export')
parser.add_argument(
'-o', '--out-dir', metavar='out_dir',
nargs='?', default='formatted_posts',
help='output directory for markdown files\
(default: formatted_posts)')
parser.add_argument(
'-p', '--photo-dir', metavar='photo_dir',
nargs='?', default='photos',
help='location of image files. this changes only links\
to photos in markdown text, so specify your\
desired location (default: photos)')
parser.add_argument(
'-m', '--media-dir', metavar='media_dir',
nargs='?', default='files',
help='location of media files. this changes only links\
to files in markdown text, so specify your \
desired location (default: files)')
args_wip = parser.add_argument_group('work in progress')
args_wip.add_argument(
'-H', '--post-header', metavar='post_header',
nargs='?',
help='yaml front matter for your posts \
(now doesn\'t work)')
args = parser.parse_args()
try:
os.mkdir(args.out_dir)
except OSError as error:
logging.warning(error)
# load json file
try:
with open(args.json, 'r', encoding='utf-8') as f:
data = json.load(f)
except FileNotFoundError:
sys.exit('result.json not found.\nPlease, specify right file')
# load only messages
raw_posts = data['messages']
for post in raw_posts:
# TODO: handle forwarded posts
if post['type'] == 'message' and 'forwarded_from' not in post:
post_date = datetime.fromisoformat(post['date'])
post_id = post['id']
post_filename = str(post_date.date()) + '-' + str(post_id) + '.md'
post_path = os.path.join(args.out_dir, post_filename)
with open(post_path, 'w', encoding='utf-8') as f:
print(print_default_post_header(post_id, post_date, None),
file=f)
print(parse_post(post, args.photo_dir, args.media_dir), file=f)
if __name__ == '__main__':
main()