diff --git a/dump-audio.py b/dump-audio.py new file mode 100755 index 0000000..2eb6dbe --- /dev/null +++ b/dump-audio.py @@ -0,0 +1,49 @@ +#!/usr/bin/env python2 +# -*- coding: utf-8 -*- +# File: dump-audio.py +# Author: Yuxin Wu + +import sys +import argparse + +from common.textutil import ensure_unicode +from wechat.parser import WeChatDBParser +from wechat.res import Resource +from wechat.render import HTMLRender +from wechat.libchathelper import LibChatHelper + +def get_args(): + parser = argparse.ArgumentParser() + parser.add_argument('name', help='name of contact') + parser.add_argument('--output', help='output mp3 dir', default='/tmp') + parser.add_argument('--db', default='decoded.db', help='path to decoded database') + parser.add_argument('--res', default='resource', help='reseource directory') + args = parser.parse_args() + return args + +if __name__ == '__main__': + args = get_args() + + name = ensure_unicode(args.name) + output_file = args.output + + parser = WeChatDBParser(args.db) + res = Resource(args.res, '') + + if name and name in parser.msgs_by_chat: + msgs = parser.msgs_by_chat[name] + else: + sys.stderr.write(u"Valid Contacts: {}\n".format(u'\n'.join(parser.msgs_by_chat.keys()))) + sys.stderr.write(u"Couldn't find that contact {}.".format(name)); + sys.exit(1) + print "Number of Messages: ", len(msgs) + assert len(msgs) > 0 + + libchat = LibChatHelper(parser, res) + msgs = libchat.convert_msgs(msgs) + voices = [m.sound for m in msgs if m.sound] + for idx, v in enumerate(voices): + p = v.find(':') + v = v[p:] + with open('/{}/{:04d}.mp3'.format(args.output, idx), 'wb') as f: + f.write(v) diff --git a/wechat/avatar.py b/wechat/avatar.py index d84b6e6..4c450e7 100644 --- a/wechat/avatar.py +++ b/wechat/avatar.py @@ -17,10 +17,17 @@ from common.textutil import ensure_bin_str, md5 class AvatarReader(object): def __init__(self, avt_dir, avt_db="avatar.index"): self.avt_dir = avt_dir + self.avt_db = avt_db + if self.avt_db is None or not os.path.isfile(self.avt_db): + logger.warn( + "Avatar database {} not found. Will not use avatar!".format(avt_db)) + self.avt_db = None def get_avatar(self, username): """ username: `username` field in db.rcontact""" + if self.avt_db is None: return None + username = ensure_bin_str(username) filename = md5(username) dir1, dir2 = filename[:2], filename[2:4] diff --git a/wechat/libchathelper.py b/wechat/libchathelper.py index cb72c53..45e5a3d 100644 --- a/wechat/libchathelper.py +++ b/wechat/libchathelper.py @@ -95,7 +95,7 @@ class LibChatHelper(object): text, img, sound, extra) def convert_msgs(self, msgs): - self.prgs = ProgressReporter("Convert", total=len(msgs)) + self.prgs = ProgressReporter("Parse Messages", total=len(msgs)) ret = [self._convert_msg(m) for m in msgs] self.prgs.finish() return ret