|
|
|
@ -1,35 +1,58 @@
|
|
|
|
#!/usr/bin/env python3
|
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
|
|
|
|
|
|
import argparse
|
|
|
|
import argparse
|
|
|
|
|
|
|
|
import os
|
|
|
|
from pathlib import Path
|
|
|
|
from pathlib import Path
|
|
|
|
from string import Template
|
|
|
|
from string import Template
|
|
|
|
import subprocess
|
|
|
|
import subprocess
|
|
|
|
from typing import Callable, Dict, Optional
|
|
|
|
from typing import (
|
|
|
|
|
|
|
|
Any,
|
|
|
|
|
|
|
|
Callable,
|
|
|
|
|
|
|
|
Dict,
|
|
|
|
|
|
|
|
Optional,
|
|
|
|
|
|
|
|
TypeAlias,
|
|
|
|
|
|
|
|
)
|
|
|
|
import urllib.parse as url
|
|
|
|
import urllib.parse as url
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def cmd_player_play(video_uri: str, start: Optional[str] = None, speed: Optional[str] = None):
|
|
|
|
def cmd_player_play(
|
|
|
|
|
|
|
|
video_uri: str,
|
|
|
|
|
|
|
|
start: Optional[str] = None,
|
|
|
|
|
|
|
|
speed: Optional[str] = None,
|
|
|
|
|
|
|
|
) -> None:
|
|
|
|
print(f"Play video {video_uri}")
|
|
|
|
print(f"Play video {video_uri}")
|
|
|
|
|
|
|
|
mpv_cmd = ["/usr/bin/env", "mpv"]
|
|
|
|
|
|
|
|
mpvadd_cmd = Path("~/bin/mpvctl").expanduser()
|
|
|
|
|
|
|
|
if mpvadd_cmd.exists():
|
|
|
|
|
|
|
|
mpv_cmd = [str(mpvadd_cmd)]
|
|
|
|
subprocess.Popen(
|
|
|
|
subprocess.Popen(
|
|
|
|
args = [e for e in [
|
|
|
|
args=[
|
|
|
|
str(Path("~/bin/mpvctl").expanduser()),
|
|
|
|
e
|
|
|
|
"add",
|
|
|
|
for e in [
|
|
|
|
video_uri,
|
|
|
|
*mpv_cmd,
|
|
|
|
#f"start={start}" if start is not None else None + "," + f"speed={speed}" if speed is not None else None,
|
|
|
|
"add",
|
|
|
|
] if e is not None],
|
|
|
|
video_uri,
|
|
|
|
stdin = subprocess.DEVNULL,
|
|
|
|
# f"start={start}" if start is not None else None + "," + f"speed={speed}" if speed is not None else None,
|
|
|
|
stdout = subprocess.DEVNULL,
|
|
|
|
]
|
|
|
|
stderr = subprocess.DEVNULL,
|
|
|
|
if e is not None
|
|
|
|
|
|
|
|
],
|
|
|
|
|
|
|
|
stdin=subprocess.DEVNULL,
|
|
|
|
|
|
|
|
stdout=subprocess.DEVNULL,
|
|
|
|
|
|
|
|
stderr=subprocess.DEVNULL,
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
URI_SCHEME = "entertainment-decider"
|
|
|
|
URI_SCHEME = "entertainment-decider"
|
|
|
|
URI_COMMANDS = {
|
|
|
|
CommandDict: TypeAlias = Dict[str, "CommandType"]
|
|
|
|
|
|
|
|
CommandType: TypeAlias = CommandDict | Callable[..., None]
|
|
|
|
|
|
|
|
URI_COMMANDS: CommandDict = {
|
|
|
|
"player": {
|
|
|
|
"player": {
|
|
|
|
"play": cmd_player_play
|
|
|
|
"play": cmd_player_play,
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
def execute_uri_command(uri: str):
|
|
|
|
|
|
|
|
|
|
|
|
def execute_uri_command(uri: str) -> Any:
|
|
|
|
parts = url.urlparse(uri, scheme=URI_SCHEME, allow_fragments=False)
|
|
|
|
parts = url.urlparse(uri, scheme=URI_SCHEME, allow_fragments=False)
|
|
|
|
if parts.scheme != URI_SCHEME:
|
|
|
|
if parts.scheme != URI_SCHEME:
|
|
|
|
if parts.scheme in {"http", "https"}:
|
|
|
|
if parts.scheme in {"http", "https"}:
|
|
|
|
@ -37,33 +60,40 @@ def execute_uri_command(uri: str):
|
|
|
|
raise Exception(f"Cannot parse URI's with scheme {parts.scheme!r}")
|
|
|
|
raise Exception(f"Cannot parse URI's with scheme {parts.scheme!r}")
|
|
|
|
path = parts.path.strip("/").split("/")
|
|
|
|
path = parts.path.strip("/").split("/")
|
|
|
|
options = dict(url.parse_qsl(parts.query))
|
|
|
|
options = dict(url.parse_qsl(parts.query))
|
|
|
|
def unknown_cmd():
|
|
|
|
|
|
|
|
|
|
|
|
def unknown_cmd() -> None:
|
|
|
|
raise Exception(f"Unknown command {parts.path}")
|
|
|
|
raise Exception(f"Unknown command {parts.path}")
|
|
|
|
current = URI_COMMANDS
|
|
|
|
|
|
|
|
|
|
|
|
current: Any = URI_COMMANDS
|
|
|
|
for path_name in path:
|
|
|
|
for path_name in path:
|
|
|
|
if callable(current) or path_name not in current:
|
|
|
|
if callable(current) or path_name not in current:
|
|
|
|
unknown_cmd()
|
|
|
|
return unknown_cmd()
|
|
|
|
current = current[path_name]
|
|
|
|
current = current[path_name]
|
|
|
|
if not callable(current):
|
|
|
|
if not callable(current):
|
|
|
|
unknown_cmd()
|
|
|
|
return unknown_cmd()
|
|
|
|
return current(**options)
|
|
|
|
return current(**options)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def misc_generate_desktop():
|
|
|
|
def misc_generate_desktop() -> None:
|
|
|
|
with Path("./entry.desktop").open("r") as fh:
|
|
|
|
template_path = os.getenv("STREAMLINED_DESKTOP_TEMPLATE") or "./entry.desktop"
|
|
|
|
|
|
|
|
with Path(template_path).open("r") as fh:
|
|
|
|
temp = Template(fh.read())
|
|
|
|
temp = Template(fh.read())
|
|
|
|
print(temp.substitute(name="Entertainment Decider", exec_path=str(Path(__file__).resolve())))
|
|
|
|
exec_path = os.getenv("STREAMLINED_EXEC_PATH") or str(Path(__file__).resolve())
|
|
|
|
|
|
|
|
print(temp.substitute(name="Entertainment Decider", exec_path=exec_path))
|
|
|
|
|
|
|
|
|
|
|
|
MISC_COMMANDS: Dict[str, Callable] = {
|
|
|
|
|
|
|
|
|
|
|
|
MISC_COMMANDS: Dict[str, Callable[..., None]] = {
|
|
|
|
"generate-desktop-file": misc_generate_desktop,
|
|
|
|
"generate-desktop-file": misc_generate_desktop,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
def execute_misc_cmd(cmd: str):
|
|
|
|
|
|
|
|
|
|
|
|
def execute_misc_cmd(cmd: str) -> None:
|
|
|
|
if cmd not in MISC_COMMANDS:
|
|
|
|
if cmd not in MISC_COMMANDS:
|
|
|
|
raise Exception(f"Unknown misc command {cmd!r}")
|
|
|
|
raise Exception(f"Unknown misc command {cmd!r}")
|
|
|
|
return MISC_COMMANDS[cmd]()
|
|
|
|
return MISC_COMMANDS[cmd]()
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
|
|
|
|
|
|
|
|
def main() -> None:
|
|
|
|
parser = argparse.ArgumentParser(prog="entertainment-decider")
|
|
|
|
parser = argparse.ArgumentParser(prog="entertainment-decider")
|
|
|
|
subparsers = parser.add_subparsers()
|
|
|
|
subparsers = parser.add_subparsers()
|
|
|
|
# uri parser
|
|
|
|
# uri parser
|
|
|
|
@ -81,5 +111,6 @@ def main():
|
|
|
|
del d["parser_cmd"]
|
|
|
|
del d["parser_cmd"]
|
|
|
|
cmd(**d)
|
|
|
|
cmd(**d)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|
|
|
|
main()
|
|
|
|
|