better error message

This commit is contained in:
Yuxin Wu
2020-06-30 23:31:56 -07:00
parent e1157a18b7
commit 8e8b1d3534
2 changed files with 11 additions and 8 deletions
+1 -1
View File
@@ -46,7 +46,7 @@ if __name__ == '__main__':
render = HTMLRender(parser, res)
htmls = render.render_msgs(msgs)
os.makedirs(os.path.dirname(output_file), exist_ok=True)
os.makedirs(os.path.dirname(os.path.abspath(output_file)), exist_ok=True)
if len(htmls) == 1:
with open(output_file, 'w') as f:
f.write(htmls[0])
+10 -7
View File
@@ -15,7 +15,7 @@ from .common.textutil import md5 as get_md5_hex, get_file_b64, get_file_md5
LIB_PATH = os.path.dirname(os.path.abspath(__file__))
DEFAULT_EMOJI_CACHE = os.path.join(LIB_PATH, '..', 'emoji.cache.pkl')
DEFAULT_EMOJI_CACHE = os.path.join(LIB_PATH, '..', 'emoji.cache')
logger = logging.getLogger(__name__)
@@ -39,7 +39,7 @@ class EmojiReader:
resource_dir: path to resource/
parser: Database parser
cache_file: a cache file to store emoji downloaded from URLs.
default to a emoji.cache.pkl file under wechat-dump.
default to a emoji.cache file under wechat-dump.
"""
self.emoji_dir = Path(resource_dir) / 'emoji'
assert self.emoji_dir.is_dir(), self.emoji_dir
@@ -89,7 +89,7 @@ class EmojiReader:
return img, format
else:
emoji_in_table = emoji_info is not None
msg = "not in database" if not emoji_in_table else f"group={subdir}"
msg = "not in database" if not emoji_in_table else f"group='{subdir}'"
logger.warning(f"Cannot find emoji {md5}: {msg}")
return None, None
@@ -132,11 +132,14 @@ class EmojiReader:
content = self._decrypt_emoji(fname)
try:
data_md5 = get_md5_hex(content)
assert data_md5 == md5
if data_md5 != md5:
if content.startswith(b"wxgf"):
raise ValueError("Unsupported mysterious image format: wxgf")
raise ValueError("Decrypted data mismatch md5!")
im = Image.open(io.BytesIO(content))
return (base64.b64encode(content).decode('ascii'), im.format.lower())
except:
logger.exception(f"Error decrypting emoji {fname}.")
except Exception as e:
logger.error(f"Error decrypting emoji {fname} : {str(e)}")
def get_data_fallback(fname):
if not imghdr.what(fname):
@@ -208,4 +211,4 @@ if __name__ == "__main__":
encurl = 'http://emoji.qpic.cn/wx_emoji/CQmBgayyMuvscRVEKN9s4HyTjKVU9iacqqhyCpdtqOVcCql5JaibjDFg/'
enckey = '8ba7f51f9f3ac58cf8ed937fc90200a6'
b64, format = EmojiReader._fetch(Dummy(), md5, None, encurl, enckey)
print("format=", format)
print("format=", format)