|
| 1 | +"""populate_webvtt_from_topics |
| 2 | +
|
| 3 | +Revision ID: 8120ebc75366 |
| 4 | +Revises: 116b2f287eab |
| 5 | +Create Date: 2025-08-11 19:11:01.316947 |
| 6 | +
|
| 7 | +""" |
| 8 | +import json |
| 9 | +from typing import Sequence, Union |
| 10 | + |
| 11 | +from alembic import op |
| 12 | +import sqlalchemy as sa |
| 13 | +from sqlalchemy import text |
| 14 | + |
| 15 | + |
| 16 | +# revision identifiers, used by Alembic. |
| 17 | +revision: str = '8120ebc75366' |
| 18 | +down_revision: Union[str, None] = '116b2f287eab' |
| 19 | +branch_labels: Union[str, Sequence[str], None] = None |
| 20 | +depends_on: Union[str, Sequence[str], None] = None |
| 21 | + |
| 22 | + |
| 23 | +def topics_to_webvtt(topics): |
| 24 | + """Convert topics list to WebVTT format string.""" |
| 25 | + if not topics: |
| 26 | + return None |
| 27 | + |
| 28 | + lines = ["WEBVTT", ""] |
| 29 | + |
| 30 | + for topic in topics: |
| 31 | + start_time = format_timestamp(topic.get("start")) |
| 32 | + end_time = format_timestamp(topic.get("end")) |
| 33 | + text = topic.get("text", "").strip() |
| 34 | + |
| 35 | + if start_time and end_time and text: |
| 36 | + lines.append(f"{start_time} --> {end_time}") |
| 37 | + lines.append(text) |
| 38 | + lines.append("") |
| 39 | + |
| 40 | + return "\n".join(lines).strip() |
| 41 | + |
| 42 | + |
| 43 | +def format_timestamp(seconds): |
| 44 | + """Format seconds to WebVTT timestamp format (HH:MM:SS.mmm).""" |
| 45 | + if seconds is None: |
| 46 | + return None |
| 47 | + |
| 48 | + hours = int(seconds // 3600) |
| 49 | + minutes = int((seconds % 3600) // 60) |
| 50 | + secs = seconds % 60 |
| 51 | + |
| 52 | + return f"{hours:02d}:{minutes:02d}:{secs:06.3f}" |
| 53 | + |
| 54 | + |
| 55 | +def upgrade() -> None: |
| 56 | + """Populate WebVTT field for all transcripts with topics.""" |
| 57 | + |
| 58 | + # Get connection |
| 59 | + connection = op.get_bind() |
| 60 | + |
| 61 | + # Query all transcripts with topics |
| 62 | + result = connection.execute( |
| 63 | + text("SELECT id, topics FROM transcript WHERE topics IS NOT NULL") |
| 64 | + ) |
| 65 | + |
| 66 | + rows = result.fetchall() |
| 67 | + print(f"Found {len(rows)} transcripts with topics") |
| 68 | + |
| 69 | + updated_count = 0 |
| 70 | + error_count = 0 |
| 71 | + |
| 72 | + for row in rows: |
| 73 | + transcript_id = row[0] |
| 74 | + topics_data = row[1] |
| 75 | + |
| 76 | + if not topics_data: |
| 77 | + continue |
| 78 | + |
| 79 | + try: |
| 80 | + # Parse JSON if it's a string |
| 81 | + if isinstance(topics_data, str): |
| 82 | + topics_data = json.loads(topics_data) |
| 83 | + |
| 84 | + # Convert topics to WebVTT format |
| 85 | + webvtt_content = topics_to_webvtt(topics_data) |
| 86 | + |
| 87 | + if webvtt_content: |
| 88 | + # Update the webvtt field |
| 89 | + connection.execute( |
| 90 | + text("UPDATE transcript SET webvtt = :webvtt WHERE id = :id"), |
| 91 | + {"webvtt": webvtt_content, "id": transcript_id} |
| 92 | + ) |
| 93 | + updated_count += 1 |
| 94 | + print(f"✓ Updated transcript {transcript_id}") |
| 95 | + |
| 96 | + except Exception as e: |
| 97 | + error_count += 1 |
| 98 | + print(f"✗ Error updating transcript {transcript_id}: {e}") |
| 99 | + |
| 100 | + print(f"\nMigration complete!") |
| 101 | + print(f" Updated: {updated_count}") |
| 102 | + print(f" Errors: {error_count}") |
| 103 | + |
| 104 | + |
| 105 | +def downgrade() -> None: |
| 106 | + """Clear WebVTT field for all transcripts.""" |
| 107 | + op.execute( |
| 108 | + text("UPDATE transcript SET webvtt = NULL") |
| 109 | + ) |
0 commit comments