Added get_default_media_plugin utility.

This commit is contained in:
Fabio Manganiello 2023-12-06 01:31:05 +01:00
parent 680de865c4
commit 2670d40094
Signed by untrusted user: blacklight
GPG key ID: D90FBA7F76362774
3 changed files with 41 additions and 10 deletions

View file

@ -151,14 +151,14 @@ class MediaPlugin(Plugin, ABC):
'f4b',
}
_supported_media_plugins = {
supported_media_plugins = [
'media.mplayer',
'media.omxplayer',
'media.mpv',
'media.vlc',
'media.chromecast',
'media.gstreamer',
}
]
_supported_media_types = ['file', 'jellyfin', 'plex', 'torrent', 'youtube']
_default_search_timeout = 60 # 60 seconds
@ -217,7 +217,7 @@ class MediaPlugin(Plugin, ABC):
if self.__class__.__name__ == 'MediaPlugin':
# Abstract class, initialize with the default configured player
for plugin_name in Config.get_plugins().keys():
if plugin_name in self._supported_media_plugins:
if plugin_name in self.supported_media_plugins:
player = get_plugin(plugin_name)
if player and player.is_local():
# Local players have priority as default if configured

View file

@ -37,7 +37,7 @@ class MediaWebtorrentPlugin(MediaPlugin):
"""
_supported_media_plugins = {
supported_media_plugins = {
'media.mplayer',
'media.omxplayer',
'media.mpv',
@ -103,7 +103,7 @@ class MediaWebtorrentPlugin(MediaPlugin):
def _init_media_player(self):
self._media_plugin = None
for plugin_name in self._supported_media_plugins:
for plugin_name in self.supported_media_plugins:
try:
if Config.get(plugin_name):
self._media_plugin = get_plugin(plugin_name)
@ -113,11 +113,9 @@ class MediaWebtorrentPlugin(MediaPlugin):
if not self._media_plugin:
raise RuntimeError(
(
'No media player specified and no '
+ 'compatible media plugin configured - '
+ 'supported media plugins: {}'
).format(self._supported_media_plugins)
+ f'supported media plugins: {self.supported_media_plugins}'
)
def _read_process_line(self):

33
platypush/utils/media.py Normal file
View file

@ -0,0 +1,33 @@
from typing import List
from platypush.config import Config
from platypush.context import get_plugin
from platypush.plugins.media import MediaPlugin
def get_default_media_plugin() -> MediaPlugin:
"""
Get the default media plugin based on the current configuration.
"""
enabled_plugins: List[MediaPlugin] = []
cfg = Config.get() or {}
for plugin_name in MediaPlugin.supported_media_plugins:
try:
plugin = get_plugin(plugin_name)
if plugin and plugin_name in cfg and not cfg[plugin_name].get('disabled'):
enabled_plugins.append(plugin)
except Exception:
pass
local_plugins = [plugin for plugin in enabled_plugins if plugin.is_local()]
if local_plugins:
return local_plugins[0]
assert (
enabled_plugins
), f'No media plugin is enabled. Supported plugins: {MediaPlugin.supported_media_plugins}'
return enabled_plugins[0]