dump-html.py 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. #!/usr/bin/env python3
  2. import os
  3. import sys
  4. import argparse
  5. import logging
  6. from wechat.parser import WeChatDBParser
  7. from wechat.res import Resource
  8. from wechat.render import HTMLRender
  9. logger = logging.getLogger("wechat")
  10. def get_args():
  11. parser = argparse.ArgumentParser()
  12. parser.add_argument('name', help='name of contact')
  13. parser.add_argument('--output', help='output html file, e.g. output.html', default='output.html')
  14. parser.add_argument('--db', default='EnMicroMsg.db.decoded',
  15. help='path to the decoded database, e.g. EnMicroMsg.db.decoded')
  16. parser.add_argument('--res', default='resource', help='the resource directory')
  17. parser.add_argument('--wxgf-server', help='address of the wxgf image decoder server')
  18. 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.')
  19. args = parser.parse_args()
  20. return args
  21. if __name__ == '__main__':
  22. args = get_args()
  23. output_file = args.output
  24. parser = WeChatDBParser(args.db)
  25. try:
  26. chatid = parser.get_chat_id(args.name)
  27. except KeyError:
  28. sys.stderr.write(u"Valid Contacts: {}\n".format(
  29. u'\n'.join(parser.all_chat_nicknames)))
  30. sys.stderr.write(u"Couldn't find the chat {}.".format(args.name));
  31. sys.exit(1)
  32. res = Resource(parser, args.res,
  33. wxgf_server=args.wxgf_server,
  34. avt_db=args.avt)
  35. msgs = parser.msgs_by_chat[chatid]
  36. logger.info(f"Number of Messages for chatid {chatid}: {len(msgs)}")
  37. assert len(msgs) > 0
  38. render = HTMLRender(parser, res)
  39. htmls = render.render_msgs(msgs)
  40. os.makedirs(os.path.dirname(os.path.abspath(output_file)), exist_ok=True)
  41. if len(htmls) == 1:
  42. with open(output_file, 'w') as f:
  43. f.write(htmls[0])
  44. else:
  45. assert output_file.endswith(".html")
  46. basename = output_file[:-5]
  47. for idx, html in enumerate(htmls):
  48. with open(basename + f'{idx:02d}.html', 'w') as f:
  49. f.write(html)
  50. res.emoji_reader.flush_cache()