From d6d2aaea550f4d6c19a68031fb1290662937efeb Mon Sep 17 00:00:00 2001 From: Yuxin Wu Date: Sat, 27 Jun 2020 16:32:07 -0700 Subject: [PATCH] handle emoji thumbnail --- wechat/audio.py | 4 ++-- wechat/res.py | 13 +++++-------- 2 files changed, 7 insertions(+), 10 deletions(-) diff --git a/wechat/audio.py b/wechat/audio.py index 6473086..48d3d51 100644 --- a/wechat/audio.py +++ b/wechat/audio.py @@ -18,7 +18,7 @@ def parse_wechat_audio_file(file_name): try: return do_parse_wechat_audio_file(file_name) except Exception as e: - logger.exception("Error when parsing audio file {}".format(file_name)) + logger.error(f"Error when parsing audio file {file_name}: {str(e)}") return "", 0 def do_parse_wechat_audio_file(file_name): @@ -56,7 +56,7 @@ def do_parse_wechat_audio_file(file_name): subproc_succ('sox -r 24000 -e signed -b 16 -c 1 {} {}'.format(raw_file, mp3_file)) os.unlink(raw_file) else: - raise NotImplementedError("Unsupported Audio Format! This is a bug!") + raise NotImplementedError("Audio file format cannot be recognized.") mp3_string = get_file_b64(mp3_file) os.unlink(mp3_file) return mp3_string, duration diff --git a/wechat/res.py b/wechat/res.py index 98019fc..081fcb8 100644 --- a/wechat/res.py +++ b/wechat/res.py @@ -204,8 +204,7 @@ class Resource(object): """ path = os.path.join(self.emoji_dir, pack_id or '') candidates = glob.glob(os.path.join(path, '{}*'.format(md5))) - candidates = [k for k in candidates if not k.endswith('_thumb') \ - and not re.match('.*_[0-9]+$', k)] + candidates = [k for k in candidates if not re.match('.*_[0-9]+$', k)] def try_use(f): if not f: return None @@ -213,7 +212,7 @@ class Resource(object): return None return f[0] - candidates = [k for k in candidates if (allow_cover or not k.endswith('_cover'))] + candidates = [k for k in candidates if (allow_cover or (not k.endswith('_cover') and not k.endswith('_thumb')))] for cand in candidates: if imghdr.what(cand): @@ -228,10 +227,8 @@ class Resource(object): """ :returns: (b64 unicode img, format)""" assert md5, md5 if md5 in self.parser.internal_emojis: - # TODO this seems broken emoji_img, format = self._get_internal_emoji(self.parser.internal_emojis[md5]) - logger.warn("Cannot get emoji {}".format(md5)) - return None, None + return emoji_img, format else: # check cache img, format = self.emoji_cache.query(md5) @@ -256,8 +253,8 @@ class Resource(object): if format: return emoji_img, format - # first 1k in emoji is encrypted - logger.warn("Cannot get emoji {} in {}".format(md5, group)) + # TODO: first 1k in emoji is encrypted + logger.warn("Cannot get emoji {} in group {}".format(md5, group)) return None, None def get_video(self, videoid):