This repository has been archived by the owner on Mar 2, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathhtml_exporter.py
449 lines (380 loc) · 16.9 KB
/
html_exporter.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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
from utils import *
import codecs
import json
MSG_MERGE_TIMEOUT = 60 * 5 # two sequential messages from the same sender are going to be merged into one if sent in this time range
class HTMLExporterOutput:
def __init__(self):
self.data = []
def append(self, text):
self.data.append(text)
def get(self):
return ''.join(self.data)
class HTMLExporterContext:
def __init__(self, progress, output, input_json, level):
self.progress = progress
self.output = output
self.input_json = input_json
self.level = level
self.prev_merge_sender = None
self.prev_merge_msg_timestamp = 0
def next_level(self):
return HTMLExporterContext(self.progress, self.output, self.input_json, self.level + 1)
class HTMLExporter:
def __init__(self, options):
self.options = options
@property
def extension(self):
return "html"
def export(self, input_json, progress):
# load stylesheet early
with codecs.open('style.css', 'r', encoding='utf-8') as f:
stylesheet = f.read()
ctx = HTMLExporterContext(progress, HTMLExporterOutput(), input_json, 1)
if self.options.arguments.embed_resources:
link_block = '<style>{stylesheet}</style>'.format(stylesheet=stylesheet)
else:
link_block = '<link rel="stylesheet" href="style.css" />'
ctx.output.append('''
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
{link_block}
</head>
<body>
<div class="messages">
'''.format(link_block=link_block))
cur_step = 0
total = len(input_json['messages'])
for msg in input_json['messages']:
if cur_step == 0:
ctx.progress.update(0, total)
ctx.output.append(self.export_message(ctx, msg))
cur_step += 1
ctx.progress.update(cur_step, total)
ctx.output.append('</div></body></html>')
files = dict()
if not self.options.arguments.embed_resources:
files['style.css'] = stylesheet
return {
'text': ctx.output.get(),
'files': files
}
def get_action_text(self, ctx, msg, action, action_text, action_mid):
action_text_dict = {
'chat_photo_update': 'Updated the chat photo',
'chat_photo_remove': 'Removed the chat photo',
'chat_create': 'Created the chat',
'chat_invite_user': 'Invited user to the chat',
'chat_kick_user': 'Kicked user from the chat',
'chat_invite_user_by_link': 'Entered the chat by invite link'
}
if action == 'chat_title_update':
return 'Changed chat title to: <span class="new-chat-title">{title}</span>'.format(title=action_text)
elif action == 'chat_pin_message':
return 'Pinned message <span class="new-chat-title">{text}</span>'.format(text=action_text)
elif action == 'chat_unpin_message':
return 'Unpinned message <span class="new-chat-title">{text}</span>'.format(text=action_text)
elif action == 'chat_kick_user':
if action_mid is None:
return 'Kicked user'
elif action_mid == msg['sender']['id']:
return 'Left the chat'
else:
user_data = ctx.input_json['users'].get(action_mid)
user_name = user_data['name'] if user_data is not None else '?'
return 'Kicked user <span class="new-chat-title">{name}</span>'.format(name=user_name)
else:
return action_text_dict.get(action, '')
def export_action_message(self, ctx, msg):
ctx.prev_merge_sender = None
sender = ctx.input_json['users'][msg['sender']['id']]
attach_block = ''
if 'attachments' in msg:
for attachment in msg['attachments']:
attach_block += self.export_attachment(ctx, attachment)
if len(attach_block) > 0:
attach_block = '<div class="msg-attachments">{attach_block}</div>'.format(attach_block=attach_block)
return '''
<div class="msg msg--level-{level} msg--action msg-action" data-json='{json}'>
<span class="msg-action__sender">{sender_fullname}</span>
:
{message}
{attach_block}
</div>
'''.format(**{
'level': ctx.level,
'json': json.dumps(msg, ensure_ascii=False) if self.options.arguments.save_json_in_html else '',
'sender_profile': sender['link'],
'sender_fullname': sender['name'],
'sender_firstname': sender['first_name'],
'sender_photo': sender['filename'],
'date': fmt_timestamp(msg['date']),
'message': self.get_action_text(ctx, msg, msg['action'], msg.get('action_text', ''), msg.get('action_mid', None)),
'attach_block': attach_block
})
def export_message(self, ctx, msg):
if 'action' in msg:
return self.export_action_message(ctx, msg)
extra_classes = []
if msg['is_important']:
extra_classes.append('msg--important')
if msg['is_updated']:
extra_classes.append('msg--edited')
sender_id = msg['sender']['id']
sender = ctx.input_json['users'][sender_id]
fwd_block = ''
if 'forwarded' in msg:
nested_context = ctx.next_level()
for fwd_msg in msg['forwarded']:
fwd_block += self.export_message(nested_context, fwd_msg)
if len(fwd_block) > 0:
fwd_block = '<div class="msg-forwarded">{fwd_block}</div>'.format(fwd_block=fwd_block)
attach_block = ''
if 'attachments' in msg:
for attachment in msg['attachments']:
attach_block += self.export_attachment(ctx, attachment)
if len(attach_block) > 0:
attach_block = '<div class="msg-attachments">{attach_block}</div>'.format(attach_block=attach_block)
extra_head_classes = []
msg_date = msg['date']
is_merged = False
merge_date_diff = 0
if sender_id == ctx.prev_merge_sender and msg_date - ctx.prev_merge_msg_timestamp < MSG_MERGE_TIMEOUT:
extra_classes.append('msg--merged')
extra_head_classes.append('msg-head--merged')
is_merged = True
merge_date_diff = msg_date - ctx.prev_merge_msg_timestamp
# do not allow the next message to be merged if we have forwarded messages
if not fwd_block:
ctx.prev_merge_sender = sender_id
ctx.prev_merge_msg_timestamp = msg_date
return '''
<div class="msg msg--level-{level} {extra_classes}" data-json='{json}'>
<div class="msg-head {extra_head_classes}">
<div class="msg-head__photo-block">
<img class="msg-head__photo" src="{sender_photo}" />
</div>
<div class="msg-head__info">
<a class="msg-head__profile" href="{sender_profile}" title="{sender_fullname}">{sender_firstname}</a>
<div class="msg-head__date-block">
<span class="msg-head__date">{date}</span>
</div>
</div>
</div>
<div class="msg-body">
<div class="msg-text">
{message}
{edited_block}
</div>
{fwd_block}
{attach_block}
</div>
</div>
'''.format(**{
'level': ctx.level,
'extra_classes': ' '.join(extra_classes) if extra_classes is not None else '',
'extra_head_classes': ' '.join(extra_head_classes) if extra_head_classes is not None else '',
'sender_profile': sender['link'],
'sender_fullname': sender['name'],
'sender_firstname': sender['first_name'],
'sender_photo': sender['filename'],
'date': fmt_timestamp(msg['date']) if not is_merged else fmt_date_diff(merge_date_diff, add_sign=True),
'edited_block': '<span class="msg-edited">(Edited {diff} after)</span>'.format(
diff=fmt_date_diff(msg['updated_at'] - msg_date)
) if msg['is_updated'] else '',
'message': msg['message'],
'fwd_block': fwd_block,
'attach_block': attach_block,
'json': json.dumps(msg, ensure_ascii=False) if self.options.arguments.save_json_in_html else ''
})
def export_attachment(self, ctx, attach):
known_types = ('photo', 'video', 'audio', 'doc', 'post', 'sticker', 'link', 'gift', 'voice')
if attach['type'] in known_types:
return getattr(self, 'handle_' + attach['type'])(ctx, attach)
else:
return self.handle_unknown(ctx, attach)
def handle_unknown(self, ctx, attach):
return '''
<div class="attach attach-{type}">
<span class="attach-{type}__title">{title}</span>
</div>
'''.format(**{
'type': attach['type'],
'title': "Unknown attachment type"
})
def handle_link(self, ctx, attach):
if 'filename' in attach and attach['filename'] is not None:
return '''
<div class="attach attach-link">
<a class="attach-link__link-block" title="{title}" href="{url}">
<div class="attach-link__image-block">
<img class="attach-link__image" src="{filename}" />
</div>
<div class="attach-link__description">
<div class="attach-link__title">{title}</div>
<div class="attach-link__description-text">{description}</div>
<div class="attach-link__caption">{caption}</div>
</div>
</a>
</div>
'''.format(**attach)
else:
return '''
<div class="attach attach-link">
<a class="attach-link__link-block attach-link__link-block--no-image" title="{title}" href="{url}">
<div class="attach-link__description">
<div class="attach-link__title">{title}</div>
<div class="attach-link__description-text">{description}</div>
<div class="attach-link__caption">{caption}</div>
</div>
</a>
</div>
'''.format(**attach)
def handle_photo(self, ctx, attach):
return '''
<div class="attach attach-photo">
<span class="attach-photo__title">{description}</span>
<img class="attach-photo__image" src="{filename}" alt="{description}" />
</div>
'''.format(**attach)
def handle_sticker(self, ctx, attach):
return '''
<div class="attach attach-sticker">
<img class="attach-sticker__image" src="{filename}" />
</div>
'''.format(**attach)
def handle_video(self, ctx, attach):
args = {**attach, **{
'duration': fmt_time(attach['duration']),
'date': fmt_timestamp(attach['date'])
}}
uploader_profile = ''
owner_id = attach['owner_id']
if owner_id in ctx.input_json['users']:
uploader_profile = '<a href="{link}">{name}</a>'.format(**ctx.input_json['users'][owner_id])
args['uploader_profile'] = uploader_profile
return '''
<div class="attach attach-video">
<a class="attach-video__link" href="{url}" title="{title}">
<img class="attach-video__thumbnail" src="{thumbnail_filename}" alt="{title}" />
<div class="attach-video__title">{title}</div>
</a>
<div class="attach-video__meta meta">
<p>Views on VK: <span class="meta__views">{views}, comments on VK: <span class="meta__comments">{comments}</span></p>
<p>Added at: <span class="meta__date">{date}</span> from {platform} by {uploader_profile}</p>
</div>
<div class="attach-video__description">{description}</div>
</div>
'''.format(**args)
def handle_post(self, ctx, attach):
args = {**attach, **{
'date': fmt_timestamp(attach['date'])
}}
attach_block = ''
if 'attachments' in attach:
for attachment in attach['attachments']:
attach_block += self.export_attachment(ctx, attachment)
if len(attach_block) > 0:
attach_block = '<div class="msg-attachments">{attach_block}</div>'.format(attach_block=attach_block)
args['attach_block'] = attach_block
head_block = ''
if 'from_id' in attach and attach['from_id'] in ctx.input_json['users']:
user_data = ctx.input_json['users'][attach['from_id']]
if 'filename' in user_data:
head_block = '''
<div class="post-head">
<div class="post-head__image-block">
<img class="post-head__image" src="{filename}" />
</div>
<div class="post-head__info">
<div class="post-head__name">
<a class="post-head__link" href="{link}">{name}</a>
</div>
<div class="post-head__date">{date}</div>
</div>
</div>
'''.format(**user_data, date=args['date'])
else:
head_block = '''
<div class="post-head">
<div class="post-head__name">
<a class="post-head__link" href="{link}">{name}</a>
</div>
<div class="post-head__date">{date}</div>
</div>
'''.format(**user_data, date=args['date'])
args['head_block'] = head_block
repost_block = ''
if 'repost' in attach:
for repost in attach['repost']:
repost_block = '''
<div class="attach-post__repost">
{repost_block}
</div>
'''.format(repost_block=self.handle_post(ctx, repost))
args['repost_block'] = repost_block
return '''
<div class="attach attach-post">
{head_block}
<div class="attach-post__text">{text}</div>
<a class="attach-post__link" href="{url}"></a>
{attach_block}
{repost_block}
</div>
'''.format(**args)
def handle_audio(self, ctx, attach):
args = {**attach, **{'duration': fmt_time(attach['duration'])}}
if 'downloaded' in attach and attach['downloaded'] is not None:
audio_block = '<audio class="attach-audio__audio" controls src="{filename}" />'.format(**attach)
else:
audio_block = '<div class="attach-audio__audio attach-audio__audio--failed">Unavailable</div>'
return '''
<div class="attach attach-audio">
<div class="attach-audio__title">
Audio:
<span class="attach-audio__author">
<span class="attach-audio__composition-artist">{artist}</span>
-
<span class="attach-audio__composition-title">{title}</span>
</span>
</div>
<span class="attach-audio__audio-block">
{audio_block}
<div class="attach-audio__duration">{duration}</div>
</span>
</div>
'''.format(**args, audio_block=audio_block)
def handle_voice(self, ctx, attach):
args = {**attach, **{'duration': fmt_time(attach['duration'])}}
return '''
<div class="attach attach-voice">
<div class="attach-voice__title">Voice message</div>
<div class="attach-voice__audio-block">
<audio class="attach-voice__audio" controls src="{filename}"></audio>
<div class="attach-voice__duration">{duration}</div>
</div>
</div>
'''.format(**args)
def handle_doc(self, ctx, attach):
args = {**attach, **{
'filename': attach['filename'] or attach["url"],
'size': fmt_size(attach['size'])
}}
return '''
<div class="attach attach-doc">
<a href="{filename}" class="attach-doc__link-block">
<div class="attach-doc__desc">
<div class="attach-doc__link" title="{title}">{title}</div>
<div class="attach-doc__size">{size}</div>
</div>
</a>
</div>
'''.format(**args)
def handle_gift(self, ctx, attach):
return '''
<div class="attach attach-gift">
<div class="attach-gift__title">Gift</div>
<img class="attach-gift__thumbnail" src="{thumbnail}" />
</div>
'''.format(**attach)