add Res.py

This commit is contained in:
ppwwyyxx
2014-11-22 21:59:59 +08:00
parent d819eca642
commit d3c20d3490
3 changed files with 48 additions and 4 deletions
+5 -1
View File
@@ -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 <[email protected]>
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:
+6 -3
View File
@@ -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 <[email protected]>
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)
+37
View File
@@ -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 <[email protected]>
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