summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorA Farzat <a@farzat.xyz>2025-10-05 07:40:52 +0300
committerA Farzat <a@farzat.xyz>2025-10-05 07:40:52 +0300
commit89e8ba0a180c5c0cef5f6539156ae58a69661b75 (patch)
tree7f1873ba723bb59ed2f9235ad0ce15ae8b87c7cf
parent3b418daf684f01a126a56b58c83120af5914f576 (diff)
downloadcsca5028-89e8ba0a180c5c0cef5f6539156ae58a69661b75.tar.gz
csca5028-89e8ba0a180c5c0cef5f6539156ae58a69661b75.zip
Make ytdlp return duration strings and handle errors
-rw-r--r--components/ytdlp/__init__.py19
1 files changed, 14 insertions, 5 deletions
diff --git a/components/ytdlp/__init__.py b/components/ytdlp/__init__.py
index c4bd4d0..e63858c 100644
--- a/components/ytdlp/__init__.py
+++ b/components/ytdlp/__init__.py
@@ -1,9 +1,18 @@
-from typing import Any, Dict
from json import dumps
+from sys import stderr
+from typing import Any, Dict
+
from yt_dlp import YoutubeDL # type: ignore
def obtain_vid_info(url: str) -> Dict[str, Any]:
- ydl_opts = { "check_formats": False, }
- with YoutubeDL(ydl_opts) as ydl:
- info = ydl.extract_info(url, download=False)
- return {"duration": info["duration"]}
+ ydl_opts = { "check_formats": False, } # We only want the durations.
+ try:
+ with YoutubeDL(ydl_opts) as ydl:
+ info = ydl.extract_info(url, download=False)
+ return { "duration_string": info["duration_string"] }
+ except Exception as e:
+ print("Ran into an exception while fetching", url + ":", e, file=stderr)
+ # This is a dummy project. If yt-dlp fails, repeatedly parsing YouTube
+ # would only get us possibly blocked. Better return an empty string
+ # instead.
+ return { "duration_string": "" }