From b1f42aa0bb30ac06dddf67bd665e45bff6a13042 Mon Sep 17 00:00:00 2001 From: STDquantum <405720329@qq.com> Date: Sat, 16 Dec 2023 12:29:51 +0800 Subject: [PATCH] =?UTF-8?q?=E5=9F=BA=E6=9C=AC=E8=A7=A3=E5=86=B3=E5=8A=A0?= =?UTF-8?q?=E5=AF=86=E8=A1=A8=E6=83=85=E5=8C=85=E7=9A=84=E8=A7=A3=E6=9E=90?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/DataBase/output_pc.py | 8 ++- app/util/emoji.py | 108 +++++++++++++++++++++++++++++++++----- 2 files changed, 101 insertions(+), 15 deletions(-) diff --git a/app/DataBase/output_pc.py b/app/DataBase/output_pc.py index 7e9fb3d..ae4193e 100644 --- a/app/DataBase/output_pc.py +++ b/app/DataBase/output_pc.py @@ -284,7 +284,11 @@ class ChildThread(QThread): timestamp = message[5] if self.output_type == Output.HTML: emoji_path = get_emoji(str_content, thumb=True, output_path=origin_docx_path + '/emoji') - emoji_path = './emoji/' + os.path.basename(emoji_path) + if emoji_path == "": + shutil.copy(f"{os.path.abspath('.')}/app/resources/icons/404.png", origin_docx_path + '/emoji/404.png') + emoji_path = "./emoji/404.png" + else: + emoji_path = './emoji/' + os.path.basename(emoji_path) if self.is_5_min(timestamp): doc.write( f'''{{ type:0, text: '{str_time}',is_send:0,avatar_path:''}},''' @@ -463,7 +467,7 @@ class ChildThread(QThread): self.emoji(f, message) elif type_ == 10000 and self.message_types.get(type_): self.system_msg(f, message) - elif type_ == 49 and sub_type == 57: + elif type_ == 49 and sub_type == 57 and self.message_types.get(1): self.refermsg(f,message) f.write(html_end) f.close() diff --git a/app/util/emoji.py b/app/util/emoji.py index d2a4d7f..f6b707a 100644 --- a/app/util/emoji.py +++ b/app/util/emoji.py @@ -11,7 +11,8 @@ emoji.py import os import traceback import xml.etree.ElementTree as ET - +import sqlite3 +import threading import requests from app.log import log, logger @@ -43,7 +44,10 @@ def get_image_format(header): def parser_xml(xml_string): assert type(xml_string) == str # Parse the XML string - root = ET.fromstring(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') @@ -61,10 +65,67 @@ def parser_xml(xml_string): 'height': height, 'cdnurl': cdnurl, 'thumburl': thumburl if thumburl else cdnurl, - 'md5': md5 if md5 else androidmd5, + 'md5': (md5 if md5 else androidmd5).lower(), } -@log +def singleton(cls): + _instance = {} + + def inner(): + if cls not in _instance: + _instance[cls] = cls() + return _instance[cls] + + return inner + +lock = threading.Lock() +db_path = "./app/Database/Msg/Emotion.db" + +@singleton +class Emotion: + def __init__(self): + self.DB = None + self.cursor: sqlite3.Cursor = None + self.open_flag = False + self.init_database() + + def init_database(self): + if not self.open_flag: + if os.path.exists(db_path): + self.DB = sqlite3.connect(db_path, check_same_thread=False) + # '''创建游标''' + self.cursor = self.DB.cursor() + self.open_flag = True + if lock.locked(): + lock.release() + + def get_emoji_url(self, md5: str): + sql = ''' + select CDNUrl + from CustomEmotion + where md5 = ? + ''' + try: + lock.acquire(True) + self.cursor.execute(sql, [md5]) + return self.cursor.fetchone()[0] + except: + md5 = md5.upper() + sql = """ + select Data + from EmotionItem + where md5 = ? + """ + self.cursor.execute(sql, [md5]) + try: + return self.cursor.fetchone()[0] + except: + return "" + finally: + lock.release() + +emoji_db = Emotion() + def download(url, output_dir, name, thumb=False): if not url: return ':/icons/icons/404.png' @@ -92,23 +153,44 @@ def get_emoji(xml_string, thumb=True, output_path=root_path) -> str: prefix = 'th_' if thumb else '' file_path = os.path.join(output_path, prefix + md5 + f) if os.path.exists(file_path): - print('表情包已存在') + # print('表情包已存在', file_path) return file_path url = emoji_info['thumburl'] if thumb else emoji_info['cdnurl'] - print("下载表情包ing:", url) - emoji_path = download(url, output_path, md5, thumb) - return emoji_path + if not url or url == "": + url = emoji_db.get_emoji_url(md5) + if type(url) == str and url != "": + print("下载表情包ing:", url) + emoji_path = download(url, output_path, md5, thumb) + return emoji_path + elif type(url) == bytes: + image_format = get_image_format(url[:8]) + if image_format: + if thumb: + output_path = os.path.join(output_path, 'th_' + md5 + '.' + image_format) + else: + 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: + f.write(url) + print("表情包数据库加载", output_path) + return output_path + else: + print("!!!未知表情包数据,信息:", xml_string, emoji_info, url) + return "" except: logger.error(traceback.format_exc()) return "" if __name__ == '__main__': - xml_string = ' ' - res1 = parser_xml(xml_string) - print(res1, res1['md5']) + db_path = r"..\DataBase\Msg\Emotion.db" + xml_string = '' + print(get_emoji(xml_string, thumb=False, output_path=".")) + # ET.fromstring() + # print(res1, res1['md5']) # download(res1['cdnurl'], "./data/emoji/", res1['md5']) # download(res1['thumburl'], "./data/emoji/", res1['md5'], True) - print(get_emoji(xml_string, True)) - print(get_emoji(xml_string, False)) + # print(get_emoji(xml_string, True)) + # print(get_emoji(xml_string, False)) # http://vweixinf.tc.qq.com/110/20403/stodownload?m=3a4d439aba02dce4834b2c54e9f15597&filekey=3043020101042f302d02016e0402534804203361346434333961626130326463653438333462326335346539663135353937020213f0040d00000004627466730000000131&hy=SH&storeid=323032313037323030373236313130303039653236646365316535316534383236386234306230303030303036653033303034666233&ef=3&bizid=1022