fxyoutube/yt_info.py (view raw)
1from yt_dlp import YoutubeDL, DownloadError
2import fxyoutube.constants as c
3
4ydl_keys = ["id", "title", "description", "uploader", "duration", "height", "width", "url"]
5
6def truncate_lines(input_str: str, max: int = 4):
7 return "\n".join(input_str.splitlines()[:max])
8
9def get_info_ytdl(yt_id: str):
10 try:
11 with YoutubeDL(c.YTDL_OPTS) as ydl:
12 info = ydl.extract_info(c.BASE_URL + yt_id, download=False)
13 yt_info = { k: info[k] for k in ydl_keys }
14 except DownloadError:
15 with YoutubeDL() as ydl:
16 info = ydl.extract_info(c.BASE_URL + yt_id, download=False)
17 yt_info = { k: info[k] for k in ydl_keys[:-3] }
18 yt_info["height"] = yt_info["width"] = 0
19 yt_info["url"] = ""
20
21 yt_info["description"] = truncate_lines(yt_info["description"])
22 return yt_info