emoji cache downloadd

This commit is contained in:
Yuxin Wu
2016-06-18 10:04:53 -07:00
parent 91ac3126f8
commit 1f2f7560bc
3 changed files with 14 additions and 6 deletions
+1 -1
View File
@@ -28,7 +28,7 @@ if __name__ == '__main__':
output_file = args.output
parser = WeChatDBParser(args.db)
res = Resource(args.res, '')
res = Resource(parser, args.res, '')
if name and name in parser.msgs_by_chat:
msgs = parser.msgs_by_chat[name]
+1 -1
View File
@@ -131,7 +131,7 @@ class HTMLRender(object):
elif msg.type == TYPE_CUSTOM_EMOJI:
pq = PyQuery(msg.content)
md5 = pq('emoticonmd5').text()
format_dict['img'] = self.res.get_emoji(md5, None)
format_dict['img'] = self.res.get_emoji_by_md5(md5)
return template.format(**format_dict)
elif msg.type == TYPE_LINK:
content = msg.msg_str()
+12 -4
View File
@@ -65,14 +65,15 @@ class EmojiCache(object):
class Resource(object):
""" multimedia resources in chat"""
def __init__(self, parser, res_dir, avt_db,
emoji_cache_file='emoji.cache'):
def __init__(self, parser, res_dir, avt_db):
def check(subdir):
assert os.path.isdir(os.path.join(res_dir, subdir)), \
"No such directory: {}".format(subdir)
[check(k) for k in ['', AVATAR_DIRNAME, IMG_DIRNAME, EMOJI_DIRNAME, VOICE_DIRNAME]]
self.emoji_cache = EmojiCache(emoji_cache_file)
self.emoji_cache = EmojiCache(
os.path.join(os.path.dirname(os.path.abspath(__file__)),
'..', 'emoji.cache'))
self.res_dir = res_dir
self.parser = parser
self.voice_cache_idx = {}
@@ -118,7 +119,14 @@ class Resource(object):
if im is None:
return ""
buf = cStringIO.StringIO()
im.save(buf, 'JPEG', quality=JPEG_QUALITY)
try:
im.save(buf, 'JPEG', quality=JPEG_QUALITY)
except IOError:
try:
# sometimes it works the second time...
im.save(buf, 'JPEG', quality=JPEG_QUALITY)
except IOError:
return ""
jpeg_str = buf.getvalue()
return base64.b64encode(jpeg_str)