all repos — FixYouTube-legacy @ dc439938a19e7440a7e26c8fd9481610c51d6a5a

A better way to embed YouTube videos everywhere (inspired by FixTweet).

fxyoutube/yt_info.py (view raw)

 1from yt_dlp import YoutubeDL, DownloadError
 2import fxyoutube.constants as c
 3
 4def truncate_lines(input_str: str, max: int = 4):
 5    return "\n".join(input_str.splitlines()[:max])
 6
 7def get_info_ytdl(yt_id: str):
 8    try:
 9        with YoutubeDL(c.YTDL_OPTS) as ydl:
10            info = ydl.extract_info(c.BASE_URL + yt_id, download=False)
11            yt_info = { k: info[k] for k in c.YTDL_KEYS[:-1] }
12            yt_info[c.URL_KEY] = info.get(c.URL_KEY, "")
13    except DownloadError:
14        return None
15    
16    yt_info["description"] = truncate_lines(yt_info["description"])
17    return yt_info