Merge pull request #32 from SwimmingLiu/main

同步分页获取未删除说说代码至主线程, 说说图片替换为高清图, 优化部分代码
This commit is contained in:
LibraHp_0928 2024-09-16 07:35:10 +08:00 committed by GitHub
commit a476cf0342
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 60 additions and 18 deletions

31
main.py
View File

@ -1,3 +1,4 @@
import shutil
from datetime import datetime from datetime import datetime
import platform import platform
import subprocess import subprocess
@ -16,6 +17,12 @@ import requests
import time import time
import platform import platform
texts = list()
all_friends = list()
other_message = list()
user_message = list()
leave_message = list()
forward_message = list()
# 信号处理函数 # 信号处理函数
def signal_handler(signal, frame): def signal_handler(signal, frame):
@ -69,6 +76,9 @@ def render_html(shuoshuo_path, zhuanfa_path):
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'):
# 将图片替换为高清图
img_url = str(img_url).replace("/m&ek=1&kp=1", "/s&ek=1&kp=1")
img_url = str(img_url).replace(r"!/m/", "!/s/")
image_html += f'<img src="{img_url}" alt="图片">\n' image_html += f'<img src="{img_url}" alt="图片">\n'
image_html += "</div>" image_html += "</div>"
comment_html = "" comment_html = ""
@ -186,8 +196,18 @@ def open_file(file_path):
# macOS 系统使用 subprocess 和 open 命令 # macOS 系统使用 subprocess 和 open 命令
subprocess.run(['open', file_path]) subprocess.run(['open', file_path])
elif platform.system() == 'Linux': elif platform.system() == 'Linux':
# Linux 系统使用 subprocess 和 xdg-open 命令 # Linux 系统,首先检查是否存在 xdg-open 工具
subprocess.run(['xdg-open', file_path]) if shutil.which('xdg-open'):
subprocess.run(['xdg-open', file_path])
# 如果 xdg-open 不存在,检查是否存在 gnome-open 工具(适用于 GNOME 桌面环境)
elif shutil.which('gnome-open'):
subprocess.run(['gnome-open', file_path])
# 如果 gnome-open 不存在,检查是否存在 kde-open 工具(适用于 KDE 桌面环境)
elif shutil.which('kde-open'):
subprocess.run(['kde-open', file_path])
# 如果以上工具都不存在,提示用户手动打开文件
else:
print("未找到可用的打开命令,请手动打开文件。")
else: else:
print(f"Unsupported OS: {platform.system()}") print(f"Unsupported OS: {platform.system()}")
@ -205,12 +225,7 @@ if __name__ == '__main__':
except Exception as e: except Exception as e:
print(f"登录失败:请重新登录,错误信息:{str(e)}") print(f"登录失败:请重新登录,错误信息:{str(e)}")
exit(0) exit(0)
texts = []
all_friends = []
other_message = []
user_message = []
leave_message = []
forward_message = []
count = Request.get_message_count() count = Request.get_message_count()
try: try:
# 注册信号处理函数 # 注册信号处理函数

View File

