Prechádzať zdrojové kódy

handle emoji thumbnail

Yuxin Wu 4 rokov pred
rodič
commit
d439dfe72d
2 zmenil súbory, kde vykonal 7 pridanie a 10 odobranie
  1. 2 2
      wechat/audio.py
  2. 5 8
      wechat/res.py

+ 2 - 2
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

+ 5 - 8
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):