add emoji parse
@@ -0,0 +1,65 @@
|
||||
#!/usr/bin/env python2
|
||||
# -*- coding: UTF-8 -*-
|
||||
# File: emoji.py
|
||||
# Date: Sun Dec 14 01:56:45 2014 +0800
|
||||
# Author: Yuxin Wu <[email protected]>
|
||||
|
||||
import os
|
||||
import re
|
||||
LIB_PATH = os.path.dirname(os.path.abspath(__file__))
|
||||
|
||||
from emojiname import gQQFaceMap as gBracketFace
|
||||
from emojiname import gUnicodeFaceMap as gUnicodeFace
|
||||
from emojiname import gEmojiCodeMap as gUnicodeCodeMap
|
||||
|
||||
class EmojiProvider(object):
|
||||
def __init__(self):
|
||||
# [微笑] -> 0
|
||||
self.bracket_face = dict([(('[' + k + ']').decode('utf-8'), v)
|
||||
for k, v in gBracketFace.iteritems()])
|
||||
|
||||
# 1f35c -> "\ue340"
|
||||
#self.unicode_face_code = gUnicodeCodeMap
|
||||
|
||||
# u'\U0001f35c' -> "e340"
|
||||
self.unicode_face = dict([(unichr(int(k, 16)), v[2:])
|
||||
for k, v in gUnicodeCodeMap.iteritems()])
|
||||
self.unicode_re = re.compile(u'[\U00010000-\U0010ffff]|[\u2600-\u2764]|\u2122|\u00a9|\u00ae')
|
||||
self.used_emoji_id = set()
|
||||
|
||||
@staticmethod
|
||||
def gen_html_elem(emoji_id):
|
||||
return '<span class="emoji emoji{}"></span>'.format(emoji_id)
|
||||
|
||||
def _replace_unicode(self, msg):
|
||||
# didn't find the code
|
||||
if not self.unicode_re.findall(msg):
|
||||
return msg
|
||||
for k, v in self.unicode_face.iteritems():
|
||||
if k in msg:
|
||||
self.used_emoji_id.add(v)
|
||||
msg = msg.replace(k, EmojiProvider.gen_html_elem(v))
|
||||
return msg
|
||||
|
||||
def _replace_bracket(self, msg):
|
||||
if not '[' in msg or not ']' in msg:
|
||||
return msg
|
||||
for k, v in self.bracket_face.iteritems():
|
||||
if k in msg:
|
||||
self.used_emoji_id.add(v)
|
||||
msg = msg.replace(k, EmojiProvider.gen_html_elem(v))
|
||||
return msg
|
||||
|
||||
def replace_emojicode(self, msg):
|
||||
""" replace the emoji code in msg
|
||||
return a html
|
||||
"""
|
||||
msg = self._replace_unicode(msg)
|
||||
msg = self._replace_bracket(msg)
|
||||
return msg
|
||||
|
||||
if __name__ == '__main__':
|
||||
emoji = EmojiProvider()
|
||||
msg = u"[挥手]哈哈呵呵hihi\U0001f684\u2728\u0001"
|
||||
msg = emoji.replace_emojicode(msg)
|
||||
print msg
|
||||
@@ -1,7 +1,7 @@
|
||||
#!/usr/bin/env python2
|
||||
# -*- coding: UTF-8 -*-
|
||||
# File: render.py
|
||||
# Date: Sat Dec 13 00:29:29 2014 +0800
|
||||
# Date: Sun Dec 14 00:12:43 2014 +0800
|
||||
# Author: Yuxin Wu <[email protected]>
|
||||
|
||||
import os
|
||||
@@ -23,8 +23,8 @@ from .msg import *
|
||||
from .utils import ensure_unicode
|
||||
|
||||
TEMPLATES_FILES = {TYPE_MSG: "TP_MSG",
|
||||
TYPE_SPEAK: "TP_SPEAK",
|
||||
TYPE_IMG: "TP_IMG"}
|
||||
TYPE_IMG: "TP_IMG",
|
||||
TYPE_SPEAK: "TP_SPEAK"}
|
||||
TEMPLATES = dict([(k, open(os.path.join(
|
||||
LIB_PATH, 'static/{}.html'.format(v))).read())
|
||||
for k, v in TEMPLATES_FILES.iteritems()])
|
||||
@@ -83,6 +83,9 @@ class HTMLRender(object):
|
||||
sender = 'you' if not msg.isSend else 'me'
|
||||
# TODO
|
||||
try:
|
||||
if msg.type == TYPE_VIDEO:
|
||||
# send a video file
|
||||
raise
|
||||
template = ensure_unicode(TEMPLATES[msg.type])
|
||||
if msg.type == TYPE_SPEAK:
|
||||
audio_str, duration = self.get_voice_mp3(msg.imgPath)
|
||||
@@ -102,8 +105,9 @@ class HTMLRender(object):
|
||||
raise
|
||||
except:
|
||||
template = ensure_unicode(TEMPLATES[1])
|
||||
content = msg.msg_str()
|
||||
return template.format(sender_label=sender,
|
||||
content=msg.msg_str())
|
||||
content=content)
|
||||
|
||||
def render_msgs(self, msgs):
|
||||
""" render msgs of the same friend"""
|
||||
|
||||
|
After Width: | Height: | Size: 1.3 KiB |
|
After Width: | Height: | Size: 1.3 KiB |
|
After Width: | Height: | Size: 1.2 KiB |
|
After Width: | Height: | Size: 1.1 KiB |
|
After Width: | Height: | Size: 914 B |
|
After Width: | Height: | Size: 1.0 KiB |
|
After Width: | Height: | Size: 891 B |
|
After Width: | Height: | Size: 898 B |
|
After Width: | Height: | Size: 1.3 KiB |
|
After Width: | Height: | Size: 1.3 KiB |
|
After Width: | Height: | Size: 1.2 KiB |
|
After Width: | Height: | Size: 1.3 KiB |
|
After Width: | Height: | Size: 1.3 KiB |
|
After Width: | Height: | Size: 1.3 KiB |
|
After Width: | Height: | Size: 1.3 KiB |
|
After Width: | Height: | Size: 1.2 KiB |
|
After Width: | Height: | Size: 1.4 KiB |
|
After Width: | Height: | Size: 1.3 KiB |
|
After Width: | Height: | Size: 1.4 KiB |
|
After Width: | Height: | Size: 1.3 KiB |
|
After Width: | Height: | Size: 1.3 KiB |
|
After Width: | Height: | Size: 1.2 KiB |
|
After Width: | Height: | Size: 1.3 KiB |
|
After Width: | Height: | Size: 1.2 KiB |
|
After Width: | Height: | Size: 1.3 KiB |
|
After Width: | Height: | Size: 1.3 KiB |
|
After Width: | Height: | Size: 1.3 KiB |
|
After Width: | Height: | Size: 1.4 KiB |
|
After Width: | Height: | Size: 1.3 KiB |
|
After Width: | Height: | Size: 1.3 KiB |
|
After Width: | Height: | Size: 1.3 KiB |
|
After Width: | Height: | Size: 1.5 KiB |
|
After Width: | Height: | Size: 1.3 KiB |
|
After Width: | Height: | Size: 1.3 KiB |
|
After Width: | Height: | Size: 1.3 KiB |
|
After Width: | Height: | Size: 907 B |
|
After Width: | Height: | Size: 1006 B |
|
After Width: | Height: | Size: 1.4 KiB |
|
After Width: | Height: | Size: 1.4 KiB |
|
After Width: | Height: | Size: 1.3 KiB |
|
After Width: | Height: | Size: 1.2 KiB |
|
After Width: | Height: | Size: 1.4 KiB |
|
After Width: | Height: | Size: 1.4 KiB |
|
After Width: | Height: | Size: 1.3 KiB |
|
After Width: | Height: | Size: 1.3 KiB |
|
After Width: | Height: | Size: 1.3 KiB |
|
After Width: | Height: | Size: 1.3 KiB |
|
After Width: | Height: | Size: 1.4 KiB |
|
After Width: | Height: | Size: 1.2 KiB |
|
After Width: | Height: | Size: 1.2 KiB |
|
After Width: | Height: | Size: 1.1 KiB |
|
After Width: | Height: | Size: 1.3 KiB |
|
After Width: | Height: | Size: 1.3 KiB |
|
After Width: | Height: | Size: 1.3 KiB |
|
After Width: | Height: | Size: 1.3 KiB |
|
After Width: | Height: | Size: 1.1 KiB |
|
After Width: | Height: | Size: 792 B |
|
After Width: | Height: | Size: 1.1 KiB |
|
After Width: | Height: | Size: 1.3 KiB |
|
After Width: | Height: | Size: 1.3 KiB |
|
After Width: | Height: | Size: 938 B |
|
After Width: | Height: | Size: 1.4 KiB |
|
After Width: | Height: | Size: 1.2 KiB |
|
After Width: | Height: | Size: 1.0 KiB |
|
After Width: | Height: | Size: 1.2 KiB |
|
After Width: | Height: | Size: 572 B |
|
After Width: | Height: | Size: 608 B |
|
After Width: | Height: | Size: 823 B |
|
After Width: | Height: | Size: 935 B |
|
After Width: | Height: | Size: 1.0 KiB |
|
After Width: | Height: | Size: 907 B |
|
After Width: | Height: | Size: 736 B |
|
After Width: | Height: | Size: 1.4 KiB |
|
After Width: | Height: | Size: 1.1 KiB |
|
After Width: | Height: | Size: 582 B |
|
After Width: | Height: | Size: 1.3 KiB |
|
After Width: | Height: | Size: 1022 B |
|
After Width: | Height: | Size: 1.2 KiB |
|
After Width: | Height: | Size: 1.2 KiB |
|
After Width: | Height: | Size: 1.3 KiB |
|
After Width: | Height: | Size: 1.1 KiB |
|
After Width: | Height: | Size: 1.0 KiB |
|
After Width: | Height: | Size: 890 B |
|
After Width: | Height: | Size: 1.3 KiB |
|
After Width: | Height: | Size: 922 B |
|
After Width: | Height: | Size: 1.0 KiB |
|
After Width: | Height: | Size: 1.1 KiB |
|
After Width: | Height: | Size: 1.1 KiB |
|
After Width: | Height: | Size: 952 B |
|
After Width: | Height: | Size: 1.1 KiB |
|
After Width: | Height: | Size: 951 B |
|
After Width: | Height: | Size: 994 B |
|
After Width: | Height: | Size: 1010 B |
|
After Width: | Height: | Size: 1.1 KiB |
|
After Width: | Height: | Size: 1.3 KiB |
|
After Width: | Height: | Size: 1015 B |
|
After Width: | Height: | Size: 818 B |