From 4bf6d0000039a356ff1d50e875bfd1a9b32d5d18 Mon Sep 17 00:00:00 2001 From: ppwwyyxx Date: Wed, 17 Dec 2014 00:09:28 +0800 Subject: [PATCH] emoji done --- lib/avatar.py | 9 +++++---- lib/emoji.py | 6 +----- lib/render.py | 25 +++++-------------------- lib/res.py | 19 ++++++++++++++++++- lib/static/wx.css | 1 - 5 files changed, 29 insertions(+), 31 deletions(-) diff --git a/lib/avatar.py b/lib/avatar.py index 6f8d779..d8b8ad8 100644 --- a/lib/avatar.py +++ b/lib/avatar.py @@ -1,11 +1,11 @@ #!/usr/bin/env python2 # -*- coding: UTF-8 -*- # File: avatar.py -# Date: Fri Dec 12 22:39:37 2014 +0800 +# Date: Wed Dec 17 00:08:06 2014 +0800 # Author: Yuxin Wu import os -import md5 +import hashlib import numpy as np from .utils import ensure_bin_str @@ -18,7 +18,7 @@ class AvatarReader(object): @staticmethod def get_filename(username): - m = md5.new() + m = hashlib.md5() m.update(username) return m.hexdigest() @@ -33,7 +33,8 @@ class AvatarReader(object): # avatar not found! return None else: - return AvatarReader.read_bm(filename) + img = AvatarReader.read_bm(filename) + return img @staticmethod def read_bm(fname): diff --git a/lib/emoji.py b/lib/emoji.py index a7bbf91..0ee2e4d 100755 --- a/lib/emoji.py +++ b/lib/emoji.py @@ -1,7 +1,7 @@ #!/usr/bin/env python2 # -*- coding: UTF-8 -*- # File: emoji.py -# Date: Tue Dec 16 23:49:40 2014 +0800 +# Date: Tue Dec 16 23:52:13 2014 +0800 # Author: Yuxin Wu import os @@ -40,7 +40,6 @@ class EmojiProvider(object): self.unicode_emoji = dict([(unichr(int(k, 16)), hex(ord(v))[2:]) for k, v in json.load(open(UNICODE_EMOJI_FILE)).iteritems()]) - print self.unicode_emoji.items()[0] self.used_emoji_id = set() @@ -62,9 +61,6 @@ class EmojiProvider(object): and (not '\:' in msg) and (not '/' in msg): return msg for k, v in self.tencent_emoji.iteritems(): - if k == u'[挥手]': - print k - print type(k) if k in msg: self.used_emoji_id.add(v) msg = msg.replace(k, self.gen_replace_elem(v)) diff --git a/lib/render.py b/lib/render.py index 3020247..9b1e9d2 100755 --- a/lib/render.py +++ b/lib/render.py @@ -1,7 +1,7 @@ #!/usr/bin/env python2 # -*- coding: UTF-8 -*- # File: render.py -# Date: Mon Dec 15 21:44:07 2014 +0800 +# Date: Wed Dec 17 00:04:02 2014 +0800 # Author: Yuxin Wu import os @@ -10,7 +10,6 @@ import glob import logging logger = logging.getLogger(__name__) -import eyed3 LIB_PATH = os.path.dirname(os.path.abspath(__file__)) HTML_FILE = os.path.join(LIB_PATH, 'static/template.html') @@ -35,6 +34,8 @@ class HTMLRender(object): self.html = ensure_unicode(open(HTML_FILE).read()) self.parser = parser self.res = res + if self.res is None: + logger.warn("Resource Directory not given. Images / Voice Message won't be displayed.") self.emoji = EmojiProvider() csss = glob.glob(os.path.join(LIB_PATH, 'static/*.css')) @@ -49,9 +50,9 @@ class HTMLRender(object): jss = glob.glob(os.path.join(LIB_PATH, 'static/*.js')) js_string = [] for js in jss: - # TODO: add js compress logger.info("Load {}".format(os.path.basename(js))) js = ensure_unicode(open(js).read()) + # TODO: add js compress js = u''.format(js) js_string.append(js) self.js_string = u"\n".join(js_string) @@ -64,22 +65,6 @@ class HTMLRender(object): avt2 = self.res.get_avatar(username) return (avt1, avt2) - def get_voice_mp3(self, imgpath): - """ return base64 string, and voice duration""" - if self.res is None: - return "" - amr_fpath = self.res.speak_data[imgpath] - mp3_file = os.path.join('/tmp', os.path.basename(amr_fpath)[:-4] + '.mp3') - # TODO is there a library to use? - ret = os.system('sox {} {}'.format(amr_fpath, mp3_file)) - if ret != 0: - logger.warn("Sox Failed!") - return "" - mp3_string = open(mp3_file, 'rb').read() - duration = eyed3.load(mp3_file).info.time_secs - os.unlink(mp3_file) - return base64.b64encode(mp3_string), duration - def render_msg(self, msg): """ render a message, return the html block""" sender = 'you' if not msg.isSend else 'me' @@ -90,7 +75,7 @@ class HTMLRender(object): raise template = ensure_unicode(TEMPLATES[msg.type]) if msg.type == TYPE_SPEAK: - audio_str, duration = self.get_voice_mp3(msg.imgPath) + audio_str, duration = self.res.get_voice_mp3(msg.imgPath) return template.format(sender_label=sender, voice_duration=duration, voice_str=audio_str) diff --git a/lib/res.py b/lib/res.py index f2efe57..ac6f8e0 100644 --- a/lib/res.py +++ b/lib/res.py @@ -1,7 +1,7 @@ #!/usr/bin/env python2 # -*- coding: UTF-8 -*- # File: res.py -# Date: Fri Dec 12 23:12:06 2014 +0800 +# Date: Wed Dec 17 00:02:13 2014 +0800 # Author: Yuxin Wu import glob @@ -10,6 +10,7 @@ import Image import cStringIO import base64 import logging +import eyed3 logger = logging.getLogger(__name__) from lib.avatar import AvatarReader @@ -44,6 +45,22 @@ class Resource(object): "Error interpreting the protocol, this is a bug!" self.speak_data[key] = full_path + def get_voice_mp3(self, imgpath): + """ return base64 string, and voice duration""" + amr_fpath = self.speak_data[imgpath] + assert amr_fpath.endswith('.amr') + mp3_file = os.path.join('/tmp', + os.path.basename(amr_fpath)[:-4] + '.mp3') + # TODO is there a library to use? + ret = os.system('sox {} {}'.format(amr_fpath, mp3_file)) + if ret != 0: + logger.warn("Sox Failed!") + return "" + mp3_string = open(mp3_file, 'rb').read() + duration = eyed3.load(mp3_file).info.time_secs + os.unlink(mp3_file) + return base64.b64encode(mp3_string), duration + @staticmethod def get_file_b64(fname): data = open(fname, 'rb').read() diff --git a/lib/static/wx.css b/lib/static/wx.css index 923138f..bc9f30f 100644 --- a/lib/static/wx.css +++ b/lib/static/wx.css @@ -170,7 +170,6 @@ body { color: #666; font: 14px/1.5 Helvetica, Arial, Tahoma; text-align: center; - overflow-y: auto; } body { position: relative;