Compare commits
2 Commits
f590e752be
...
ba04f03d71
Author | SHA1 | Date | |
---|---|---|---|
ba04f03d71 | |||
58e2b09b8a |
@ -1,6 +1,7 @@
|
||||
from dataclasses import dataclass
|
||||
from pathlib import Path
|
||||
import json
|
||||
from typing import Any
|
||||
|
||||
from microkodi.helpers import recursive_dict_merge
|
||||
|
||||
@ -34,13 +35,9 @@ class Config:
|
||||
# URL scheme -> netloc (or '*' for fallback) -> fully-qualified player class
|
||||
players: dict[str, dict[str, str]]
|
||||
|
||||
card: str | None
|
||||
connector: str | None
|
||||
|
||||
@property
|
||||
def watch_connector(self) -> bool:
|
||||
return self.card is not None and self.connector is not None
|
||||
|
||||
# The entire configuration file for use in user scripts
|
||||
options: dict[str, Any]
|
||||
|
||||
def load_config(config_path: Path | None) -> Config:
|
||||
if config_path is None:
|
||||
config_data = {}
|
||||
@ -66,6 +63,5 @@ def load_config(config_path: Path | None) -> Config:
|
||||
},
|
||||
config_data.get("players", {}),
|
||||
),
|
||||
card=config_data.get("card"),
|
||||
connector=config_data.get("connector"),
|
||||
options=config_data.get("options", {}),
|
||||
)
|
||||
|
@ -91,10 +91,6 @@ if __name__ == "__main__":
|
||||
sys.exit(-1)
|
||||
|
||||
engine.rootObjects()[0].setProperty("bridge", bridge)
|
||||
|
||||
if config.watch_connector:
|
||||
logger.info("Will be watching display if it's gone")
|
||||
|
||||
exit_code = app.exec()
|
||||
del engine
|
||||
sys.exit(exit_code)
|
56
scripts/youtube.py
Normal file
56
scripts/youtube.py
Normal file
@ -0,0 +1,56 @@
|
||||
from urllib.parse import ParseResult, urlunparse, urlparse
|
||||
import logging
|
||||
|
||||
from microkodi.repository import I
|
||||
from microkodi.config import Config
|
||||
from microkodi.helpers import recursive_dict_merge
|
||||
|
||||
import yt_dlp
|
||||
|
||||
DEFAULT_CONFIG = {
|
||||
"format": "bestvideo[width<=1920]+bestaudio",
|
||||
"ytdlp_options": {}
|
||||
}
|
||||
|
||||
def youtube_url_transformer(url: ParseResult) -> tuple[list[str], str]:
|
||||
logger = logging.getLogger("Youtube")
|
||||
youtube_config = I.get("YoutubeConfig")
|
||||
opts = {
|
||||
"format": youtube_config["format"],
|
||||
**youtube_config["ytdlp_options"],
|
||||
}
|
||||
logger.debug("Using config for yt-dlp: %s", opts)
|
||||
|
||||
with yt_dlp.YoutubeDL(opts) as ytdl:
|
||||
info = ytdl.extract_info(urlunparse(url), download=False)
|
||||
|
||||
user_agent = None
|
||||
audio_url = None
|
||||
video_url = None
|
||||
for format in info["requested_formats"]:
|
||||
if format["width"] is None:
|
||||
audio_url = format["url"]
|
||||
else:
|
||||
user_agent = format["http_headers"]["User-Agent"]
|
||||
video_url = format["url"]
|
||||
|
||||
args = [
|
||||
f'--input-slave={audio_url}',
|
||||
] if audio_url else None
|
||||
return args, urlparse(video_url)
|
||||
|
||||
def init():
|
||||
# Create the config
|
||||
config: Config = I.get("Config")
|
||||
youtube_config = recursive_dict_merge(
|
||||
DEFAULT_CONFIG,
|
||||
config.options.get(
|
||||
"me.polynom.microkodi.youtube",
|
||||
{},
|
||||
),
|
||||
)
|
||||
I.register("YoutubeConfig", youtube_config)
|
||||
|
||||
# Register the transformers
|
||||
I.get("VlcConfig").register_domain_transformer("youtu.be", youtube_url_transformer)
|
||||
I.get("VlcConfig").register_domain_transformer("www.youtube.com", youtube_url_transformer)
|
Loading…
Reference in New Issue
Block a user