#!/usr/bin/env python3 # -*- coding: utf-8 -*- import json import pymysql from datetime import datetime # 数据库配置 DB_CONFIG = { "host": "localhost", "user": "your_db_user", "password": "your_db_password", "database": "your_db_name", "charset": "utf8mb4" } # 举报配置 REPORT_CONFIG = { "reporter_id": 1, # 举报人的用户ID "reported_pt_site": "站点名称", "reason_category": "scam", "description": "详细情况描述", "evidences": [ "https://example.com/image1.png", "https://example.com/image2.jpg" ] } def load_users(json_file): with open(json_file, 'r', encoding='utf-8') as f: return json.load(f) def insert_report(cursor, username, email): now = datetime.utcnow() cursor.execute( "INSERT INTO reports (reporter_id, reported_pt_site, reported_username, reported_email, " "reason_category, description, status, created_at, updated_at) " "VALUES (%s, %s, %s, %s, %s, %s, 'pending', %s, %s)", (REPORT_CONFIG["reporter_id"], REPORT_CONFIG["reported_pt_site"], username, email, REPORT_CONFIG["reason_category"], REPORT_CONFIG["description"], now, now) ) return cursor.lastrowid def insert_evidences(cursor, report_id): now = datetime.utcnow() for url in REPORT_CONFIG["evidences"]: cursor.execute( "INSERT INTO evidences (report_id, file_url, file_type, created_at, updated_at) " "VALUES (%s, %s, 'image_url', %s, %s)", (report_id, url, now, now) ) def main(): users = load_users("users.json") conn = pymysql.connect(**DB_CONFIG) try: cursor = conn.cursor() print(f"开始批量写入,共 {len(users)} 个用户\n") success_count = 0 for idx, user in enumerate(users, 1): username = user.get("username") email = user.get("email") if not email: print(f"[{idx}] 跳过: 缺少邮箱") continue print(f"[{idx}] 写入: {username or '(无)'} ({email})...", end=" ") try: report_id = insert_report(cursor, username, email) insert_evidences(cursor, report_id) conn.commit() print(f"✓ 成功 (ID: {report_id})") success_count += 1 except Exception as e: conn.rollback() print(f"✗ 失败: {e}") print(f"\n完成! 成功: {success_count}/{len(users)}") finally: conn.close() if __name__ == "__main__": main()