| import asyncio |
| import os |
| import sys |
| import shutil |
|
|
| |
| sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))) |
|
|
| from app.services.supabase_client import get_supabase_service |
| from app.config import get_settings |
|
|
| async def clear_all_video_data(): |
| """ |
| Clears all video files and associated database records for team and personal videos. |
| """ |
| supabase = get_supabase_service() |
| settings = get_settings() |
| |
| print("π Starting comprehensive video data cleanup...") |
| |
| |
| print("--- πΊ Fetching all video records ---") |
| videos = await supabase.select("videos") |
| print(f"Found {len(videos)} video records.") |
| |
| |
| print("--- π Deleting local and cloud files ---") |
| for video in videos: |
| video_id = str(video.get("id")) |
| storage_path = video.get("storage_path") |
| |
| |
| if storage_path and os.path.exists(storage_path): |
| try: |
| os.remove(storage_path) |
| print(f"Deleted original: {storage_path}") |
| except Exception as e: |
| print(f"Error deleting original for {video_id}: {e}") |
| |
| |
| annotated_path = os.path.join("output_videos", "annotated", f"{video_id}.mp4") |
| if os.path.exists(annotated_path): |
| try: |
| os.remove(annotated_path) |
| print(f"Deleted annotated: {annotated_path}") |
| except Exception as e: |
| print(f"Error deleting annotated for {video_id}: {e}") |
| |
| |
| personal_output_dir = os.path.join("uploads", "personal_output") |
| for suffix in ["_output.mp4", "_output.avi", "_report.txt", "_input.mp4", "_input.avi"]: |
| personal_path = os.path.join(personal_output_dir, f"{video_id}{suffix}") |
| if os.path.exists(personal_path): |
| try: |
| os.remove(personal_path) |
| print(f"Deleted personal output: {personal_path}") |
| except Exception: |
| pass |
| |
| |
| reports_dir = "reports" |
| report_path = os.path.join(reports_dir, f"{video_id}_report.txt") |
| if os.path.exists(report_path): |
| try: |
| os.remove(report_path) |
| print(f"Deleted report: {report_path}") |
| except Exception: |
| pass |
|
|
| |
| if supabase.is_connected: |
| try: |
| |
| uploader_id = video.get("uploader_id") |
| filename = os.path.basename(storage_path) if storage_path else f"{video_id}.mp4" |
| cloud_path = f"{uploader_id}/{filename}" |
| await supabase.delete_file("videos", cloud_path) |
| |
| |
| personal_cloud_path = f"{uploader_id}/{video_id}_output.mp4" |
| await supabase.delete_file("personal-analysis-videos", personal_cloud_path) |
| except Exception: |
| pass |
|
|
| |
| print("--- π§Ή Purging directories ---") |
| dirs_to_purge = [ |
| os.path.join("uploads", "personal_output"), |
| "output_videos/annotated", |
| "stubs", |
| "reports", |
| ] |
| |
| for d in dirs_to_purge: |
| if os.path.exists(d): |
| try: |
| for f in os.listdir(d): |
| fpath = os.path.join(d, f) |
| if os.path.isfile(fpath): |
| os.remove(fpath) |
| elif os.path.isdir(fpath): |
| shutil.rmtree(fpath) |
| print(f"Purged directory: {d}") |
| except Exception as e: |
| print(f"Error purging {d}: {e}") |
|
|
| |
| print("--- ποΈ Clearing database tables ---") |
| |
| |
| |
| |
| video_related_tables = [ |
| "analytics", |
| "personal_analyses", |
| "activities" |
| ] |
| |
| for table in video_related_tables: |
| try: |
| print(f"Clearing video-related records in {table}...") |
| |
| await supabase._run_sync(lambda: supabase.client.table(table).delete().neq("id", "00000000-0000-0000-0000-000000000000").execute()) |
| print(f"Successfully cleared {table}") |
| except Exception as e: |
| print(f"Note: Error clearing {table}: {e}") |
|
|
| |
| try: |
| print("Clearing videos table (cascading)...") |
| await supabase._run_sync(lambda: supabase.client.table("videos").delete().neq("id", "00000000-0000-0000-0000-000000000000").execute()) |
| print("Successfully cleared videos table") |
| except Exception as e: |
| print(f"Error clearing videos table: {e}") |
|
|
| print("β
Cleanup complete.") |
|
|
| if __name__ == "__main__": |
| asyncio.run(clear_all_video_data()) |
|
|