emoji done

This commit is contained in:
ppwwyyxx
2014-12-17 00:09:28 +08:00
parent 378ad73751
commit 4bf6d00000
5 changed files with 29 additions and 31 deletions
+5 -4
View File
@@ -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 <[email protected]>
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):
+1 -5
View File
@@ -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 <[email protected]>
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))
+5 -20
View File
@@ -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 <[email protected]>
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'<script type="text/javascript">{}</script>'.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)
+18 -1
View File
@@ -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 <[email protected]>
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()
-1
View File
@@ -170,7 +170,6 @@ body {
color: #666;
font: 14px/1.5 Helvetica, Arial, Tahoma;
text-align: center;
overflow-y: auto;
}
body {
position: relative;