Skip to content

Commit f1f7bbd

Browse files
committed
feat: add script to repair video captions from captions.json for broken videos
1 parent 217542f commit f1f7bbd

2 files changed

Lines changed: 63 additions & 24 deletions

File tree

tools/add_captions_from_file.py

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
"""
2+
This script adds captions to the database from a captions.json file
3+
for videos that have broken status 1.
4+
5+
The necessity for this workaround arises because the APIs used during development
6+
to fetch YouTube video captions are blocked on the production server due to being flagged as
7+
a bot by YouTube.
8+
9+
Use this script for repairing video entries when automatic caption fetching fails.
10+
11+
"""
12+
13+
from zeeguu.core.model import db
14+
from zeeguu.core.model.video import Video
15+
from zeeguu.core.model.caption import Caption
16+
from zeeguu.api.app import create_app
17+
from zeeguu.core.youtube_api.youtube_api import get_captions
18+
19+
20+
app = create_app()
21+
app.app_context().push()
22+
db_session = db.session
23+
24+
# Get all videos with broken status 1
25+
videos_with_broken_status_1 = Video.query.filter_by(broken=1).all()
26+
print(f"Found {len(videos_with_broken_status_1)} videos with broken status 1")
27+
28+
# For each video with broken status 1, check if it has captions in captions.json
29+
for video in videos_with_broken_status_1:
30+
captions = get_captions(video.video_unique_key, "NOT_USED")
31+
if captions:
32+
# Add captions from captions.json
33+
try:
34+
captions_list = captions["captions"]
35+
for caption in captions_list:
36+
new_caption = Caption.create(
37+
session=db_session,
38+
video=video,
39+
time_start=caption["time_start"],
40+
time_end=caption["time_end"],
41+
text=caption["text"],
42+
)
43+
db_session.add(new_caption)
44+
# Save all captions for video
45+
print(
46+
f"Saving {len(captions_list)} captions for video {video.video_unique_key}..."
47+
)
48+
db_session.commit()
49+
50+
# Set video broken status to 0 to indicate that the video is functional
51+
print(
52+
f"Setting video broken status to 0 for video {video.video_unique_key}..."
53+
)
54+
video.broken = 0 # SQLAlchemy detects this change on the managed object
55+
db_session.commit()
56+
print(f"Successfully added captions for video {video.video_unique_key}.")
57+
except Exception as e:
58+
print(f"Error adding captions for video {video.video_unique_key}: {e}")
59+
db_session.rollback()
60+
raise e
61+
62+
else:
63+
print(f"Video {video.video_unique_key} does not have captions in captions.json")

tools/migrate_local_videos.py

Lines changed: 0 additions & 24 deletions
This file was deleted.

0 commit comments

Comments
 (0)