fix some efficient issue

This commit is contained in:
ppwwyyxx
2015-01-08 00:25:09 +08:00
parent 1a655c37e7
commit add87ef670
4 changed files with 38 additions and 19 deletions
+15 -7
View File
@@ -1,7 +1,7 @@
#!/usr/bin/env python2
# -*- coding: UTF-8 -*-
# File: msg.py
# Date: Wed Jan 07 23:30:41 2015 +0800
# Date: Wed Jan 07 23:59:45 2015 +0800
# Author: Yuxin Wu <[email protected]>
import re
@@ -22,6 +22,8 @@ TYPE_VOIP = 50
TYPE_WX_VIDEO = 62 # video took by wechat
TYPE_SYSTEM = 10000
TYPE_CUSTOM_EMOJI = 1048625
TYPE_LOCATION_SHARING = -1879048186
TYPE_APP_MSG = 16777265
class WeChatMsg(object):
FIELDS = ["msgSvrId","type","isSend","createTime","talker","content","imgPath"]
@@ -56,8 +58,6 @@ class WeChatMsg(object):
except:
pass
return "LOCATION:" + label + " ({},{})".format(loc['x'], loc['y'])
elif self.type == TYPE_VOIP:
return "REQUEST VIDEO CHAT"
elif self.type == TYPE_LINK:
pq = PyQuery(self.content)
url = pq('url').text()
@@ -67,10 +67,6 @@ class WeChatMsg(object):
u"No title or url found in TYPE_LINK: {}".format(self.content)
return u"FILE:{}".format(title)
return u"URL:{}".format(url)
elif self.type == TYPE_VIDEO_FILE:
return "VIDEO FILE"
elif self.type == TYPE_WX_VIDEO:
return "WeChat VIDEO"
elif self.type == TYPE_NAMECARD:
try:
pq = PyQuery(self.content)
@@ -87,10 +83,22 @@ class WeChatMsg(object):
if not name:
name = ""
return u"NAMECARD: {}".format(name)
elif self.type == TYPE_APP_MSG:
pq = PyQuery(self.content)
return pq('title').text()
elif self.type == TYPE_VIDEO_FILE:
return "VIDEO FILE"
elif self.type == TYPE_WX_VIDEO:
return "WeChat VIDEO"
elif self.type == TYPE_VOIP:
return "REQUEST VIDEO CHAT"
elif self.type == TYPE_LOCATION_SHARING:
return "LOCATION SHARING"
elif self.type == TYPE_EMOJI:
# TODO add emoji name
return self.content_no_first_line
else:
# TODO replace smiley with text
return self.content_no_first_line
@property
+1 -1
View File
@@ -1,7 +1,7 @@
#!/usr/bin/env python2
# -*- coding: UTF-8 -*-
# File: msgslice.py
# Date: Wed Dec 24 22:06:41 2014 +0800
# Date: Thu Jan 08 00:15:49 2015 +0800
# Author: Yuxin Wu <[email protected]>
class MessageSlicerByTime(object):
+20 -10
View File
@@ -1,7 +1,7 @@
#!/usr/bin/env python2
# -*- coding: UTF-8 -*-
# File: render.py
# Date: Wed Jan 07 23:43:59 2015 +0800
# Date: Thu Jan 08 00:22:33 2015 +0800
# Author: Yuxin Wu <[email protected]>
import os
@@ -60,18 +60,27 @@ class HTMLRender(object):
js = ensure_unicode(open(js).read())
self.js_string.append(js)
def get_all_css(self):
@property
def all_css(self):
if hasattr(self, 'final_css'):
return self.final_css
def process(css):
css = css_compress(css)
return u'<style type="text/css">{}</style>'.format(css)
return u"\n".join(map(process, self.css_string))
self.final_css = u"\n".join(map(process, self.css_string))
return self.final_css
def get_all_js(self):
@property
def all_js(self):
if hasattr(self, 'final_js'):
return self.final_js
def process(js):
# TODO: add js compress
return u'<script type="text/javascript">{}</script>'.format(js)
return u"\n".join(map(process, self.js_string))
self.final_js = u"\n".join(map(process, self.js_string))
return self.final_js
#@timing(total=True)
def render_msg(self, msg):
""" render a message, return the html block"""
# TODO for chatroom, add nickname on avatar
@@ -155,11 +164,12 @@ class HTMLRender(object):
blocks.extend([self.render_msg(m) for m in slice])
self.prgs.trigger(len(slice))
return self.html.format(extra_css=self.get_all_css(),
extra_js=self.get_all_js(),
talker=msgs[0].talker_name,
messages=u''.join(blocks)
)
# string operation is extremely slow
return self.html.format(extra_css=self.all_css,
extra_js=self.all_js,
talker=msgs[0].talker_name,
messages=u''.join(blocks)
)
def prepare_avatar_css(self, talkers):
avatar_tpl= ensure_unicode(open(FRIEND_AVATAR_CSS_FILE).read())
+2 -1
View File
@@ -1,7 +1,7 @@
#!/usr/bin/env python2
# -*- coding: UTF-8 -*-
# File: res.py
# Date: Wed Jan 07 22:30:40 2015 +0800
# Date: Thu Jan 08 00:08:43 2015 +0800
# Author: Yuxin Wu <[email protected]>
import glob
@@ -53,6 +53,7 @@ class Resource(object):
"Error interpreting the protocol, this is potentially a bug!"
self.speak_data[key] = full_path
#@timing(total=True)
def get_voice_mp3(self, imgpath):
""" return base64 string, and voice duration"""
amr_fpath = self.speak_data[imgpath]