From fbafd1800db89cd2f672e7187e711ec05b27b09c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=81=8D=E5=85=AE=E6=83=9A=E5=85=AE?= <1173718158@qq.com> Date: Sun, 22 Dec 2024 17:46:17 +0800 Subject: [PATCH] . --- py/LunaTranslator/cishu/mdict.py | 50 ++++++++++---------------------- 1 file changed, 15 insertions(+), 35 deletions(-) diff --git a/py/LunaTranslator/cishu/mdict.py b/py/LunaTranslator/cishu/mdict.py index 2e5917ae..27c00a74 100644 --- a/py/LunaTranslator/cishu/mdict.py +++ b/py/LunaTranslator/cishu/mdict.py @@ -2235,40 +2235,14 @@ class mdict(cishubase): # print(html) return html - def get_mime_type_from_magic(self, magic_bytes): - if magic_bytes.startswith(b"OggS"): - return "audio/ogg" - elif magic_bytes.startswith(b"\x1A\x45\xDF\xA3"): # EBML header (Matroska) - return "video/webm" - elif ( - magic_bytes.startswith(b"\x52\x49\x46\x46") and magic_bytes[8:12] == b"WEBP" - ): - return "image/webp" - elif magic_bytes.startswith(b"\xFF\xD8\xFF"): - return "image/jpeg" - elif magic_bytes.startswith(b"\x89\x50\x4E\x47\x0D\x0A\x1A\x0A"): - return "image/png" - elif magic_bytes.startswith(b"GIF87a") or magic_bytes.startswith(b"GIF89a"): - return "image/gif" - elif magic_bytes.startswith(b"\x00\x00\x01\xBA") or magic_bytes.startswith( - b"\x00\x00\x01\xB3" - ): - return "video/mpeg" - elif magic_bytes.startswith(b"\x49\x44\x33") or magic_bytes.startswith( - b"\xFF\xFB" - ): - return "audio/mpeg" - else: - return "application/octet-stream" - def parseaudio(self, html_content, url, file_content): base64_content = base64.b64encode(file_content).decode("utf-8") uid = str(uuid.uuid4()) # with open(uid+'.mp3','wb') as ff: # ff.write(file_content) - audio = ''.format( - uid, self.get_mime_type_from_magic(file_content), base64_content + audio = ''.format( + uid, base64_content ) html_content = audio + html_content.replace( url, @@ -2278,7 +2252,6 @@ class mdict(cishubase): def parse_url_in_mdd(self, index: IndexBuilder, url1: str): url1 = url1.replace("/", "\\") - if not url1.startswith("\\"): if url1.startswith("."): url1 = url1[1:] @@ -2440,7 +2413,6 @@ class mdict(cishubase): for url in src_matches + href_matches: if url.startswith("#"): # a href # 页内跳转 continue - try: file_content = self.tryloadurl(index, base, url, tolongvals) except: @@ -2463,9 +2435,10 @@ class mdict(cishubase): elif _type == 2: html_content = self.parseaudio(html_content, url, file_content) elif _type == 0: - base64_content = base64.b64encode(file_content).decode("utf-8") + varname = "var_" + hashlib.md5(file_content).hexdigest() + tolongvals[varname] = base64.b64encode(file_content).decode() html_content = html_content.replace( - url, "data:application/octet-stream;base64," + base64_content + url,varname ) return '