""" Update task images script. Updates all tasks to use a CORS-friendly image URL. """ import sys import os import json # Add parent directory to path sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) from database import get_db_connection # 新的图片 URL (支持 CORS) NEW_IMAGE_URL = "https://picsum.photos/id/40/800/600" # 旧的图片 URL OLD_IMAGE_URL = "https://bkimg.cdn.bcebos.com/pic/8cb1cb1349540923dd54185ba804c609b3de9c82e3d8" def update_task_images(): """Update all tasks with the old image URL to use the new one.""" with get_db_connection() as conn: cursor = conn.cursor() # Get all tasks cursor.execute("SELECT id, data FROM tasks") rows = cursor.fetchall() updated_count = 0 for row in rows: task_id = row["id"] data_str = row["data"] try: data = json.loads(data_str) if isinstance(data_str, str) else data_str # Check if this task has the old image URL if data.get("image") == OLD_IMAGE_URL: data["image"] = NEW_IMAGE_URL cursor.execute( "UPDATE tasks SET data = %s WHERE id = %s", (json.dumps(data, ensure_ascii=False), task_id) ) updated_count += 1 print(f"✓ 更新任务: {task_id}") except Exception as e: print(f"✗ 跳过任务 {task_id}: {e}") print(f"\n共更新 {updated_count} 个任务") return updated_count if __name__ == "__main__": print("=" * 60) print("更新任务图片 URL 脚本") print("=" * 60) print(f"旧 URL: {OLD_IMAGE_URL}") print(f"新 URL: {NEW_IMAGE_URL}") print("-" * 60) try: count = update_task_images() print("\n" + "=" * 60) print("更新完成!") print("=" * 60) except Exception as e: print(f"\n错误: {e}") import traceback traceback.print_exc() sys.exit(1)