From 0c30fdcb752b604d88f25eca81b0f16734b1fc3e Mon Sep 17 00:00:00 2001 From: STDquantum <405720329@qq.com> Date: Fri, 12 Jan 2024 16:13:20 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8Demoji=E5=A4=A7=E9=83=A8?= =?UTF-8?q?=E5=88=86=E4=BC=9Abug=E6=8E=89=E7=9A=84bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/util/emoji.py | 127 +++++++++++++++++++++++++--------------------- 1 file changed, 69 insertions(+), 58 deletions(-) diff --git a/app/util/emoji.py b/app/util/emoji.py index 3dd70e7..3b9fb13 100644 --- a/app/util/emoji.py +++ b/app/util/emoji.py @@ -17,11 +17,12 @@ from PyQt5.QtGui import QPixmap import requests from app.log import log, logger + lock = threading.Lock() db_path = "./app/Database/Msg/Emotion.db" -root_path = './data/emoji/' -if not os.path.exists('./data'): - os.mkdir('./data') +root_path = "./data/emoji/" +if not os.path.exists("./data"): + os.mkdir("./data") if not os.path.exists(root_path): os.mkdir(root_path) @@ -30,10 +31,10 @@ if not os.path.exists(root_path): def get_image_format(header): # 定义图片格式的 magic numbers image_formats = { - b'\xFF\xD8\xFF': 'jpeg', - b'\x89\x50\x4E\x47\x0D\x0A\x1A\x0A': 'png', - b'\x47\x49\x46': 'gif', - b'\x42\x4D': 'bmp', + b"\xFF\xD8\xFF": "jpeg", + b"\x89\x50\x4E\x47\x0D\x0A\x1A\x0A": "png", + b"\x47\x49\x46": "gif", + b"\x42\x4D": "bmp", # 添加其他图片格式的 magic numbers } # 判断文件的图片格式 @@ -47,27 +48,32 @@ def get_image_format(header): @log def parser_xml(xml_string): assert type(xml_string) == str - root = ET.fromstring(xml_string) - emoji = root.find('./emoji') + # Parse the XML string + try: + root = ET.fromstring(xml_string) + except: + root = ET.fromstring(xml_string.replace("&", "&")) + emoji = root.find("./emoji") # Accessing attributes of the 'emoji' element - fromusername = emoji.get('fromusername') - tousername = emoji.get('tousername') - md5 = emoji.get('md5') - cdnurl = emoji.get('cdnurl') - encrypturl = emoji.get('encrypturl') - thumburl = emoji.get('thumburl') - externurl = emoji.get('externurl') - androidmd5 = emoji.get('androidmd5') - width = emoji.get('width') - height = emoji.get('height') + fromusername = emoji.get("fromusername") + tousername = emoji.get("tousername") + md5 = emoji.get("md5") + cdnurl = emoji.get("cdnurl") + encrypturl = emoji.get("encrypturl") + thumburl = emoji.get("thumburl") + externurl = emoji.get("externurl") + androidmd5 = emoji.get("androidmd5") + width = emoji.get("width") + height = emoji.get("height") return { - 'width': width, - 'height': height, - 'cdnurl': cdnurl, - 'thumburl': thumburl if thumburl else cdnurl, - 'md5': (md5 if md5 else androidmd5).lower(), + "width": width, + "height": height, + "cdnurl": cdnurl, + "thumburl": thumburl if thumburl else cdnurl, + "md5": (md5 if md5 else androidmd5).lower(), } + def singleton(cls): _instance = {} @@ -78,6 +84,7 @@ def singleton(cls): return inner + # 一定要保证只有一个实例对象 @singleton class Emotion: @@ -98,9 +105,9 @@ class Emotion: lock.release() def get_emoji_url(self, md5: str, thumb: bool) -> str | bytes: - '''供下载用,返回可能是url可能是bytes''' + """供下载用,返回可能是url可能是bytes""" if thumb: - sql = ''' + sql = """ select case when thumburl is NULL or thumburl = '' then cdnurl @@ -108,13 +115,13 @@ class Emotion: end as selected_url from CustomEmotion where md5 = ? - ''' + """ else: - sql = ''' + sql = """ select CDNUrl from CustomEmotion where md5 = ? - ''' + """ try: lock.acquire(True) self.cursor.execute(sql, [md5]) @@ -128,14 +135,14 @@ class Emotion: """ self.cursor.execute(sql, [md5]) res = self.cursor.fetchone() - return res[0] if res else '' + return res[0] if res else "" finally: lock.release() def get_emoji_URL(self, md5: str, thumb: bool): - '''只管url,另外的不管''' + """只管url,另外的不管""" if thumb: - sql = ''' + sql = """ select case when thumburl is NULL or thumburl = '' then cdnurl @@ -143,13 +150,13 @@ class Emotion: end as selected_url from CustomEmotion where md5 = ? - ''' + """ else: - sql = ''' + sql = """ select CDNUrl from CustomEmotion where md5 = ? - ''' + """ try: lock.acquire(True) self.cursor.execute(sql, [md5]) @@ -158,6 +165,7 @@ class Emotion: return "" finally: lock.release() + def close(self): if self.open_flag: try: @@ -178,12 +186,12 @@ def download(url, output_dir, name, thumb=False): image_format = get_image_format(byte[:8]) if image_format: if thumb: - output_path = os.path.join(output_dir, 'th_' + name + '.' + image_format) + output_path = os.path.join(output_dir, "th_" + name + "." + image_format) else: - output_path = os.path.join(output_dir, name + '.' + image_format) + output_path = os.path.join(output_dir, name + "." + image_format) else: output_path = os.path.join(output_dir, name) - with open(output_path, 'wb') as f: + with open(output_path, "wb") as f: f.write(resp.content) return output_path @@ -195,7 +203,7 @@ def get_most_emoji(messages): emoji_info = parser_xml(str_content) if emoji_info is None: continue - md5 = emoji_info['md5'] + md5 = emoji_info["md5"] if not md5: continue try: @@ -205,11 +213,11 @@ def get_most_emoji(messages): md5_nums = [(num[0], key, num[1]) for key, num in dic.items()] md5_nums.sort(key=lambda x: x[0], reverse=True) if not md5_nums: - return '', 0 + return "", 0 md5 = md5_nums[0][1] num = md5_nums[0][0] emoji_info = md5_nums[0][2] - url = emoji_info['cdnurl'] + url = emoji_info["cdnurl"] if not url or url == "": url = Emotion().get_emoji_url(md5, False) return url, num @@ -219,14 +227,14 @@ def get_emoji(xml_string, thumb=True, output_path=root_path) -> str: """供下载用""" try: emoji_info = parser_xml(xml_string) - md5 = emoji_info['md5'] - image_format = ['.png', '.gif', '.jpeg'] + md5 = emoji_info["md5"] + image_format = [".png", ".gif", ".jpeg"] for f in image_format: - prefix = 'th_' if thumb else '' + prefix = "th_" if thumb else "" file_path = os.path.join(output_path, prefix + md5 + f) if os.path.exists(file_path): return file_path - url = emoji_info['thumburl'] if thumb else emoji_info['cdnurl'] + url = emoji_info["thumburl"] if thumb else emoji_info["cdnurl"] if not url or url == "": url = Emotion().get_emoji_url(md5, thumb) if type(url) == str and url != "": @@ -237,50 +245,53 @@ def get_emoji(xml_string, thumb=True, output_path=root_path) -> str: image_format = get_image_format(url[:8]) if image_format: if thumb: - output_path = os.path.join(output_path, 'th_' + md5 + '.' + image_format) + output_path = os.path.join( + output_path, "th_" + md5 + "." + image_format + ) else: - output_path = os.path.join(output_path, md5 + '.' + image_format) + output_path = os.path.join(output_path, md5 + "." + image_format) else: output_path = os.path.join(output_path, md5) - with open(output_path, 'wb') as f: + with open(output_path, "wb") as f: f.write(url) print("表情包数据库加载", output_path) return output_path else: print("!!!未知表情包数据,信息:", xml_string, emoji_info, url) - output_path = os.path.join(output_path, '404.png') + output_path = os.path.join(output_path, "404.png") if not os.path.exists(output_path): - QPixmap(':/icons/icons/404.png').save(output_path) + QPixmap(":/icons/icons/404.png").save(output_path) return output_path except: logger.error(traceback.format_exc()) output_path = os.path.join(output_path, "404.png") if not os.path.exists(output_path): - QPixmap(':/icons/icons/404.png').save(output_path) + QPixmap(":/icons/icons/404.png").save(output_path) return output_path def get_emoji_path(xml_string, thumb=True, output_path=root_path) -> str: try: emoji_info = parser_xml(xml_string) - md5 = emoji_info['md5'] - image_format = ['.png', '.gif', '.jpeg'] + md5 = emoji_info["md5"] + image_format = [".png", ".gif", ".jpeg"] for f in image_format: - prefix = 'th_' if thumb else '' + prefix = "th_" if thumb else "" file_path = os.path.join(output_path, prefix + md5 + f) return file_path except: logger.error(traceback.format_exc()) output_path = os.path.join(output_path, "404.png") return output_path - + + def get_emoji_url(xml_string, thumb=True) -> str: """不管下载,只返回url""" try: emoji_info = parser_xml(xml_string) - md5 = emoji_info['md5'] + md5 = emoji_info["md5"] url = emoji_info["thumburl" if thumb else "cdnurl"] - if not url or url == '': + if not url or url == "": url = Emotion().get_emoji_URL(md5=md5, thumb=thumb) return url except: @@ -289,7 +300,7 @@ def get_emoji_url(xml_string, thumb=True) -> str: return output_path -if __name__ == '__main__': +if __name__ == "__main__": # xml_string = ' ' # res1 = parser_xml(xml_string) # print(res1, res1['md5'])