From 7e6521007d5d2b82b5f92fd64d8cd46fb46a186d Mon Sep 17 00:00:00 2001 From: Anish Sane Date: Fri, 10 Apr 2026 16:53:10 +0530 Subject: [PATCH] Add filter_media functionality to the `Kodi` class. This function takes 2 arguments: media and search_query. `media` is a list of movies/tv shows/episodes etc. This supplied media is filtered against the search query by a fuzzy match logic. This functionality will be used by Home Assistant for the 'Search and play' intent. The caller of this function should first get the complete list of media using the individual get_ functions. Then they should call the filter_media to filter based on the search_query. In addition to search_query, the user can also provide a parameter score_cutoff % (defaults to 80%) to be used as a threshold for fuzzy filtering. --- pykodi/kodi.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/pykodi/kodi.py b/pykodi/kodi.py index 2cbb5eb..9c473d8 100644 --- a/pykodi/kodi.py +++ b/pykodi/kodi.py @@ -6,6 +6,7 @@ import jsonrpc_base import jsonrpc_async import jsonrpc_websocket +from rapidfuzz import fuzz, process, utils def get_kodi_connection( @@ -411,6 +412,25 @@ async def get_players(self): """Return the active player objects.""" return await self._server.Player.GetActivePlayers() + async def filter_media(self, media, search_query=None, score_cutoff=80): + """Filter the provided media list by the search query. Return the filtered list.""" + + if search_query is None: + return media + + media_labels = [i["label"] for i in media] + matching_media_indices = process.extract( + search_query, + media_labels, + scorer=fuzz.WRatio, + processor=utils.default_process, + score_cutoff=score_cutoff + ) + + filtered_media = [{**media[idx], 'fuzz_score':fuzz_score} for (label, fuzz_score, idx) in matching_media_indices] + + return filtered_media + async def send_notification(self, title, message, icon="info", displaytime=10000): """Display on-screen message.""" await self._server.GUI.ShowNotification(title, message, icon, displaytime)