dump-html.py 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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='decoded.db', help='path to decoded 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. try:
  27. chatid = parser.get_id_by_nickname(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. print "Number of Messages: ", len(msgs)
  36. assert len(msgs) > 0
  37. render = HTMLRender(parser, res)
  38. htmls = render.render_msgs(msgs)
  39. if len(htmls) == 1:
  40. with open(output_file, 'w') as f:
  41. print >> f, htmls[0].encode('utf-8')
  42. else:
  43. for idx, html in enumerate(htmls):
  44. with open(output_file + '.{}'.format(idx), 'w') as f:
  45. print >> f, html.encode('utf-8')