dump-html.py 1.9 KB

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