68 lines
3.9 KiB
Python
68 lines
3.9 KiB
Python
"""Resolve Kronic-vs-Jellyfin destination with live, allowlisted Jellyfin sessions.
|
|
|
|
Never authorize a target from user-authored text; the target must be returned by
|
|
Balroc's live Jellyfin /clients endpoint and be server-side approved.
|
|
"""
|
|
from __future__ import annotations
|
|
import re
|
|
|
|
|
|
def _norm(text):
|
|
return ' '.join(re.findall(r'[a-z0-9]+', str(text or '').casefold()))
|
|
|
|
|
|
def _aliases(name):
|
|
n = _norm(name)
|
|
names = {n}
|
|
if n.startswith('kodi '):
|
|
rest = n[5:]
|
|
names.add(rest + ' kodi')
|
|
names.add('kodi ' + rest)
|
|
if rest in ('lwrliving', 'lower living', 'lowerliving'):
|
|
names.update(('lower living room kodi', 'lower living room cody', 'lower living kodi', 'downstairs kodi', 'kodi downstairs', 'lower living room tv'))
|
|
if rest == 'main':
|
|
names.update(('main living room kodi', 'main living room cody', 'main kodi', 'living room kodi'))
|
|
if rest == 'bedroom':
|
|
names.update(('bedroom kodi', 'bedroom cody', 'kodi bedroom'))
|
|
return names
|
|
|
|
|
|
def resolve(message, destination, clients):
|
|
"""Return a safe route decision: {'mode': 'local'|'remote'|'error', ...}.
|
|
|
|
Browser-selected destinations use device_id; natural-language matches exact
|
|
live, approved client names/aliases. Unknown targets never silently become
|
|
a music or local-media search.
|
|
"""
|
|
if not isinstance(message, str) or not message.strip():
|
|
return {'mode':'error', 'reply':'Enter a media command.'}
|
|
destination = str(destination or 'kronic').strip()
|
|
available = [c for c in clients if isinstance(c, dict) and c.get('session_id') and c.get('device_id')]
|
|
if destination.lower() not in ('', 'kronic', 'local'):
|
|
found = [c for c in available if str(c['device_id']) == destination]
|
|
if len(found) != 1:
|
|
return {'mode':'error', 'reply':'That player is no longer active. Refresh the list of Jellyfin clients.'}
|
|
choice = found[0]
|
|
if not choice.get('approved') or not choice.get('controllable'):
|
|
return {'mode':'error', 'reply':'That Jellyfin client is not approved or cannot accept remote commands.'}
|
|
return {'mode':'remote', 'device':choice['device'], 'message':message.strip() + '\nTarget Jellyfin playback client: ' + str(choice['device']) + ' (device ID ' + str(choice['device_id']) + '). Use the device ID ' + str(choice['device_id']) + ' for the Jellyfin device argument.'}
|
|
# The explicit "on [device]" clause overrides Kronic's default. Match from
|
|
# the end so that titles containing normal room words are not misclassified.
|
|
cleaned = _norm(message)
|
|
tail = re.search(r'\b(?:on|to|using|in)\s+(?:the\s+)?(.+)$', cleaned)
|
|
if not tail:
|
|
return {'mode':'local', 'message':message}
|
|
target = tail.group(1).strip()
|
|
matches = [c for c in available if target in _aliases(c.get('device'))]
|
|
if len(matches) == 1:
|
|
choice = matches[0]
|
|
if not choice.get('approved') or not choice.get('controllable'):
|
|
return {'mode':'error', 'reply':'That Jellyfin player is active but not approved for remote control.'}
|
|
return {'mode':'remote', 'device':choice['device'], 'message':message.strip() + '\nTarget Jellyfin playback client: ' + str(choice['device']) + ' (device ID ' + str(choice['device_id']) + '). Use the device ID ' + str(choice['device_id']) + ' for the Jellyfin device argument.'}
|
|
if len(matches) > 1:
|
|
return {'mode':'error', 'reply':'Several Jellyfin clients match that name. Choose the exact device from the player selector.'}
|
|
# Explicit external-player wording cannot silently fall back to Kronic's music search.
|
|
if re.search(r'\b(kodi|cody|jellyfin|fire ?tv|firestick|android tv|web player|browser player)\b', target):
|
|
return {'mode':'error', 'reply':'I could not identify an approved active Jellyfin destination. Choose a client from the dropdown or check whether its app is connected.'}
|
|
return {'mode':'local', 'message':message}
|