dump-html.py 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. #!/usr/bin/env python2
  2. # -*- coding: UTF-8 -*-
  3. # File: dump-html.py
  4. # Date: Wed Mar 25 17:44:20 2015 +0800
  5. # Author: Yuxin Wu <[email protected]>
  6. import sys
  7. import argparse
  8. from common.textutil import ensure_unicode
  9. from wechat.parser import WeChatDBParser
  10. from wechat.res import Resource
  11. from wechat.render import HTMLRender
  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')
  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. res = Resource(parser, args.res, args.avt)
  27. if name and name in parser.msgs_by_chat:
  28. msgs = parser.msgs_by_chat[name]
  29. else:
  30. sys.stderr.write(u"Valid Contacts: {}\n".format(u'\n'.join(parser.msgs_by_chat.keys())))
  31. sys.stderr.write(u"Couldn't find that contact {}.".format(name));
  32. sys.exit(1)
  33. print "Number of Messages: ", len(msgs)
  34. assert len(msgs) > 0
  35. render = HTMLRender(parser, res)
  36. htmls = render.render_msgs(msgs)
  37. if len(htmls) == 1:
  38. with open(output_file, 'w') as f:
  39. print >> f, htmls[0].encode('utf-8')
  40. else:
  41. for idx, html in enumerate(htmls):
  42. with open(output_file + '.{}'.format(idx), 'w') as f:
  43. print >> f, html.encode('utf-8')