dotfiles/scripts/romote

61 lines
2.2 KiB
Python
Executable file

#!/usr/bin/python
from blessed import Terminal
from pathlib import Path
import urllib.parse
import httpx
def roku(key):
match key:
case 'INSERT':key='PowerOn'
case 'DELETE':key='PowerOff'
case 'ESCAPE':key='Back'
case 'HOME'|'UP'|'LEFT'|'RIGHT'|'DOWN'|'BACKSPACE':pass
case 'ENTER':key='Select'
case 'PGUP':key='VolumeUp'
case 'PGDOWN':key='VolumeDown'
case 'END':key='VolumeMute'
case '?':key='InstantReplay'
case '/':key='Info'
case '<':key='Rev'
case 'TAB':key='Play'
case '>':key='Fwd'
case _:key=f'Lit_{urllib.parse.quote(key)}'
httpx.post(f"http://{IP}:8060/keypress/{key}")
path = Path("~/.romote").expanduser()
if not path.exists():
path.touch()
with path.open("r") as f:
IPs = [line.strip("\n").split(",") for line in f]
if IPs:
print("\n".join([f"{i+1}. {' - '.join(l)}" for i,l in enumerate(IPs)]))
IP = input("Choose one from the list, or add a new one: ")
IP = IPs[int(IP)-1][0] if int(IP) <= len(IPs) else "192.168.1."+IP
else:
IP = input("Enter the IP you want to use: ")
if len(IP) <= 3:IP = "192.168.1."+IP
if not any(existing[0] == IP for existing in IPs):
with path.open("a") as f:f.write(f"{IP}\n")
t = Terminal()
print(t.clear + IP)
print("Insert/Delete to power on or off")
print("Escape to go back")
print("Home to go home")
print("Arrow keys to navigate")
print("Enter for OK")
print("PageUp/PageDn to control volume")
print("End to mute")
print("? to replay")
print("/ to get info")
print("< to rewind")
print("Tab to play/pause")
print("> to fast-forward")
print("Use other keys to type")
while True:
with t.cbreak():
key = t.inkey()
if key.name:
roku(key.name.split("_")[1])
else:
roku(key)