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
|
#!/usr/bin/env python
# This script parses output from Telegram channel and converts each post to
# jekyll-applicable post in markdown.
#
# Telegram creates result.json file, as well as different directories containing
# multimedia, photos, etc. This script creates new directory and populates it
# with formatted posts ready to publish.
#
# TODO summary:
# - replies
# - single/muliple tags
# - forwarded posts
import os
import sys
import json
from datetime import datetime
# post:
# header
# [photo?]
# text
# [media?]
# text:
# [str|list(str|obj, ...)]
def print_post_header(post_title, post_date, post_tag):
# TODO: handle post tag/tags
post_header = '---\ntitle: {title}\ndate: {date}\n\
tag: {tag}\nlayout: post\n---\n'.format(\
title=post_title, date=post_date, tag=post_tag)
return post_header
def parse_post_photo(post, media_dir):
post_photo_src = post['photo'][7:]
post_photo_src = media_dir + '/' + post_photo_src
post_photo = '\n\n'.format(\
src=post_photo_src)
return post_photo
def md_str(string):
string = string.replace('\n','\n\n')
string = string.replace('. ', '.\n')
return string
def parse_text_object(obj):
'''
Parse object from post text.
Objects are text links, plain links, underlined text, strikethrough text,
italic text, bold text, code blocks and hashtags.
This is a mess, but what is better?
'''
obj_type = obj['type']
obj_text = obj['text']
if obj_type == 'hashtag':
post_tag = obj_text
return post_tag
elif obj_type == 'text_link':
post_text_link = '[{text}]({href})'.format(text=obj_text, \
href=obj['href'])
return post_text_link
elif obj_type == 'link':
post_link = '[link]({href})'.format(href=obj_text)
return post_link
# I dunno how this appeared, but it seems like hyphenated numbers
# are treated as phone numbers, so return them as plain text.
elif obj_type == 'phone':
return obj_text
# output = '*{str}*'.format(str=string.strip())
# output += '\n' * string.count('\n') * string.endswith('\n')
elif obj_type == 'bold':
post_inline_bold = '**{text}**'.format(text=obj_text.strip())
return post_inline_bold
elif obj_type == 'italic':
post_inline_italic = '*{text}*'.format(text=obj_text.strip())
return post_inline_italic
elif obj_type == 'underline':
post_inline_underline = '<u>{text}</u>'.format(text=obj_text.strip())
return post_inline_underline
elif obj_type == 'strikethrough':
post_inline_strike = '<s>{text}</s>'.format(text=obj_text.strip())
return post_inline_strike
elif obj_type == 'code' or obj_type == 'pre':
post_inline_code = '```\n{text}\n```'.format(text=obj_text)
return post_inline_code
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):
# get filename without parent directory
post_media_src = post['file'][post['file'].rfind("/") + 1:]
# add parent directory
post_media_src = media_dir + '/' + post_media_src
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(post):
post_output = ''
# optional image
photo_dir = '/photos'
if 'photo' in post:
post_output += str(parse_post_photo(post, photo_dir))
# post text
post_output += str(parse_post_text(post))
# optional media
media_dir = '/files'
if 'media_type' in post:
post_output += str(parse_post_media(post, media_dir))
return post_output
def main():
# try directory from first argument
try:
input_dir = sys.argv[1]
except IndexError as e:
# if it's not specified, use current directory
input_dir = '.'
# create output directory
out_dir = input_dir + '/' + 'formatted_posts'
try:
os.mkdir(out_dir)
except FileExistsError as e:
pass
# load json file
json_path = input_dir + '/' + 'result.json'
try:
with open(json_path, 'r') as f:
data = json.load(f)
except FileNotFoundError as e:
sys.exit('result.json not found.\nPlease, specify right directory')
# 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 = out_dir + '/' + str(post_date.date()) + '-' \
+ str(post_id) + '.md'
with open (post_filename, 'w') as f:
print(print_post_header(
post_id, post_date, None),
file=f)
print(parse_post(post), file=f)
if __name__ == '__main__':
main()
|