File size: 5,249 Bytes
28bb610
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#!/usr/bin/env python3
# /// script
# requires-python = ">=3.12"
# dependencies = [
#     "assemblyai",
#     "requests",
# ]
# ///
"""
Script to delete all transcripts from AssemblyAI account.

This script will:
1. Fetch all transcripts using the paginated API
2. Delete each transcript individually
3. Provide progress updates and error handling

Usage:
    python delete_all_transcripts.py

Make sure to set your ASSEMBLYAI_API_KEY environment variable or
modify the script to include your API key directly.
"""

import os
import sys
import time
from typing import Any, Dict, List

import assemblyai as aai
import requests


def get_api_key() -> str:
    """Get API key from environment variable or prompt user."""
    api_key = os.getenv("ASSEMBLYAI_API_KEY")
    if not api_key:
        api_key = input("Enter your AssemblyAI API key: ").strip()

    if not api_key:
        print("Error: No API key provided")
        sys.exit(1)

    return api_key


def fetch_all_transcripts(api_key: str) -> List[Dict[str, Any]]:
    """
    Fetch all transcripts from AssemblyAI account using pagination.

    Returns:
        List of transcript dictionaries with at least 'id' field
    """
    base_url = "https://api.assemblyai.com/v2/transcript"
    headers = {"Authorization": api_key}

    all_transcripts = []
    current_url = base_url

    print("πŸ“‹ Fetching all transcripts...")

    while current_url:
        try:
            response = requests.get(current_url, headers=headers)
            response.raise_for_status()

            data = response.json()
            transcripts = data.get("transcripts", [])
            all_transcripts.extend(transcripts)

            # Print progress
            total_fetched = len(all_transcripts)
            total_count = data.get("page_details", {}).get("result_count", "unknown")
            print(f"   Fetched {total_fetched} transcripts (total: {total_count})")

            # Get next page URL
            current_url = data.get("page_details", {}).get("next_url")

            # Small delay to be respectful to the API
            time.sleep(0.1)

        except requests.exceptions.RequestException as e:
            print(f"❌ Error fetching transcripts: {e}")
            sys.exit(1)

    print(f"βœ… Found {len(all_transcripts)} total transcripts")
    return all_transcripts


def delete_transcript_by_id(transcript_id: str, api_key: str) -> bool:
    """
    Delete a single transcript by ID.

    Returns:
        True if successful, False otherwise
    """
    try:
        # Set API key for AssemblyAI SDK
        aai.settings.api_key = api_key

        # Delete using SDK method
        transcript = aai.Transcript.get_by_id(transcript_id)
        transcript.delete_by_id(transcript_id)

        return True

    except Exception as e:
        print(f"   ❌ Failed to delete {transcript_id}: {e}")
        return False


def delete_all_transcripts(transcripts: List[Dict[str, Any]], api_key: str) -> None:
    """
    Delete all transcripts with progress reporting.
    """
    if not transcripts:
        print("βœ… No transcripts to delete")
        return

    total_count = len(transcripts)
    print(f"\nπŸ—‘οΈ  Starting deletion of {total_count} transcripts...")

    # Ask for confirmation
    confirmation = input(
        f"\n⚠️  WARNING: This will permanently delete {total_count} transcripts.\n"
        "Are you sure you want to continue? (type 'DELETE' to confirm): "
    )

    if confirmation != "DELETE":
        print("❌ Deletion cancelled")
        return

    successful_deletions = 0
    failed_deletions = 0

    for i, transcript in enumerate(transcripts, 1):
        transcript_id = transcript.get("id")
        if not transcript_id:
            print(f"   ⚠️  Skipping transcript {i}: No ID found")
            failed_deletions += 1
            continue

        print(f"   Deleting {i}/{total_count}: {transcript_id}")

        if delete_transcript_by_id(transcript_id, api_key):
            successful_deletions += 1
        else:
            failed_deletions += 1

        # Progress update every 10 deletions
        if i % 10 == 0:
            print(
                f"   Progress: {i}/{total_count} processed ({successful_deletions} successful, {failed_deletions} failed)"
            )

        # Small delay to be respectful to the API
        time.sleep(0.2)

    print(f"\nπŸ“Š Deletion Summary:")
    print(f"   βœ… Successfully deleted: {successful_deletions}")
    print(f"   ❌ Failed deletions: {failed_deletions}")
    print(f"   πŸ“‹ Total processed: {total_count}")

    if failed_deletions == 0:
        print("πŸŽ‰ All transcripts deleted successfully!")
    else:
        print(
            f"⚠️  {failed_deletions} transcripts could not be deleted. Check the error messages above."
        )


def main():
    """Main function to orchestrate the deletion process."""
    print("πŸŽ™οΈ  AssemblyAI Transcript Deletion Tool")
    print("=====================================")

    # Get API key
    api_key = get_api_key()

    # Fetch all transcripts
    transcripts = fetch_all_transcripts(api_key)

    # Delete all transcripts
    delete_all_transcripts(transcripts, api_key)


if __name__ == "__main__":
    main()