import os import argparse import json import html import base64 import mimetypes import urllib.request def generate_gift_card(product_name, price, evaluation, thank_you_json, return_gift_json, vibe_code, image_url, output_path="gift_card_result.html"): """ 生成现代风格的交互式礼品鉴定卡片。 """ # --- 图片转 Base64 逻辑 (保持上一步功能) --- final_image_src = image_url try: image_data = None mime_type = None if image_url.startswith(('http://', 'https://')): req = urllib.request.Request(image_url, headers={'User-Agent': 'Mozilla/5.0'}) with urllib.request.urlopen(req, timeout=10) as response: image_data = response.read() mime_type = response.headers.get_content_type() else: if os.path.exists(image_url): mime_type, _ = mimetypes.guess_type(image_url) with open(image_url, "rb") as f: image_data = f.read() if image_data: if not mime_type: mime_type = "image/jpeg" b64_str = base64.b64encode(image_data).decode('utf-8') final_image_src = f"data:{mime_type};base64,{b64_str}" except Exception as e: print(f"⚠️ 图片转换 Base64 失败,使用原链接。错误: {e}") # --- 1. 数据解析 --- try: thank_you_data = json.loads(thank_you_json) except: thank_you_data = [{"style": "通用版", "content": thank_you_json}] try: return_gift_data = json.loads(return_gift_json) except: return_gift_data = [{"target": "通用建议", "item": return_gift_json, "reason": "万能回礼"}] # --- 2. 风格配置 --- styles = { "luxury": { "page_bg": "bg-neutral-900", "card_bg": "bg-neutral-900/80 backdrop-blur-xl border border-white/10", "text_main": "text-white", "text_sub": "text-neutral-400", "accent": "text-amber-400", "tag_bg": "bg-amber-400/20 text-amber-400", "btn_hover": "hover:bg-amber-400 hover:text-black", "img_bg": "bg-neutral-800" # 图片衬底色 }, "standard": { "page_bg": "bg-stone-200", "card_bg": "bg-white/95 backdrop-blur-xl border border-stone-200", "text_main": "text-stone-800", "text_sub": "text-stone-500", "accent": "text-red-600", "tag_bg": "bg-red-50 text-red-600", "btn_hover": "hover:bg-red-600 hover:text-white", "img_bg": "bg-stone-100" }, "budget": { "page_bg": "bg-yellow-50", "card_bg": "bg-white border-4 border-black shadow-[8px_8px_0px_0px_rgba(0,0,0,1)]", "text_main": "text-black", "text_sub": "text-gray-600", "accent": "text-blue-600", "tag_bg": "bg-black text-white", "btn_hover": "hover:bg-blue-600 hover:text-white", "img_bg": "bg-gray-200" } } st = styles.get(vibe_code, styles["standard"]) if "img_bg" not in st: st["img_bg"] = "bg-black/5" # 兼容兜底 # --- 3. 辅助逻辑 --- is_dark_mode = "text-white" in st['text_main'] bubble_bg = "bg-white/10 border-white/10" if is_dark_mode else "bg-black/5 border-black/5" bubble_hover = "hover:bg-white/20" if is_dark_mode else "hover:bg-black/10" divider_color = "border-white/20" if is_dark_mode else "border-black/10" # --- 4. HTML 构建 --- thank_you_html = "" for item in thank_you_data: thank_you_html += f"""
{item['style']} 点击复制

{item['content']}

✓ 已复制
""" return_gift_html = "" for item in return_gift_data: return_gift_html += f"""
{item['target']}
{item['item']}
{item['reason']}
""" html_content = f""" 礼品鉴定报告
AI Gift Analysis

{product_name}

当前估值 {price}

专家鉴定评价

{evaluation}
AI
首席鉴定官 Verified Analysis

私信回复话术

高情商回复,点击卡片即可复制

{thank_you_html}

推荐回礼策略

基于价格区间的最优解

{return_gift_html}

Designed by AI Gift Agent • 春节特别版

""" try: directory = os.path.dirname(output_path) if directory: os.makedirs(directory, exist_ok=True) with open(output_path, "w", encoding="utf-8") as f: f.write(html_content) return os.path.abspath(output_path) except Exception as e: return f"Error saving HTML file: {str(e)}" if __name__ == "__main__": parser = argparse.ArgumentParser(description="Generate Gift Card HTML") parser.add_argument("action", nargs="?", help="Action command") parser.add_argument("--product_name", required=True) parser.add_argument("--price", required=True) parser.add_argument("--evaluation", required=True) parser.add_argument("--thank_you_json", required=True) parser.add_argument("--return_gift_json", required=True) parser.add_argument("--vibe_code", required=True) parser.add_argument("--image_url", required=True) parser.add_argument("--output_path", required=True) args = parser.parse_args() result_path = generate_gift_card( product_name=args.product_name, price=args.price, evaluation=args.evaluation, thank_you_json=args.thank_you_json, return_gift_json=args.return_gift_json, vibe_code=args.vibe_code, image_url=args.image_url, output_path=args.output_path ) print(f"HTML Card generated successfully: {result_path}")