@ -1,4 +1,5 @@
import json import json
import math
import os import os
import re import re
import sys import sys
@ -11,13 +12,15 @@ from util import RequestUtil as Request
from util import LoginUtil from util import LoginUtil
from util import ToolsUtil as Tool from util import ToolsUtil as Tool
WORKDIR = "./resource/fetch-all/" QQ_NUMBER = Request.uin
WORKDIR = f"./resource/fetch-all/{QQ_NUMBER}" # 通过QQ号进行区分配置文件防止误加载其他用户信息
USER_QZONE_INFO = 'user_qzone_info.json' USER_QZONE_INFO = 'user_qzone_info.json'
QZONE_MOMENTS_ALL = 'qzone_moments_all.json' QZONE_MOMENTS_ALL = 'qzone_moments_all.json'
# 获取所有可见的未删除的说说+高清图片包含2014年之前 # 获取所有可见的未删除的说说+高清图片包含2014年之前
def get_visible_moments_list(): def get_visible_moments_list():
# 1. 获取说说总条数 # 1. 获取说说总条数
user_qzone_info = Tool.read_txt_file(WORKDIR, USER_QZONE_INFO) user_qzone_info = Tool.read_txt_file(WORKDIR, USER_QZONE_INFO)
if not user_qzone_info: if not user_qzone_info:
@ -25,15 +28,16 @@ def get_visible_moments_list():
qq_userinfo_response = get_user_qzone_info(1) qq_userinfo_response = get_user_qzone_info(1)
Tool.write_txt_file(WORKDIR, USER_QZONE_INFO, qq_userinfo_response) Tool.write_txt_file(WORKDIR, USER_QZONE_INFO, qq_userinfo_response)
user_qzone_info = Tool.read_txt_file(WORKDIR, USER_QZONE_INFO) user_qzone_info = Tool.read_txt_file(WORKDIR, USER_QZONE_INFO)
if not Tool.is_valid_json(user_qzone_info): if not Tool.is_valid_json(user_qzone_info):
print("获取QQ空间信息失败") print("获取QQ空间信息失败")
return None return None
json_dict = json.loads(user_qzone_info) json_dict = json.loads(user_qzone_info)
totalMomentsCount = json_dict['total'] total_moments_count = json_dict['total']
print(f'你的未删除说说总条数{totalMomentsCount}') print(f'你的未删除说说总条数{total_moments_count}')
# 当前未删除说说总数为0, 直接返回 # 当前未删除说说总数为0, 直接返回
if totalMomentsCount == 0: if total_moments_count == 0:
return None return None
# 2. 获取所有说说数据 # 2. 获取所有说说数据
@ -41,8 +45,22 @@ def get_visible_moments_list():
qzone_moments_all = Tool.read_txt_file(WORKDIR, QZONE_MOMENTS_ALL) qzone_moments_all = Tool.read_txt_file(WORKDIR, QZONE_MOMENTS_ALL)
if not qzone_moments_all: if not qzone_moments_all:
# 缓存未找到,开始请求获取所有未删除说说 # 缓存未找到,开始请求获取所有未删除说说
qq_userinfo_response = get_user_qzone_info(totalMomentsCount) # qq_userinfo_response = get_user_qzone_info(totalMomentsCount)
Tool.write_txt_file(WORKDIR, QZONE_MOMENTS_ALL, qq_userinfo_response) # Tool.write_txt_file(WORKDIR, QZONE_MOMENTS_ALL, qq_userinfo_response)
# qzone_moments_all = Tool.read_txt_file(WORKDIR, QZONE_MOMENTS_ALL)
default_page_size = 30 # 默认一页30条
total_page_num = math.ceil(total_moments_count / default_page_size) # 总页数
all_page_data = [] # 用于存储所有页的数据
for current_page_num in range(0, total_page_num):
# 数据偏移量
pos = current_page_num * default_page_size
qq_userinfo_response = get_user_qzone_info(default_page_size, pos)
current_page_data = json.loads(qq_userinfo_response)["msglist"]
if current_page_data:
all_page_data.extend(current_page_data)
time.sleep(0.02)
qq_userinfo = json.dumps({"msglist": all_page_data}, ensure_ascii=False, indent=2)
Tool.write_txt_file(WORKDIR, QZONE_MOMENTS_ALL, qq_userinfo)
qzone_moments_all = Tool.read_txt_file(WORKDIR, QZONE_MOMENTS_ALL) qzone_moments_all = Tool.read_txt_file(WORKDIR, QZONE_MOMENTS_ALL)
if not Tool.is_valid_json(qzone_moments_all): if not Tool.is_valid_json(qzone_moments_all):
@ -81,7 +99,7 @@ def get_visible_moments_list():
# 获取用户QQ空间相关信息 # 获取用户QQ空间相关信息
def get_user_qzone_info(num): def get_user_qzone_info(page_size, offset=0):
url = 'https://user.qzone.qq.com/proxy/domain/taotao.qq.com/cgi-bin/emotion_cgi_msglist_v6' url = 'https://user.qzone.qq.com/proxy/domain/taotao.qq.com/cgi-bin/emotion_cgi_msglist_v6'
cookies = Request.cookies cookies = Request.cookies
g_tk = LoginUtil.bkn(cookies.get('p_skey')) g_tk = LoginUtil.bkn(cookies.get('p_skey'))
@ -109,8 +127,8 @@ def get_user_qzone_info(num):
'uin': f'{qqNumber}', 'uin': f'{qqNumber}',
'ftype': '0', 'ftype': '0',
'sort': '0', 'sort': '0',
'pos': '0', 'pos': f'{offset}',
'num': f'{num}', 'num': f'{page_size}',
'replynum': '100', 'replynum': '100',
'g_tk': f'{g_tk}', 'g_tk': f'{g_tk}',
'callback': '_preloadCallback', 'callback': '_preloadCallback',

View File

@ -61,6 +61,7 @@ def show_author_info():
def get_html_template(): def get_html_template():
# HTML模板
# HTML模板 # HTML模板
html_template = """ html_template = """
<!DOCTYPE html> <!DOCTYPE html>
@ -116,6 +117,7 @@ def get_html_template():
max-width: 33vw; max-width: 33vw;
max-height: 33vh; max-height: 33vh;
border-radius: 10px; border-radius: 10px;
cursor: pointer;
}} }}
.comments {{ .comments {{
margin-top: 5px; /* 调整这里的值来减少间距 */ margin-top: 5px; /* 调整这里的值来减少间距 */
@ -147,7 +149,14 @@ def get_html_template():
<body> <body>
{posts} {posts}
<script>
// 为所有图片添加点击事件
document.querySelectorAll(".image img").forEach(img => {{
img.addEventListener("click", function() {{
window.open(this.src, '_blank'); // 打开图片链接并在新标签页中展示
}});
}});
</script>
</body> </body>
</html> </html>
""" """