diff --git a/lib/Msg.py b/lib/Msg.py index 514295f..ca0514f 100644 --- a/lib/Msg.py +++ b/lib/Msg.py @@ -1,7 +1,7 @@ #!/usr/bin/env python2 # -*- coding: UTF-8 -*- # File: Msg.py -# Date: Sat Nov 22 08:09:56 2014 +0800 +# Date: Sat Nov 22 20:58:01 2014 +0800 # Author: Yuxin Wu from datetime import datetime @@ -38,6 +38,10 @@ class WeChatMsg(object): if self.content: self.content = ensure_unicode(self.content) + def msg_html(self, res): + # TODO + assert res is not None + def msg_str(self): # TODO: fix more types if self.type == TYPE_LOCATION: diff --git a/lib/Parser.py b/lib/Parser.py index a9e1672..6e55f65 100644 --- a/lib/Parser.py +++ b/lib/Parser.py @@ -1,7 +1,7 @@ #!/usr/bin/env python2 # -*- coding: UTF-8 -*- # File: Parser.py -# Date: Sat Nov 22 08:07:56 2014 +0800 +# Date: Sat Nov 22 20:48:04 2014 +0800 # Author: Yuxin Wu import sqlite3 @@ -9,6 +9,7 @@ from collections import defaultdict import itertools from .Msg import WeChatMsg +from .Res import Resource from .utils import ensure_unicode """ tables in concern: @@ -22,7 +23,7 @@ rcontact """ class WeChatDBParser(object): - def __init__(self, db_fname): + def __init__(self, db_fname, res_dir=None): """ db_fname: EnMicroMsg.db""" self.db_fname = db_fname self.db_conn = sqlite3.connect(self.db_fname) @@ -30,6 +31,8 @@ class WeChatDBParser(object): self.contacts = {} self.msgs_by_talker = defaultdict(list) + self.res = Resource(res_dir) if res_dir else None + def _parse_contact(self): contacts = self.cc.execute( """ @@ -63,7 +66,7 @@ SELECT {} FROM message if msgs is None: msgs = itertools.chain.from_iterable(self.msgs_by_talker.itervalues()) for msg in msgs: - if msg.type == 43: + if msg.type == 34: ret.append(msg) return sorted(ret) diff --git a/lib/Res.py b/lib/Res.py new file mode 100644 index 0000000..b9694bd --- /dev/null +++ b/lib/Res.py @@ -0,0 +1,37 @@ +#!/usr/bin/env python2 +# -*- coding: UTF-8 -*- +# File: Res.py +# Date: Sat Nov 22 20:54:25 2014 +0800 +# Author: Yuxin Wu + +import glob +import os + +VOICE_DIRNAME = 'voice2' + +class Resource(object): + """ multimedia resources in chat""" + def __init__(self, res_dir): + assert os.path.isdir(res_dir), "No such directory: {}".format(res_dir) + self.res_dir = res_dir + self.init() + + def init(self): + """ load some index in memory""" + self.speak_data = {} + for root, subdirs, files in os.walk( + os.path.join(self.res_dir, VOICE_DIRNAME)): + if subdirs: + continue + for f in files: + if not f.endswith('amr'): + continue + full_path = os.path.join(root, f) + key = f[4:-4] # msg_xxxxx.amr + assert len(key) == 26, \ + "Error interpreting the protocol, this is a bug!" + self.speak_data[key] = full_path + + + +