Format and lint youtube.py

This commit is contained in:
PapaTutuWawa 2025-01-13 22:02:19 +00:00
parent 71ee104658
commit c353ed5eb7

View File

@ -7,10 +7,8 @@ from microkodi.helpers import recursive_dict_merge
import yt_dlp import yt_dlp
DEFAULT_CONFIG = { DEFAULT_CONFIG = {"format": "bestvideo[width<=1920]+bestaudio", "ytdlp_options": {}}
"format": "bestvideo[width<=1920]+bestaudio",
"ytdlp_options": {}
}
def youtube_url_transformer(url: ParseResult) -> tuple[list[str], str]: def youtube_url_transformer(url: ParseResult) -> tuple[list[str], str]:
logger = logging.getLogger("Youtube") logger = logging.getLogger("Youtube")
@ -24,21 +22,26 @@ def youtube_url_transformer(url: ParseResult) -> tuple[list[str], str]:
with yt_dlp.YoutubeDL(opts) as ytdl: with yt_dlp.YoutubeDL(opts) as ytdl:
info = ytdl.extract_info(urlunparse(url), download=False) info = ytdl.extract_info(urlunparse(url), download=False)
user_agent = None #user_agent = None
audio_url = None audio_url = None
video_url = None video_url = None
for format in info["requested_formats"]: for format in info["requested_formats"]:
if format["width"] is None: if format["width"] is None:
audio_url = format["url"] audio_url = format["url"]
else: else:
user_agent = format["http_headers"]["User-Agent"] #user_agent = format["http_headers"]["User-Agent"]
video_url = format["url"] video_url = format["url"]
args = [ args = (
f'--input-slave={audio_url}', [
] if audio_url else None f"--input-slave={audio_url}",
]
if audio_url
else None
)
return args, urlparse(video_url) return args, urlparse(video_url)
def init(): def init():
# Create the config # Create the config
config: Config = I.get("Config") config: Config = I.get("Config")
@ -53,5 +56,9 @@ def init():
# Register the transformers # Register the transformers
I.get("VlcConfig").register_domain_transformer("youtu.be", youtube_url_transformer) I.get("VlcConfig").register_domain_transformer("youtu.be", youtube_url_transformer)
I.get("VlcConfig").register_domain_transformer("youtube.com", youtube_url_transformer) I.get("VlcConfig").register_domain_transformer(
I.get("VlcConfig").register_domain_transformer("www.youtube.com", youtube_url_transformer) "youtube.com", youtube_url_transformer
)
I.get("VlcConfig").register_domain_transformer(
"www.youtube.com", youtube_url_transformer
)