all repos — FixYouTube-legacy @ d0969d3a3bc3c7ae41ee5c0636aaa7b5255736c0

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

fixyoutube/yt_info.py (view raw)

 1from yt_dlp import YoutubeDL, DownloadError
 2import fixyoutube.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