dump-msg.py 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. #!/usr/bin/env python3
  2. # -*- coding: UTF-8 -*-
  3. import logging
  4. from wechat.parser import WeChatDBParser
  5. from wechat.common.textutil import safe_filename
  6. import sys, os
  7. logger = logging.getLogger("wechat")
  8. if __name__ == '__main__':
  9. if len(sys.argv) != 3:
  10. sys.exit("Usage: {0} <path to decrypted_database.db> <output_dir>".format(sys.argv[0]))
  11. db_file = sys.argv[1]
  12. output_dir = sys.argv[2]
  13. try:
  14. os.mkdir(output_dir)
  15. except:
  16. pass
  17. if not os.path.isdir(output_dir):
  18. sys.exit("Error creating directory {}".format(output_dir))
  19. parser = WeChatDBParser(db_file)
  20. for chatid, msgs in parser.msgs_by_chat.items():
  21. name = parser.contacts[chatid]
  22. if len(name) == 0:
  23. logger.info(f"Chat {chatid} doesn't have a valid display name.")
  24. name = str(id(chatid))
  25. logger.info(f"Writing msgs for {name}")
  26. safe_name = safe_filename(name)
  27. outf = os.path.join(output_dir, safe_name + '.txt')
  28. if os.path.isfile(outf):
  29. logger.info(f"File {outf} exists! Skip contact {name}")
  30. continue
  31. with open(outf, 'w') as f:
  32. for m in msgs:
  33. f.write(str(m))
  34. f.write("\n")