Merge pull request #34 from SwimmingLiu/main

说说网页版图片支持九宫格和Emoji,优化部分代码
This commit is contained in:
LibraHp_0928 2024-09-16 21:28:22 +08:00 committed by GitHub
commit 4bde77e815
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 55 additions and 34 deletions

26
main.py
View File

@ -1,9 +1,7 @@
import shutil import shutil
from datetime import datetime from datetime import datetime
import platform
import subprocess import subprocess
from bs4 import BeautifulSoup from bs4 import BeautifulSoup
from tqdm import trange
import util.RequestUtil as Request import util.RequestUtil as Request
import util.ToolsUtil as Tools import util.ToolsUtil as Tools
import util.ConfigUtil as Config import util.ConfigUtil as Config
@ -24,6 +22,7 @@ user_message = list()
leave_message = list() leave_message = list()
forward_message = list() forward_message = list()
# 信号处理函数 # 信号处理函数
def signal_handler(signal, frame): def signal_handler(signal, frame):
# 在手动结束程序时保存已有的数据 # 在手动结束程序时保存已有的数据
@ -72,7 +71,11 @@ def render_html(shuoshuo_path, zhuanfa_path):
if len(content_lst) == 1: if len(content_lst) == 1:
continue continue
nickname = content_lst[0] nickname = content_lst[0]
# 将nickname当中的QQ表情替换为img标签
nickname = re.sub(r'\[em\](.*?)\[/em\]', Tools.replace_em_to_img, nickname)
message = content_lst[1] message = content_lst[1]
# 将message当中的QQ表情替换为img标签
message = re.sub(r'\[em\](.*?)\[/em\]', Tools.replace_em_to_img, message)
image_html = '<div class="image">' image_html = '<div class="image">'
for img_url in img_url_lst: for img_url in img_url_lst:
if img_url and img_url.startswith('http'): if img_url and img_url.startswith('http'):
@ -87,6 +90,9 @@ def render_html(shuoshuo_path, zhuanfa_path):
comments = eval(comments) comments = eval(comments)
for comment in comments: for comment in comments:
comment_create_time, comment_content, comment_nickname, comment_uin = comment 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_avatar_url = f"https://q.qlogo.cn/headimg_dl?dst_uin={comment_uin}&spec=640&img_type=jpg"
comment_html += comment_template.format( comment_html += comment_template.format(
avatar_url=comment_avatar_url, avatar_url=comment_avatar_url,
@ -135,12 +141,13 @@ def save_data():
# 可见说说中可能存在多张图片 # 可见说说中可能存在多张图片
item_pic_links = str(item[2]).split(",") item_pic_links = str(item[2]).split(",")
for item_pic_link in item_pic_links: 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: # 如果图片链接为空或者不是http链接则跳过
# 保存图片 if not item_pic_link or len(item_pic_link) == 0 or 'http' not in item_pic_link:
pic_name = re.sub(r'[\\/:*?"<>|]', '_', item_text) + '.jpg' continue
# 去除非法字符 / Emoji表情
pic_name = re.sub(r'\[em\].*?\[/em\]|[^\w\s]|[\\/:*?"<>|\r\n]+', '_', item_text).replace(" ", "") + '.jpg'
# 去除文件名中的空格 # 去除文件名中的空格
pic_name = pic_name.replace(' ', '') pic_name = pic_name.replace(' ', '')
# 限制文件名长度 # 限制文件名长度
if len(pic_name) > 40: if len(pic_name) > 40:
pic_name = pic_name[:40] + '.jpg' pic_name = pic_name[:40] + '.jpg'
@ -212,11 +219,6 @@ def open_file(file_path):
print(f"Unsupported OS: {platform.system()}") 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__': if __name__ == '__main__':
try: try:
user_info = Request.get_login_user_info() user_info = Request.get_login_user_info()
@ -268,7 +270,7 @@ if __name__ == '__main__':
if user_moments and len(user_moments) > 0: if user_moments and len(user_moments) > 0:
# 如果可见说说的内容是从消息列表恢复的说说内容子集,则不添加到消息列表中 # 如果可见说说的内容是从消息列表恢复的说说内容子集,则不添加到消息列表中
texts = [t for t in texts if 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) texts.extend(user_moments)
except Exception as err: except Exception as err:
print(f"获取未删除QQ空间记录发生异常: {str(err)}") print(f"获取未删除QQ空间记录发生异常: {str(err)}")

View File

@ -61,7 +61,6 @@ def show_author_info():
def get_html_template(): def get_html_template():
# HTML模板
# HTML模板 # HTML模板
html_template = """ html_template = """
<!DOCTYPE html> <!DOCTYPE html>
@ -108,14 +107,17 @@ def get_html_template():
}} }}
.image {{ .image {{
margin-top: 10px; margin-top: 10px;
display: flex; display: grid;
justify-content: space-around; grid-template-columns: repeat(3, 1fr); /* 将图片分成3列 */
align-items: center; /* 使两张图片垂直对齐 */ grid-gap: 10px; /* 设置图片之间的间距 */
padding: 20px; justify-items: center; /* 居中显示图片 */
}} }}
.image img {{ .image img {{
max-width: 33vw; width: 100%; /* 图片宽度100%填充父容器 */
max-height: 33vh; height: auto; /* 固定高度150px */
object-fit: cover; /* 保持比例裁剪图片 */
max-width: 33vw; /* 限制图片的最大宽度 */
max-height: 33vh; /* 限制图片的最大高度 */
border-radius: 10px; border-radius: 10px;
cursor: pointer; cursor: pointer;
}} }}
@ -229,3 +231,20 @@ def read_txt_file(workdir, file_name):
return None return None
# QQ空间表情替换 [em]xxx[/em] 为 <img src="http://qzonestyle.gtimg.cn/qzone/em/xxx.gif">
def replace_em_to_img(match):
# 获取匹配的 xxx 部分
emoji_code = match.group(1)
return f'<img src="http://qzonestyle.gtimg.cn/qzone/em/{emoji_code}.gif" alt="{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