aboutsummaryrefslogtreecommitdiff
path: root/components/ytdlp/__init__.py
blob: 83fc1c8ab24771428a9ec374a03ed92887497f25 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
from json import dumps
from random import randrange
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, } # 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 obviously random
        # string instead.
        return { "duration_string": "-1:%02d" % randrange(60) }