From c81a3080ca3bc3420cb3dcf9d04b53d088df679e Mon Sep 17 00:00:00 2001 From: jinye_huang Date: Mon, 12 May 2025 18:51:15 +0800 Subject: [PATCH] =?UTF-8?q?=E5=9F=BA=E6=9C=AC=E5=8F=AF=E7=94=A8=E7=9A=84?= =?UTF-8?q?=E5=88=86=E5=8F=91=E6=A8=A1=E5=9D=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- scripts/distribute_content.py | 71 +++++++++++++++++++++++++++++------ scripts/distribute_example.sh | 14 ++++--- 2 files changed, 69 insertions(+), 16 deletions(-) diff --git a/scripts/distribute_content.py b/scripts/distribute_content.py index d2ea341..fac3566 100644 --- a/scripts/distribute_content.py +++ b/scripts/distribute_content.py @@ -13,6 +13,7 @@ import json import smtplib import zipfile import time +import glob from email.mime.text import MIMEText from email.mime.multipart import MIMEMultipart from email.mime.application import MIMEApplication @@ -72,9 +73,38 @@ def parse_arguments(): help='上一次分发结果CSV或报告文件路径,用于避免重复发送') parser.add_argument('--skip-sent-success', action='store_true', help='跳过上次成功发送的文章') + parser.add_argument('--zip-filename', type=str, default=None, + help='指定ZIP压缩包的基本文件名(不含扩展名),如"文旅小红书带货笔记内容0512"') return parser.parse_args() +def find_additional_images(file_path): + """查找与文章相关的所有额外图片""" + if not file_path or pd.isna(file_path) or not os.path.exists(file_path): + return [] + + # 获取文章目录 + article_dir = os.path.dirname(file_path) + + # 查找可能的额外图片目录 + additional_images_dir = os.path.join(article_dir, "additional_images") + if not os.path.exists(additional_images_dir): + # 尝试向上一级寻找 + parent_dir = os.path.dirname(article_dir) + additional_images_dir = os.path.join(parent_dir, "additional_images") + if not os.path.exists(additional_images_dir): + return [] + + # 查找所有图片文件 + image_patterns = ["*.jpg", "*.jpeg", "*.png", "*.gif", "*.webp"] + image_files = [] + + for pattern in image_patterns: + image_files.extend(glob.glob(os.path.join(additional_images_dir, pattern))) + + logger.info(f"找到 {len(image_files)} 张额外图片: {additional_images_dir}") + return image_files + def read_user_csv(user_csv_path, email_column, username_column): """读取用户CSV文件""" try: @@ -239,12 +269,17 @@ def prepare_distribution_csv(users_with_content, output_dir): rows = [] for user in users_with_content: for content in user['contents']: + # 查找额外图片 + output_txt_path = content['OutputTxtPath'] + additional_images = find_additional_images(output_txt_path) + rows.append({ 'email': user['email'], 'username': user['username'], 'file_path': content['OutputTxtPath'], 'poster_path': content['PosterPath'], 'article_json_path': content['ArticleJsonPath'], + 'additional_images': ';'.join(additional_images), # 保存为分号分隔的字符串 'entry_id': content['EntryID'], 'topic_index': content.get('TopicIndex', ''), 'variant_index': content.get('VariantIndex', ''), @@ -271,6 +306,9 @@ def create_zip_file(files, output_path): os.makedirs(os.path.dirname(output_path), exist_ok=True) with zipfile.ZipFile(output_path, 'w', zipfile.ZIP_DEFLATED) as zipf: for file_path in files: + if not file_path or pd.isna(file_path): + continue + if os.path.exists(file_path): arcname = os.path.basename(file_path) zipf.write(file_path, arcname=arcname) @@ -325,7 +363,7 @@ def send_single_email(from_addr, password, to_addr, subject, content, attachment return False def send_emails(distribution_csv, output_dir, email_from, email_password, - subject, smtp_server, smtp_port, use_ssl, test_mode, delay=2): + subject, smtp_server, smtp_port, use_ssl, test_mode, delay=2, zip_filename=None): """发送邮件""" try: # 确保输出目录存在 @@ -365,13 +403,22 @@ def send_emails(distribution_csv, output_dir, email_from, email_password, # 收集所有文件路径 files = [] for row in rows: + # 收集文本文件 file_path = row.get('file_path') - if file_path and os.path.exists(file_path): + if file_path and not pd.isna(file_path) and os.path.exists(file_path): files.append(file_path) + # 收集海报图片 poster_path = row.get('poster_path') - if poster_path and os.path.exists(poster_path): + if poster_path and not pd.isna(poster_path) and os.path.exists(poster_path): files.append(poster_path) + + # 收集所有额外图片 + additional_images = row.get('additional_images', '') + if additional_images and not pd.isna(additional_images): + for img_path in additional_images.split(';'): + if img_path.strip() and os.path.exists(img_path): + files.append(img_path) if not files: logger.warning(f"邮箱 {email} 没有有效的附件文件,跳过") @@ -385,8 +432,14 @@ def send_emails(distribution_csv, output_dir, email_from, email_password, continue # 创建ZIP文件 - zip_filename = f"content_{datetime.now().strftime('%Y%m%d_%H%M%S')}_{hash(email) % 10000}.zip" - zip_path = os.path.join(output_dir, "temp_zips", zip_filename) + if zip_filename: + # 使用指定的文件名 + zip_filename_with_ext = f"{zip_filename}.zip" + else: + # 使用默认的时间戳文件名 + zip_filename_with_ext = f"content_{datetime.now().strftime('%Y%m%d_%H%M%S')}_{hash(email) % 10000}.zip" + + zip_path = os.path.join(output_dir, "temp_zips", zip_filename_with_ext) zip_file = create_zip_file(files, zip_path) if zip_file: @@ -406,12 +459,8 @@ def send_emails(distribution_csv, output_dir, email_from, email_password, files_count = len(files) entries_count = len(rows) - email_content = f"""尊敬的用户: + email_content = f"""您好!请查收今日带货笔记(文案+配图),内容在文件压缩包内。具体挂载商品等操作流程请查看对应达人微信群内信息。 -您好!附件中是您的旅游内容创作成果,请查收。 -本次发送了 {entries_count} 篇文章,共 {files_count} 个文件。 - -祝好! """ # 发送邮件 @@ -662,7 +711,7 @@ def main(): send_result_csv = send_emails( distribution_csv, args.output_dir, args.email_from, args.email_password, args.subject, args.smtp_server, args.smtp_port, args.use_ssl, args.test_mode, - args.delay + args.delay, args.zip_filename ) # 生成分发报告 diff --git a/scripts/distribute_example.sh b/scripts/distribute_example.sh index f94946e..d9bf0f0 100755 --- a/scripts/distribute_example.sh +++ b/scripts/distribute_example.sh @@ -14,9 +14,11 @@ USER_CSV="$BASE_DIR/output/5.12 copy.csv" MANIFEST_CSV="$BASE_DIR/output/2025-05-12_09-33-12/manifest_2025-05-12_09-33-12.csv" EMAIL_FROM="zwysendemail@163.com" EMAIL_PASSWORD="NMhVGFmCJkGEy3B5" -SUBJECT="您的旅游内容创作" +SUBJECT="文旅小红书带货笔记内容0512" # 上一次分发结果文件(如果存在) PREVIOUS_DIST="$BASE_DIR/distribution_results/distribution_summary_20250512_183328.csv" +# 压缩包文件名 +ZIP_FILENAME="文旅小红书带货笔记内容0512" # 创建必要的目录 mkdir -p "$LOG_DIR" @@ -41,9 +43,9 @@ python scripts/distribute_content.py \ --subject "$SUBJECT" \ --article-per-user 1 \ --judge-only-success \ - --test-mode \ --previous-distribution "$PREVIOUS_DIST" \ - --skip-sent-success + --skip-sent-success \ + --zip-filename "$ZIP_FILENAME" # 实际发送邮件的命令(取消注释以启用) # echo "开始实际发送邮件..." @@ -60,7 +62,8 @@ python scripts/distribute_content.py \ # --judge-only-success \ # --max-send-count 10 \ # 限制最多发送给10个用户 # --previous-distribution "$PREVIOUS_DIST" \ -# --skip-sent-success +# --skip-sent-success \ +# --zip-filename "$ZIP_FILENAME" # 不使用过滤功能的示例 # python scripts/distribute_content.py \ @@ -71,6 +74,7 @@ python scripts/distribute_content.py \ # --email-password "$EMAIL_PASSWORD" \ # --subject "$SUBJECT" \ # --article-per-user 3 \ -# --judge-only-success +# --judge-only-success \ +# --zip-filename "$ZIP_FILENAME" echo "脚本执行完成 - $(date)" \ No newline at end of file