Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -28,20 +28,27 @@ def retrieve_transcript(video_url):
|
|
28 |
|
29 |
# Parse the player response JSON using json.loads
|
30 |
player = match.group(1)
|
31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
32 |
|
33 |
# Extract metadata
|
34 |
metadata = {
|
35 |
-
"title": player["videoDetails"]
|
36 |
-
"duration": player["videoDetails"]
|
37 |
-
"author": player["videoDetails"]
|
38 |
-
"views": player["videoDetails"]
|
39 |
}
|
40 |
|
41 |
# Get the tracks and sort them by priority
|
42 |
tracks = player.get("captions", {}).get("playerCaptionsTracklistRenderer", {}).get("captionTracks", [])
|
43 |
if not tracks:
|
44 |
-
return "
|
45 |
|
46 |
# Sort tracks by priority
|
47 |
tracks.sort(compare_tracks)
|
@@ -69,11 +76,6 @@ def retrieve_transcript(video_url):
|
|
69 |
.replace("\s+", " ")
|
70 |
)
|
71 |
|
72 |
-
result = {
|
73 |
-
"metadata": metadata,
|
74 |
-
"transcript": parsed_transcript,
|
75 |
-
}
|
76 |
-
|
77 |
return f"Title: {metadata['title']}\nAuthor: {metadata['author']}\nViews: {metadata['views']}\nDuration: {metadata['duration']} seconds\n\nTranscript:\n{parsed_transcript}"
|
78 |
|
79 |
|
|
|
28 |
|
29 |
# Parse the player response JSON using json.loads
|
30 |
player = match.group(1)
|
31 |
+
try:
|
32 |
+
player = json.loads(player) # Use json.loads for secure parsing
|
33 |
+
except json.JSONDecodeError:
|
34 |
+
return "Error decoding YouTube response. The response format may have changed."
|
35 |
+
|
36 |
+
# Check if videoDetails exists
|
37 |
+
if "videoDetails" not in player:
|
38 |
+
return "The video details could not be found. The video might be private, restricted, or unavailable."
|
39 |
|
40 |
# Extract metadata
|
41 |
metadata = {
|
42 |
+
"title": player["videoDetails"].get("title", "Unknown Title"),
|
43 |
+
"duration": player["videoDetails"].get("lengthSeconds", "Unknown Duration"),
|
44 |
+
"author": player["videoDetails"].get("author", "Unknown Author"),
|
45 |
+
"views": player["videoDetails"].get("viewCount", "Unknown Views"),
|
46 |
}
|
47 |
|
48 |
# Get the tracks and sort them by priority
|
49 |
tracks = player.get("captions", {}).get("playerCaptionsTracklistRenderer", {}).get("captionTracks", [])
|
50 |
if not tracks:
|
51 |
+
return f"Title: {metadata['title']}\n\nNo captions available for this video."
|
52 |
|
53 |
# Sort tracks by priority
|
54 |
tracks.sort(compare_tracks)
|
|
|
76 |
.replace("\s+", " ")
|
77 |
)
|
78 |
|
|
|
|
|
|
|
|
|
|
|
79 |
return f"Title: {metadata['title']}\nAuthor: {metadata['author']}\nViews: {metadata['views']}\nDuration: {metadata['duration']} seconds\n\nTranscript:\n{parsed_transcript}"
|
80 |
|
81 |
|