diff --git a/dump_html.py b/dump_html.py new file mode 100755 index 0000000..f0daa79 --- /dev/null +++ b/dump_html.py @@ -0,0 +1,24 @@ +#!/usr/bin/env python2 +# -*- coding: UTF-8 -*- +# File: dump_html.py +# Date: Sat Nov 22 23:51:45 2014 +0800 +# Author: Yuxin Wu + +import sys +if len(sys.argv) != 3: + sys.exit("Usage: {0} ".format(sys.argv[0])) + +from lib.utils import ensure_unicode +from lib.parser import WeChatDBParser +from lib.render import HTMLRender + +db_file = sys.argv[1] +name = ensure_unicode(sys.argv[2]) + +parser = WeChatDBParser(db_file) +parser.parse() +msgs = parser.msgs_by_talker[name] + +render = HTMLRender() +html = render.render_msgs(msgs).encode('utf-8') +print html diff --git a/lib/msg.py b/lib/msg.py index 1627a77..5ff164c 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 22:28:15 2014 +0800 +# File: msg.py +# Date: Sat Nov 22 23:23:52 2014 +0800 # Author: Yuxin Wu from datetime import datetime @@ -35,6 +35,7 @@ class WeChatMsg(object): for f, v in zip(WeChatMsg.FIELDS, row): setattr(self, f, v) self.createTime = datetime.fromtimestamp(self.createTime / 1000) + self.talker_name = None if self.content: self.content = ensure_unicode(self.content) @@ -70,7 +71,8 @@ class WeChatMsg(object): def __repr__(self): ret = u"{}|{}:{}:{}".format( self.type, - ensure_unicode(self.talker) if not self.isSend else 'me', + (self.talker if not self.talker_name else self.talker_name) \ + if not self.isSend else 'me', self.createTime, ensure_unicode(self.msg_str())).encode('utf-8') if self.imgPath: diff --git a/lib/parser.py b/lib/parser.py index 3cee4b7..efdd897 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 22:32:05 2014 +0800 +# Date: Sat Nov 22 23:23:44 2014 +0800 # Author: Yuxin Wu import sqlite3 @@ -56,7 +56,7 @@ SELECT {} FROM message for k, v in self.msgs_by_talker.iteritems()]) for k, v in self.msgs_by_talker.iteritems(): for msg in v: - msg.talker = k + msg.talker_name = ensure_unicode(k) def _find_msg_by_type(self, msgs=None): ret = [] diff --git a/lib/render.py b/lib/render.py index 7beb009..171948d 100755 --- a/lib/render.py +++ b/lib/render.py @@ -1,7 +1,7 @@ #!/usr/bin/env python2 # -*- coding: UTF-8 -*- -# File: Render.py -# Date: Sat Nov 22 22:29:56 2014 +0800 +# File: render.py +# Date: Sat Nov 22 23:24:10 2014 +0800 # Author: Yuxin Wu import os @@ -9,16 +9,42 @@ LIB_PATH = os.path.dirname(os.path.abspath(__file__)) CSS_FILE = os.path.join(LIB_PATH, 'static/wx.css') HTML_FILE = os.path.join(LIB_PATH, 'static/template.html') +try: + from csscompressor import compress as css_compress +except: + css_compress = lambda x: x + +from .msg import * +from .utils import ensure_unicode + +TEMPLATES_FILES = {TYPE_MSG: "TP_MSG"} +TEMPLATES = dict([(k, open(os.path.join( + LIB_PATH, 'static/{}.html'.format(v))).read()) + for k, v in TEMPLATES_FILES.iteritems()]) + + + class HTMLRender(object): def __init__(self, res=None): - self.css = open(CSS_FILE).read().replace('\n', '') - self.html = open(HTML_FILE).read() + self.css = ensure_unicode(css_compress(open(CSS_FILE).read())) + self.html = ensure_unicode(open(HTML_FILE).read()) self.res = res def render_msg(self, msg): - """ render a message""" + """ render a message, return the block""" # TODO - pass + #template = ensure_unicode(TEMPLATES[msg.type]) + template = ensure_unicode(TEMPLATES[1]) + if False: + return "" + else: + return template.format(sender_label='you' if not msg.isSend else 'me', + content=msg.msg_str()) + + def render_msgs(self, msgs): + blocks = [self.render_msg(m) for m in msgs] + return self.html.format(style=self.css, talker=msgs[0].talker_name, + messages=u''.join(blocks)) if __name__ == '__main__': r = HTMLRender() diff --git a/lib/static/TP_MSG.html b/lib/static/TP_MSG.html new file mode 100644 index 0000000..a9ebc8e --- /dev/null +++ b/lib/static/TP_MSG.html @@ -0,0 +1,14 @@ +
+
+ +
+
+
+
+
{content}
+
+
+
+
+
+
diff --git a/lib/static/template.html b/lib/static/template.html index db9fada..cf80842 100644 --- a/lib/static/template.html +++ b/lib/static/template.html @@ -1,30 +1,30 @@ - - - WeChat - - + + + WeChat + + - -
-
-
-
-
-
-

{talker}

-
-
-
-
- {messages} -
-
-
-
-
-
+ +
+
+
+
+
+
+

{talker}

+
+
+
+
+ {messages} +
+
+
+
+
+
diff --git a/lib/static/wx.css b/lib/static/wx.css index 7abef02..89e259c 100644 --- a/lib/static/wx.css +++ b/lib/static/wx.css @@ -166,9 +166,9 @@ html, body { height: 100%; min-height: 755px; - background: #4e5359 url(../images/bg_universal1844c2.png) repeat 0 0; + background: #4e5359 repeat 0 0; color: #666; - font: 14px/1.5 Helvetica, "微软雅黑", "黑体", Arial, Tahoma; + font: 14px/1.5 Helvetica, Arial, Tahoma; text-align: center; overflow-y: auto; } @@ -2435,7 +2435,7 @@ ul.faceTab a.chooseFaceTab { border: none; border-bottom: 1px solid #fff; border: 1px solid #ccc\9; - font: 14px/1.5 Helvetica, "微软雅黑", "黑体", Arial, Tahoma; + font: 14px/1.5 Helvetica, Arial, Tahoma; line-height: 1.2em; resize: none; } @@ -5266,7 +5266,7 @@ a.btnBlue:active .btnBluePanel { word-wrap: break-word; resize: none; overflow: auto; - font: 14px/1.5 Helvetica, "微软雅黑", "黑体", Arial, Tahoma; + font: 14px/1.5 Helvetica, Arial, Tahoma; line-height: 1.2em; } .feedbackWin .opArea {