每天5点发送秀人图册,密码为4000

This commit is contained in:
liuwei
2025-02-24 17:08:46 +08:00
parent c64b58540d
commit 8bcfe55909
5 changed files with 114 additions and 3 deletions

View File

@@ -49,7 +49,7 @@ def main(chat_type: int):
robot.onEveryTime("18:00", robot.game_auto_tasks)
# 秀人网每天自动下载帖子
robot.onEveryTime("03:00", robot.xiu_ren_download_task)
robot.onEveryTime("17:00", robot.xiu_ren_download_task)
# 让机器人一直跑
robot.keepRunningAndBlockProcess()

View File

@@ -538,6 +538,7 @@ class Robot(Job):
def xiu_ren_download_task(self):
try:
xiuren_dowload_pic()
path = xiuren_dowload_pic()
self.wcf.send_file(path, "45317011307@chatroom")
except Exception as e:
self.LOG.error(f"xiuren_dowload_pic error{e}")

View File

@@ -31,3 +31,4 @@ def get_xiuren_pic():
return os.path.abspath(random_file_path)
else:
return None

View File

@@ -5,6 +5,8 @@ import time
import random
import re
from xiuren.xiuren_pdf import generate_pdf_from_images
def get_html(url, session):
headers = {
@@ -131,7 +133,7 @@ def xiuren_dowload_pic():
return
processed_count = 0 # 记录已处理的帖子数量
target_count = 2 # 目标处理2个新帖子
target_count = 1 # 目标处理2个新帖子
for post in post_info:
if processed_count >= target_count:
@@ -167,6 +169,9 @@ def xiuren_dowload_pic():
print(f"完成处理帖子 {post_number}")
processed_count += 1
# 将下载好的帖子生成PDF
return generate_pdf_from_images('.')
if __name__ == '__main__':
xiuren_dowload_pic()

104
xiuren/xiuren_pdf.py Normal file
View File

@@ -0,0 +1,104 @@
import os
from reportlab.lib.pagesizes import A3
from reportlab.platypus import SimpleDocTemplate, Image
from PyPDF2 import PdfReader, PdfWriter
from PIL import Image as PILImage
def create_pdf_from_images(directory, output_pdf):
image_extensions = {'.jpg', '.jpeg', '.png', '.gif', '.bmp', '.tiff', '.webp'}
image_files = []
# 遍历目录并筛选图片文件
for root, dirs, files in os.walk(directory):
for file in files:
_, ext = os.path.splitext(file)
if ext.lower() in image_extensions:
image_files.append(os.path.join(root, file))
# 如果没有找到任何图片,返回
if not image_files:
print(f"No image files found in {directory}.")
return
# 按文件名排序,确保图片按顺序
image_files.sort()
# 设置PDF文档使用 A3 页面大小
doc = SimpleDocTemplate(output_pdf, pagesize=A3)
# 创建图片列表
image_list = []
for image_file in image_files:
# 使用 Pillow 获取图片的尺寸
with PILImage.open(image_file) as img:
img_width, img_height = img.size
# 设置目标宽度适应A3页面宽度最大842点
target_width = 800 # 设置图片宽度为最大800点适应A3页面
# 计算新的高度,保持原始宽高比
target_height = int(img_height * (target_width / img_width))
# 限制图片的最大高度,避免超出页面
max_page_height = 1091 - 100 # 留出一些空间A3页面高度1191点
if target_height > max_page_height:
target_height = max_page_height
target_width = int(target_height * (img_width / img_height)) # 调整宽度保持比例
# 创建图片对象并添加到图片列表
img = Image(image_file, width=target_width, height=target_height)
image_list.append(img)
# 生成 PDF
doc.build(image_list)
print(f"PDF {output_pdf} created successfully.")
def encrypt_pdf(input_pdf, output_pdf, password):
# 使用 PyPDF2 加密 PDF
writer = PdfWriter()
with open(input_pdf, "rb") as file:
reader = PdfReader(file)
for page in reader.pages:
writer.add_page(page)
with open(output_pdf, "wb") as file:
writer.encrypt(password)
writer.write(file)
print(f"PDF {output_pdf} encrypted successfully.")
def generate_pdf_from_images(directory):
# 获取当前目录下所有数字命名的文件夹
folder_names = [folder for folder in os.listdir(directory)
if os.path.isdir(os.path.join(directory, folder)) and folder.isdigit()]
# 如果没有数字命名的文件夹,提示并返回
if not folder_names:
print("No numeric-named folders found.")
return
# 循环处理每个数字命名的文件夹
for folder_name in folder_names:
folder_path = os.path.join(directory, folder_name)
# 设置 PDF 输出路径,使用文件夹名称作为文件名,并存储到 ./PDF 目录下
output_pdf = f"./PDF/{folder_name}.pdf"
# 检查 PDF 文件是否已存在
if os.path.exists(output_pdf):
print(f"PDF {output_pdf} already exists. Skipping...")
continue
# 创建目录
os.makedirs("./PDF", exist_ok=True)
# 创建 PDF
create_pdf_from_images(folder_path, output_pdf)
# 加密 PDF
encrypt_pdf(output_pdf, output_pdf, "4000") # 密码设置为 4000
# 返回PDF位置用于发送到群里
return os.path.abspath(output_pdf)