From 3e331172c32732664d48854f72857fa5a93f491b Mon Sep 17 00:00:00 2001 From: SwimmingLiu Date: Mon, 16 Sep 2024 20:13:00 +0800 Subject: [PATCH 1/5] =?UTF-8?q?=E4=BC=98=E5=8C=96=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=EF=BC=8C=E5=8E=BB=E9=99=A4=E9=9D=9E=E6=B3=95=E5=AD=97=E7=AC=A6?= =?UTF-8?q?/Emoji=E8=A1=A8=E6=83=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.py | 38 ++++++++++++++++++++------------------ 1 file changed, 20 insertions(+), 18 deletions(-) diff --git a/main.py b/main.py index 95a043e..bdd48fb 100644 --- a/main.py +++ b/main.py @@ -24,6 +24,7 @@ leave_message = list() forward_message = list() + # 信号处理函数 def signal_handler(signal, frame): # 在手动结束程序时保存已有的数据 @@ -135,23 +136,24 @@ def save_data(): # 可见说说中可能存在多张图片 item_pic_links = str(item[2]).split(",") for item_pic_link in item_pic_links: - if item_pic_link is not None and len(item_pic_link) > 0 and 'http' in item_pic_link: - # 保存图片 - pic_name = re.sub(r'[\\/:*?"<>|]', '_', item_text) + '.jpg' - # 去除文件名中的空格 - pic_name = pic_name.replace(' ', '') - - # 限制文件名长度 - if len(pic_name) > 40: - pic_name = pic_name[:40] + '.jpg' - # pic_name = pic_name.split(':')[1] + '.jpg' - response = requests.get(item_pic_link) - if response.status_code == 200: - # 防止图片重名 - if os.path.exists(pic_save_path + pic_name): - pic_name = pic_name.split('.')[0] + "_" + str(int(time.time())) + '.jpg' - with open(pic_save_path + pic_name, 'wb') as f: - f.write(response.content) + # 如果图片链接为空或者不是http链接,则跳过 + if not item_pic_link or len(item_pic_link) == 0 or 'http' not in item_pic_link: + continue + # 去除非法字符 / Emoji表情 + pic_name = re.sub(r'\[em\].*?\[/em\]|[^\w\s]|[\\/:*?"<>|\r\n]+', '_', item_text).replace(" ", "") + '.jpg' + # 去除文件名中的空格 + pic_name = pic_name.replace(' ', '') + # 限制文件名长度 + if len(pic_name) > 40: + pic_name = pic_name[:40] + '.jpg' + # pic_name = pic_name.split(':')[1] + '.jpg' + response = requests.get(item_pic_link) + if response.status_code == 200: + # 防止图片重名 + if os.path.exists(pic_save_path + pic_name): + pic_name = pic_name.split('.')[0] + "_" + str(int(time.time())) + '.jpg' + with open(pic_save_path + pic_name, 'wb') as f: + f.write(response.content) if user_nickname in item_text: if '留言' in item_text: leave_message.append(item[:-1]) @@ -262,7 +264,7 @@ def get_content_from_split(content): texts.append([put_time, text, img]) except Exception as e: print(f"获取QQ空间互动消息发生异常: {str(e)}") - texts = [t + [""] for t in texts] # 确保texts是四列, 防止后续保存结果出现问题 + texts = [t + [""] for t in texts] # 确保texts是四列, 防止后续保存结果出现问题 try: user_moments = GetAllMoments.get_visible_moments_list() if user_moments and len(user_moments) > 0: From 169829fa1b52c96b35cff2f9bf7e98fe0304c100 Mon Sep 17 00:00:00 2001 From: SwimmingLiu Date: Mon, 16 Sep 2024 20:16:38 +0800 Subject: [PATCH 2/5] =?UTF-8?q?=E5=B0=86=E6=98=B5=E7=A7=B0=E3=80=81?= =?UTF-8?q?=E8=AF=B4=E8=AF=B4=E5=86=85=E5=AE=B9=E3=80=81=E8=AF=84=E8=AE=BA?= =?UTF-8?q?=E5=8C=BA=E5=BD=93=E4=B8=AD=E7=9A=84QQ=E8=A1=A8=E6=83=85?= =?UTF-8?q?=E6=9B=BF=E6=8D=A2=E4=B8=BAimg=E6=A0=87=E7=AD=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.py | 7 +++++++ util/ToolsUtil.py | 6 ++++++ 2 files changed, 13 insertions(+) diff --git a/main.py b/main.py index bdd48fb..3cbe256 100644 --- a/main.py +++ b/main.py @@ -73,7 +73,11 @@ def render_html(shuoshuo_path, zhuanfa_path): if len(content_lst) == 1: continue nickname = content_lst[0] + # 将nickname当中的QQ表情替换为img标签 + nickname = re.sub(r'\[em\](.*?)\[/em\]', Tools.replace_em_to_img, nickname) message = content_lst[1] + # 将message当中的QQ表情替换为img标签 + message = re.sub(r'\[em\](.*?)\[/em\]', Tools.replace_em_to_img, message) image_html = '
' for img_url in img_url_lst: if img_url and img_url.startswith('http'): @@ -88,6 +92,9 @@ def render_html(shuoshuo_path, zhuanfa_path): comments = eval(comments) for comment in comments: comment_create_time, comment_content, comment_nickname, comment_uin = comment + # 将评论人昵称和评论内容当中的QQ表情替换为img标签 + comment_nickname = re.sub(r'\[em\](.*?)\[/em\]', Tools.replace_em_to_img, comment_nickname) + comment_content = re.sub(r'\[em\](.*?)\[/em\]', Tools.replace_em_to_img, comment_content) comment_avatar_url = f"https://q.qlogo.cn/headimg_dl?dst_uin={comment_uin}&spec=640&img_type=jpg" comment_html += comment_template.format( avatar_url=comment_avatar_url, diff --git a/util/ToolsUtil.py b/util/ToolsUtil.py index 83362f3..4c146cb 100644 --- a/util/ToolsUtil.py +++ b/util/ToolsUtil.py @@ -229,3 +229,9 @@ def read_txt_file(workdir, file_name): return None +# QQ空间表情替换 [em]xxx[/em] 为 +def replace_em_to_img(match): + # 获取匹配的 xxx 部分 + emoji_code = match.group(1) + return f'{emoji_code}' + From 79eaa7d4077c7eccb0dfd06458ed1b4bdf1b45f8 Mon Sep 17 00:00:00 2001 From: SwimmingLiu Date: Mon, 16 Sep 2024 20:19:53 +0800 Subject: [PATCH 3/5] =?UTF-8?q?=E8=AF=B4=E8=AF=B4=E7=BD=91=E9=A1=B5?= =?UTF-8?q?=E7=89=88=E5=9B=BE=E7=89=87=E6=94=AF=E6=8C=81=E4=B9=9D=E5=AE=AB?= =?UTF-8?q?=E6=A0=BC=E6=98=BE=E7=A4=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- util/ToolsUtil.py | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/util/ToolsUtil.py b/util/ToolsUtil.py index 4c146cb..2fe7f14 100644 --- a/util/ToolsUtil.py +++ b/util/ToolsUtil.py @@ -61,7 +61,6 @@ def show_author_info(): def get_html_template(): - # HTML模板 # HTML模板 html_template = """ @@ -108,17 +107,20 @@ def get_html_template(): }} .image {{ margin-top: 10px; - display: flex; - justify-content: space-around; - align-items: center; /* 使两张图片垂直对齐 */ - padding: 20px; + display: grid; + grid-template-columns: repeat(3, 1fr); /* 将图片分成3列 */ + grid-gap: 10px; /* 设置图片之间的间距 */ + justify-items: center; /* 居中显示图片 */ }} .image img {{ - max-width: 33vw; - max-height: 33vh; + width: 100%; /* 图片宽度100%填充父容器 */ + height: auto; /* 固定高度150px */ + object-fit: cover; /* 保持比例裁剪图片 */ + max-width: 33vw; /* 限制图片的最大宽度 */ + max-height: 33vh; /* 限制图片的最大高度 */ border-radius: 10px; cursor: pointer; - }} + }} .comments {{ margin-top: 5px; /* 调整这里的值来减少间距 */ background-color: #444; From 5072295eb35a306d5cd0587979d16d7b85a04241 Mon Sep 17 00:00:00 2001 From: SwimmingLiu Date: Mon, 16 Sep 2024 20:22:20 +0800 Subject: [PATCH 4/5] =?UTF-8?q?=E4=BC=98=E5=8C=96=E6=9C=AA=E5=88=A0?= =?UTF-8?q?=E9=99=A4=E8=AF=B4=E8=AF=B4=E5=90=88=E5=B9=B6=E8=87=B3=E4=B8=BB?= =?UTF-8?q?=E7=BA=BF=E7=A8=8B=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.py | 7 +------ util/ToolsUtil.py | 11 +++++++++++ 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/main.py b/main.py index 3cbe256..3662c25 100644 --- a/main.py +++ b/main.py @@ -221,11 +221,6 @@ def open_file(file_path): print(f"Unsupported OS: {platform.system()}") -def get_content_from_split(content): - content_split = str(content).split(":") - return content_split[1].strip() if len(content_split) > 1 else content.strip() - - if __name__ == '__main__': try: user_info = Request.get_login_user_info() @@ -277,7 +272,7 @@ def get_content_from_split(content): if user_moments and len(user_moments) > 0: # 如果可见说说的内容是从消息列表恢复的说说内容子集,则不添加到消息列表中 texts = [t for t in texts if - not any(get_content_from_split(u[1]) in get_content_from_split(t[1]) for u in user_moments)] + not any(Tools.is_any_mutual_exist(t[1], u[1]) for u in user_moments)] texts.extend(user_moments) except Exception as err: print(f"获取未删除QQ空间记录发生异常: {str(err)}") diff --git a/util/ToolsUtil.py b/util/ToolsUtil.py index 2fe7f14..93ac55a 100644 --- a/util/ToolsUtil.py +++ b/util/ToolsUtil.py @@ -237,3 +237,14 @@ def replace_em_to_img(match): emoji_code = match.group(1) return f'{emoji_code}' + +def get_content_from_split(content): + content_split = str(content).split(":") + return content_split[1].strip() if len(content_split) > 1 else content.strip() + + +# 判断两个字符串是否存在互相包含的情况 +def is_any_mutual_exist(str1, str2): + str1 = get_content_from_split(str1) + str2 = get_content_from_split(str2) + return str1 in str2 or str2 in str1 From 476091ff1b4cd1dbf015da25d6fd12d205364ca5 Mon Sep 17 00:00:00 2001 From: SwimmingLiu Date: Mon, 16 Sep 2024 20:25:45 +0800 Subject: [PATCH 5/5] =?UTF-8?q?=E4=BC=98=E5=8C=96=E5=86=97=E4=BD=99?= =?UTF-8?q?=E9=83=A8=E5=88=86=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/main.py b/main.py index 3662c25..f08ce99 100644 --- a/main.py +++ b/main.py @@ -1,9 +1,7 @@ import shutil from datetime import datetime -import platform import subprocess from bs4 import BeautifulSoup -from tqdm import trange import util.RequestUtil as Request import util.ToolsUtil as Tools import util.ConfigUtil as Config