-
Notifications
You must be signed in to change notification settings - Fork 308
/
Copy pathdump-html.py
executable file
·66 lines (56 loc) · 2.44 KB
/
dump-html.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
#!/usr/bin/env python3
import os
import sys
import argparse
import logging
from datetime import datetime
from wechat.parser import WeChatDBParser
from wechat.res import Resource
from wechat.render import HTMLRender
logger = logging.getLogger("wechat")
def get_args():
parser = argparse.ArgumentParser()
parser.add_argument('name', help='name of contact')
parser.add_argument('--output', help='output html file, e.g. output.html', default='output.html')
parser.add_argument('--db', default='EnMicroMsg.db.decrypted',
help='path to the decrypted database, e.g. EnMicroMsg.db.decrypted')
parser.add_argument('--res', default='resource', help='the resource directory')
parser.add_argument('--wxgf-server', help='address of the wxgf image decoder server')
parser.add_argument('--avt', default='avatar.index', help='path to avatar.index file that only exists in old version of wechat. Ignore for new version of wechat.')
parser.add_argument('--start', help='start time in format of YYYY-MM-DD HH:MM:SS',
type=datetime.fromisoformat)
args = parser.parse_args()
return args
if __name__ == '__main__':
args = get_args()
output_file = args.output
parser = WeChatDBParser(args.db)
try:
chatid = parser.get_chat_id(args.name)
except KeyError:
sys.stderr.write(u"Valid Contacts: {}\n".format(
u'\n'.join(parser.all_chat_nicknames)))
sys.stderr.write(u"Couldn't find the chat {}.".format(args.name));
sys.exit(1)
res = Resource(parser, args.res,
wxgf_server=args.wxgf_server,
avt_db=args.avt)
msgs = parser.msgs_by_chat[chatid]
logger.info(f"Number of Messages for chatid {chatid}: {len(msgs)}")
assert len(msgs) > 0
if args.start is not None:
msgs = [msg for msg in msgs if msg.createTime > args.start]
logger.info(f"Number of Messages after {args.start}: {len(msgs)}")
render = HTMLRender(parser, res)
htmls = render.render_msgs(msgs)
os.makedirs(os.path.dirname(os.path.abspath(output_file)), exist_ok=True)
if len(htmls) == 1:
with open(output_file, 'w') as f:
f.write(htmls[0])
else:
assert output_file.endswith(".html")
basename = output_file[:-5]
for idx, html in enumerate(htmls):
with open(basename + f'{idx:02d}.html', 'w') as f:
f.write(html)
res.emoji_reader.flush_cache()